comparison org.eclipse.jface.examples.databinding/src/org/eclipse/jface/examples/databinding/snippets/Snippet011ValidateMultipleBindingsSnippet.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) 2007 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 ******************************************************************************/
11
12 package org.eclipse.jface.examples.databinding.snippets;
13
14 import org.eclipse.core.databinding.DataBindingContext;
15 import org.eclipse.core.databinding.UpdateValueStrategy;
16 import org.eclipse.core.databinding.observable.Realm;
17 import org.eclipse.core.databinding.observable.value.IObservableValue;
18 import org.eclipse.core.databinding.observable.value.IValueChangeListener;
19 import org.eclipse.core.databinding.observable.value.ValueChangeEvent;
20 import org.eclipse.core.databinding.observable.value.WritableValue;
21 import org.eclipse.core.databinding.validation.IValidator;
22 import org.eclipse.core.databinding.validation.ValidationStatus;
23 import org.eclipse.core.runtime.IStatus;
24 import org.eclipse.jface.databinding.swt.SWTObservables;
25 import org.eclipse.swt.SWT;
26 import org.eclipse.swt.layout.GridLayout;
27 import org.eclipse.swt.widgets.Composite;
28 import org.eclipse.swt.widgets.Display;
29 import org.eclipse.swt.widgets.Shell;
30 import org.eclipse.swt.widgets.Text;
31
32 /**
33 * Snippet that validates values across multiple bindings on change of each
34 * observable. If the values of the target observables are not equal the model
35 * is not updated. When the values are equal they will be written to sysout.
36 *
37 * @author Brad Reynolds
38 */
39 public class Snippet011ValidateMultipleBindingsSnippet {
40 public static void main(String[] args) {
41 Realm.runWithDefault(SWTObservables.getRealm(Display.getDefault()),
42 new Runnable() {
43 public void run() {
44 Snippet011ValidateMultipleBindingsSnippet.run();
45 }
46 });
47 }
48
49 private static void run() {
50 Shell shell = new Shell();
51
52 View view = new View(shell);
53 final Model model = new Model();
54
55 DataBindingContext dbc = new DataBindingContext();
56 dbc.bindValue(SWTObservables.observeText(view.text1, SWT.Modify),
57 model.value1, new UpdateValueStrategy()
58 .setAfterConvertValidator(new CrossFieldValidator(
59 model.value2)), null);
60 dbc.bindValue(SWTObservables.observeText(view.text2, SWT.Modify),
61 model.value2, new UpdateValueStrategy()
62 .setAfterConvertValidator(new CrossFieldValidator(
63 model.value1)), null);
64
65 // DEBUG - print to show value change
66 model.value1.addValueChangeListener(new IValueChangeListener() {
67 public void handleValueChange(ValueChangeEvent event) {
68 System.out.println("Value 1: " + model.value1.getValue());
69 }
70 });
71
72 // DEBUG - print to show value change
73 model.value2.addValueChangeListener(new IValueChangeListener() {
74 public void handleValueChange(ValueChangeEvent event) {
75 System.out.println("Value 2: " + model.value2.getValue());
76 }
77 });
78
79 shell.pack();
80 shell.open();
81 Display display = shell.getDisplay();
82 while (!shell.isDisposed()) {
83 if (!display.readAndDispatch())
84 display.sleep();
85 }
86 display.dispose();
87 }
88
89 /**
90 * @since 3.2
91 *
92 */
93 private static final class CrossFieldValidator implements IValidator {
94 /**
95 *
96 */
97 private final IObservableValue other;
98
99 /**
100 * @param model
101 */
102 private CrossFieldValidator(IObservableValue other) {
103 this.other = other;
104 }
105
106 public IStatus validate(Object value) {
107 if (!value.equals(other.getValue())) {
108 return ValidationStatus.ok();
109 }
110 return ValidationStatus.error("values cannot be the same");
111 }
112 }
113
114 static class Model {
115 WritableValue value1 = new WritableValue();
116 WritableValue value2 = new WritableValue();
117 }
118
119 static class View {
120 Text text1;
121 Text text2;
122
123 View(Composite composite) {
124 composite.setLayout(new GridLayout(2, true));
125 text1 = new Text(composite, SWT.BORDER);
126 text2 = new Text(composite, SWT.BORDER);
127 }
128 }
129 }