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