comparison org.eclipse.jface.examples.databinding/src/org/eclipse/jface/examples/databinding/snippets/Snippet019TreeViewerWithListFactory.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.Snippet019TreeViewerWithListFactory;
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.ArrayList; 18 import java.util.ArrayList;
17 import java.util.List; 19 import java.util.List;
44 import org.eclipse.swt.widgets.Tree; 46 import org.eclipse.swt.widgets.Tree;
45 import org.eclipse.swt.widgets.TreeItem; 47 import org.eclipse.swt.widgets.TreeItem;
46 48
47 public class Snippet019TreeViewerWithListFactory { 49 public class Snippet019TreeViewerWithListFactory {
48 50
49 private Button pasteButton; 51 private Button pasteButton;
50 private Button copyButton; 52 private Button copyButton;
51 private Shell shell; 53 private Shell shell;
52 private Button addChildBeanButton; 54 private Button addChildBeanButton;
53 private Button removeBeanButton; 55 private Button removeBeanButton;
54 private TreeViewer beanViewer; 56 private TreeViewer beanViewer;
55 private Tree tree; 57 private Tree tree;
56 private Text beanText; 58 private Text beanText;
57 private DataBindingContext m_bindingContext; 59 private DataBindingContext m_bindingContext;
58 60
59 private Bean input = createBean("input"); 61 private Bean input = createBean("input");
60 private IObservableValue clipboard; 62 private IObservableValue clipboard;
61 63
62 /** 64 /**
63 * Launch the application 65 * Launch the application
64 * 66 *
65 * @param args 67 * @param args
66 */ 68 */
67 public static void main(String[] args) { 69 public static void main(String[] args) {
68 Display display = Display.getDefault(); 70 Display display = Display.getDefault();
69 Realm.runWithDefault(SWTObservables.getRealm(display), new Runnable() { 71 Realm.runWithDefault(SWTObservables.getRealm(display), new Runnable() {
70 public void run() { 72 public void run() {
71 try { 73 try {
72 Snippet019TreeViewerWithListFactory window = new Snippet019TreeViewerWithListFactory(); 74 Snippet019TreeViewerWithListFactory window = new Snippet019TreeViewerWithListFactory();
73 window.open(); 75 window.open();
74 } catch (Exception e) { 76 } catch (Exception e) {
75 e.printStackTrace(); 77 e.printStackTrace();
76 } 78 }
77 } 79 }
78 }); 80 });
79 } 81 }
80 82
81 /** 83 /**
82 * Open the window 84 * Open the window
83 */ 85 */
84 public void open() { 86 public void open() {
85 final Display display = Display.getDefault(); 87 final Display display = Display.getDefault();
86 createContents(); 88 createContents();
87 shell.open(); 89 shell.open();
88 shell.layout(); 90 shell.layout();
89 while (!shell.isDisposed()) { 91 while (!shell.isDisposed()) {
90 if (!display.readAndDispatch()) 92 if (!display.readAndDispatch())
91 display.sleep(); 93 display.sleep();
92 } 94 }
93 } 95 }
94 96
95 /** 97 /**
96 * Create contents of the window 98 * Create contents of the window
97 */ 99 */
98 protected void createContents() { 100 protected void createContents() {
99 shell = new Shell(); 101 shell = new Shell();
100 final GridLayout gridLayout_1 = new GridLayout(); 102 final GridLayout gridLayout_1 = new GridLayout();
101 gridLayout_1.numColumns = 2; 103 gridLayout_1.numColumns = 2;
102 shell.setLayout(gridLayout_1); 104 shell.setLayout(gridLayout_1);
103 shell.setSize(535, 397); 105 shell.setSize(535, 397);
104 shell.setText("SWT Application"); 106 shell.setText("SWT Application");
105 107
106 final Composite group = new Composite(shell, SWT.NONE); 108 final Composite group = new Composite(shell, SWT.NONE);
107 final RowLayout rowLayout = new RowLayout(); 109 final RowLayout rowLayout = new RowLayout();
108 rowLayout.marginTop = 0; 110 rowLayout.marginTop = 0;
109 rowLayout.marginRight = 0; 111 rowLayout.marginRight = 0;
110 rowLayout.marginLeft = 0; 112 rowLayout.marginLeft = 0;
111 rowLayout.marginBottom = 0; 113 rowLayout.marginBottom = 0;
112 rowLayout.pack = false; 114 rowLayout.pack = false;
113 group.setLayout(rowLayout); 115 group.setLayout(rowLayout);
114 group 116 group
115 .setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 117 .setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false,
116 2, 1)); 118 2, 1));
117 119
118 final Button addRootButton = new Button(group, SWT.NONE); 120 final Button addRootButton = new Button(group, SWT.NONE);
119 addRootButton.addSelectionListener(new SelectionAdapter() { 121 addRootButton.addSelectionListener(new SelectionAdapter() {
120 public void widgetSelected(final SelectionEvent e) { 122 public void widgetSelected(final SelectionEvent e) {
121 List list = input.getList(); 123 List list = input.getList();
122 Bean root = createBean("root"); 124 Bean root = createBean("root");
123 list.add(root); 125 list.add(root);
124 input.setList(list); 126 input.setList(list);
125 127
126 beanViewer.setSelection(new StructuredSelection(root)); 128 beanViewer.setSelection(new StructuredSelection(root));
127 beanText.selectAll(); 129 beanText.selectAll();
128 beanText.setFocus(); 130 beanText.setFocus();
129 } 131 }
130 }); 132 });
131 addRootButton.setText("Add Root"); 133 addRootButton.setText("Add Root");
132 134
133 addChildBeanButton = new Button(group, SWT.NONE); 135 addChildBeanButton = new Button(group, SWT.NONE);
134 addChildBeanButton.addSelectionListener(new SelectionAdapter() { 136 addChildBeanButton.addSelectionListener(new SelectionAdapter() {
135 public void widgetSelected(final SelectionEvent e) { 137 public void widgetSelected(final SelectionEvent e) {
136 Bean parent = getSelectedBean(); 138 Bean parent = getSelectedBean();
137 List list = new ArrayList(parent.getList()); 139 List list = new ArrayList(parent.getList());
138 Bean child = createBean("child"); 140 Bean child = createBean("child");
139 list.add(child); 141 list.add(child);
140 parent.setList(list); 142 parent.setList(list);
141 143
142 beanViewer.setSelection(new StructuredSelection(child)); 144 beanViewer.setSelection(new StructuredSelection(child));
143 beanText.selectAll(); 145 beanText.selectAll();
144 beanText.setFocus(); 146 beanText.setFocus();
145 } 147 }
146 }); 148 });
147 addChildBeanButton.setText("Add Child"); 149 addChildBeanButton.setText("Add Child");
148 150
149 removeBeanButton = new Button(group, SWT.NONE); 151 removeBeanButton = new Button(group, SWT.NONE);
150 removeBeanButton.addSelectionListener(new SelectionAdapter() { 152 removeBeanButton.addSelectionListener(new SelectionAdapter() {
151 public void widgetSelected(final SelectionEvent e) { 153 public void widgetSelected(final SelectionEvent e) {
152 TreeItem selectedItem = beanViewer.getTree().getSelection()[0]; 154 TreeItem selectedItem = beanViewer.getTree().getSelection()[0];
153 TreeItem parentItem = selectedItem.getParentItem(); 155 TreeItem parentItem = selectedItem.getParentItem();
154 Bean parent; 156 Bean parent;
155 int index; 157 int index;
156 if (parentItem == null) { 158 if (parentItem == null) {
157 parent = input; 159 parent = input;
158 index = beanViewer.getTree().indexOf(selectedItem); 160 index = beanViewer.getTree().indexOf(selectedItem);
159 } else { 161 } else {
160 parent = (Bean) parentItem.getData(); 162 parent = (Bean) parentItem.getData();
161 index = parentItem.indexOf(selectedItem); 163 index = parentItem.indexOf(selectedItem);
162 } 164 }
163 165
164 List list = new ArrayList(parent.getList()); 166 List list = new ArrayList(parent.getList());
165 list.remove(index); 167 list.remove(index);
166 parent.setList(list); 168 parent.setList(list);
167 } 169 }
168 }); 170 });
169 removeBeanButton.setText("Remove"); 171 removeBeanButton.setText("Remove");
170 172
171 copyButton = new Button(group, SWT.NONE); 173 copyButton = new Button(group, SWT.NONE);
172 copyButton.addSelectionListener(new SelectionAdapter() { 174 copyButton.addSelectionListener(new SelectionAdapter() {
173 public void widgetSelected(final SelectionEvent e) { 175 public void widgetSelected(final SelectionEvent e) {
174 clipboard.setValue(getSelectedBean()); 176 clipboard.setValue(getSelectedBean());
175 } 177 }
176 }); 178 });
177 copyButton.setText("Copy"); 179 copyButton.setText("Copy");
178 180
179 pasteButton = new Button(group, SWT.NONE); 181 pasteButton = new Button(group, SWT.NONE);
180 pasteButton.addSelectionListener(new SelectionAdapter() { 182 pasteButton.addSelectionListener(new SelectionAdapter() {
181 public void widgetSelected(final SelectionEvent e) { 183 public void widgetSelected(final SelectionEvent e) {
182 Bean copy = (Bean) clipboard.getValue(); 184 Bean copy = (Bean) clipboard.getValue();
183 if (copy == null) 185 if (copy == null)
184 return; 186 return;
185 Bean parent = getSelectedBean(); 187 Bean parent = getSelectedBean();
186 if (parent == null) 188 if (parent == null)
187 parent = input; 189 parent = input;
188 190
189 List list = new ArrayList(parent.getList()); 191 List list = new ArrayList(parent.getList());
190 list.add(copy); 192 list.add(copy);
191 parent.setList(list); 193 parent.setList(list);
192 194
193 beanViewer.setSelection(new StructuredSelection(copy)); 195 beanViewer.setSelection(new StructuredSelection(copy));
194 beanText.selectAll(); 196 beanText.selectAll();
195 beanText.setFocus(); 197 beanText.setFocus();
196 } 198 }
197 }); 199 });
198 pasteButton.setText("Paste"); 200 pasteButton.setText("Paste");
199 201
200 final Button refreshButton = new Button(group, SWT.NONE); 202 final Button refreshButton = new Button(group, SWT.NONE);
201 refreshButton.addSelectionListener(new SelectionAdapter() { 203 refreshButton.addSelectionListener(new SelectionAdapter() {
202 public void widgetSelected(final SelectionEvent e) { 204 public void widgetSelected(final SelectionEvent e) {
203 beanViewer.refresh(); 205 beanViewer.refresh();
204 } 206 }
205 }); 207 });
206 refreshButton.setText("Refresh"); 208 refreshButton.setText("Refresh");
207 209
208 beanViewer = new TreeViewer(shell, SWT.FULL_SELECTION | SWT.BORDER); 210 beanViewer = new TreeViewer(shell, SWT.FULL_SELECTION | SWT.BORDER);
209 beanViewer.setUseHashlookup(true); 211 beanViewer.setUseHashlookup(true);
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.list("list", 279 ViewerSupport.bind(beanViewer, input, BeanProperties.list("list",
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 List list; 287 private List list;
286 288
287 public Bean(String text) { 289 public Bean(String text) {
288 this.text = text; 290 this.text = text;
289 list = new ArrayList(); 291 list = new ArrayList();
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 List getList() { 311 public List getList() {
310 if (list == null) 312 if (list == null)
311 return null; 313 return null;
312 return new ArrayList(list); 314 return new ArrayList(list);
313 } 315 }
314 316
315 public void setList(List list) { 317 public void setList(List list) {
316 if (list != null) 318 if (list != null)
317 list = new ArrayList(list); 319 list = new ArrayList(list);
318 changeSupport.firePropertyChange("list", this.list, 320 changeSupport.firePropertyChange("list", this.list,
319 this.list = list); 321 this.list = list);
320 } 322 }
321 323
322 public boolean hasListeners(String propertyName) { 324 public bool hasListeners(String propertyName) {
323 return changeSupport.hasListeners(propertyName); 325 return changeSupport.hasListeners(propertyName);
324 } 326 }
325 } 327 }
326 } 328 }
329 void main( String[] args ){
330 Snippet019TreeViewerWithListFactory.main(args);
331 }