Mercurial > projects > dwt2
comparison org.eclipse.jface.examples.databinding/src/org/eclipse/jface/examples/databinding/snippets/Snippet008ComputedValue.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 |
---|---|
8 * Contributors: | 8 * Contributors: |
9 * Brad Reynolds - initial API and implementation | 9 * Brad Reynolds - initial API and implementation |
10 * Matthew Hall - bug 260329 | 10 * Matthew Hall - bug 260329 |
11 ******************************************************************************/ | 11 ******************************************************************************/ |
12 | 12 |
13 package org.eclipse.jface.examples.databinding.snippets; | 13 module org.eclipse.jface.examples.databinding.snippets.Snippet008ComputedValue; |
14 | |
15 import java.lang.all; | |
14 | 16 |
15 import org.eclipse.core.databinding.DataBindingContext; | 17 import org.eclipse.core.databinding.DataBindingContext; |
16 import org.eclipse.core.databinding.UpdateValueStrategy; | 18 import org.eclipse.core.databinding.UpdateValueStrategy; |
17 import org.eclipse.core.databinding.observable.ObservableTracker; | 19 import org.eclipse.core.databinding.observable.ObservableTracker; |
18 import org.eclipse.core.databinding.observable.Realm; | 20 import org.eclipse.core.databinding.observable.Realm; |
35 * name as the user enters first and last name. | 37 * name as the user enters first and last name. |
36 * | 38 * |
37 * @since 3.2 | 39 * @since 3.2 |
38 */ | 40 */ |
39 public class Snippet008ComputedValue { | 41 public class Snippet008ComputedValue { |
40 /** | 42 /** |
41 * @param args | 43 * @param args |
42 */ | 44 */ |
43 public static void main(String[] args) { | 45 public static void main(String[] args) { |
44 final Display display = new Display(); | 46 final Display display = new Display(); |
45 Realm.runWithDefault(SWTObservables.getRealm(display), new Runnable() { | 47 Realm.runWithDefault(SWTObservables.getRealm(display), new Runnable() { |
46 public void run() { | 48 public void run() { |
47 Shell shell = new Shell(display); | 49 Shell shell = new Shell(display); |
48 shell.setLayout(new FillLayout()); | 50 shell.setLayout(new FillLayout()); |
49 | 51 |
50 final UI ui = new UI(shell); | 52 final UI ui = new UI(shell); |
51 final Data data = new Data(); | 53 final Data data = new Data(); |
52 | 54 |
53 // Bind the UI to the Data. | 55 // Bind the UI to the Data. |
54 DataBindingContext dbc = new DataBindingContext(); | 56 DataBindingContext dbc = new DataBindingContext(); |
55 dbc.bindValue(SWTObservables.observeText(ui.firstName, | 57 dbc.bindValue(SWTObservables.observeText(ui.firstName, |
56 SWT.Modify), data.firstName); | 58 SWT.Modify), data.firstName); |
57 dbc.bindValue(SWTObservables.observeText(ui.lastName, | 59 dbc.bindValue(SWTObservables.observeText(ui.lastName, |
58 SWT.Modify), data.lastName); | 60 SWT.Modify), data.lastName); |
59 | 61 |
60 // Construct the formatted name observable. | 62 // Construct the formatted name observable. |
61 FormattedName formattedName = new FormattedName(data.firstName, | 63 FormattedName formattedName = new FormattedName(data.firstName, |
62 data.lastName); | 64 data.lastName); |
63 | 65 |
64 // Bind the formatted name Text to the formatted name | 66 // Bind the formatted name Text to the formatted name |
65 // observable. | 67 // observable. |
66 dbc.bindValue(SWTObservables.observeText(ui.formattedName, | 68 dbc.bindValue(SWTObservables.observeText(ui.formattedName, |
67 SWT.None), formattedName, new UpdateValueStrategy(false, UpdateValueStrategy.POLICY_NEVER), null); | 69 SWT.None), formattedName, new UpdateValueStrategy(false, UpdateValueStrategy.POLICY_NEVER), null); |
68 | 70 |
69 shell.pack(); | 71 shell.pack(); |
70 shell.open(); | 72 shell.open(); |
71 while (!shell.isDisposed()) { | 73 while (!shell.isDisposed()) { |
72 if (!display.readAndDispatch()) | 74 if (!display.readAndDispatch()) |
73 display.sleep(); | 75 display.sleep(); |
74 } | 76 } |
75 } | 77 } |
76 }); | 78 }); |
77 display.dispose(); | 79 display.dispose(); |
78 } | 80 } |
79 | 81 |
80 /** | 82 /** |
81 * Creates the formatted name on change of the first or last name | 83 * Creates the formatted name on change of the first or last name |
82 * observables. | 84 * observables. |
83 * <p> | 85 * <p> |
84 * The key to understanding ComputedValue is understanding that it knows of | 86 * The key to understanding ComputedValue is understanding that it knows of |
85 * the observables that are queried without being told. This is done with | 87 * the observables that are queried without being told. This is done with |
86 * {@link ObservableTracker} voodoo. When calculate() is invoked | 88 * {@link ObservableTracker} voodoo. When calculate() is invoked |
87 * <code>ObservableTracker</code> records the observables that are | 89 * <code>ObservableTracker</code> records the observables that are |
88 * queried. It then exposes those observables and <code>ComputedValue</code> | 90 * queried. It then exposes those observables and <code>ComputedValue</code> |
89 * can listen to changes in those objects and react accordingly. | 91 * can listen to changes in those objects and react accordingly. |
90 * </p> | 92 * </p> |
91 * | 93 * |
92 * @since 3.2 | 94 * @since 3.2 |
93 */ | 95 */ |
94 static class FormattedName extends ComputedValue { | 96 static class FormattedName extends ComputedValue { |
95 private IObservableValue firstName; | 97 private IObservableValue firstName; |
96 | 98 |
97 private IObservableValue lastName; | 99 private IObservableValue lastName; |
98 | 100 |
99 FormattedName(IObservableValue firstName, IObservableValue lastName) { | 101 FormattedName(IObservableValue firstName, IObservableValue lastName) { |
100 this.firstName = firstName; | 102 this.firstName = firstName; |
101 this.lastName = lastName; | 103 this.lastName = lastName; |
102 } | 104 } |
103 | 105 |
104 protected Object calculate() { | 106 protected Object calculate() { |
105 String lastName = (String) this.lastName.getValue(); | 107 String lastName = (String) this.lastName.getValue(); |
106 String firstName = (String) this.firstName.getValue(); | 108 String firstName = (String) this.firstName.getValue(); |
107 lastName = (lastName != null && lastName.length() > 0) ? lastName | 109 lastName = (lastName != null && lastName.length() > 0) ? lastName |
108 : "[Last Name]"; | 110 : "[Last Name]"; |
109 firstName = (firstName != null && firstName.length() > 0) ? firstName | 111 firstName = (firstName != null && firstName.length() > 0) ? firstName |
110 : "[First Name]"; | 112 : "[First Name]"; |
111 | 113 |
112 StringBuffer buffer = new StringBuffer(); | 114 StringBuffer buffer = new StringBuffer(); |
113 buffer.append(lastName).append(", ").append(firstName); | 115 buffer.append(lastName).append(", ").append(firstName); |
114 | 116 |
115 return buffer.toString(); | 117 return buffer.toString(); |
116 } | 118 } |
117 } | 119 } |
118 | 120 |
119 static class Data { | 121 static class Data { |
120 final WritableValue firstName; | 122 final WritableValue firstName; |
121 | 123 |
122 final WritableValue lastName; | 124 final WritableValue lastName; |
123 | 125 |
124 Data() { | 126 Data() { |
125 firstName = new WritableValue("", String.class); | 127 firstName = new WritableValue("", String.class); |
126 lastName = new WritableValue("", String.class); | 128 lastName = new WritableValue("", String.class); |
127 } | 129 } |
128 } | 130 } |
129 | 131 |
130 /** | 132 /** |
131 * Composite that creates the UI. | 133 * Composite that creates the UI. |
132 * | 134 * |
133 * @since 3.2 | 135 * @since 3.2 |
134 */ | 136 */ |
135 static class UI extends Composite { | 137 static class UI extends Composite { |
136 final Text firstName; | 138 final Text firstName; |
137 | 139 |
138 final Text lastName; | 140 final Text lastName; |
139 | 141 |
140 final Text formattedName; | 142 final Text formattedName; |
141 | 143 |
142 UI(Composite parent) { | 144 UI(Composite parent) { |
143 super(parent, SWT.NONE); | 145 super(parent, SWT.NONE); |
144 | 146 |
145 GridLayoutFactory.swtDefaults().numColumns(2).applyTo(this); | 147 GridLayoutFactory.swtDefaults().numColumns(2).applyTo(this); |
146 | 148 |
147 new Label(this, SWT.NONE).setText("First Name:"); | 149 new Label(this, SWT.NONE).setText("First Name:"); |
148 new Label(this, SWT.NONE).setText("Last Name"); | 150 new Label(this, SWT.NONE).setText("Last Name"); |
149 | 151 |
150 GridDataFactory gdf = GridDataFactory.swtDefaults().align(SWT.FILL, | 152 GridDataFactory gdf = GridDataFactory.swtDefaults().align(SWT.FILL, |
151 SWT.FILL).grab(true, false); | 153 SWT.FILL).grab(true, false); |
152 firstName = new Text(this, SWT.BORDER); | 154 firstName = new Text(this, SWT.BORDER); |
153 gdf.applyTo(firstName); | 155 gdf.applyTo(firstName); |
154 | 156 |
155 lastName = new Text(this, SWT.BORDER); | 157 lastName = new Text(this, SWT.BORDER); |
156 gdf.applyTo(lastName); | 158 gdf.applyTo(lastName); |
157 | 159 |
158 gdf = GridDataFactory.swtDefaults().span(2, 1).grab(true, false) | 160 gdf = GridDataFactory.swtDefaults().span(2, 1).grab(true, false) |
159 .align(SWT.FILL, SWT.BEGINNING); | 161 .align(SWT.FILL, SWT.BEGINNING); |
160 Label label = new Label(this, SWT.NONE); | 162 Label label = new Label(this, SWT.NONE); |
161 label.setText("Formatted Name:"); | 163 label.setText("Formatted Name:"); |
162 gdf.applyTo(label); | 164 gdf.applyTo(label); |
163 | 165 |
164 formattedName = new Text(this, SWT.BORDER); | 166 formattedName = new Text(this, SWT.BORDER); |
165 formattedName.setEditable(false); | 167 formattedName.setEditable(false); |
166 gdf.applyTo(formattedName); | 168 gdf.applyTo(formattedName); |
167 } | 169 } |
168 } | 170 } |
169 } | 171 } |
172 | |
173 void main( String[] args ){ | |
174 Snippet008ComputedValue.main(args); | |
175 } |