/******************************************************************************* * Copyright (c) 2000, 2004 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * IBM Corporation - initial API and implementation *******************************************************************************/ package org.eclipse.swt.snippets; /* * Snippet demonstrating the use of ValidatorRoundtrip. * A ValidatorRoundtrip is used to restore the last valid * value on screen after a validation error occurs. * * For a list of all SWT example snippets see * http://www.eclipse.org/swt/snippets/ */ import org.eclipse.core.databinding.Binding; import org.eclipse.core.databinding.DataBindingContext; import org.eclipse.core.databinding.UpdateValueStrategy; import org.eclipse.core.databinding.beans.PojoObservables; import org.eclipse.core.databinding.observable.Realm; import org.eclipse.core.databinding.validation.ValidationStatus; import org.eclipse.core.runtime.IStatus; import org.eclipse.jface.databinding.swt.SWTObservables; import org.eclipse.jface.dialogs.Dialog; import org.eclipse.jface.fieldassist.ControlDecoration; import org.eclipse.jface.resource.JFaceResources; import org.eclipse.swt.SWT; import org.eclipse.swt.layout.FormAttachment; import org.eclipse.swt.layout.FormData; import org.eclipse.swt.layout.FormLayout; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Text; public class SnippetRoundtrip { /** * the model bound to the UI. * */ private static class MyModel { private String myText; public String getMyText() { return myText; } public void setMyText(String myText) { this.myText = myText; } } /** * * A composite that will contains a text to validate. * */ private static class MainCompo extends Composite { private final MyModel model = new MyModel(); private Text text; private ControlDecoration ctrlDeco; private Button button; /** * * @param parent * @param style */ public MainCompo(Composite parent, int style) { super(parent, style); doLayout(); } private void doLayout() { setLayout(new FormLayout()); Label lbl = new Label(this, SWT.NONE); lbl.setText("Enter a text without 'aa' :"); text = new Text(this, SWT.BORDER); ctrlDeco = new ControlDecoration(text, SWT.TOP | SWT.LEFT); ctrlDeco.hide(); ctrlDeco.setImage(JFaceResources .getImage(Dialog.DLG_IMG_MESSAGE_WARNING)); button = new Button(this, SWT.NONE); button.setText("Hello"); FormData data = new FormData(); data.left = new FormAttachment(0, 100, 5); data.top = new FormAttachment(text, 0, SWT.CENTER); lbl.setLayoutData(data); data = new FormData(); data.left = new FormAttachment(lbl, 5+ctrlDeco.getImage().getBounds().width, SWT.RIGHT); data.top = new FormAttachment(0, 100, 5); data.right = new FormAttachment(100,100,-5); text.setLayoutData(data); data = new FormData(); data.left = new FormAttachment(lbl, 5, SWT.RIGHT); data.top = new FormAttachment(text,5); button.setLayoutData(data); } /** * we bind text to MyModel.myText */ public void binding(final DataBindingContext dbc) { // let's go binding text to MyModel.myText // we will do validation // In case the string typed by the user contains 'aa', we do the roundtrip to go // back to the last correct value. // A sequence like 'bb' is allowed but leads to a warning displayed // to the user. UpdateValueStrategy strgTargetToModel = new UpdateValueStrategy( UpdateValueStrategy.POLICY_UPDATE); UpdateValueStrategy strgModelToTarget = new UpdateValueStrategy( UpdateValueStrategy.POLICY_UPDATE); Binding b = dbc.bindValue(SWTObservables.observeText(text, SWT.FocusOut), PojoObservables.observeValue(model, "myText"), strgTargetToModel, strgModelToTarget); strgTargetToModel.setAfterGetValidator(new ValidatorRoundtrip(dbc .getValidationRealm(), b) { @Override public IStatus doValidation(Object value) { if (value == null) { ctrlDeco.hide(); return ValidationStatus.ok(); } String strVal = (String) value; //error, contains 'aa' if (strVal.indexOf("aa") != -1) { return ValidationStatus .error("the value of this text must NOT contains 'aa'!!"); } //warning, contains 'bb' if (strVal.indexOf("bb") != -1) { ctrlDeco.show(); ctrlDeco.setDescriptionText("be careful, the value of this text contains 'bb' !!"); return ValidationStatus .warning(ctrlDeco.getDescriptionText()); } ctrlDeco.hide(); return ValidationStatus.ok(); } }); } } /** * Main entry point * @param args */ public static void main(String[] args) { final Display display = new Display(); Realm.runWithDefault(SWTObservables.getRealm(display), new Runnable() { public void run() { Shell shell = new Shell(display); shell.setBounds(0,0,400,200); shell.setLayout(new FormLayout()); MainCompo compo = new MainCompo(shell, SWT.NONE); FormData data = new FormData(); data.left = new FormAttachment(0, 100,0); data.right = new FormAttachment(100, 100,0); data.top = new FormAttachment(0, 100,0); data.bottom = new FormAttachment(100, 100,0); compo.setLayoutData(data); final DataBindingContext dbc = new DataBindingContext(); compo.binding(dbc); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } } }); display.dispose(); } }