comparison org.eclipse.jface.examples.databinding/src/org/eclipse/jface/examples/databinding/snippets/Snippet011ValidateMultipleBindingsSnippet.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
10 ******************************************************************************/ 10 ******************************************************************************/
11 11
12 module org.eclipse.jface.examples.databinding.snippets.Snippet011ValidateMultipleBindingsSnippet; 12 module org.eclipse.jface.examples.databinding.snippets.Snippet011ValidateMultipleBindingsSnippet;
13 13
14 import java.lang.all; 14 import java.lang.all;
15 import tango.io.Stdout;
15 16
16 import org.eclipse.core.databinding.DataBindingContext; 17 import org.eclipse.core.databinding.DataBindingContext;
17 import org.eclipse.core.databinding.UpdateValueStrategy; 18 import org.eclipse.core.databinding.UpdateValueStrategy;
18 import org.eclipse.core.databinding.observable.Realm; 19 import org.eclipse.core.databinding.observable.Realm;
19 import org.eclipse.core.databinding.observable.value.IObservableValue; 20 import org.eclipse.core.databinding.observable.value.IObservableValue;
39 * @author Brad Reynolds 40 * @author Brad Reynolds
40 */ 41 */
41 public class Snippet011ValidateMultipleBindingsSnippet { 42 public class Snippet011ValidateMultipleBindingsSnippet {
42 public static void main(String[] args) { 43 public static void main(String[] args) {
43 Realm.runWithDefault(SWTObservables.getRealm(Display.getDefault()), 44 Realm.runWithDefault(SWTObservables.getRealm(Display.getDefault()),
44 new Runnable() { 45 new class() Runnable {
45 public void run() { 46 public void run() {
46 Snippet011ValidateMultipleBindingsSnippet.run(); 47 Snippet011ValidateMultipleBindingsSnippet.run();
47 } 48 }
48 }); 49 });
49 } 50 }
54 View view = new View(shell); 55 View view = new View(shell);
55 final Model model = new Model(); 56 final Model model = new Model();
56 57
57 DataBindingContext dbc = new DataBindingContext(); 58 DataBindingContext dbc = new DataBindingContext();
58 dbc.bindValue(SWTObservables.observeText(view.text1, SWT.Modify), 59 dbc.bindValue(SWTObservables.observeText(view.text1, SWT.Modify),
59 model.value1, new UpdateValueStrategy() 60 model.value1, (new UpdateValueStrategy())
60 .setAfterConvertValidator(new CrossFieldValidator( 61 .setAfterConvertValidator(new CrossFieldValidator(
61 model.value2)), null); 62 model.value2)), null);
62 dbc.bindValue(SWTObservables.observeText(view.text2, SWT.Modify), 63 dbc.bindValue(SWTObservables.observeText(view.text2, SWT.Modify),
63 model.value2, new UpdateValueStrategy() 64 model.value2, (new UpdateValueStrategy())
64 .setAfterConvertValidator(new CrossFieldValidator( 65 .setAfterConvertValidator(new CrossFieldValidator(
65 model.value1)), null); 66 model.value1)), null);
66 67
67 // DEBUG - print to show value change 68 // DEBUG - print to show value change
68 model.value1.addValueChangeListener(new IValueChangeListener() { 69 model.value1.addValueChangeListener(new class() IValueChangeListener {
69 public void handleValueChange(ValueChangeEvent event) { 70 public void handleValueChange(ValueChangeEvent event) {
70 System.out.println("Value 1: " + model.value1.getValue()); 71 Stdout.formatln("Value 1: {}", model.value1.getValue());
71 } 72 }
72 }); 73 });
73 74
74 // DEBUG - print to show value change 75 // DEBUG - print to show value change
75 model.value2.addValueChangeListener(new IValueChangeListener() { 76 model.value2.addValueChangeListener(new class() IValueChangeListener {
76 public void handleValueChange(ValueChangeEvent event) { 77 public void handleValueChange(ValueChangeEvent event) {
77 System.out.println("Value 2: " + model.value2.getValue()); 78 Stdout.formatln("Value 2: {}", model.value2.getValue());
78 } 79 }
79 }); 80 });
80 81
81 shell.pack(); 82 shell.pack();
82 shell.open(); 83 shell.open();
90 91
91 /** 92 /**
92 * @since 3.2 93 * @since 3.2
93 * 94 *
94 */ 95 */
95 private static final class CrossFieldValidator implements IValidator { 96 private static final class CrossFieldValidator : IValidator {
96 /** 97 /**
97 * 98 *
98 */ 99 */
99 private final IObservableValue other; 100 private final IObservableValue other;
100 101
101 /** 102 /**
102 * @param model 103 * @param model
103 */ 104 */
104 private CrossFieldValidator(IObservableValue other) { 105 private this(IObservableValue other) {
105 this.other = other; 106 this.other = other;
106 } 107 }
107 108
108 public IStatus validate(Object value) { 109 public IStatus validate(Object value) {
109 if (!value.equals(other.getValue())) { 110 if (!value.opEquals(other.getValue())) {
110 return ValidationStatus.ok(); 111 return ValidationStatus.ok();
111 } 112 }
112 return ValidationStatus.error("values cannot be the same"); 113 return ValidationStatus.error("values cannot be the same");
113 } 114 }
114 } 115 }
115 116
116 static class Model { 117 static class Model {
117 WritableValue value1 = new WritableValue(); 118 WritableValue value1;
118 WritableValue value2 = new WritableValue(); 119 WritableValue value2;
120 this(){
121 value1 = new WritableValue();
122 value2 = new WritableValue();
123 }
119 } 124 }
120 125
121 static class View { 126 static class View {
122 Text text1; 127 Text text1;
123 Text text2; 128 Text text2;
124 129
125 View(Composite composite) { 130 this(Composite composite) {
126 composite.setLayout(new GridLayout(2, true)); 131 composite.setLayout(new GridLayout(2, true));
127 text1 = new Text(composite, SWT.BORDER); 132 text1 = new Text(composite, SWT.BORDER);
128 text2 = new Text(composite, SWT.BORDER); 133 text2 = new Text(composite, SWT.BORDER);
129 } 134 }
130 } 135 }