changeset 93:18a80add24ac

Work on databinding snippets
author Frank Benoit <benoit@tionex.de>
date Sun, 19 Apr 2009 13:49:53 +0200
parents ebefa5c2eab4
children 1d37a7813832
files org.eclipse.jface.examples.databinding/src/org/eclipse/jface/examples/databinding/snippets/Snippet001NestedSelectionWithCombo.d org.eclipse.jface.examples.databinding/src/org/eclipse/jface/examples/databinding/snippets/Snippet004DataBindingContextErrorLabel.d
diffstat 2 files changed, 205 insertions(+), 189 deletions(-) [+]
line wrap: on
line diff
--- a/org.eclipse.jface.examples.databinding/src/org/eclipse/jface/examples/databinding/snippets/Snippet001NestedSelectionWithCombo.d	Sun Apr 19 13:49:38 2009 +0200
+++ b/org.eclipse.jface.examples.databinding/src/org/eclipse/jface/examples/databinding/snippets/Snippet001NestedSelectionWithCombo.d	Sun Apr 19 13:49:53 2009 +0200
@@ -11,8 +11,9 @@
  *     Matthew Hall - bug 260329
  ******************************************************************************/
 
-package org.eclipse.jface.examples.databinding.snippets;
+module org.eclipse.jface.examples.databinding.snippets.Snippet001NestedSelectionWithCombo;
 
+import java.lang.all;
 import java.beans.PropertyChangeListener;
 import java.beans.PropertyChangeSupport;
 import java.util.ArrayList;
