comparison org.eclipse.jface.examples.databinding/src/org/eclipse/jface/examples/databinding/snippets/Snippet020TreeViewerWithSetFactory.d @ 90:6086085e153d

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