/******************************************************************************* * 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; /* * * 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.conversion.Converter; import org.eclipse.core.databinding.observable.Realm; import org.eclipse.core.databinding.observable.value.IObservableValue; import org.eclipse.core.runtime.IStatus; import org.eclipse.jface.databinding.swt.SWTObservables; 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 SnippetUpdtStrategEx { /** * 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; } } /** * * Represents the updatevaluestrategy that will be used to manually update * the target * */ private static class UpdateValueStrategyEx extends UpdateValueStrategy { private Binding binding; /** * * @param policy */ public UpdateValueStrategyEx(int policy) { super(policy); } public void setBinding(Binding binding) { this.binding = binding; } @Override protected IStatus doSet(IObservableValue observableValue, Object value) { IStatus status = super.doSet(observableValue, value); if (binding != null) binding.updateModelToTarget(); return status; } } /** * * 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 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); 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, 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. // Unfortunately, this solution does not work if we type two times //the same bad text final UpdateValueStrategyEx strgTargetToModel = new UpdateValueStrategyEx( UpdateValueStrategy.POLICY_UPDATE); final UpdateValueStrategy strgModelToTarget = new UpdateValueStrategy( UpdateValueStrategy.POLICY_UPDATE); Binding b = dbc.bindValue(SWTObservables.observeText(text, SWT.FocusOut), PojoObservables .observeValue(model, "myText"), strgTargetToModel, strgModelToTarget); //don't forget to set the binding strgTargetToModel.setBinding(b); strgTargetToModel.setConverter(new Converter(String.class, String.class) { @Override public Object convert(Object fromObject) { if (fromObject == null) { return ""; } String strVal = (String) fromObject; // error, contains 'aa' if (strVal.indexOf("aa") != -1) { //force the update with the last valid value return model.getMyText(); } return strVal; } }); } } /** * 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(); } }