@@ -45,172 +46,182 @@
  * person or edit the person's name.
  */
 public class Snippet001NestedSelectionWithCombo {
-	public static void main(String[] args) {
-		ViewModel viewModel = new ViewModel();
-		Shell shell = new View(viewModel).createShell();
+    public static void main(String[] args) {
+        ViewModel viewModel = new ViewModel();
+        Shell shell = (new View(viewModel)).createShell();
 
-		// The SWT event loop
-		Display display = Display.getCurrent();
-		while (!shell.isDisposed()) {
-			if (!display.readAndDispatch()) {
-				display.sleep();
-			}
-		}
-	}
+        // The SWT event loop
+        Display display = Display.getCurrent();
+        while (!shell.isDisposed()) {
+            if (!display.readAndDispatch()) {
+                display.sleep();
+            }
+        }
+    }
 
-	// Minimal JavaBeans support
-	public static abstract class AbstractModelObject {
-		private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(
-				this);
+    // Minimal JavaBeans support
+    public static abstract class AbstractModelObject {
+        private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(
+                this);
 
-		public void addPropertyChangeListener(PropertyChangeListener listener) {
-			propertyChangeSupport.addPropertyChangeListener(listener);
-		}
+        public void addPropertyChangeListener(PropertyChangeListener listener) {
+            propertyChangeSupport.addPropertyChangeListener(listener);
+        }
 
-		public void addPropertyChangeListener(String propertyName,
-				PropertyChangeListener listener) {
-			propertyChangeSupport.addPropertyChangeListener(propertyName,
-					listener);
-		}
+        public void addPropertyChangeListener(String propertyName,
+                PropertyChangeListener listener) {
+            propertyChangeSupport.addPropertyChangeListener(propertyName,
+                    listener);
+        }
 
-		public void removePropertyChangeListener(PropertyChangeListener listener) {
-			propertyChangeSupport.removePropertyChangeListener(listener);
-		}
+        public void removePropertyChangeListener(PropertyChangeListener listener) {
+            propertyChangeSupport.removePropertyChangeListener(listener);
+        }
 
-		public void removePropertyChangeListener(String propertyName,
-				PropertyChangeListener listener) {
-			propertyChangeSupport.removePropertyChangeListener(propertyName,
-					listener);
-		}
+        public void removePropertyChangeListener(String propertyName,
+                PropertyChangeListener listener) {
+            propertyChangeSupport.removePropertyChangeListener(propertyName,
+                    listener);
+        }
 
-		protected void firePropertyChange(String propertyName, Object oldValue,
-				Object newValue) {
-			propertyChangeSupport.firePropertyChange(propertyName, oldValue,
-					newValue);
-		}
-	}
+        protected void firePropertyChange(String propertyName, Object oldValue,
+                Object newValue) {
+            propertyChangeSupport.firePropertyChange(propertyName, oldValue,
+                    newValue);
+        }
+    }
 
-	// The data model class. This is normally a persistent class of some sort.
-	// 
-	// This example implements full JavaBeans bound properties so that changes
-	// to instances of this class will automatically be propogated to the UI.
-	public static class Person extends AbstractModelObject {
-		// Constructor
-		public Person(String name, String city) {
-			this.name = name;
-			this.city = city;
-		}
+    // The data model class. This is normally a persistent class of some sort.
+    // 
+    // This example implements full JavaBeans bound properties so that changes
+    // to instances of this class will automatically be propogated to the UI.
+    public static class Person : AbstractModelObject {
+        // Constructor
+        public this(String name, String city) {
+            this.name = name;
+            this.city = city;
+        }
 
-		// Some JavaBean bound properties...
-		String name;
+        // Some JavaBean bound properties...
+        String name;
 
-		String city;
+        String city;
 
-		public String getName() {
-			return name;
-		}
+        public String getName() {
+            return name;
+        }
 
-		public void setName(String name) {
-			String oldValue = this.name;
-			this.name = name;
-			firePropertyChange("name", oldValue, name);
-		}
+        public void setName(String name) {
+            String oldValue = this.name;
+            this.name = name;
+            firePropertyChange("name", oldValue, name);
+        }
 
-		public String getCity() {
-			return city;
-		}
+        public String getCity() {
+            return city;
+        }
 
-		public void setCity(String city) {
-			String oldValue = this.city;
-			this.city = city;
-			firePropertyChange("city", oldValue, city);
-		}
-	}
+        public void setCity(String city) {
+            String oldValue = this.city;
+            this.city = city;
+            firePropertyChange("city", oldValue, city);
+        }
+    }
 
-	// The View's model--the root of our GUI's Model graph
-	//
-	// Typically each View class has a corresponding ViewModel class.
-	// The ViewModel is responsible for getting the objects to edit from the
-	// DAO. Since this snippet doesn't have any persistent objects to
-	// retrieve, this ViewModel just instantiates some objects to edit.
-	// 
-	// This ViewModel also implements JavaBean bound properties.
-	static class ViewModel extends AbstractModelObject {
-		// The model to bind
-		private ArrayList people = new ArrayList();
-		{
-			people.add(new Person("Wile E. Coyote", "Tucson"));
-			people.add(new Person("Road Runner", "Lost Horse"));
-			people.add(new Person("Bugs Bunny", "Forrest"));
-		}
+    // The View's model--the root of our GUI's Model graph
+    //
+    // Typically each View class has a corresponding ViewModel class.
+    // The ViewModel is responsible for getting the objects to edit from the
+    // DAO. Since this snippet doesn't have any persistent objects to
+    // retrieve, this ViewModel just instantiates some objects to edit.
+    // 
+    // This ViewModel also implements JavaBean bound properties.
+    static class ViewModel : AbstractModelObject {
+        // The model to bind
+        private ArrayList people;
+        private void initPeople(){
+            people = new ArrayList();
+            people.add(new Person("Wile E. Coyote", "Tucson"));
+            people.add(new Person("Road Runner", "Lost Horse"));
+            people.add(new Person("Bugs Bunny", "Forrest"));
+        }
 
-		// Choice of cities for the Combo
-		private ArrayList cities = new ArrayList();
-		{
-			cities.add("Tucson");
-			cities.add("AcmeTown");
-			cities.add("Lost Horse");
-			cities.add("Forrest");
-			cities.add("Lost Mine");
-		}
+        // Choice of cities for the Combo
+        private ArrayList cities;
+        private void initCities(){
+            cities = new ArrayList();
+            cities.add("Tucson");
+            cities.add("AcmeTown");
+            cities.add("Lost Horse");
+            cities.add("Forrest");
+            cities.add("Lost Mine");
+        }
+        this(){
+            initPeople();
+            initCities();
+        }
 
-		public ArrayList getPeople() {
-			return people;
-		}
+        public ArrayList getPeople() {
+            return people;
+        }
 
-		public ArrayList getCities() {
-			return cities;
-		}
-	}
+        public ArrayList getCities() {
+            return cities;
+        }
+    }
 
-	// The GUI view
-	static class View {
-		private ViewModel viewModel;
+    // The GUI view
+    static class View {
+        private ViewModel viewModel;
 
-		public View(ViewModel viewModel) {
-			this.viewModel = viewModel;
-		}
+        public this(ViewModel viewModel) {
+            this.viewModel = viewModel;
+        }
 
-		public Shell createShell() {
-			// Build a UI
-			Shell shell = new Shell(Display.getCurrent());
-			Realm realm = SWTObservables.getRealm(shell.getDisplay());
+        public Shell createShell() {
+            // Build a UI
+            Shell shell = new Shell(Display.getCurrent());
+            Realm realm = SWTObservables.getRealm(shell.getDisplay());
 
-			List peopleList = new List(shell, SWT.BORDER);
-			Text name = new Text(shell, SWT.BORDER);
-			Combo city = new Combo(shell, SWT.BORDER | SWT.READ_ONLY);
+            List peopleList = new List(shell, SWT.BORDER);
+            Text name = new Text(shell, SWT.BORDER);
+            Combo city = new Combo(shell, SWT.BORDER | SWT.READ_ONLY);
 
-			ListViewer peopleListViewer = new ListViewer(peopleList);
-			IObservableMap attributeMap = BeansObservables.observeMap(
-					Observables.staticObservableSet(realm, new HashSet(
-							viewModel.getPeople())), Person.class, "name");
-			peopleListViewer.setLabelProvider(new ObservableMapLabelProvider(
-					attributeMap));
-			peopleListViewer.setContentProvider(new ArrayContentProvider());
-			peopleListViewer.setInput(viewModel.getPeople());
+            ListViewer peopleListViewer = new ListViewer(peopleList);
+            IObservableMap attributeMap = BeansObservables.observeMap(
+                    Observables.staticObservableSet(realm, new HashSet(
+                            viewModel.getPeople())), Class.formType!(Person), "name");
+            peopleListViewer.setLabelProvider(new ObservableMapLabelProvider(
+                    attributeMap));
+            peopleListViewer.setContentProvider(new ArrayContentProvider());
+            peopleListViewer.setInput(viewModel.getPeople());
 
-			DataBindingContext dbc = new DataBindingContext(realm);
-			IObservableValue selectedPerson = ViewersObservables
-					.observeSingleSelection(peopleListViewer);
-			dbc.bindValue(SWTObservables.observeText(name, SWT.Modify),
-					BeansObservables.observeDetailValue(selectedPerson,
-							"name", String.class));
+            DataBindingContext dbc = new DataBindingContext(realm);
+            IObservableValue selectedPerson = ViewersObservables
+                    .observeSingleSelection(peopleListViewer);
+            dbc.bindValue(SWTObservables.observeText(name, SWT.Modify),
+                    BeansObservables.observeDetailValue(selectedPerson,
+                            "name", Class.fromType!(String)));
 
-			ComboViewer cityViewer = new ComboViewer(city);
-			cityViewer.setContentProvider(new ArrayContentProvider());
-			cityViewer.setInput(viewModel.getCities());
+            ComboViewer cityViewer = new ComboViewer(city);
+            cityViewer.setContentProvider(new ArrayContentProvider());
+            cityViewer.setInput(viewModel.getCities());
 
-			IObservableValue citySelection = ViewersObservables
-					.observeSingleSelection(cityViewer);
-			dbc.bindValue(citySelection, BeansObservables.observeDetailValue(
-					selectedPerson, "city", String.class));
+            IObservableValue citySelection = ViewersObservables
+                    .observeSingleSelection(cityViewer);
+            dbc.bindValue(citySelection, BeansObservables.observeDetailValue(
+                    selectedPerson, "city", Class.fromType!(String)));
 
-			GridLayoutFactory.swtDefaults().applyTo(shell);
-			// Open and return the Shell
-			shell.pack();
-			shell.open();
-			return shell;
-		}
-	}
+            GridLayoutFactory.swtDefaults().applyTo(shell);
+            // Open and return the Shell
+            shell.pack();
+            shell.open();
+            return shell;
+        }
+    }
 
 }
