comparison org.eclipse.jface.examples.databinding/src/org/eclipse/jface/examples/databinding/snippets/Snippet004DataBindingContextErrorLabel.d @ 93:18a80add24ac

Work on databinding snippets
author Frank Benoit <benoit@tionex.de>
date Sun, 19 Apr 2009 13:49:53 +0200
parents 6086085e153d
children
comparison
equal deleted inserted replaced
92:ebefa5c2eab4 93:18a80add24ac
9 * Brad Reynolds - initial API and implementation 9 * Brad Reynolds - initial API and implementation
10 * Brad Reynolds - bug 116920, 159768 10 * Brad Reynolds - bug 116920, 159768
11 * Matthew Hall - bug 260329 11 * Matthew Hall - bug 260329
12 ******************************************************************************/ 12 ******************************************************************************/
13 13
14 package org.eclipse.jface.examples.databinding.snippets; 14 module org.eclipse.jface.examples.databinding.snippets.Snippet004DataBindingContextErrorLabel;
15 15
16 import java.lang.all;
16 import org.eclipse.core.databinding.AggregateValidationStatus; 17 import org.eclipse.core.databinding.AggregateValidationStatus;
17 import org.eclipse.core.databinding.DataBindingContext; 18 import org.eclipse.core.databinding.DataBindingContext;
18 import org.eclipse.core.databinding.UpdateValueStrategy; 19 import org.eclipse.core.databinding.UpdateValueStrategy;
19 import org.eclipse.core.databinding.observable.Realm; 20 import org.eclipse.core.databinding.observable.Realm;
20 import org.eclipse.core.databinding.observable.value.WritableValue; 21 import org.eclipse.core.databinding.observable.value.WritableValue;
36 * {@link DataBindingContext} to a label. http://www.eclipse.org 37 * {@link DataBindingContext} to a label. http://www.eclipse.org
37 * 38 *
38 * @since 3.2 39 * @since 3.2
39 */ 40 */
40 public class Snippet004DataBindingContextErrorLabel { 41 public class Snippet004DataBindingContextErrorLabel {
41 public static void main(String[] args) { 42 public static void main(String[] args) {
42 final Display display = new Display(); 43 Display display = new Display();
43 Realm.runWithDefault(SWTObservables.getRealm(display), new Runnable() { 44 Realm.runWithDefault(SWTObservables.getRealm(display), dgRunnable( ( Display display_){
44 public void run() { 45 Shell shell = new Shell(display_);
45 Shell shell = new Shell(display); 46 shell.setText("Data Binding Snippet 004");
46 shell.setText("Data Binding Snippet 004"); 47 shell.setLayout(new GridLayout(2, false));
47 shell.setLayout(new GridLayout(2, false));
48 48
49 new Label(shell, SWT.NONE).setText("Enter '5' to be valid:"); 49 (new Label(shell, SWT.NONE)).setText("Enter '5' to be valid:");
50 50
51 Text text = new Text(shell, SWT.BORDER); 51 Text text = new Text(shell, SWT.BORDER);
52 WritableValue value = WritableValue.withValueType(String.class); 52 WritableValue value = WritableValue.withValueType(Class.fromType!(String));
53 new Label(shell, SWT.NONE).setText("Error:"); 53 (new Label(shell, SWT.NONE)).setText("Error:");
54 54
55 Label errorLabel = new Label(shell, SWT.BORDER); 55 Label errorLabel = new Label(shell, SWT.BORDER);
56 errorLabel.setForeground(display.getSystemColor(SWT.COLOR_RED)); 56 errorLabel.setForeground(display_.getSystemColor(SWT.COLOR_RED));
57 GridDataFactory.swtDefaults().hint(200, SWT.DEFAULT).applyTo( 57 GridDataFactory.swtDefaults().hint(200, SWT.DEFAULT).applyTo(
58 errorLabel); 58 errorLabel);
59 59
60 DataBindingContext dbc = new DataBindingContext(); 60 DataBindingContext dbc = new DataBindingContext();
61 61
62 // Bind the text to the value. 62 // Bind the text to the value.
63 dbc.bindValue( 63 dbc.bindValue(
64 SWTObservables.observeText(text, SWT.Modify), 64 SWTObservables.observeText(text, SWT.Modify),
65 value, 65 value,
66 new UpdateValueStrategy().setAfterConvertValidator(new FiveValidator()), 66 (new UpdateValueStrategy()).setAfterConvertValidator(new FiveValidator()),
67 null); 67 null);
68 68
69 // Bind the error label to the validation error on the dbc. 69 // Bind the error label to the validation error on the dbc.
70 dbc.bindValue(SWTObservables.observeText(errorLabel), 70 dbc.bindValue(SWTObservables.observeText(errorLabel),
71 new AggregateValidationStatus(dbc.getBindings(), 71 new AggregateValidationStatus(dbc.getBindings(),
72 AggregateValidationStatus.MAX_SEVERITY)); 72 AggregateValidationStatus.MAX_SEVERITY), null, null);
73 // DWT: is overloading in newer version
74 //AggregateValidationStatus.MAX_SEVERITY));
73 75
74 shell.pack(); 76 shell.pack();
75 shell.open(); 77 shell.open();
76 while (!shell.isDisposed()) { 78 while (!shell.isDisposed()) {
77 if (!display.readAndDispatch()) 79 if (!display_.readAndDispatch())
78 display.sleep(); 80 display_.sleep();
79 } 81 }
80 } 82 }, display));
81 }); 83 display.dispose();
82 display.dispose(); 84 }
83 }
84 85
85 /** 86 /**
86 * Validator that returns validation errors for any value other than 5. 87 * Validator that returns validation errors for any value other than 5.
87 * 88 *
88 * @since 3.2 89 * @since 3.2
89 */ 90 */
90 private static class FiveValidator implements IValidator { 91 private static class FiveValidator : IValidator {
91 public IStatus validate(Object value) { 92 public IStatus validate(Object value) {
92 return ("5".equals(value)) ? Status.OK_STATUS : ValidationStatus 93 return ("5".equals(stringcast(value))) ? Status.OK_STATUS : ValidationStatus
93 .error("the value was '" + value + "', not '5'"); 94 .error(Format("the value was '{}', not '5'", value ));
94 } 95 }
95 } 96 }
96 } 97 }
98
99 void main( String[] args ){
100 Snippet004DataBindingContextErrorLabel.main( args );
101 }