view org.eclipse.jface.examples.databinding/src/org/eclipse/jface/examples/databinding/snippets/Snippet016TableUpdater.d @ 99:5d5bd660917f

build some databind snippets
author Frank Benoit <benoit@tionex.de>
date Wed, 22 Apr 2009 18:59:26 +0200
parents 6086085e153d
children
line wrap: on
line source

/*******************************************************************************
 * Copyright (c) 2007 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 ******************************************************************************/

module org.eclipse.jface.examples.databinding.snippets.Snippet016TableUpdater;

import java.lang.all;

import org.eclipse.core.databinding.observable.Realm;
import org.eclipse.core.databinding.observable.list.WritableList;
import org.eclipse.core.databinding.observable.value.WritableValue;
import org.eclipse.jface.databinding.swt.SWTObservables;
import org.eclipse.jface.internal.databinding.provisional.swt.TableUpdater;
import org.eclipse.jface.layout.GridLayoutFactory;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;

/**
 * @since 3.2
 * 
 */
public class Snippet016TableUpdater {
    public static void main(String[] args) {
        final Display display = new Display();

        Realm.runWithDefault(SWTObservables.getRealm(display), new Runnable() {
            public void run() {
                final Shell shell = createShell(display);
                GridLayoutFactory.fillDefaults().generateLayout(shell);
                shell.open();
                // The SWT event loop
                while (!shell.isDisposed()) {
                    if (!display.readAndDispatch()) {
                        display.sleep();
                    }
                }
            }
        });
    }

    static class Stuff {
        private WritableValue counter = new WritableValue(new Integer(1), Integer.class);

        public Stuff(final Display display) {
            display.timerExec(1000, new Runnable() {
                public void run() {
                    counter.setValue(new Integer(1 + ((Integer) counter
                            .getValue()).intValue()));
                    display.timerExec(1000, this);
                }
            });
        }
        
        public String toString() {
            return counter.getValue().toString();
        }
    }

    protected static Shell createShell(final Display display) {
        Shell shell = new Shell();
        Table t = new Table(shell, SWT.VIRTUAL);
        final WritableList list = new WritableList();
        new TableUpdater(t, list) {

            protected void updateItem(int index, TableItem item, Object element) {
                item.setText(element.toString());
            }
        };
        display.timerExec(2000, new Runnable() {
            public void run() {
                list.add(new Stuff(display));
                display.timerExec(2000, this);
            }
        });
        return shell;
    }

}
void main( String[] args ){
    Snippet016TableUpdater.main(args);
}