comparison org.eclipse.jface.examples.databinding/src/org/eclipse/jface/examples/databinding/snippets/Snippet012CompositeUpdater.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 e884642ad36e
comparison
equal deleted inserted replaced
98:48d4ee626868 99:5d5bd660917f
7 * 7 *
8 * Contributors: 8 * Contributors:
9 * IBM Corporation - initial API and implementation 9 * IBM Corporation - initial API and implementation
10 ******************************************************************************/ 10 ******************************************************************************/
11 11
12 package org.eclipse.jface.examples.databinding.snippets; 12 module org.eclipse.jface.examples.databinding.snippets.Snippet012CompositeUpdater;
13
14 import java.lang.all;
13 15
14 import java.util.Timer; 16 import java.util.Timer;
15 import java.util.TimerTask; 17 import java.util.TimerTask;
16 18
17 import org.eclipse.core.databinding.observable.Realm; 19 import org.eclipse.core.databinding.observable.Realm;
34 * @since 3.2 36 * @since 3.2
35 * 37 *
36 */ 38 */
37 public class Snippet012CompositeUpdater { 39 public class Snippet012CompositeUpdater {
38 40
39 public static void main(String[] args) { 41 public static void main(String[] args) {
40 final Display display = new Display(); 42 final Display display = new Display();
41 Realm.runWithDefault(SWTObservables.getRealm(display), new Runnable() { 43 Realm.runWithDefault(SWTObservables.getRealm(display), new Runnable() {
42 public void run() { 44 public void run() {
43 Shell shell = new Shell(display); 45 Shell shell = new Shell(display);
44 46
45 final WritableList list = new WritableList(); 47 final WritableList list = new WritableList();
46 48
47 Button button = new Button(shell, SWT.PUSH); 49 Button button = new Button(shell, SWT.PUSH);
48 button.setText("add"); 50 button.setText("add");
49 button.addSelectionListener(new SelectionAdapter() { 51 button.addSelectionListener(new SelectionAdapter() {
50 public void widgetSelected( 52 public void widgetSelected(
51 org.eclipse.swt.events.SelectionEvent e) { 53 org.eclipse.swt.events.SelectionEvent e) {
52 list.add(0, new Counter()); 54 list.add(0, new Counter());
53 } 55 }
54 }); 56 });
55 57
56 final Composite composite = new Composite(shell, SWT.None); 58 final Composite composite = new Composite(shell, SWT.None);
57 59
58 new CompositeUpdater(composite, list) { 60 new CompositeUpdater(composite, list) {
59 protected Widget createWidget(int index) { 61 protected Widget createWidget(int index) {
60 Label label = new Label(composite, SWT.BORDER); 62 Label label = new Label(composite, SWT.BORDER);
61 //requestLayout(label); 63 //requestLayout(label);
62 return label; 64 return label;
63 } 65 }
64 66
65 protected void updateWidget(Widget widget, Object element) { 67 protected void updateWidget(Widget widget, Object element) {
66 ((Label) widget).setText(((Counter) element).getValue() 68 ((Label) widget).setText(((Counter) element).getValue()
67 + ""); 69 + "");
68 requestLayout((Label)widget); 70 requestLayout((Label)widget);
69 } 71 }
70 }; 72 };
71 GridLayoutFactory.fillDefaults().numColumns(10).generateLayout(composite); 73 GridLayoutFactory.fillDefaults().numColumns(10).generateLayout(composite);
72 74
73 GridDataFactory.fillDefaults().grab(true, true).applyTo( 75 GridDataFactory.fillDefaults().grab(true, true).applyTo(
74 composite); 76 composite);
75 77
76 GridLayoutFactory.fillDefaults().generateLayout(shell); 78 GridLayoutFactory.fillDefaults().generateLayout(shell);
77 shell.pack(); 79 shell.pack();
78 shell.open(); 80 shell.open();
79 while (!shell.isDisposed()) { 81 while (!shell.isDisposed()) {
80 if (!display.readAndDispatch()) 82 if (!display.readAndDispatch())
81 display.sleep(); 83 display.sleep();
82 } 84 }
83 } 85 }
84 }); 86 });
85 display.dispose(); 87 display.dispose();
86 } 88 }
87 89
88 static Timer timer = new Timer(true); 90 static Timer timer = new Timer(true);
89 91
90 static class Counter extends WritableValue { 92 static class Counter extends WritableValue {
91 Counter() { 93 Counter() {
92 super(new Integer(0), Integer.class); 94 super(new Integer(0), Integer.class);
93 scheduleIncrementTask(); 95 scheduleIncrementTask();
94 } 96 }
95 97
96 private void scheduleIncrementTask() { 98 private void scheduleIncrementTask() {
97 timer.schedule(new TimerTask() { 99 timer.schedule(new TimerTask() {
98 public void run() { 100 public void run() {
99 // we have to get onto the realm (UI thread) to perform the 101 // we have to get onto the realm (UI thread) to perform the
100 // increment 102 // increment
101 getRealm().asyncExec(new Runnable() { 103 getRealm().asyncExec(new Runnable() {
102 public void run() { 104 public void run() {
103 Integer currentVal = (Integer) getValue(); 105 Integer currentVal = (Integer) getValue();
104 setValue(new Integer(currentVal.intValue() + 1)); 106 setValue(new Integer(currentVal.intValue() + 1));
105 } 107 }
106 }); 108 });
107 scheduleIncrementTask(); 109 scheduleIncrementTask();
108 } 110 }
109 }, 1000); 111 }, 1000);
110 } 112 }
111 } 113 }
112 } 114 }
115 void main( String[] args ){
116 Snippet012CompositeUpdater.main(args);
117 }