Mercurial > projects > dwt2
comparison org.eclipse.jface.examples.databinding/src/org/eclipse/jface/examples/databinding/snippets/Snippet001NestedSelectionWithCombo.d @ 93:18a80add24ac
Work on databinding snippets
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Sun, 19 Apr 2009 13:49:53 +0200 |
parents | 6086085e153d |
children | 5d5bd660917f |
comparison
equal
deleted
inserted
replaced
92:ebefa5c2eab4 | 93:18a80add24ac |
---|---|
9 * The Pampered Chef, Inc. - initial API and implementation | 9 * The Pampered Chef, Inc. - initial API and implementation |
10 * Brad Reynolds - bug 116920 | 10 * Brad Reynolds - bug 116920 |
11 * Matthew Hall - bug 260329 | 11 * Matthew Hall - bug 260329 |
12 ******************************************************************************/ | 12 ******************************************************************************/ |
13 | 13 |
14 package org.eclipse.jface.examples.databinding.snippets; | 14 module org.eclipse.jface.examples.databinding.snippets.Snippet001NestedSelectionWithCombo; |
15 | 15 |
16 import java.lang.all; | |
16 import java.beans.PropertyChangeListener; | 17 import java.beans.PropertyChangeListener; |
17 import java.beans.PropertyChangeSupport; | 18 import java.beans.PropertyChangeSupport; |
18 import java.util.ArrayList; | 19 import java.util.ArrayList; |
19 import java.util.HashSet; | 20 import java.util.HashSet; |
20 | 21 |
43 * At the first level, user may select a person.<br> | 44 * At the first level, user may select a person.<br> |
44 * At the second level, user may select a city to associate with the selected<br> | 45 * At the second level, user may select a city to associate with the selected<br> |
45 * person or edit the person's name. | 46 * person or edit the person's name. |
46 */ | 47 */ |
47 public class Snippet001NestedSelectionWithCombo { | 48 public class Snippet001NestedSelectionWithCombo { |
48 public static void main(String[] args) { | 49 public static void main(String[] args) { |
49 ViewModel viewModel = new ViewModel(); | 50 ViewModel viewModel = new ViewModel(); |
50 Shell shell = new View(viewModel).createShell(); | 51 Shell shell = (new View(viewModel)).createShell(); |
51 | 52 |
52 // The SWT event loop | 53 // The SWT event loop |
53 Display display = Display.getCurrent(); | 54 Display display = Display.getCurrent(); |
54 while (!shell.isDisposed()) { | 55 while (!shell.isDisposed()) { |
55 if (!display.readAndDispatch()) { | 56 if (!display.readAndDispatch()) { |
56 display.sleep(); | 57 display.sleep(); |
57 } | 58 } |
58 } | 59 } |
59 } | 60 } |
60 | 61 |
61 // Minimal JavaBeans support | 62 // Minimal JavaBeans support |
62 public static abstract class AbstractModelObject { | 63 public static abstract class AbstractModelObject { |
63 private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport( | 64 private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport( |
64 this); | 65 this); |
65 | 66 |
66 public void addPropertyChangeListener(PropertyChangeListener listener) { | 67 public void addPropertyChangeListener(PropertyChangeListener listener) { |
67 propertyChangeSupport.addPropertyChangeListener(listener); | 68 propertyChangeSupport.addPropertyChangeListener(listener); |
68 } | 69 } |
69 | 70 |
70 public void addPropertyChangeListener(String propertyName, | 71 public void addPropertyChangeListener(String propertyName, |
71 PropertyChangeListener listener) { | 72 PropertyChangeListener listener) { |
72 propertyChangeSupport.addPropertyChangeListener(propertyName, | 73 propertyChangeSupport.addPropertyChangeListener(propertyName, |
73 listener); | 74 listener); |
74 } | 75 } |
75 | 76 |
76 public void removePropertyChangeListener(PropertyChangeListener listener) { | 77 public void removePropertyChangeListener(PropertyChangeListener listener) { |
77 propertyChangeSupport.removePropertyChangeListener(listener); | 78 propertyChangeSupport.removePropertyChangeListener(listener); |
78 } | 79 } |
79 | 80 |
80 public void removePropertyChangeListener(String propertyName, | 81 public void removePropertyChangeListener(String propertyName, |
81 PropertyChangeListener listener) { | 82 PropertyChangeListener listener) { |
82 propertyChangeSupport.removePropertyChangeListener(propertyName, | 83 propertyChangeSupport.removePropertyChangeListener(propertyName, |
83 listener); | 84 listener); |
84 } | 85 } |
85 | 86 |
86 protected void firePropertyChange(String propertyName, Object oldValue, | 87 protected void firePropertyChange(String propertyName, Object oldValue, |
87 Object newValue) { | 88 Object newValue) { |
88 propertyChangeSupport.firePropertyChange(propertyName, oldValue, | 89 propertyChangeSupport.firePropertyChange(propertyName, oldValue, |
89 newValue); | 90 newValue); |
90 } | 91 } |
91 } | 92 } |
92 | 93 |
93 // The data model class. This is normally a persistent class of some sort. | 94 // The data model class. This is normally a persistent class of some sort. |
94 // | 95 // |
95 // This example implements full JavaBeans bound properties so that changes | 96 // This example implements full JavaBeans bound properties so that changes |
96 // to instances of this class will automatically be propogated to the UI. | 97 // to instances of this class will automatically be propogated to the UI. |
97 public static class Person extends AbstractModelObject { | 98 public static class Person : AbstractModelObject { |
98 // Constructor | 99 // Constructor |
99 public Person(String name, String city) { | 100 public this(String name, String city) { |
100 this.name = name; | 101 this.name = name; |
101 this.city = city; | 102 this.city = city; |
102 } | 103 } |
103 | 104 |
104 // Some JavaBean bound properties... | 105 // Some JavaBean bound properties... |
105 String name; | 106 String name; |
106 | 107 |
107 String city; | 108 String city; |
108 | 109 |
109 public String getName() { | 110 public String getName() { |
110 return name; | 111 return name; |
111 } | 112 } |
112 | 113 |
113 public void setName(String name) { | 114 public void setName(String name) { |
114 String oldValue = this.name; | 115 String oldValue = this.name; |
115 this.name = name; | 116 this.name = name; |
116 firePropertyChange("name", oldValue, name); | 117 firePropertyChange("name", oldValue, name); |
117 } | 118 } |
118 | 119 |
119 public String getCity() { | 120 public String getCity() { |
120 return city; | 121 return city; |
121 } | 122 } |
122 | 123 |
123 public void setCity(String city) { | 124 public void setCity(String city) { |
124 String oldValue = this.city; | 125 String oldValue = this.city; |
125 this.city = city; | 126 this.city = city; |
126 firePropertyChange("city", oldValue, city); | 127 firePropertyChange("city", oldValue, city); |
127 } | 128 } |
128 } | 129 } |
129 | 130 |
130 // The View's model--the root of our GUI's Model graph | 131 // The View's model--the root of our GUI's Model graph |
131 // | 132 // |
132 // Typically each View class has a corresponding ViewModel class. | 133 // Typically each View class has a corresponding ViewModel class. |
133 // The ViewModel is responsible for getting the objects to edit from the | 134 // The ViewModel is responsible for getting the objects to edit from the |
134 // DAO. Since this snippet doesn't have any persistent objects to | 135 // DAO. Since this snippet doesn't have any persistent objects to |
135 // retrieve, this ViewModel just instantiates some objects to edit. | 136 // retrieve, this ViewModel just instantiates some objects to edit. |
136 // | 137 // |
137 // This ViewModel also implements JavaBean bound properties. | 138 // This ViewModel also implements JavaBean bound properties. |
138 static class ViewModel extends AbstractModelObject { | 139 static class ViewModel : AbstractModelObject { |
139 // The model to bind | 140 // The model to bind |
140 private ArrayList people = new ArrayList(); | 141 private ArrayList people; |
141 { | 142 private void initPeople(){ |
142 people.add(new Person("Wile E. Coyote", "Tucson")); | 143 people = new ArrayList(); |
143 people.add(new Person("Road Runner", "Lost Horse")); | 144 people.add(new Person("Wile E. Coyote", "Tucson")); |
144 people.add(new Person("Bugs Bunny", "Forrest")); | 145 people.add(new Person("Road Runner", "Lost Horse")); |
145 } | 146 people.add(new Person("Bugs Bunny", "Forrest")); |
146 | 147 } |
147 // Choice of cities for the Combo | 148 |
148 private ArrayList cities = new ArrayList(); | 149 // Choice of cities for the Combo |
149 { | 150 private ArrayList cities; |
150 cities.add("Tucson"); | 151 private void initCities(){ |
151 cities.add("AcmeTown"); | 152 cities = new ArrayList(); |
152 cities.add("Lost Horse"); | 153 cities.add("Tucson"); |
153 cities.add("Forrest"); | 154 cities.add("AcmeTown"); |
154 cities.add("Lost Mine"); | 155 cities.add("Lost Horse"); |
155 } | 156 cities.add("Forrest"); |
156 | 157 cities.add("Lost Mine"); |
157 public ArrayList getPeople() { | 158 } |
158 return people; | 159 this(){ |
159 } | 160 initPeople(); |
160 | 161 initCities(); |
161 public ArrayList getCities() { | 162 } |
162 return cities; | 163 |
163 } | 164 public ArrayList getPeople() { |
164 } | 165 return people; |
165 | 166 } |
166 // The GUI view | 167 |
167 static class View { | 168 public ArrayList getCities() { |
168 private ViewModel viewModel; | 169 return cities; |
169 | 170 } |
170 public View(ViewModel viewModel) { | 171 } |
171 this.viewModel = viewModel; | 172 |
172 } | 173 // The GUI view |
173 | 174 static class View { |
174 public Shell createShell() { | 175 private ViewModel viewModel; |
175 // Build a UI | 176 |
176 Shell shell = new Shell(Display.getCurrent()); | 177 public this(ViewModel viewModel) { |
177 Realm realm = SWTObservables.getRealm(shell.getDisplay()); | 178 this.viewModel = viewModel; |
178 | 179 } |
179 List peopleList = new List(shell, SWT.BORDER); | 180 |
180 Text name = new Text(shell, SWT.BORDER); | 181 public Shell createShell() { |
181 Combo city = new Combo(shell, SWT.BORDER | SWT.READ_ONLY); | 182 // Build a UI |
182 | 183 Shell shell = new Shell(Display.getCurrent()); |
183 ListViewer peopleListViewer = new ListViewer(peopleList); | 184 Realm realm = SWTObservables.getRealm(shell.getDisplay()); |
184 IObservableMap attributeMap = BeansObservables.observeMap( | 185 |
185 Observables.staticObservableSet(realm, new HashSet( | 186 List peopleList = new List(shell, SWT.BORDER); |
186 viewModel.getPeople())), Person.class, "name"); | 187 Text name = new Text(shell, SWT.BORDER); |
187 peopleListViewer.setLabelProvider(new ObservableMapLabelProvider( | 188 Combo city = new Combo(shell, SWT.BORDER | SWT.READ_ONLY); |
188 attributeMap)); | 189 |
189 peopleListViewer.setContentProvider(new ArrayContentProvider()); | 190 ListViewer peopleListViewer = new ListViewer(peopleList); |
190 peopleListViewer.setInput(viewModel.getPeople()); | 191 IObservableMap attributeMap = BeansObservables.observeMap( |
191 | 192 Observables.staticObservableSet(realm, new HashSet( |
192 DataBindingContext dbc = new DataBindingContext(realm); | 193 viewModel.getPeople())), Class.formType!(Person), "name"); |
193 IObservableValue selectedPerson = ViewersObservables | 194 peopleListViewer.setLabelProvider(new ObservableMapLabelProvider( |
194 .observeSingleSelection(peopleListViewer); | 195 attributeMap)); |
195 dbc.bindValue(SWTObservables.observeText(name, SWT.Modify), | 196 peopleListViewer.setContentProvider(new ArrayContentProvider()); |
196 BeansObservables.observeDetailValue(selectedPerson, | 197 peopleListViewer.setInput(viewModel.getPeople()); |
197 "name", String.class)); | 198 |
198 | 199 DataBindingContext dbc = new DataBindingContext(realm); |
199 ComboViewer cityViewer = new ComboViewer(city); | 200 IObservableValue selectedPerson = ViewersObservables |
200 cityViewer.setContentProvider(new ArrayContentProvider()); | 201 .observeSingleSelection(peopleListViewer); |
201 cityViewer.setInput(viewModel.getCities()); | 202 dbc.bindValue(SWTObservables.observeText(name, SWT.Modify), |
202 | 203 BeansObservables.observeDetailValue(selectedPerson, |
203 IObservableValue citySelection = ViewersObservables | 204 "name", Class.fromType!(String))); |
204 .observeSingleSelection(cityViewer); | 205 |
205 dbc.bindValue(citySelection, BeansObservables.observeDetailValue( | 206 ComboViewer cityViewer = new ComboViewer(city); |
206 selectedPerson, "city", String.class)); | 207 cityViewer.setContentProvider(new ArrayContentProvider()); |
207 | 208 cityViewer.setInput(viewModel.getCities()); |
208 GridLayoutFactory.swtDefaults().applyTo(shell); | 209 |
209 // Open and return the Shell | 210 IObservableValue citySelection = ViewersObservables |
210 shell.pack(); | 211 .observeSingleSelection(cityViewer); |
211 shell.open(); | 212 dbc.bindValue(citySelection, BeansObservables.observeDetailValue( |
212 return shell; | 213 selectedPerson, "city", Class.fromType!(String))); |
213 } | 214 |
214 } | 215 GridLayoutFactory.swtDefaults().applyTo(shell); |
216 // Open and return the Shell | |
217 shell.pack(); | |
218 shell.open(); | |
219 return shell; | |
220 } | |
221 } | |
215 | 222 |
216 } | 223 } |
224 void main( String[] args ){ | |
225 Snippet001NestedSelectionWithCombo.main( args ); | |
226 } | |
227 |