Mercurial > projects > dwt2
comparison org.eclipse.jface.examples.databinding/src/org/eclipse/jface/examples/databinding/snippets/Snippet029TreeViewerMultiListProperty.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) 2009 Matthew Hall 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 * Matthew Hall - initial API and implementation (bug 175735) | |
10 * Matthew Hall - bugs 262407, 260337 | |
11 ******************************************************************************/ | |
12 | |
13 package org.eclipse.jface.examples.databinding.snippets; | |
14 | |
15 import java.beans.PropertyChangeListener; | |
16 import java.beans.PropertyChangeSupport; | |
17 import java.util.ArrayList; | |
18 import java.util.List; | |
19 | |
20 import org.eclipse.core.databinding.beans.BeanProperties; | |
21 import org.eclipse.core.databinding.observable.Realm; | |
22 import org.eclipse.core.databinding.property.list.IListProperty; | |
23 import org.eclipse.core.databinding.property.list.MultiListProperty; | |
24 import org.eclipse.jface.databinding.swt.SWTObservables; | |
25 import org.eclipse.jface.databinding.viewers.ObservableListTreeContentProvider; | |
26 import org.eclipse.jface.databinding.viewers.ObservableMapLabelProvider; | |
27 import org.eclipse.jface.viewers.TreeViewer; | |
28 import org.eclipse.swt.SWT; | |
29 import org.eclipse.swt.graphics.GC; | |
30 import org.eclipse.swt.graphics.Image; | |
31 import org.eclipse.swt.layout.FillLayout; | |
32 import org.eclipse.swt.layout.GridLayout; | |
33 import org.eclipse.swt.widgets.Display; | |
34 import org.eclipse.swt.widgets.Shell; | |
35 | |
36 /** | |
37 * @since 3.2 | |
38 * | |
39 */ | |
40 public class Snippet029TreeViewerMultiListProperty { | |
41 protected Shell shell; | |
42 private TreeViewer viewer; | |
43 | |
44 /** | |
45 * Launch the application | |
46 * | |
47 * @param args | |
48 */ | |
49 public static void main(String[] args) { | |
50 try { | |
51 Snippet029TreeViewerMultiListProperty window = new Snippet029TreeViewerMultiListProperty(); | |
52 window.open(); | |
53 } catch (Exception e) { | |
54 e.printStackTrace(); | |
55 } | |
56 } | |
57 | |
58 /** | |
59 * Open the window | |
60 */ | |
61 public void open() { | |
62 final Display display = Display.getDefault(); | |
63 Realm.runWithDefault(SWTObservables.getRealm(display), new Runnable() { | |
64 public void run() { | |
65 createContents(); | |
66 shell.open(); | |
67 shell.layout(); | |
68 while (!shell.isDisposed()) { | |
69 if (!display.readAndDispatch()) | |
70 display.sleep(); | |
71 } | |
72 } | |
73 }); | |
74 } | |
75 | |
76 protected void createContents() { | |
77 shell = new Shell(); | |
78 shell.setSize(509, 375); | |
79 shell.setText("Snippet028DuplexingObservableValue.java"); | |
80 final GridLayout gridLayout = new GridLayout(); | |
81 gridLayout.makeColumnsEqualWidth = true; | |
82 gridLayout.numColumns = 4; | |
83 shell.setLayout(new FillLayout()); | |
84 | |
85 viewer = new TreeViewer(shell, SWT.FULL_SELECTION | SWT.MULTI | |
86 | SWT.BORDER); | |
87 | |
88 bindUI(); | |
89 } | |
90 | |
91 // Minimal JavaBeans support | |
92 public static abstract class AbstractModelObject { | |
93 private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport( | |
94 this); | |
95 | |
96 public void addPropertyChangeListener(PropertyChangeListener listener) { | |
97 propertyChangeSupport.addPropertyChangeListener(listener); | |
98 } | |
99 | |
100 public void addPropertyChangeListener(String propertyName, | |
101 PropertyChangeListener listener) { | |
102 propertyChangeSupport.addPropertyChangeListener(propertyName, | |
103 listener); | |
104 } | |
105 | |
106 public void removePropertyChangeListener(PropertyChangeListener listener) { | |
107 propertyChangeSupport.removePropertyChangeListener(listener); | |
108 } | |
109 | |
110 public void removePropertyChangeListener(String propertyName, | |
111 PropertyChangeListener listener) { | |
112 propertyChangeSupport.removePropertyChangeListener(propertyName, | |
113 listener); | |
114 } | |
115 | |
116 protected void firePropertyChange(String propertyName, Object oldValue, | |
117 Object newValue) { | |
118 propertyChangeSupport.firePropertyChange(propertyName, oldValue, | |
119 newValue); | |
120 } | |
121 } | |
122 | |
123 public static class Catalog extends AbstractModelObject { | |
124 private String name; | |
125 private List catalogs = new ArrayList(); | |
126 private List items = new ArrayList(); | |
127 | |
128 public Catalog(String name) { | |
129 this.name = name; | |
130 } | |
131 | |
132 public String getName() { | |
133 return name; | |
134 } | |
135 | |
136 public void setName(String name) { | |
137 firePropertyChange("name", this.name, this.name = name); | |
138 } | |
139 | |
140 public List getCatalogs() { | |
141 return catalogs; | |
142 } | |
143 | |
144 public void setCatalogs(List catalogs) { | |
145 firePropertyChange("catalogs", this.catalogs, | |
146 this.catalogs = catalogs); | |
147 } | |
148 | |
149 public List getItems() { | |
150 return items; | |
151 } | |
152 | |
153 public void setItems(List items) { | |
154 firePropertyChange("items", this.items, this.items = items); | |
155 } | |
156 } | |
157 | |
158 public static class CatalogItem extends AbstractModelObject { | |
159 private String name; | |
160 | |
161 public CatalogItem(String name) { | |
162 this.name = name; | |
163 } | |
164 | |
165 public String getName() { | |
166 return name; | |
167 } | |
168 | |
169 public void setName(String name) { | |
170 firePropertyChange("name", this.name, this.name = name); | |
171 } | |
172 } | |
173 | |
174 private void bindUI() { | |
175 List items; | |
176 | |
177 Catalog fruits = new Catalog("Fruits"); | |
178 items = new ArrayList(); | |
179 items.add(new CatalogItem("Apple")); | |
180 items.add(new CatalogItem("Orange")); | |
181 fruits.setCatalogs(items); | |
182 | |
183 Catalog vegetables = new Catalog("Vegetables"); | |
184 items = new ArrayList(); | |
185 items.add(new CatalogItem("Peas")); | |
186 items.add(new CatalogItem("Carrots")); | |
187 items.add(new CatalogItem("Potatoes")); | |
188 vegetables.setItems(items); | |
189 | |
190 Catalog foods = new Catalog("Foods"); | |
191 items = new ArrayList(); | |
192 items.add(fruits); | |
193 items.add(vegetables); | |
194 foods.setCatalogs(items); | |
195 | |
196 items = new ArrayList(); | |
197 items.add(new CatalogItem("Own Hand")); | |
198 foods.setItems(items); | |
199 | |
200 Catalog catalog = new Catalog("Main Catalog"); | |
201 items = new ArrayList(); | |
202 items.add(foods); | |
203 catalog.setCatalogs(items); | |
204 | |
205 IListProperty childrenProperty = new MultiListProperty( | |
206 new IListProperty[] { BeanProperties.list("catalogs"), | |
207 BeanProperties.list("items") }); | |
208 | |
209 ObservableListTreeContentProvider contentProvider = new ObservableListTreeContentProvider( | |
210 childrenProperty.listFactory(), null); | |
211 viewer.setContentProvider(contentProvider); | |
212 | |
213 ObservableMapLabelProvider labelProvider = new ObservableMapLabelProvider( | |
214 BeanProperties.value("name").observeDetail( | |
215 contentProvider.getKnownElements())) { | |
216 Image catalogImage = createCatalogImage(); | |
217 Image catalogItemImage = createCatalogItemImage(); | |
218 | |
219 public Image getImage(Object element) { | |
220 if (element instanceof Catalog) | |
221 return catalogImage; | |
222 if (element instanceof CatalogItem) | |
223 return catalogItemImage; | |
224 return super.getImage(element); | |
225 } | |
226 | |
227 public void dispose() { | |
228 catalogImage.dispose(); | |
229 catalogItemImage.dispose(); | |
230 super.dispose(); | |
231 } | |
232 | |
233 private Image createCatalogImage() { | |
234 Display display = Display.getCurrent(); | |
235 Image catalogImage = new Image(display, 12, 12); | |
236 GC gc = new GC(catalogImage); | |
237 gc.setBackground(display.getSystemColor(SWT.COLOR_GREEN)); | |
238 gc.fillArc(1, 1, 10, 10, 0, 360); | |
239 gc.dispose(); | |
240 return catalogImage; | |
241 } | |
242 | |
243 private Image createCatalogItemImage() { | |
244 Display display = Display.getCurrent(); | |
245 Image catalogImage = new Image(display, 12, 12); | |
246 GC gc = new GC(catalogImage); | |
247 gc.setBackground(display.getSystemColor(SWT.COLOR_CYAN)); | |
248 gc.fillPolygon(new int[] { 1, 10, 5, 1, 6, 1, 10, 10, 1, 10 }); | |
249 gc.dispose(); | |
250 return catalogImage; | |
251 } | |
252 }; | |
253 viewer.setLabelProvider(labelProvider); | |
254 | |
255 viewer.setInput(catalog); | |
256 } | |
257 } |