+void main( String[] args ){
+    Snippet001NestedSelectionWithCombo.main( args );
+}
+
--- a/org.eclipse.jface.examples.databinding/src/org/eclipse/jface/examples/databinding/snippets/Snippet004DataBindingContextErrorLabel.d	Sun Apr 19 13:49:38 2009 +0200
+++ b/org.eclipse.jface.examples.databinding/src/org/eclipse/jface/examples/databinding/snippets/Snippet004DataBindingContextErrorLabel.d	Sun Apr 19 13:49:53 2009 +0200
@@ -11,8 +11,9 @@
  *     Matthew Hall - bug 260329
  ******************************************************************************/
 
-package org.eclipse.jface.examples.databinding.snippets;
+module org.eclipse.jface.examples.databinding.snippets.Snippet004DataBindingContextErrorLabel;
 
+import java.lang.all;
 import org.eclipse.core.databinding.AggregateValidationStatus;
 import org.eclipse.core.databinding.DataBindingContext;
 import org.eclipse.core.databinding.UpdateValueStrategy;
@@ -38,59 +39,63 @@
  * @since 3.2
  */
 public class Snippet004DataBindingContextErrorLabel {
-	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.setText("Data Binding Snippet 004");
-				shell.setLayout(new GridLayout(2, false));
+    public static void main(String[] args) {
+        Display display = new Display();
+        Realm.runWithDefault(SWTObservables.getRealm(display), dgRunnable( ( Display display_){
+            Shell shell = new Shell(display_);
+            shell.setText("Data Binding Snippet 004");
+            shell.setLayout(new GridLayout(2, false));
 
-				new Label(shell, SWT.NONE).setText("Enter '5' to be valid:");
+            (new Label(shell, SWT.NONE)).setText("Enter '5' to be valid:");
 
-				Text text = new Text(shell, SWT.BORDER);
-				WritableValue value = WritableValue.withValueType(String.class);
-				new Label(shell, SWT.NONE).setText("Error:");
+            Text text = new Text(shell, SWT.BORDER);
+            WritableValue value = WritableValue.withValueType(Class.fromType!(String));
+            (new Label(shell, SWT.NONE)).setText("Error:");
 
-				Label errorLabel = new Label(shell, SWT.BORDER);
-				errorLabel.setForeground(display.getSystemColor(SWT.COLOR_RED));
-				GridDataFactory.swtDefaults().hint(200, SWT.DEFAULT).applyTo(
-						errorLabel);
+            Label errorLabel = new Label(shell, SWT.BORDER);
+            errorLabel.setForeground(display_.getSystemColor(SWT.COLOR_RED));
+            GridDataFactory.swtDefaults().hint(200, SWT.DEFAULT).applyTo(
+                    errorLabel);
 
-				DataBindingContext dbc = new DataBindingContext();
+            DataBindingContext dbc = new DataBindingContext();
 
-				// Bind the text to the value.
-				dbc.bindValue(
-						SWTObservables.observeText(text, SWT.Modify),
-						value,
-						new UpdateValueStrategy().setAfterConvertValidator(new FiveValidator()),
-						null);
+            // Bind the text to the value.
+            dbc.bindValue(
+                    SWTObservables.observeText(text, SWT.Modify),
+                    value,
+                    (new UpdateValueStrategy()).setAfterConvertValidator(new FiveValidator()),
+                    null);
 
-				// Bind the error label to the validation error on the dbc.
-				dbc.bindValue(SWTObservables.observeText(errorLabel),
-						new AggregateValidationStatus(dbc.getBindings(),
-								AggregateValidationStatus.MAX_SEVERITY));
+            // Bind the error label to the validation error on the dbc.
+            dbc.bindValue(SWTObservables.observeText(errorLabel),
+                    new AggregateValidationStatus(dbc.getBindings(),
+                            AggregateValidationStatus.MAX_SEVERITY), null, null);
+                            // DWT: is overloading in newer version
+                            //AggregateValidationStatus.MAX_SEVERITY));
+
+            shell.pack();
+            shell.open();
+            while (!shell.isDisposed()) {
+                if (!display_.readAndDispatch())
+                    display_.sleep();
+            }
+        }, display));
+        display.dispose();
+    }
 
-				shell.pack();
-				shell.open();
-				while (!shell.isDisposed()) {
-					if (!display.readAndDispatch())
-						display.sleep();
-				}
-			}
-		});
-		display.dispose();
-	}
+    /**
+     * Validator that returns validation errors for any value other than 5.
+     * 
+     * @since 3.2
+     */
+    private static class FiveValidator : IValidator {
+        public IStatus validate(Object value) {
+            return ("5".equals(stringcast(value))) ? Status.OK_STATUS : ValidationStatus
+                    .error(Format("the value was '{}', not '5'", value ));
+        }
+    }
+}
 
-	/**
-	 * Validator that returns validation errors for any value other than 5.
-	 * 
-	 * @since 3.2
-	 */
-	private static class FiveValidator implements IValidator {
-		public IStatus validate(Object value) {
-			return ("5".equals(value)) ? Status.OK_STATUS : ValidationStatus
-					.error("the value was '" + value + "', not '5'");
-		}
-	}
+void main( String[] args ){
+    Snippet004DataBindingContextErrorLabel.main( args );
 }