Mercurial > projects > dwt2
comparison org.eclipse.jface.examples.databinding/src/org/eclipse/jface/examples/databinding/snippets/Snippet020TreeViewerWithSetFactory.d @ 99:5d5bd660917f
build some databind snippets
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Wed, 22 Apr 2009 18:59:26 +0200 |
parents | 6086085e153d |
children | e884642ad36e |
comparison
equal
deleted
inserted
replaced
98:48d4ee626868 | 99:5d5bd660917f |
---|---|
7 * | 7 * |
8 * Contributors: | 8 * Contributors: |
9 * IBM Corporation - initial API and implementation | 9 * IBM Corporation - initial API and implementation |
10 * Matthew Hall - bugs 260329, 260337 | 10 * Matthew Hall - bugs 260329, 260337 |
11 *******************************************************************************/ | 11 *******************************************************************************/ |
12 package org.eclipse.jface.examples.databinding.snippets; | 12 module org.eclipse.jface.examples.databinding.snippets.Snippet020TreeViewerWithSetFactory; |
13 | |
14 import java.lang.all; | |
13 | 15 |
14 import java.beans.PropertyChangeListener; | 16 import java.beans.PropertyChangeListener; |
15 import java.beans.PropertyChangeSupport; | 17 import java.beans.PropertyChangeSupport; |
16 import java.util.HashSet; | 18 import java.util.HashSet; |
17 import java.util.Set; | 19 import java.util.Set; |
45 import org.eclipse.swt.widgets.Tree; | 47 import org.eclipse.swt.widgets.Tree; |
46 import org.eclipse.swt.widgets.TreeItem; | 48 import org.eclipse.swt.widgets.TreeItem; |
47 | 49 |
48 public class Snippet020TreeViewerWithSetFactory { | 50 public class Snippet020TreeViewerWithSetFactory { |
49 | 51 |
50 private Button pasteButton; | 52 private Button pasteButton; |
51 private Button copyButton; | 53 private Button copyButton; |
52 private Shell shell; | 54 private Shell shell; |
53 private Button addChildBeanButton; | 55 private Button addChildBeanButton; |
54 private Button removeBeanButton; | 56 private Button removeBeanButton; |
55 private TreeViewer beanViewer; | 57 private TreeViewer beanViewer; |
56 private Tree tree; | 58 private Tree tree; |
57 private Text beanText; | 59 private Text beanText; |
58 private DataBindingContext m_bindingContext; | 60 private DataBindingContext m_bindingContext; |
59 | 61 |
60 private Bean input = createBean("input"); | 62 private Bean input = createBean("input"); |
61 private IObservableValue clipboard; | 63 private IObservableValue clipboard; |
62 static int counter = 0; | 64 static int counter = 0; |
63 | 65 |
64 /** | 66 /** |
65 * Launch the application | 67 * Launch the application |
66 * | 68 * |
67 * @param args | 69 * @param args |
68 */ | 70 */ |
69 public static void main(String[] args) { | 71 public static void main(String[] args) { |
70 Display display = Display.getDefault(); | 72 Display display = Display.getDefault(); |
71 Realm.runWithDefault(SWTObservables.getRealm(display), new Runnable() { | 73 Realm.runWithDefault(SWTObservables.getRealm(display), new Runnable() { |
72 public void run() { | 74 public void run() { |
73 try { | 75 try { |
74 Snippet020TreeViewerWithSetFactory window = new Snippet020TreeViewerWithSetFactory(); | 76 Snippet020TreeViewerWithSetFactory window = new Snippet020TreeViewerWithSetFactory(); |
75 window.open(); | 77 window.open(); |
76 } catch (Exception e) { | 78 } catch (Exception e) { |
77 e.printStackTrace(); | 79 e.printStackTrace(); |
78 } | 80 } |
79 } | 81 } |
80 }); | 82 }); |
81 } | 83 } |
82 | 84 |
83 /** | 85 /** |
84 * Open the window | 86 * Open the window |
85 */ | 87 */ |
86 public void open() { | 88 public void open() { |
87 final Display display = Display.getDefault(); | 89 final Display display = Display.getDefault(); |
88 createContents(); | 90 createContents(); |
89 shell.open(); | 91 shell.open(); |
90 shell.layout(); | 92 shell.layout(); |
91 while (!shell.isDisposed()) { | 93 while (!shell.isDisposed()) { |
92 if (!display.readAndDispatch()) | 94 if (!display.readAndDispatch()) |
93 display.sleep(); | 95 display.sleep(); |
94 } | 96 } |
95 } | 97 } |
96 | 98 |
97 /** | 99 /** |
98 * Create contents of the window | 100 * Create contents of the window |
99 */ | 101 */ |
100 protected void createContents() { | 102 protected void createContents() { |
101 shell = new Shell(); | 103 shell = new Shell(); |
102 final GridLayout gridLayout_1 = new GridLayout(); | 104 final GridLayout gridLayout_1 = new GridLayout(); |
103 gridLayout_1.numColumns = 2; | 105 gridLayout_1.numColumns = 2; |
104 shell.setLayout(gridLayout_1); | 106 shell.setLayout(gridLayout_1); |
105 shell.setSize(535, 397); | 107 shell.setSize(535, 397); |
106 shell.setText("SWT Application"); | 108 shell.setText("SWT Application"); |
107 | 109 |
108 final Composite group = new Composite(shell, SWT.NONE); | 110 final Composite group = new Composite(shell, SWT.NONE); |
109 final RowLayout rowLayout = new RowLayout(); | 111 final RowLayout rowLayout = new RowLayout(); |
110 rowLayout.marginTop = 0; | 112 rowLayout.marginTop = 0; |
111 rowLayout.marginRight = 0; | 113 rowLayout.marginRight = 0; |
112 rowLayout.marginLeft = 0; | 114 rowLayout.marginLeft = 0; |
113 rowLayout.marginBottom = 0; | 115 rowLayout.marginBottom = 0; |
114 rowLayout.pack = false; | 116 rowLayout.pack = false; |
115 group.setLayout(rowLayout); | 117 group.setLayout(rowLayout); |
116 group | 118 group |
117 .setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, | 119 .setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, |
118 2, 1)); | 120 2, 1)); |
119 | 121 |
120 final Button addRootButton = new Button(group, SWT.NONE); | 122 final Button addRootButton = new Button(group, SWT.NONE); |
121 addRootButton.addSelectionListener(new SelectionAdapter() { | 123 addRootButton.addSelectionListener(new SelectionAdapter() { |
122 public void widgetSelected(final SelectionEvent e) { | 124 public void widgetSelected(final SelectionEvent e) { |
123 Set set = input.getSet(); | 125 Set set = input.getSet(); |
124 Bean root = createBean("root"); | 126 Bean root = createBean("root"); |
125 set.add(root); | 127 set.add(root); |
126 input.setSet(set); | 128 input.setSet(set); |
127 | 129 |
128 beanViewer.setSelection(new StructuredSelection(root)); | 130 beanViewer.setSelection(new StructuredSelection(root)); |
129 beanText.selectAll(); | 131 beanText.selectAll(); |
130 beanText.setFocus(); | 132 beanText.setFocus(); |
131 } | 133 } |
132 }); | 134 }); |
133 addRootButton.setText("Add Root"); | 135 addRootButton.setText("Add Root"); |
134 | 136 |
135 addChildBeanButton = new Button(group, SWT.NONE); | 137 addChildBeanButton = new Button(group, SWT.NONE); |
136 addChildBeanButton.addSelectionListener(new SelectionAdapter() { | 138 addChildBeanButton.addSelectionListener(new SelectionAdapter() { |
137 public void widgetSelected(final SelectionEvent e) { | 139 public void widgetSelected(final SelectionEvent e) { |
138 Bean parent = getSelectedBean(); | 140 Bean parent = getSelectedBean(); |
139 Set set = new HashSet(parent.getSet()); | 141 Set set = new HashSet(parent.getSet()); |
140 Bean child = createBean("child" + (counter++)); | 142 Bean child = createBean("child" + (counter++)); |
141 set.add(child); | 143 set.add(child); |
142 parent.setSet(set); | 144 parent.setSet(set); |
143 | 145 |
144 // beanViewer.setSelection(new StructuredSelection(parent)); | 146 // beanViewer.setSelection(new StructuredSelection(parent)); |
145 // beanText.selectAll(); | 147 // beanText.selectAll(); |
146 // beanText.setFocus(); | 148 // beanText.setFocus(); |
147 } | 149 } |
148 }); | 150 }); |
149 addChildBeanButton.setText("Add Child"); | 151 addChildBeanButton.setText("Add Child"); |
150 | 152 |
151 removeBeanButton = new Button(group, SWT.NONE); | 153 removeBeanButton = new Button(group, SWT.NONE); |
152 removeBeanButton.addSelectionListener(new SelectionAdapter() { | 154 removeBeanButton.addSelectionListener(new SelectionAdapter() { |
153 public void widgetSelected(final SelectionEvent e) { | 155 public void widgetSelected(final SelectionEvent e) { |
154 TreeItem selectedItem = beanViewer.getTree().getSelection()[0]; | 156 TreeItem selectedItem = beanViewer.getTree().getSelection()[0]; |
155 Bean bean = (Bean) selectedItem.getData(); | 157 Bean bean = (Bean) selectedItem.getData(); |
156 TreeItem parentItem = selectedItem.getParentItem(); | 158 TreeItem parentItem = selectedItem.getParentItem(); |
157 Bean parent; | 159 Bean parent; |
158 if (parentItem == null) | 160 if (parentItem == null) |
159 parent = input; | 161 parent = input; |
160 else | 162 else |
161 parent = (Bean) parentItem.getData(); | 163 parent = (Bean) parentItem.getData(); |
162 | 164 |
163 Set set = new HashSet(parent.getSet()); | 165 Set set = new HashSet(parent.getSet()); |
164 set.remove(bean); | 166 set.remove(bean); |
165 parent.setSet(set); | 167 parent.setSet(set); |
166 } | 168 } |
167 }); | 169 }); |
168 removeBeanButton.setText("Remove"); | 170 removeBeanButton.setText("Remove"); |
169 | 171 |
170 copyButton = new Button(group, SWT.NONE); | 172 copyButton = new Button(group, SWT.NONE); |
171 copyButton.addSelectionListener(new SelectionAdapter() { | 173 copyButton.addSelectionListener(new SelectionAdapter() { |
172 public void widgetSelected(final SelectionEvent e) { | 174 public void widgetSelected(final SelectionEvent e) { |
173 clipboard.setValue(getSelectedBean()); | 175 clipboard.setValue(getSelectedBean()); |
174 } | 176 } |
175 }); | 177 }); |
176 copyButton.setText("Copy"); | 178 copyButton.setText("Copy"); |
177 | 179 |
178 pasteButton = new Button(group, SWT.NONE); | 180 pasteButton = new Button(group, SWT.NONE); |
179 pasteButton.addSelectionListener(new SelectionAdapter() { | 181 pasteButton.addSelectionListener(new SelectionAdapter() { |
180 public void widgetSelected(final SelectionEvent e) { | 182 public void widgetSelected(final SelectionEvent e) { |
181 Bean copy = (Bean) clipboard.getValue(); | 183 Bean copy = (Bean) clipboard.getValue(); |
182 if (copy == null) | 184 if (copy == null) |
183 return; | 185 return; |
184 Bean parent = getSelectedBean(); | 186 Bean parent = getSelectedBean(); |
185 if (parent == null) | 187 if (parent == null) |
186 parent = input; | 188 parent = input; |
187 | 189 |
188 Set set = new HashSet(parent.getSet()); | 190 Set set = new HashSet(parent.getSet()); |
189 set.add(copy); | 191 set.add(copy); |
190 parent.setSet(set); | 192 parent.setSet(set); |
191 | 193 |
192 beanViewer.setSelection(new StructuredSelection(copy)); | 194 beanViewer.setSelection(new StructuredSelection(copy)); |
193 beanText.selectAll(); | 195 beanText.selectAll(); |
194 beanText.setFocus(); | 196 beanText.setFocus(); |
195 } | 197 } |
196 }); | 198 }); |
197 pasteButton.setText("Paste"); | 199 pasteButton.setText("Paste"); |
198 | 200 |
199 final Button refreshButton = new Button(group, SWT.NONE); | 201 final Button refreshButton = new Button(group, SWT.NONE); |
200 refreshButton.addSelectionListener(new SelectionAdapter() { | 202 refreshButton.addSelectionListener(new SelectionAdapter() { |
201 public void widgetSelected(final SelectionEvent e) { | 203 public void widgetSelected(final SelectionEvent e) { |
202 beanViewer.refresh(); | 204 beanViewer.refresh(); |
203 } | 205 } |
204 }); | 206 }); |
205 refreshButton.setText("Refresh"); | 207 refreshButton.setText("Refresh"); |
206 | 208 |
207 beanViewer = new TreeViewer(shell, SWT.FULL_SELECTION | SWT.BORDER); | 209 beanViewer = new TreeViewer(shell, SWT.FULL_SELECTION | SWT.BORDER); |
208 beanViewer.setUseHashlookup(true); | 210 beanViewer.setUseHashlookup(true); |
209 beanViewer.setComparator(new ViewerComparator()); | 211 beanViewer.setComparator(new ViewerComparator()); |
210 tree = beanViewer.getTree(); | 212 tree = beanViewer.getTree(); |
211 tree.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1)); | 213 tree.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1)); |
212 | 214 |
213 final Label itemNameLabel = new Label(shell, SWT.NONE); | 215 final Label itemNameLabel = new Label(shell, SWT.NONE); |
214 itemNameLabel.setText("Item Name"); | 216 itemNameLabel.setText("Item Name"); |
215 | 217 |
216 beanText = new Text(shell, SWT.BORDER); | 218 beanText = new Text(shell, SWT.BORDER); |
217 final GridData gd_beanValue = new GridData(SWT.FILL, SWT.CENTER, true, | 219 final GridData gd_beanValue = new GridData(SWT.FILL, SWT.CENTER, true, |
218 false); | 220 false); |
219 beanText.setLayoutData(gd_beanValue); | 221 beanText.setLayoutData(gd_beanValue); |
220 m_bindingContext = initDataBindings(); | 222 m_bindingContext = initDataBindings(); |
221 // | 223 // |
222 initExtraBindings(m_bindingContext); | 224 initExtraBindings(m_bindingContext); |
223 } | 225 } |
224 | 226 |
225 private static Bean createBean(String name) { | 227 private static Bean createBean(String name) { |
226 return new Bean(name); | 228 return new Bean(name); |
227 } | 229 } |
228 | 230 |
229 protected DataBindingContext initDataBindings() { | 231 protected DataBindingContext initDataBindings() { |
230 IObservableValue treeViewerSelectionObserveSelection = ViewersObservables | 232 IObservableValue treeViewerSelectionObserveSelection = ViewersObservables |
231 .observeSingleSelection(beanViewer); | 233 .observeSingleSelection(beanViewer); |
232 IObservableValue textTextObserveWidget = SWTObservables.observeText( | 234 IObservableValue textTextObserveWidget = SWTObservables.observeText( |
233 beanText, SWT.Modify); | 235 beanText, SWT.Modify); |
234 IObservableValue treeViewerValueObserveDetailValue = BeansObservables | 236 IObservableValue treeViewerValueObserveDetailValue = BeansObservables |
235 .observeDetailValue(treeViewerSelectionObserveSelection, | 237 .observeDetailValue(treeViewerSelectionObserveSelection, |
236 "text", String.class); | 238 "text", String.class); |
237 // | 239 // |
238 // | 240 // |
239 DataBindingContext bindingContext = new DataBindingContext(); | 241 DataBindingContext bindingContext = new DataBindingContext(); |
240 // | 242 // |
241 bindingContext.bindValue(textTextObserveWidget, | 243 bindingContext.bindValue(textTextObserveWidget, |
242 treeViewerValueObserveDetailValue); | 244 treeViewerValueObserveDetailValue); |
243 // | 245 // |
244 return bindingContext; | 246 return bindingContext; |
245 } | 247 } |
246 | 248 |
247 private Bean getSelectedBean() { | 249 private Bean getSelectedBean() { |
248 IStructuredSelection selection = (IStructuredSelection) beanViewer | 250 IStructuredSelection selection = (IStructuredSelection) beanViewer |
249 .getSelection(); | 251 .getSelection(); |
250 if (selection.isEmpty()) | 252 if (selection.isEmpty()) |
251 return null; | 253 return null; |
252 return (Bean) selection.getFirstElement(); | 254 return (Bean) selection.getFirstElement(); |
253 } | 255 } |
254 | 256 |
255 private void initExtraBindings(DataBindingContext dbc) { | 257 private void initExtraBindings(DataBindingContext dbc) { |
256 final IObservableValue beanViewerSelection = ViewersObservables | 258 final IObservableValue beanViewerSelection = ViewersObservables |
257 .observeSingleSelection(beanViewer); | 259 .observeSingleSelection(beanViewer); |
258 IObservableValue beanSelected = new ComputedValue(Boolean.TYPE) { | 260 IObservableValue beanSelected = new ComputedValue(bool.TYPE) { |
259 protected Object calculate() { | 261 protected Object calculate() { |
260 return Boolean.valueOf(beanViewerSelection.getValue() != null); | 262 return bool.valueOf(beanViewerSelection.getValue() != null); |
261 } | 263 } |
262 }; | 264 }; |
263 dbc.bindValue(SWTObservables.observeEnabled(addChildBeanButton), | 265 dbc.bindValue(SWTObservables.observeEnabled(addChildBeanButton), |
264 beanSelected); | 266 beanSelected); |
265 dbc.bindValue(SWTObservables.observeEnabled(removeBeanButton), | 267 dbc.bindValue(SWTObservables.observeEnabled(removeBeanButton), |
266 beanSelected); | 268 beanSelected); |
267 | 269 |
268 clipboard = new WritableValue(); | 270 clipboard = new WritableValue(); |
269 dbc.bindValue(SWTObservables.observeEnabled(copyButton), beanSelected); | 271 dbc.bindValue(SWTObservables.observeEnabled(copyButton), beanSelected); |
270 dbc.bindValue(SWTObservables.observeEnabled(pasteButton), | 272 dbc.bindValue(SWTObservables.observeEnabled(pasteButton), |
271 new ComputedValue(Boolean.TYPE) { | 273 new ComputedValue(bool.TYPE) { |
272 protected Object calculate() { | 274 protected Object calculate() { |
273 return Boolean.valueOf(clipboard.getValue() != null); | 275 return bool.valueOf(clipboard.getValue() != null); |
274 } | 276 } |
275 }); | 277 }); |
276 | 278 |
277 ViewerSupport.bind(beanViewer, input, BeanProperties.set("set", | 279 ViewerSupport.bind(beanViewer, input, BeanProperties.set("set", |
278 Bean.class), BeanProperties.value(Bean.class, "text")); | 280 Bean.class), BeanProperties.value(Bean.class, "text")); |
279 } | 281 } |
280 | 282 |
281 static class Bean { | 283 static class Bean { |
282 /* package */PropertyChangeSupport changeSupport = new PropertyChangeSupport( | 284 /* package */PropertyChangeSupport changeSupport = new PropertyChangeSupport( |
283 this); | 285 this); |
284 private String text; | 286 private String text; |
285 private Set set; | 287 private Set set; |
286 | 288 |
287 public Bean(String text) { | 289 public Bean(String text) { |
288 this.text = text; | 290 this.text = text; |
289 set = new HashSet(); | 291 set = new HashSet(); |
290 } | 292 } |
291 | 293 |
292 public void addPropertyChangeListener(PropertyChangeListener listener) { | 294 public void addPropertyChangeListener(PropertyChangeListener listener) { |
293 changeSupport.addPropertyChangeListener(listener); | 295 changeSupport.addPropertyChangeListener(listener); |
294 } | 296 } |
295 | 297 |
296 public void removePropertyChangeListener(PropertyChangeListener listener) { | 298 public void removePropertyChangeListener(PropertyChangeListener listener) { |
297 changeSupport.removePropertyChangeListener(listener); | 299 changeSupport.removePropertyChangeListener(listener); |
298 } | 300 } |
299 | 301 |
300 public String getText() { | 302 public String getText() { |
301 return text; | 303 return text; |
302 } | 304 } |
303 | 305 |
304 public void setText(String value) { | 306 public void setText(String value) { |
305 changeSupport.firePropertyChange("text", this.text, | 307 changeSupport.firePropertyChange("text", this.text, |
306 this.text = value); | 308 this.text = value); |
307 } | 309 } |
308 | 310 |
309 public Set getSet() { | 311 public Set getSet() { |
310 if (set == null) | 312 if (set == null) |
311 return null; | 313 return null; |
312 return new HashSet(set); | 314 return new HashSet(set); |
313 } | 315 } |
314 | 316 |
315 public void setSet(Set set) { | 317 public void setSet(Set set) { |
316 if (set != null) | 318 if (set != null) |
317 set = new HashSet(set); | 319 set = new HashSet(set); |
318 changeSupport.firePropertyChange("set", this.set, this.set = set); | 320 changeSupport.firePropertyChange("set", this.set, this.set = set); |
319 } | 321 } |
320 | 322 |
321 public boolean hasListeners(String propertyName) { | 323 public bool hasListeners(String propertyName) { |
322 return changeSupport.hasListeners(propertyName); | 324 return changeSupport.hasListeners(propertyName); |
323 } | 325 } |
324 } | 326 } |
325 } | 327 } |
328 void main( String[] args ){ | |
329 Snippet020TreeViewerWithSetFactory.main(args); | |
330 } |