78
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2006, 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 * Stefan Xenos, IBM - initial API and implementation
|
|
11 *******************************************************************************/
|
|
12 module org.eclipse.jface.internal.databinding.provisional.viewers.TreeNode;
|
85
|
13 import org.eclipse.jface.internal.databinding.provisional.viewers.UnorderedTreeContentProvider;
|
78
|
14
|
|
15 import java.lang.all;
|
|
16
|
|
17 import java.util.Collections;
|
|
18 import java.util.HashSet;
|
|
19 import java.util.Set;
|
|
20
|
|
21 import org.eclipse.core.databinding.observable.IStaleListener;
|
|
22 import org.eclipse.core.databinding.observable.Observables;
|
|
23 import org.eclipse.core.databinding.observable.StaleEvent;
|
|
24 import org.eclipse.core.databinding.observable.set.IObservableSet;
|
|
25 import org.eclipse.core.databinding.observable.set.ISetChangeListener;
|
|
26 import org.eclipse.core.databinding.observable.set.SetChangeEvent;
|
|
27 import org.eclipse.core.databinding.observable.set.SetDiff;
|
|
28 import org.eclipse.jface.databinding.viewers.ObservableListTreeContentProvider;
|
|
29 import org.eclipse.jface.databinding.viewers.ObservableSetTreeContentProvider;
|
|
30 import org.eclipse.jface.viewers.TreeViewer;
|
|
31 import org.eclipse.swt.widgets.Control;
|
|
32
|
|
33 /**
|
|
34 *
|
|
35 * @since 1.0
|
|
36 * @deprecated Use {@link ObservableSetTreeContentProvider} or
|
|
37 * {@link ObservableListTreeContentProvider} instead.
|
|
38 */
|
|
39 /* package */ class TreeNode : ISetChangeListener, IStaleListener {
|
|
40 private UnorderedTreeContentProvider contentProvider;
|
|
41 private Object element;
|
|
42
|
|
43 // Stores the set of parents (null if there are less than 2)
|
|
44 private HashSet parents = null;
|
|
45
|
|
46 // Stores one representative parent. If there is more than one parent,
|
|
47 // the complete set of parents can be found in the parents set.
|
|
48 Object parent;
|
|
49
|
|
50 /**
|
|
51 * Set of child elements.
|
|
52 */
|
|
53 private IObservableSet children;
|
|
54
|
|
55 private bool hasPendingNode = false;
|
85
|
56 private bool isStale_;
|
78
|
57 private bool listeningToChildren = false;
|
|
58 private bool prefetchEnqueued = false;
|
|
59
|
|
60 /**
|
|
61 * @param element
|
|
62 * @param cp
|
|
63 */
|
|
64 public this(Object element, UnorderedTreeContentProvider cp) {
|
|
65 this.element = element;
|
|
66 this.contentProvider = cp;
|
|
67 children = contentProvider.createChildSet(element);
|
|
68 if (children is null) {
|
|
69 children = Observables.emptyObservableSet();
|
|
70 listeningToChildren = true;
|
|
71 }
|
|
72 hasPendingNode = children.isStale();
|
|
73 }
|
|
74
|
|
75 /**
|
|
76 * @param parent
|
|
77 */
|
|
78 public void addParent(Object parent) {
|
|
79 if (this.parent is null) {
|
|
80 this.parent = parent;
|
|
81 } else {
|
85
|
82 if (parent.opEquals(this.parent)) {
|
78
|
83 return;
|
|
84 }
|
|
85 if (parents is null) {
|
|
86 parents = new HashSet();
|
|
87 parents.add(this.parent);
|
|
88 }
|
|
89 parents.add(parent);
|
|
90 }
|
|
91 }
|
|
92
|
|
93 /**
|
|
94 * @param parent
|
|
95 */
|
|
96 public void removeParent(Object parent) {
|
|
97 if (this.parents !is null) {
|
|
98 parents.remove(parent);
|
|
99 }
|
|
100
|
|
101 if (parent is this.parent) {
|
|
102 if (parents is null || parents.isEmpty()) {
|
|
103 this.parent = null;
|
|
104 } else {
|
|
105 this.parent = parents.iterator().next();
|
|
106 }
|
|
107 }
|
|
108
|
|
109 if (this.parents !is null && this.parents.size() <= 1) {
|
|
110 this.parents = null;
|
|
111 }
|
|
112 }
|
|
113
|
|
114 /**
|
|
115 * Returns the set of children for this node. If new children are discovered later, they
|
|
116 * will be added directly to the viewer.
|
|
117 *
|
|
118 * @return TODO
|
|
119 */
|
|
120 public Set getChildren() {
|
|
121 if (!listeningToChildren) {
|
|
122 listeningToChildren = true;
|
|
123 children.addSetChangeListener(this);
|
|
124 hasPendingNode = children.isEmpty() && children.isStale();
|
|
125 children.addStaleListener(this);
|
|
126 updateStale();
|
|
127 }
|
|
128
|
|
129 // If the child set is stale and empty, show the "pending" node
|
|
130 if (hasPendingNode) {
|
|
131 Object pendingNode = contentProvider.getPendingNode();
|
|
132 return Collections.singleton(pendingNode);
|
|
133 }
|
|
134 return children;
|
|
135 }
|
|
136
|
|
137 /**
|
|
138 * @return TODO
|
|
139 */
|
|
140 public IObservableSet getChildrenSet() {
|
|
141 return children;
|
|
142 }
|
|
143
|
|
144 private void updateStale() {
|
|
145 bool willBeStale = children.isStale();
|
85
|
146 if (willBeStale !is isStale_) {
|
|
147 isStale_ = willBeStale;
|
78
|
148
|
85
|
149 contentProvider.changeStale(isStale_? 1 : -1);
|
78
|
150 }
|
|
151 }
|
|
152
|
|
153 /**
|
|
154 * @return TODO
|
|
155 */
|
|
156 public bool isStale() {
|
85
|
157 return isStale_;
|
78
|
158 }
|
|
159
|
|
160 /**
|
|
161 * Returns true if the viewer should show a plus sign for expanding this
|
|
162 * node.
|
|
163 *
|
|
164 * @return TODO
|
|
165 */
|
|
166 public bool shouldShowPlus() {
|
|
167 if (children is null) {
|
|
168 // if (!hasPendingNode) {
|
|
169 // hasPendingNode = true;
|
|
170 // contentProvider.add(element, Collections.singleton(contentProvider.getPendingNode()));
|
|
171 // }
|
|
172 return true;
|
|
173 }
|
|
174 if (!listeningToChildren && !prefetchEnqueued) {
|
|
175 prefetchEnqueued = true;
|
|
176 contentProvider.enqueuePrefetch(this);
|
|
177 }
|
|
178 return !listeningToChildren || hasPendingNode || !children.isEmpty();
|
|
179 }
|
|
180
|
|
181 /**
|
|
182 * Disposes this node and removes all remaining children.
|
|
183 */
|
|
184 public void dispose() {
|
|
185 if (children !is null) {
|
|
186 if (listeningToChildren) {
|
|
187 contentProvider.remove(element, children, true);
|
|
188 children.removeSetChangeListener(this);
|
|
189 children.removeStaleListener(this);
|
|
190 }
|
|
191 children.dispose();
|
|
192 children = null;
|
|
193
|
85
|
194 if (listeningToChildren && isStale_) {
|
78
|
195 contentProvider.changeStale(-1);
|
|
196 }
|
|
197 }
|
|
198 }
|
|
199
|
|
200 /**
|
|
201 * @return TODO
|
|
202 */
|
|
203 public bool isDisposed() {
|
|
204 return children is null;
|
|
205 }
|
|
206
|
|
207 /**
|
|
208 * Returns one representative parent, or null if this node is unparented. Use
|
|
209 * getParents() to get the complete set of known parents.
|
|
210 *
|
|
211 * @return TODO
|
|
212 */
|
|
213 public Object getParent() {
|
|
214 return parent;
|
|
215 }
|
|
216
|
|
217 /**
|
|
218 *
|
|
219 * @return the set of all known parents for this node
|
|
220 */
|
|
221 public Set getParents() {
|
|
222 if (parents is null) {
|
|
223 if (parent is null) {
|
|
224 return Collections.EMPTY_SET;
|
|
225 }
|
|
226 return Collections.singleton(parent);
|
|
227 }
|
|
228 return parents;
|
|
229 }
|
|
230
|
|
231 /**
|
|
232 * Called when the child set changes. Should not be called directly by the viewer.
|
|
233 */
|
|
234 public void handleSetChange(SetChangeEvent event) {
|
|
235 SetDiff diff = event.diff;
|
|
236 TreeViewer viewer = this.contentProvider.getViewer();
|
|
237 if (viewer !is null) {
|
|
238 Control control = viewer.getControl();
|
|
239 if (control !is null) {
|
|
240 if (control.isDisposed()) {
|
|
241 // If the widgetry was disposed without notifying the content provider, then
|
|
242 // dispose the content provider now and stop processing events.
|
|
243 contentProvider.dispose();
|
|
244 return;
|
|
245 }
|
|
246 }
|
|
247 }
|
|
248
|
|
249 bool shouldHavePendingNode = children.isEmpty() && children.isStale();
|
|
250
|
|
251 Set additions = diff.getAdditions();
|
|
252 // Check if we should add the pending node
|
|
253 if (shouldHavePendingNode && !hasPendingNode) {
|
|
254 HashSet newAdditions = new HashSet();
|
|
255 newAdditions.addAll(additions);
|
|
256 newAdditions.add(contentProvider.getPendingNode());
|
|
257 additions = newAdditions;
|
|
258 hasPendingNode = true;
|
|
259 }
|
|
260
|
|
261 Set removals = diff.getRemovals();
|
|
262 // Check if we should remove the pending node
|
|
263 if (!shouldHavePendingNode && hasPendingNode) {
|
|
264 HashSet newRemovals = new HashSet();
|
|
265 newRemovals.addAll(removals);
|
|
266 newRemovals.add(contentProvider.getPendingNode());
|
|
267 removals = newRemovals;
|
|
268 hasPendingNode = false;
|
|
269 }
|
|
270 if (!additions.isEmpty()) {
|
|
271 contentProvider.add(element, additions);
|
|
272 }
|
|
273 if (!removals.isEmpty()) {
|
|
274 contentProvider.remove(element, removals, children.isEmpty() && !hasPendingNode);
|
|
275 }
|
|
276
|
|
277 updateStale();
|
|
278 }
|
|
279
|
|
280 public void handleStale(StaleEvent staleEvent) {
|
|
281 TreeViewer viewer = this.contentProvider.getViewer();
|
|
282 if (viewer !is null) {
|
|
283 Control control = viewer.getControl();
|
|
284 if (control !is null) {
|
|
285 if (control.isDisposed()) {
|
|
286 // If the widgetry was disposed without notifying the content provider, then
|
|
287 // dispose the content provider now and stop processing events.
|
|
288 contentProvider.dispose();
|
|
289 return;
|
|
290 }
|
|
291 }
|
|
292 }
|
|
293
|
|
294 bool shouldHavePendingNode = children.isEmpty() && children.isStale();
|
|
295
|
|
296 // Check if we should add the pending node
|
|
297 if (shouldHavePendingNode && !hasPendingNode) {
|
|
298 hasPendingNode = shouldHavePendingNode;
|
|
299 contentProvider.add(element, Collections.singleton(contentProvider.getPendingNode()));
|
|
300 }
|
|
301
|
|
302 // Check if we should remove the pending node
|
|
303 if (!shouldHavePendingNode && hasPendingNode) {
|
|
304 hasPendingNode = shouldHavePendingNode;
|
|
305 contentProvider.remove(element, Collections.singleton(contentProvider.getPendingNode()), true);
|
|
306 }
|
|
307
|
|
308 updateStale();
|
|
309 }
|
|
310
|
|
311 /**
|
|
312 * @return TODO
|
|
313 */
|
|
314 public Object getElement() {
|
|
315 return element;
|
|
316 }
|
|
317
|
|
318 /**
|
|
319 *
|
|
320 */
|
|
321 public void prefetch() {
|
|
322 TreeViewer viewer = this.contentProvider.getViewer();
|
|
323 if (viewer !is null) {
|
|
324 Control control = viewer.getControl();
|
|
325 if (control !is null) {
|
|
326 if (control.isDisposed()) {
|
|
327 // If the widgetry has been disposed, then avoid sending anything
|
|
328 // to the viewer.
|
|
329 return;
|
|
330 }
|
|
331 }
|
|
332 }
|
|
333
|
|
334 Set children = getChildren();
|
|
335 if (!children.isEmpty()) {
|
|
336 contentProvider.add(element, children);
|
|
337 } else {
|
|
338 // We need to remove the + sign, and adding/removing elements won't do the trick
|
|
339 contentProvider.getViewer().refresh(element);
|
|
340 }
|
|
341 }
|
|
342 }
|