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