Mercurial > projects > dwt2
view org.eclipse.jface.examples.databinding/src/org/eclipse/jface/examples/databinding/snippets/Snippet008ComputedValue.d @ 100:e884642ad36e
more work on examples
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Thu, 23 Apr 2009 00:02:38 +0200 |
parents | 5d5bd660917f |
children |
line wrap: on
line source
/******************************************************************************* * Copyright (c) 2006 Brad Reynolds 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: * Brad Reynolds - initial API and implementation * Matthew Hall - bug 260329 ******************************************************************************/ module org.eclipse.jface.examples.databinding.snippets.Snippet008ComputedValue; import java.lang.all; import org.eclipse.core.databinding.DataBindingContext; import org.eclipse.core.databinding.UpdateValueStrategy; import org.eclipse.core.databinding.observable.ObservableTracker; import org.eclipse.core.databinding.observable.Realm; import org.eclipse.core.databinding.observable.value.ComputedValue; import org.eclipse.core.databinding.observable.value.IObservableValue; import org.eclipse.core.databinding.observable.value.WritableValue; import org.eclipse.jface.databinding.swt.SWTObservables; import org.eclipse.jface.layout.GridDataFactory; import org.eclipse.jface.layout.GridLayoutFactory; import org.eclipse.swt.SWT; import org.eclipse.swt.layout.FillLayout; 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; /** * Snippet that demostrates a simple use case using ComputedValue to format a * name as the user enters first and last name. * * @since 3.2 */ public class Snippet008ComputedValue { /** * @param args */ public static void main(String[] args) { final Display display = new Display(); Realm.runWithDefault(SWTObservables.getRealm(display), dgRunnable(() { Shell shell = new Shell(display); shell.setLayout(new FillLayout()); final UI ui = new UI(shell); final Data data = new Data(); // Bind the UI to the Data. DataBindingContext dbc = new DataBindingContext(); dbc.bindValue(SWTObservables.observeText(ui.firstName, SWT.Modify), data.firstName, null, null); dbc.bindValue(SWTObservables.observeText(ui.lastName, SWT.Modify), data.lastName, null, null); // Construct the formatted name observable. FormattedName formattedName = new FormattedName(data.firstName, data.lastName); // Bind the formatted name Text to the formatted name // observable. dbc.bindValue(SWTObservables.observeText(ui.formattedName, SWT.None), formattedName, new UpdateValueStrategy(false, UpdateValueStrategy.POLICY_NEVER), null); shell.pack(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } })); display.dispose(); } /** * Creates the formatted name on change of the first or last name * observables. * <p> * The key to understanding ComputedValue is understanding that it knows of * the observables that are queried without being told. This is done with * {@link ObservableTracker} voodoo. When calculate() is invoked * <code>ObservableTracker</code> records the observables that are * queried. It then exposes those observables and <code>ComputedValue</code> * can listen to changes in those objects and react accordingly. * </p> * * @since 3.2 */ static class FormattedName : ComputedValue { private IObservableValue firstName; private IObservableValue lastName; this(IObservableValue firstName, IObservableValue lastName) { this.firstName = firstName; this.lastName = lastName; } protected Object calculate() { String lastName = stringcast( this.lastName.getValue()); String firstName = stringcast( this.firstName.getValue()); lastName = (lastName !is null && lastName.length() > 0) ? lastName : "[Last Name]"; firstName = (firstName !is null && firstName.length() > 0) ? firstName : "[First Name]"; StringBuffer buffer = new StringBuffer(); buffer.append(lastName).append(", ").append(firstName); return stringcast(buffer.toString()); } } static class Data { const WritableValue firstName; const WritableValue lastName; this() { firstName = new WritableValue(stringcast(""), Class.fromType!(String)); lastName = new WritableValue(stringcast(""), Class.fromType!(String)); } } /** * Composite that creates the UI. * * @since 3.2 */ static class UI : Composite { const Text firstName; const Text lastName; const Text formattedName; this(Composite parent) { super(parent, SWT.NONE); GridLayoutFactory.swtDefaults().numColumns(2).applyTo(this); (new Label(this, SWT.NONE)).setText("First Name:"); (new Label(this, SWT.NONE)).setText("Last Name"); GridDataFactory gdf = GridDataFactory.swtDefaults().align_(SWT.FILL, SWT.FILL).grab(true, false); firstName = new Text(this, SWT.BORDER); gdf.applyTo(firstName); lastName = new Text(this, SWT.BORDER); gdf.applyTo(lastName); gdf = GridDataFactory.swtDefaults().span(2, 1).grab(true, false) .align_(SWT.FILL, SWT.BEGINNING); Label label = new Label(this, SWT.NONE); label.setText("Formatted Name:"); gdf.applyTo(label); formattedName = new Text(this, SWT.BORDER); formattedName.setEditable(false); gdf.applyTo(formattedName); } } } void main( String[] args ){ Snippet008ComputedValue.main(args); }