comparison org.eclipse.jface.examples.databinding/src/org/eclipse/jface/examples/databinding/snippets/Snippet004DataBindingContextErrorLabel.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 18a80add24ac
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 * Brad Reynolds - bug 116920, 159768
11 * Matthew Hall - bug 260329
12 ******************************************************************************/
13
14 package org.eclipse.jface.examples.databinding.snippets;
15
16 import org.eclipse.core.databinding.AggregateValidationStatus;
17 import org.eclipse.core.databinding.DataBindingContext;
18 import org.eclipse.core.databinding.UpdateValueStrategy;
19 import org.eclipse.core.databinding.observable.Realm;
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.core.runtime.Status;
25 import org.eclipse.jface.databinding.swt.SWTObservables;
26 import org.eclipse.jface.layout.GridDataFactory;
27 import org.eclipse.swt.SWT;
28 import org.eclipse.swt.layout.GridLayout;
29 import org.eclipse.swt.widgets.Display;
30 import org.eclipse.swt.widgets.Label;
31 import org.eclipse.swt.widgets.Shell;
32 import org.eclipse.swt.widgets.Text;
33
34 /**
35 * Snippet that displays how to bind the validation error of the
36 * {@link DataBindingContext} to a label. http://www.eclipse.org
37 *
38 * @since 3.2
39 */
40 public class Snippet004DataBindingContextErrorLabel {
41 public static void main(String[] args) {
42 final Display display = new Display();
43 Realm.runWithDefault(SWTObservables.getRealm(display), new Runnable() {
44 public void run() {
45 Shell shell = new Shell(display);
46 shell.setText("Data Binding Snippet 004");
47 shell.setLayout(new GridLayout(2, false));
48
49 new Label(shell, SWT.NONE).setText("Enter '5' to be valid:");
50
51 Text text = new Text(shell, SWT.BORDER);
52 WritableValue value = WritableValue.withValueType(String.class);
53 new Label(shell, SWT.NONE).setText("Error:");
54
55 Label errorLabel = new Label(shell, SWT.BORDER);
56 errorLabel.setForeground(display.getSystemColor(SWT.COLOR_RED));
57 GridDataFactory.swtDefaults().hint(200, SWT.DEFAULT).applyTo(
58 errorLabel);
59
60 DataBindingContext dbc = new DataBindingContext();
61
62 // Bind the text to the value.
63 dbc.bindValue(
64 SWTObservables.observeText(text, SWT.Modify),
65 value,
66 new UpdateValueStrategy().setAfterConvertValidator(new FiveValidator()),
67 null);
68
69 // Bind the error label to the validation error on the dbc.
70 dbc.bindValue(SWTObservables.observeText(errorLabel),
71 new AggregateValidationStatus(dbc.getBindings(),
72 AggregateValidationStatus.MAX_SEVERITY));
73
74 shell.pack();
75 shell.open();
76 while (!shell.isDisposed()) {
77 if (!display.readAndDispatch())
78 display.sleep();
79 }
80 }
81 });
82 display.dispose();
83 }
84
85 /**
86 * Validator that returns validation errors for any value other than 5.
87 *
88 * @since 3.2
89 */
90 private static class FiveValidator implements IValidator {
91 public IStatus validate(Object value) {
92 return ("5".equals(value)) ? Status.OK_STATUS : ValidationStatus
93 .error("the value was '" + value + "', not '5'");
94 }
95 }
96 }