comparison org.eclipse.jface.examples.databinding/src/org/eclipse/jface/examples/databinding/snippets/Snippet009TableViewer.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
comparison
equal deleted inserted replaced
99:5d5bd660917f 100:e884642ad36e
17 import java.beans.PropertyChangeListener; 17 import java.beans.PropertyChangeListener;
18 import java.beans.PropertyChangeSupport; 18 import java.beans.PropertyChangeSupport;
19 import java.util.LinkedList; 19 import java.util.LinkedList;
20 import java.util.List; 20 import java.util.List;
21 21
22 import org.eclipse.core.databinding.beans.BeanProperties; 22 //import org.eclipse.core.databinding.beans.BeanProperties;
23 import org.eclipse.core.databinding.DataBindingContext;
24 import org.eclipse.core.databinding.beans.BeansObservables;
23 import org.eclipse.core.databinding.observable.Realm; 25 import org.eclipse.core.databinding.observable.Realm;
24 import org.eclipse.core.databinding.observable.list.WritableList; 26 import org.eclipse.core.databinding.observable.list.WritableList;
27 import org.eclipse.core.databinding.observable.map.IObservableMap;
25 import org.eclipse.jface.databinding.swt.SWTObservables; 28 import org.eclipse.jface.databinding.swt.SWTObservables;
26 import org.eclipse.jface.databinding.viewers.ViewerSupport; 29 //import org.eclipse.jface.databinding.viewers.ViewerSupport;
30 import org.eclipse.jface.databinding.viewers.ObservableListContentProvider;
31 import org.eclipse.jface.databinding.viewers.ObservableMapLabelProvider;
27 import org.eclipse.jface.viewers.TableViewer; 32 import org.eclipse.jface.viewers.TableViewer;
28 import org.eclipse.swt.SWT; 33 import org.eclipse.swt.SWT;
29 import org.eclipse.swt.layout.FillLayout; 34 import org.eclipse.swt.layout.FillLayout;
30 import org.eclipse.swt.widgets.Display; 35 import org.eclipse.swt.widgets.Display;
31 import org.eclipse.swt.widgets.Shell; 36 import org.eclipse.swt.widgets.Shell;
40 final Display display = Display.getDefault(); 45 final Display display = Display.getDefault();
41 46
42 // In an RCP application, the threading Realm will be set for you 47 // In an RCP application, the threading Realm will be set for you
43 // automatically by the Workbench. In an SWT application, you can do 48 // automatically by the Workbench. In an SWT application, you can do
44 // this once, wrpping your binding method call. 49 // this once, wrpping your binding method call.
45 Realm.runWithDefault(SWTObservables.getRealm(display), new Runnable() { 50 Realm.runWithDefault(SWTObservables.getRealm(display), dgRunnable(() {
46 public void run() {
47 51
48 ViewModel viewModel = new ViewModel(); 52 ViewModel viewModel = new ViewModel();
49 Shell shell = new View(viewModel).createShell(); 53 Shell shell = (new View(viewModel)).createShell();
50 54
51 // The SWT event loop 55 // The SWT event loop
52 while (!shell.isDisposed()) { 56 while (!shell.isDisposed()) {
53 if (!display.readAndDispatch()) { 57 if (!display.readAndDispatch()) {
54 display.sleep(); 58 display.sleep();
55 }
56 } 59 }
57 } 60 }
58 }); 61 }));
59 } 62 }
60 63
61 // Minimal JavaBeans support 64 // Minimal JavaBeans support
62 public static abstract class AbstractModelObject { 65 public static abstract class AbstractModelObject {
63 private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport( 66 private PropertyChangeSupport propertyChangeSupport;
67 this(){
68 propertyChangeSupport = new PropertyChangeSupport(
64 this); 69 this);
70 }
65 71
66 public void addPropertyChangeListener(PropertyChangeListener listener) { 72 public void addPropertyChangeListener(PropertyChangeListener listener) {
67 propertyChangeSupport.addPropertyChangeListener(listener); 73 propertyChangeSupport.addPropertyChangeListener(listener);
68 } 74 }
69 75
89 newValue); 95 newValue);
90 } 96 }
91 } 97 }
92 98
93 // The data model class. This is normally a persistent class of some sort. 99 // The data model class. This is normally a persistent class of some sort.
94 static class Person extends AbstractModelObject { 100 static class Person : AbstractModelObject {
95 // A property... 101 // A property...
96 String name = "John Smith"; 102 String name = "John Smith";
97 103
98 public Person(String name) { 104 public this(String name) {
99 this.name = name; 105 this.name = name;
100 } 106 }
101 107
102 public String getName() { 108 public String getName() {
103 return name; 109 return name;
104 } 110 }
105 111
106 public void setName(String name) { 112 public void setName(String name) {
107 String oldValue = this.name; 113 String oldValue = this.name;
108 this.name = name; 114 this.name = name;
109 firePropertyChange("name", oldValue, name); 115 firePropertyChange("name", stringcast(oldValue), stringcast(name));
110 } 116 }
111 } 117 }
112 118
113 // The View's model--the root of our Model graph for this particular GUI. 119 // The View's model--the root of our Model graph for this particular GUI.
114 // 120 //
116 // The ViewModel is responsible for getting the objects to edit from the 122 // The ViewModel is responsible for getting the objects to edit from the
117 // data access tier. Since this snippet doesn't have any persistent objects 123 // data access tier. Since this snippet doesn't have any persistent objects
118 // ro retrieve, this ViewModel just instantiates a model object to edit. 124 // ro retrieve, this ViewModel just instantiates a model object to edit.
119 static class ViewModel { 125 static class ViewModel {
120 // The model to bind 126 // The model to bind
121 private List people = new LinkedList(); 127 private List people;
122 { 128 this(){
129 people = new LinkedList();
123 people.add(new Person("Steve Northover")); 130 people.add(new Person("Steve Northover"));
124 people.add(new Person("Grant Gayed")); 131 people.add(new Person("Grant Gayed"));
125 people.add(new Person("Veronika Irvine")); 132 people.add(new Person("Veronika Irvine"));
126 people.add(new Person("Mike Wilson")); 133 people.add(new Person("Mike Wilson"));
127 people.add(new Person("Christophe Cornu")); 134 people.add(new Person("Christophe Cornu"));
137 // The GUI view 144 // The GUI view
138 static class View { 145 static class View {
139 private ViewModel viewModel; 146 private ViewModel viewModel;
140 private Table committers; 147 private Table committers;
141 148
142 public View(ViewModel viewModel) { 149 public this(ViewModel viewModel) {
143 this.viewModel = viewModel; 150 this.viewModel = viewModel;
144 } 151 }
145 152
146 public Shell createShell() { 153 public Shell createShell() {
147 // Build a UI 154 // Build a UI
152 committers.setLinesVisible(true); 159 committers.setLinesVisible(true);
153 TableColumn column = new TableColumn(committers, SWT.NONE); 160 TableColumn column = new TableColumn(committers, SWT.NONE);
154 161
155 // Set up data binding. 162 // Set up data binding.
156 TableViewer peopleViewer = new TableViewer(committers); 163 TableViewer peopleViewer = new TableViewer(committers);
157 ViewerSupport.bind(peopleViewer, new WritableList(viewModel 164
158 .getPeople(), Person.class), BeanProperties.value( 165 ///ViewerSupport.bind(peopleViewer, new WritableList(viewModel
159 Person.class, "name")); 166 /// .getPeople(), Class.fromType!(Person)), BeanProperties.value(
167 /// Class.fromType!(Person), "name"));
168
169 // Create a standard content provider
170 ObservableListContentProvider peopleViewerContentProvider =
171 new ObservableListContentProvider();
172 peopleViewer.setContentProvider(peopleViewerContentProvider);
173
174 // And a standard label provider that maps columns
175 IObservableMap[] attributeMaps = BeansObservables.observeMaps(
176 peopleViewerContentProvider.getKnownElements(), Class.fromType!(Person),
177 [ "name" ]);
178 peopleViewer.setLabelProvider(new ObservableMapLabelProvider(attributeMaps));
179
180 // Now set the Viewer's input
181 peopleViewer.setInput(new WritableList(viewModel.getPeople(), Class.fromType!(Person)));
160 182
161 column.pack(); 183 column.pack();
162 184
163 // Open and return the Shell 185 // Open and return the Shell
164 shell.setSize(100, 300); 186 shell.setSize(100, 300);