Mercurial > projects > dwt2
comparison org.eclipse.jface.examples.databinding/src/org/eclipse/jface/examples/databinding/snippets/Snippet027ExternalValidator.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) 2008, 2009 Code 9 Corporation 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 * Chris Aniszczyk <zx@code9.com> - initial API and implementation | |
10 * Boris Bokowski, IBM - minor changes | |
11 ******************************************************************************/ | |
12 | |
13 package org.eclipse.jface.examples.databinding.snippets; | |
14 | |
15 import java.beans.PropertyChangeListener; | |
16 import java.beans.PropertyChangeSupport; | |
17 | |
18 import org.eclipse.core.databinding.DataBindingContext; | |
19 import org.eclipse.core.databinding.beans.BeansObservables; | |
20 import org.eclipse.core.databinding.observable.Realm; | |
21 import org.eclipse.core.databinding.observable.value.IObservableValue; | |
22 import org.eclipse.core.databinding.validation.MultiValidator; | |
23 import org.eclipse.core.databinding.validation.ValidationStatus; | |
24 import org.eclipse.core.runtime.IStatus; | |
25 import org.eclipse.core.runtime.Status; | |
26 import org.eclipse.jface.databinding.swt.SWTObservables; | |
27 import org.eclipse.jface.databinding.wizard.WizardPageSupport; | |
28 import org.eclipse.jface.wizard.IWizard; | |
29 import org.eclipse.jface.wizard.Wizard; | |
30 import org.eclipse.jface.wizard.WizardDialog; | |
31 import org.eclipse.jface.wizard.WizardPage; | |
32 import org.eclipse.swt.SWT; | |
33 import org.eclipse.swt.layout.GridData; | |
34 import org.eclipse.swt.layout.GridLayout; | |
35 import org.eclipse.swt.widgets.Composite; | |
36 import org.eclipse.swt.widgets.Display; | |
37 import org.eclipse.swt.widgets.Label; | |
38 import org.eclipse.swt.widgets.Text; | |
39 | |
40 /** | |
41 * This snippet demonstrates how to integrate an external validator | |
42 * | |
43 * @since 3.5 | |
44 */ | |
45 public class Snippet027ExternalValidator extends WizardPage { | |
46 | |
47 private Text nameValue; | |
48 private Text emailValue; | |
49 private Text phoneNumberValue; | |
50 | |
51 private Contact contact; | |
52 | |
53 // Minimal JavaBeans support | |
54 public static abstract class AbstractModelObject { | |
55 private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport( | |
56 this); | |
57 | |
58 public void addPropertyChangeListener(PropertyChangeListener listener) { | |
59 propertyChangeSupport.addPropertyChangeListener(listener); | |
60 } | |
61 | |
62 public void addPropertyChangeListener(String propertyName, | |
63 PropertyChangeListener listener) { | |
64 propertyChangeSupport.addPropertyChangeListener(propertyName, | |
65 listener); | |
66 } | |
67 | |
68 public void removePropertyChangeListener(PropertyChangeListener listener) { | |
69 propertyChangeSupport.removePropertyChangeListener(listener); | |
70 } | |
71 | |
72 public void removePropertyChangeListener(String propertyName, | |
73 PropertyChangeListener listener) { | |
74 propertyChangeSupport.removePropertyChangeListener(propertyName, | |
75 listener); | |
76 } | |
77 | |
78 protected void firePropertyChange(String propertyName, Object oldValue, | |
79 Object newValue) { | |
80 propertyChangeSupport.firePropertyChange(propertyName, oldValue, | |
81 newValue); | |
82 } | |
83 } | |
84 | |
85 static class Contact extends AbstractModelObject { | |
86 String name; | |
87 String email; | |
88 String phoneNumber; | |
89 | |
90 public Contact(String name, String email, String number) { | |
91 this.name = name; | |
92 this.email = email; | |
93 this.phoneNumber = number; | |
94 } | |
95 | |
96 public String getName() { | |
97 return name; | |
98 } | |
99 | |
100 public void setName(String name) { | |
101 String oldValue = this.name; | |
102 this.name = name; | |
103 firePropertyChange("name", oldValue, name); | |
104 } | |
105 | |
106 public String getEmail() { | |
107 return email; | |
108 } | |
109 | |
110 public void setEmail(String email) { | |
111 String oldValue = this.email; | |
112 this.email = email; | |
113 firePropertyChange("email", oldValue, email); | |
114 } | |
115 | |
116 public String getPhoneNumber() { | |
117 return phoneNumber; | |
118 } | |
119 | |
120 public void setPhoneNumber(String number) { | |
121 String oldValue = this.phoneNumber; | |
122 this.phoneNumber = number; | |
123 firePropertyChange("phoneNumber", oldValue, number); | |
124 } | |
125 | |
126 public IStatus validate() { | |
127 if (name.indexOf(' ') == -1) { | |
128 return ValidationStatus | |
129 .error("Please enter both first and last name separated by a space."); | |
130 } | |
131 if (email.indexOf('@') == -1) { | |
132 return ValidationStatus | |
133 .error("Please enter a valid email address containing '@'."); | |
134 } | |
135 if (!phoneNumber.startsWith("+")) { | |
136 return ValidationStatus | |
137 .error("Please enter the phone number in international format starting with '+'."); | |
138 } | |
139 return Status.OK_STATUS; | |
140 } | |
141 | |
142 } | |
143 | |
144 /** | |
145 * Create the wizard | |
146 */ | |
147 public Snippet027ExternalValidator() { | |
148 super("snippet024"); | |
149 setTitle("Snippet 024 - External Validation"); | |
150 setDescription("Please enter contact details."); | |
151 } | |
152 | |
153 /** | |
154 * Create contents of the wizard | |
155 * | |
156 * @param parent | |
157 */ | |
158 public void createControl(Composite parent) { | |
159 Composite container = new Composite(parent, SWT.NULL); | |
160 final GridLayout gridLayout = new GridLayout(); | |
161 gridLayout.numColumns = 2; | |
162 container.setLayout(gridLayout); | |
163 setControl(container); | |
164 | |
165 final Label nameLabel = new Label(container, SWT.NONE); | |
166 nameLabel.setText("Name"); | |
167 | |
168 nameValue = new Text(container, SWT.BORDER); | |
169 final GridData gd = new GridData(SWT.FILL, SWT.CENTER, true, false); | |
170 nameValue.setLayoutData(gd); | |
171 | |
172 final Label emailLabel = new Label(container, SWT.NONE); | |
173 emailLabel.setText("Email"); | |
174 | |
175 emailValue = new Text(container, SWT.BORDER); | |
176 emailValue.setLayoutData(gd); | |
177 | |
178 final Label phoneLabel = new Label(container, SWT.NONE); | |
179 phoneLabel.setText("Phone"); | |
180 | |
181 phoneNumberValue = new Text(container, SWT.BORDER); | |
182 phoneNumberValue.setLayoutData(gd); | |
183 | |
184 contact = new Contact("BorisBokowski", "boris.at.somecompany.com", | |
185 "1-123-456-7890"); | |
186 | |
187 bindUI(); | |
188 } | |
189 | |
190 private void bindUI() { | |
191 DataBindingContext dbc = new DataBindingContext(); | |
192 | |
193 final IObservableValue name = BeansObservables.observeValue(contact, | |
194 "name"); | |
195 dbc.bindValue(SWTObservables.observeText(nameValue, SWT.Modify), name, | |
196 null, null); | |
197 | |
198 final IObservableValue email = BeansObservables.observeValue(contact, | |
199 "email"); | |
200 dbc.bindValue(SWTObservables.observeText(emailValue, SWT.Modify), | |
201 email, null, null); | |
202 | |
203 final IObservableValue phone = BeansObservables.observeValue(contact, | |
204 "phoneNumber"); | |
205 dbc.bindValue(SWTObservables.observeText(phoneNumberValue, SWT.Modify), | |
206 phone, null, null); | |
207 | |
208 MultiValidator validator = new MultiValidator() { | |
209 protected IStatus validate() { | |
210 | |
211 // Everything accessed here will trigger re-validation. | |
212 name.getValue(); | |
213 email.getValue(); | |
214 phone.getValue(); | |
215 | |
216 System.out.println("Validating..."); | |
217 | |
218 return contact.validate(); | |
219 } | |
220 }; | |
221 dbc.addValidationStatusProvider(validator); | |
222 | |
223 WizardPageSupport.create(this, dbc); | |
224 } | |
225 | |
226 static class ExternalValidationWizard extends Wizard { | |
227 public void addPages() { | |
228 addPage(new Snippet027ExternalValidator()); | |
229 } | |
230 | |
231 public String getWindowTitle() { | |
232 return "Snippet 024 - External Validation"; | |
233 } | |
234 | |
235 public boolean performFinish() { | |
236 return true; | |
237 } | |
238 } | |
239 | |
240 public static void main(String[] args) { | |
241 Display display = new Display(); | |
242 | |
243 Realm.runWithDefault(SWTObservables.getRealm(display), new Runnable() { | |
244 public void run() { | |
245 IWizard wizard = new ExternalValidationWizard(); | |
246 WizardDialog dialog = new WizardDialog(null, wizard); | |
247 dialog.open(); | |
248 | |
249 // The SWT event loop | |
250 Display display = Display.getCurrent(); | |
251 while (dialog.getShell() != null | |
252 && !dialog.getShell().isDisposed()) { | |
253 if (!display.readAndDispatch()) { | |
254 display.sleep(); | |
255 } | |
256 } | |
257 } | |
258 }); | |
259 | |
260 display.dispose(); | |
261 } | |
262 } |