Mercurial > projects > dwt2
comparison org.eclipse.jface.examples.databinding/src/org/eclipse/jface/examples/databinding/snippets/Snippet012CompositeUpdater.d @ 90:6086085e153d
Added databinding snippets. unmodified java sources.
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Sun, 19 Apr 2009 11:33:55 +0200 |
parents | |
children | 5d5bd660917f |
comparison
equal
deleted
inserted
replaced
89:0ca1aee7decf | 90:6086085e153d |
---|---|
1 /******************************************************************************* | |
2 * Copyright (c) 2007 IBM Corporation and others. | |
3 * All rights reserved. This program and the accompanying materials | |
4 * are made available under the terms of the Eclipse Public License v1.0 | |
5 * which accompanies this distribution, and is available at | |
6 * http://www.eclipse.org/legal/epl-v10.html | |
7 * | |
8 * Contributors: | |
9 * IBM Corporation - initial API and implementation | |
10 ******************************************************************************/ | |
11 | |
12 package org.eclipse.jface.examples.databinding.snippets; | |
13 | |
14 import java.util.Timer; | |
15 import java.util.TimerTask; | |
16 | |
17 import org.eclipse.core.databinding.observable.Realm; | |
18 import org.eclipse.core.databinding.observable.list.WritableList; | |
19 import org.eclipse.core.databinding.observable.value.WritableValue; | |
20 import org.eclipse.jface.databinding.swt.SWTObservables; | |
21 import org.eclipse.jface.internal.databinding.provisional.swt.CompositeUpdater; | |
22 import org.eclipse.jface.layout.GridDataFactory; | |
23 import org.eclipse.jface.layout.GridLayoutFactory; | |
24 import org.eclipse.swt.SWT; | |
25 import org.eclipse.swt.events.SelectionAdapter; | |
26 import org.eclipse.swt.widgets.Button; | |
27 import org.eclipse.swt.widgets.Composite; | |
28 import org.eclipse.swt.widgets.Display; | |
29 import org.eclipse.swt.widgets.Label; | |
30 import org.eclipse.swt.widgets.Shell; | |
31 import org.eclipse.swt.widgets.Widget; | |
32 | |
33 /** | |
34 * @since 3.2 | |
35 * | |
36 */ | |
37 public class Snippet012CompositeUpdater { | |
38 | |
39 public static void main(String[] args) { | |
40 final Display display = new Display(); | |
41 Realm.runWithDefault(SWTObservables.getRealm(display), new Runnable() { | |
42 public void run() { | |
43 Shell shell = new Shell(display); | |
44 | |
45 final WritableList list = new WritableList(); | |
46 | |
47 Button button = new Button(shell, SWT.PUSH); | |
48 button.setText("add"); | |
49 button.addSelectionListener(new SelectionAdapter() { | |
50 public void widgetSelected( | |
51 org.eclipse.swt.events.SelectionEvent e) { | |
52 list.add(0, new Counter()); | |
53 } | |
54 }); | |
55 | |
56 final Composite composite = new Composite(shell, SWT.None); | |
57 | |
58 new CompositeUpdater(composite, list) { | |
59 protected Widget createWidget(int index) { | |
60 Label label = new Label(composite, SWT.BORDER); | |
61 //requestLayout(label); | |
62 return label; | |
63 } | |
64 | |
65 protected void updateWidget(Widget widget, Object element) { | |
66 ((Label) widget).setText(((Counter) element).getValue() | |
67 + ""); | |
68 requestLayout((Label)widget); | |
69 } | |
70 }; | |
71 GridLayoutFactory.fillDefaults().numColumns(10).generateLayout(composite); | |
72 | |
73 GridDataFactory.fillDefaults().grab(true, true).applyTo( | |
74 composite); | |
75 | |
76 GridLayoutFactory.fillDefaults().generateLayout(shell); | |
77 shell.pack(); | |
78 shell.open(); | |
79 while (!shell.isDisposed()) { | |
80 if (!display.readAndDispatch()) | |
81 display.sleep(); | |
82 } | |
83 } | |
84 }); | |
85 display.dispose(); | |
86 } | |
87 | |
88 static Timer timer = new Timer(true); | |
89 | |
90 static class Counter extends WritableValue { | |
91 Counter() { | |
92 super(new Integer(0), Integer.class); | |
93 scheduleIncrementTask(); | |
94 } | |
95 | |
96 private void scheduleIncrementTask() { | |
97 timer.schedule(new TimerTask() { | |
98 public void run() { | |
99 // we have to get onto the realm (UI thread) to perform the | |
100 // increment | |
101 getRealm().asyncExec(new Runnable() { | |
102 public void run() { | |
103 Integer currentVal = (Integer) getValue(); | |
104 setValue(new Integer(currentVal.intValue() + 1)); | |
105 } | |
106 }); | |
107 scheduleIncrementTask(); | |
108 } | |
109 }, 1000); | |
110 } | |
111 } | |
112 } |