comparison org.eclipse.jface.examples.databinding/src/org/eclipse/jface/examples/databinding/snippets/Snippet008ComputedValue.d @ 100:e884642ad36e

more work on examples
author Frank Benoit <benoit@tionex.de>
date Thu, 23 Apr 2009 00:02:38 +0200
parents 5d5bd660917f
children
comparison
equal deleted inserted replaced
99:5d5bd660917f 100:e884642ad36e
42 /** 42 /**
43 * @param args 43 * @param args
44 */ 44 */
45 public static void main(String[] args) { 45 public static void main(String[] args) {
46 final Display display = new Display(); 46 final Display display = new Display();
47 Realm.runWithDefault(SWTObservables.getRealm(display), new Runnable() { 47 Realm.runWithDefault(SWTObservables.getRealm(display), dgRunnable(() {
48 public void run() { 48 Shell shell = new Shell(display);
49 Shell shell = new Shell(display); 49 shell.setLayout(new FillLayout());
50 shell.setLayout(new FillLayout());
51 50
52 final UI ui = new UI(shell); 51 final UI ui = new UI(shell);
53 final Data data = new Data(); 52 final Data data = new Data();
54 53
55 // Bind the UI to the Data. 54 // Bind the UI to the Data.
56 DataBindingContext dbc = new DataBindingContext(); 55 DataBindingContext dbc = new DataBindingContext();
57 dbc.bindValue(SWTObservables.observeText(ui.firstName, 56 dbc.bindValue(SWTObservables.observeText(ui.firstName,
58 SWT.Modify), data.firstName); 57 SWT.Modify), data.firstName, null, null);
59 dbc.bindValue(SWTObservables.observeText(ui.lastName, 58 dbc.bindValue(SWTObservables.observeText(ui.lastName,
60 SWT.Modify), data.lastName); 59 SWT.Modify), data.lastName, null, null);
61 60
62 // Construct the formatted name observable. 61 // Construct the formatted name observable.
63 FormattedName formattedName = new FormattedName(data.firstName, 62 FormattedName formattedName = new FormattedName(data.firstName,
64 data.lastName); 63 data.lastName);
65 64
66 // Bind the formatted name Text to the formatted name 65 // Bind the formatted name Text to the formatted name
67 // observable. 66 // observable.
68 dbc.bindValue(SWTObservables.observeText(ui.formattedName, 67 dbc.bindValue(SWTObservables.observeText(ui.formattedName,
69 SWT.None), formattedName, new UpdateValueStrategy(false, UpdateValueStrategy.POLICY_NEVER), null); 68 SWT.None), formattedName, new UpdateValueStrategy(false, UpdateValueStrategy.POLICY_NEVER), null);
70 69
71 shell.pack(); 70 shell.pack();
72 shell.open(); 71 shell.open();
73 while (!shell.isDisposed()) { 72 while (!shell.isDisposed()) {
74 if (!display.readAndDispatch()) 73 if (!display.readAndDispatch())
75 display.sleep(); 74 display.sleep();
76 }
77 } 75 }
78 }); 76 }));
79 display.dispose(); 77 display.dispose();
80 } 78 }
81 79
82 /** 80 /**
83 * Creates the formatted name on change of the first or last name 81 * Creates the formatted name on change of the first or last name
91 * can listen to changes in those objects and react accordingly. 89 * can listen to changes in those objects and react accordingly.
92 * </p> 90 * </p>
93 * 91 *
94 * @since 3.2 92 * @since 3.2
95 */ 93 */
96 static class FormattedName extends ComputedValue { 94 static class FormattedName : ComputedValue {
97 private IObservableValue firstName; 95 private IObservableValue firstName;
98 96
99 private IObservableValue lastName; 97 private IObservableValue lastName;
100 98
101 FormattedName(IObservableValue firstName, IObservableValue lastName) { 99 this(IObservableValue firstName, IObservableValue lastName) {
102 this.firstName = firstName; 100 this.firstName = firstName;
103 this.lastName = lastName; 101 this.lastName = lastName;
104 } 102 }
105 103
106 protected Object calculate() { 104 protected Object calculate() {
107 String lastName = (String) this.lastName.getValue(); 105 String lastName = stringcast( this.lastName.getValue());
108 String firstName = (String) this.firstName.getValue(); 106 String firstName = stringcast( this.firstName.getValue());
109 lastName = (lastName != null && lastName.length() > 0) ? lastName 107 lastName = (lastName !is null && lastName.length() > 0) ? lastName
110 : "[Last Name]"; 108 : "[Last Name]";
111 firstName = (firstName != null && firstName.length() > 0) ? firstName 109 firstName = (firstName !is null && firstName.length() > 0) ? firstName
112 : "[First Name]"; 110 : "[First Name]";
113 111
114 StringBuffer buffer = new StringBuffer(); 112 StringBuffer buffer = new StringBuffer();
115 buffer.append(lastName).append(", ").append(firstName); 113 buffer.append(lastName).append(", ").append(firstName);
116 114
117 return buffer.toString(); 115 return stringcast(buffer.toString());
118 } 116 }
119 } 117 }
120 118
121 static class Data { 119 static class Data {
122 final WritableValue firstName; 120 const WritableValue firstName;
123 121
124 final WritableValue lastName; 122 const WritableValue lastName;
125 123
126 Data() { 124 this() {
127 firstName = new WritableValue("", String.class); 125 firstName = new WritableValue(stringcast(""), Class.fromType!(String));
128 lastName = new WritableValue("", String.class); 126 lastName = new WritableValue(stringcast(""), Class.fromType!(String));
129 } 127 }
130 } 128 }
131 129
132 /** 130 /**
133 * Composite that creates the UI. 131 * Composite that creates the UI.
134 * 132 *
135 * @since 3.2 133 * @since 3.2
136 */ 134 */
137 static class UI extends Composite { 135 static class UI : Composite {
138 final Text firstName; 136 const Text firstName;
139 137
140 final Text lastName; 138 const Text lastName;
141 139
142 final Text formattedName; 140 const Text formattedName;
143 141
144 UI(Composite parent) { 142 this(Composite parent) {
145 super(parent, SWT.NONE); 143 super(parent, SWT.NONE);
146 144
147 GridLayoutFactory.swtDefaults().numColumns(2).applyTo(this); 145 GridLayoutFactory.swtDefaults().numColumns(2).applyTo(this);
148 146
149 new Label(this, SWT.NONE).setText("First Name:"); 147 (new Label(this, SWT.NONE)).setText("First Name:");
150 new Label(this, SWT.NONE).setText("Last Name"); 148 (new Label(this, SWT.NONE)).setText("Last Name");
151 149
152 GridDataFactory gdf = GridDataFactory.swtDefaults().align(SWT.FILL, 150 GridDataFactory gdf = GridDataFactory.swtDefaults().align_(SWT.FILL,
153 SWT.FILL).grab(true, false); 151 SWT.FILL).grab(true, false);
154 firstName = new Text(this, SWT.BORDER); 152 firstName = new Text(this, SWT.BORDER);
155 gdf.applyTo(firstName); 153 gdf.applyTo(firstName);
156 154
157 lastName = new Text(this, SWT.BORDER); 155 lastName = new Text(this, SWT.BORDER);
158 gdf.applyTo(lastName); 156 gdf.applyTo(lastName);
159 157
160 gdf = GridDataFactory.swtDefaults().span(2, 1).grab(true, false) 158 gdf = GridDataFactory.swtDefaults().span(2, 1).grab(true, false)
161 .align(SWT.FILL, SWT.BEGINNING); 159 .align_(SWT.FILL, SWT.BEGINNING);
162 Label label = new Label(this, SWT.NONE); 160 Label label = new Label(this, SWT.NONE);
163 label.setText("Formatted Name:"); 161 label.setText("Formatted Name:");
164 gdf.applyTo(label); 162 gdf.applyTo(label);
165 163
166 formattedName = new Text(this, SWT.BORDER); 164 formattedName = new Text(this, SWT.BORDER);