Mercurial > projects > dwt-addons
annotate dwtx/jface/viewers/AbstractTreeViewer.d @ 40:da5ad8eedf5d
debug prints, dwt.dwthelper restructure, ...
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Thu, 10 Apr 2008 08:59:39 +0200 |
parents | 644f1334b451 |
children | ea8ff534f622 |
rev | line source |
---|---|
10 | 1 /******************************************************************************* |
2 * Copyright (c) 2000, 2007 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 * Tom Schindl <tom.schindl@bestsolution.at> - bug 153993, bug 167323, bug 175192 | |
11 * Port to the D programming language: | |
12 * Frank Benoit <benoit@tionex.de> | |
13 *******************************************************************************/ | |
14 | |
15 module dwtx.jface.viewers.AbstractTreeViewer; | |
16 | |
17 import dwtx.jface.viewers.ColumnViewer; | |
18 import dwtx.jface.viewers.TreePath; | |
19 import dwtx.jface.viewers.ViewerComparator; | |
20 import dwtx.jface.viewers.ITreeViewerListener; | |
21 import dwtx.jface.viewers.TreeExpansionEvent; | |
22 import dwtx.jface.viewers.CustomHashtable; | |
23 import dwtx.jface.viewers.IContentProvider; | |
24 import dwtx.jface.viewers.ISelection; | |
25 import dwtx.jface.viewers.ColumnViewerEditor; | |
26 import dwtx.jface.viewers.ViewerLabel; | |
27 import dwtx.jface.viewers.ViewerRow; | |
28 import dwtx.jface.viewers.TreePathViewerSorter; | |
29 import dwtx.jface.viewers.ViewerFilter; | |
30 import dwtx.jface.viewers.ViewerColumn; | |
31 import dwtx.jface.viewers.ViewerCell; | |
32 import dwtx.jface.viewers.ITreePathContentProvider; | |
33 import dwtx.jface.viewers.ITreeContentProvider; | |
34 import dwtx.jface.viewers.TreeSelection; | |
35 import dwtx.jface.viewers.DoubleClickEvent; | |
36 import dwtx.jface.viewers.IElementComparer; | |
37 import dwtx.jface.viewers.ITreeSelection; | |
38 import dwtx.jface.viewers.IBaseLabelProvider; | |
39 import dwtx.jface.viewers.ITreePathLabelProvider; | |
40 | |
41 import tango.util.collection.LinkSeq; | |
42 import tango.util.collection.ArraySeq; | |
43 import tango.util.collection.model.Seq; | |
44 import tango.util.collection.model.SeqView; | |
45 | |
46 | |
47 import dwt.DWT; | |
48 import dwt.custom.BusyIndicator; | |
49 import dwt.events.SelectionEvent; | |
50 import dwt.events.SelectionListener; | |
51 import dwt.events.TreeEvent; | |
52 import dwt.events.TreeListener; | |
53 import dwt.graphics.Point; | |
54 import dwt.widgets.Control; | |
55 import dwt.widgets.Item; | |
56 import dwt.widgets.Widget; | |
57 import dwtx.core.runtime.Assert; | |
58 import dwtx.core.runtime.ListenerList; | |
59 import dwtx.jface.util.SafeRunnable; | |
60 | |
61 import dwt.dwthelper.utils; | |
62 import dwt.dwthelper.Runnable; | |
40
da5ad8eedf5d
debug prints, dwt.dwthelper restructure, ...
Frank Benoit <benoit@tionex.de>
parents:
39
diff
changeset
|
63 import tango.util.log.Trace; |
10 | 64 |
65 /** | |
66 * Abstract base implementation for tree-structure-oriented viewers (trees and | |
67 * table trees). | |
68 * <p> | |
69 * Nodes in the tree can be in either an expanded or a collapsed state, | |
70 * depending on whether the children on a node are visible. This class | |
71 * introduces public methods for controlling the expanding and collapsing of | |
72 * nodes. | |
73 * </p> | |
74 * <p> | |
75 * As of 3.2, AbstractTreeViewer supports multiple equal elements (each with a | |
76 * different parent chain) in the tree. This support requires that clients | |
77 * enable the element map by calling <code>setUseHashLookup(true)</code>. | |
78 * </p> | |
79 * <p> | |
80 * Content providers for abstract tree viewers must implement one of the | |
81 * interfaces <code>ITreeContentProvider</code> or (as of 3.2, to support | |
82 * multiple equal elements) <code>ITreePathContentProvider</code>. | |
83 * </p> | |
84 * | |
85 * @see TreeViewer | |
86 */ | |
87 public abstract class AbstractTreeViewer : ColumnViewer { | |
88 | |
89 alias ColumnViewer.buildLabel buildLabel; | |
90 alias ColumnViewer.setSelection setSelection; | |
91 | |
92 /** | |
93 * Constant indicating that all levels of the tree should be expanded or | |
94 * collapsed. | |
95 * | |
96 * @see #expandToLevel(int) | |
97 * @see #collapseToLevel(Object, int) | |
98 */ | |
99 public static const int ALL_LEVELS = -1; | |
100 | |
101 /** | |
102 * List of registered tree listeners (element type: | |
103 * <code>TreeListener</code>). | |
104 */ | |
105 private ListenerList treeListeners; | |
106 | |
107 /** | |
108 * The level to which the tree is automatically expanded each time the | |
109 * viewer's input is changed (that is, by <code>setInput</code>). A value | |
110 * of 0 means that auto-expand is off. | |
111 * | |
112 * @see #setAutoExpandLevel | |
113 */ | |
114 private int expandToLevel_ = 0; | |
115 | |
116 /** | |
117 * Safe runnable used to update an item. | |
118 */ | |
119 class UpdateItemSafeRunnable : SafeRunnable { | |
120 private Object element; | |
121 | |
122 private Item item; | |
123 | |
124 this(Item item, Object element) { | |
125 this.item = item; | |
126 this.element = element; | |
127 } | |
128 | |
129 public void run() { | |
130 doUpdateItem(item, element); | |
131 } | |
132 | |
133 } | |
134 | |
135 /** | |
136 * Creates an abstract tree viewer. The viewer has no input, no content | |
137 * provider, a default label provider, no sorter, no filters, and has | |
138 * auto-expand turned off. | |
139 */ | |
140 protected this() { | |
141 treeListeners = new ListenerList(); | |
142 // do nothing | |
143 } | |
144 | |
145 /** | |
146 * Adds the given child elements to this viewer as children of the given | |
147 * parent element. If this viewer does not have a sorter, the elements are | |
148 * added at the end of the parent's list of children in the order given; | |
149 * otherwise, the elements are inserted at the appropriate positions. | |
150 * <p> | |
151 * This method should be called (by the content provider) when elements have | |
152 * been added to the model, in order to cause the viewer to accurately | |
153 * reflect the model. This method only affects the viewer, not the model. | |
154 * </p> | |
155 * | |
156 * @param parentElementOrTreePath | |
157 * the parent element | |
158 * @param childElements | |
159 * the child elements to add | |
160 */ | |
161 public void add(Object parentElementOrTreePath, Object[] childElements) { | |
162 Assert.isNotNull(parentElementOrTreePath); | |
163 assertElementsNotNull(childElements); | |
164 if (isBusy()) | |
165 return; | |
166 Widget[] widgets = internalFindItems(parentElementOrTreePath); | |
167 // If parent hasn't been realized yet, just ignore the add. | |
168 if (widgets.length is 0) { | |
169 return; | |
170 } | |
171 | |
172 for (int i = 0; i < widgets.length; i++) { | |
173 internalAdd(widgets[i], parentElementOrTreePath, childElements); | |
174 } | |
175 } | |
176 | |
177 /** | |
178 * Find the items for the given element of tree path | |
179 * | |
180 * @param parentElementOrTreePath | |
181 * the element or tree path | |
182 * @return the items for that element | |
183 * | |
184 * @since 3.3 | |
185 */ | |
186 final protected Widget[] internalFindItems(Object parentElementOrTreePath) { | |
187 Widget[] widgets; | |
188 if ( auto path = cast(TreePath) parentElementOrTreePath ) { | |
189 Widget w = internalFindItem(path); | |
190 if (w is null) { | |
191 widgets = null; | |
192 } else { | |
193 widgets = [ w ]; | |
194 } | |
195 } else { | |
196 widgets = findItems(parentElementOrTreePath); | |
197 } | |
198 return widgets; | |
199 } | |
200 | |
201 /** | |
202 * Return the item at the given path or <code>null</code> | |
203 * | |
204 * @param path | |
205 * the path | |
206 * @return {@link Widget} the item at that path | |
207 */ | |
208 private Widget internalFindItem(TreePath path) { | |
209 Widget[] widgets = findItems(path.getLastSegment()); | |
210 for (int i = 0; i < widgets.length; i++) { | |
211 Widget widget = widgets[i]; | |
212 if ( auto item = cast(Item)widget ) { | |
213 TreePath p = getTreePathFromItem(item); | |
214 if (p.opEquals(path)) { | |
215 return widget; | |
216 } | |
217 } | |
218 } | |
219 return null; | |
220 } | |
221 | |
222 /** | |
223 * Adds the given child elements to this viewer as children of the given | |
224 * parent element. | |
225 * <p> | |
226 * EXPERIMENTAL. Not to be used except by JDT. This method was added to | |
227 * support JDT's explorations into grouping by working sets, which requires | |
228 * viewers to support multiple equal elements. See bug 76482 for more | |
229 * details. This support will likely be removed in Eclipse 3.2 in favor of | |
230 * proper support for multiple equal elements. | |
231 * </p> | |
232 * | |
233 * @param widget | |
234 * the widget for the parent element | |
235 * @param parentElementOrTreePath | |
236 * the parent element | |
237 * @param childElements | |
238 * the child elements to add | |
239 * @since 3.1 | |
240 */ | |
241 protected void internalAdd(Widget widget, Object parentElementOrTreePath, | |
242 Object[] childElements) { | |
243 Object parent; | |
244 TreePath path; | |
245 if ( auto path = cast(TreePath) parentElementOrTreePath ) { | |
246 parent = path.getLastSegment(); | |
247 } else { | |
248 parent = parentElementOrTreePath; | |
249 path = null; | |
250 } | |
251 | |
252 // optimization! | |
253 // if the widget is not expanded we just invalidate the subtree | |
254 if ( auto ti = cast(Item)widget ) { | |
255 if (!getExpanded(ti)) { | |
256 bool needDummy = isExpandable(ti, path, parent); | |
257 bool haveDummy = false; | |
258 // remove all children | |
259 Item[] items = getItems(ti); | |
260 for (int i = 0; i < items.length; i++) { | |
261 if (items[i].getData() !is null) { | |
262 disassociate(items[i]); | |
263 items[i].dispose(); | |
264 } else { | |
265 if (needDummy && !haveDummy) { | |
266 haveDummy = true; | |
267 } else { | |
268 items[i].dispose(); | |
269 } | |
270 } | |
271 } | |
272 // append a dummy if necessary | |
273 if (needDummy && !haveDummy) { | |
274 newItem(ti, DWT.NULL, -1); | |
275 } | |
276 return; | |
277 } | |
278 } | |
279 | |
280 if (childElements.length > 0) { | |
281 // TODO: Add filtering back? | |
282 Object[] filtered = filter(parentElementOrTreePath, childElements); | |
283 ViewerComparator comparator = getComparator(); | |
284 if (comparator !is null) { | |
285 if ( auto tpvs = cast(TreePathViewerSorter) comparator ) { | |
286 if (path is null) { | |
287 path = internalGetSorterParentPath(widget, comparator); | |
288 } | |
289 tpvs.sort(this, path, filtered); | |
290 } else { | |
291 comparator.sort(this, filtered); | |
292 } | |
293 } | |
294 createAddedElements(widget, filtered); | |
295 } | |
296 } | |
297 | |
298 /** | |
299 * Filter the children elements. | |
300 * | |
301 * @param parentElementOrTreePath | |
302 * the parent element or path | |
303 * @param elements | |
304 * the child elements | |
305 * @return the filter list of children | |
306 */ | |
307 private Object[] filter(Object parentElementOrTreePath, Object[] elements) { | |
308 ViewerFilter[] filters = getFilters(); | |
309 if (filters !is null) { | |
310 auto filtered = new ArraySeq!(Object); | |
311 filtered.capacity(elements.length); | |
312 for (int i = 0; i < elements.length; i++) { | |
313 bool add = true; | |
314 for (int j = 0; j < filters.length; j++) { | |
315 add = filters[j].select(this, parentElementOrTreePath, | |
316 elements[i]); | |
317 if (!add) { | |
318 break; | |
319 } | |
320 } | |
321 if (add) { | |
322 filtered.append(elements[i]); | |
323 } | |
324 } | |
325 return filtered.toArray(); | |
326 } | |
327 return elements; | |
328 } | |
329 | |
330 /** | |
331 * Create the new elements in the parent widget. If the child already exists | |
332 * do nothing. | |
333 * | |
334 * @param widget | |
335 * @param elements | |
336 * Sorted list of elements to add. | |
337 */ | |
338 private void createAddedElements(Widget widget, Object[] elements) { | |
339 | |
340 if (elements.length is 1) { | |
341 if (opEquals(elements[0], widget.getData())) { | |
342 return; | |
343 } | |
344 } | |
345 | |
346 ViewerComparator comparator = getComparator(); | |
347 TreePath parentPath = internalGetSorterParentPath(widget, comparator); | |
348 Item[] items = getChildren(widget); | |
349 | |
350 // As the items are sorted already we optimize for a | |
351 // start position | |
352 int lastInsertion = 0; | |
353 | |
354 // Optimize for the empty case | |
355 if (items.length is 0) { | |
356 for (int i = 0; i < elements.length; i++) { | |
357 createTreeItem(widget, elements[i], -1); | |
358 } | |
359 return; | |
360 } | |
361 | |
362 for (int i = 0; i < elements.length; i++) { | |
363 bool newItem = true; | |
364 Object element = elements[i]; | |
365 int index; | |
366 if (comparator is null) { | |
367 if (itemExists(items, element)) { | |
368 internalRefresh(element); | |
369 newItem = false; | |
370 } | |
371 index = -1; | |
372 } else { | |
373 lastInsertion = insertionPosition(items, comparator, | |
374 lastInsertion, element, parentPath); | |
375 // As we are only searching the original array we keep track of | |
376 // those positions only | |
377 if (lastInsertion is items.length) { | |
378 index = -1; | |
379 } else {// See if we should just refresh | |
380 while (lastInsertion < items.length | |
381 && internalCompare(comparator, parentPath, element, | |
382 items[lastInsertion].getData()) is 0) { | |
383 // As we cannot assume the sorter is consistent with | |
384 // equals() - therefore we can | |
385 // just check against the item prior to this index (if | |
386 // any) | |
387 if (items[lastInsertion].getData().opEquals(element)) { | |
388 // refresh the element in case it has new children | |
389 internalRefresh(element); | |
390 newItem = false; | |
391 } | |
392 lastInsertion++;// We had an insertion so increment | |
393 } | |
394 // Did we get to the end? | |
395 if (lastInsertion is items.length) { | |
396 index = -1; | |
397 } else { | |
398 index = lastInsertion + i; // Add the index as the | |
399 // array is growing | |
400 } | |
401 } | |
402 } | |
403 if (newItem) { | |
404 createTreeItem(widget, element, index); | |
405 } | |
406 } | |
407 } | |
408 | |
409 /** | |
410 * See if element is the data of one of the elements in items. | |
411 * | |
412 * @param items | |
413 * @param element | |
414 * @return <code>true</code> if the element matches. | |
415 */ | |
416 private bool itemExists(Item[] items, Object element) { | |
417 if (usingElementMap()) { | |
418 Widget[] existingItems = findItems(element); | |
419 // optimization for two common cases | |
420 if (existingItems.length is 0) { | |
421 return false; | |
422 } else if (existingItems.length is 1) { | |
423 if (items.length > 0 && null !is cast(Item)existingItems[0] ) { | |
424 Item existingItem = cast(Item) existingItems[0]; | |
425 return getParentItem(existingItem) is getParentItem(items[0]); | |
426 } | |
427 } | |
428 } | |
429 for (int i = 0; i < items.length; i++) { | |
430 if (items[i].getData().opEquals(element)) { | |
431 return true; | |
432 } | |
433 } | |
434 return false; | |
435 } | |
436 | |
437 /** | |
438 * Returns the index where the item should be inserted. It uses sorter to | |
439 * determine the correct position, if sorter is not assigned, returns the | |
440 * index of the element after the last. | |
441 * | |
442 * @param items | |
443 * the items to search | |
444 * @param comparator | |
445 * The comparator to use. | |
446 * @param lastInsertion | |
447 * the start index to start search for position from this allows | |
448 * optimizing search for multiple elements that are sorted | |
449 * themselves. | |
450 * @param element | |
451 * element to find position for. | |
452 * @param parentPath | |
453 * the tree path for the element's parent or <code>null</code> | |
454 * if the element is a root element or the sorter is not a | |
455 * {@link TreePathViewerSorter} | |
456 * @return the index to use when inserting the element. | |
457 * | |
458 */ | |
459 | |
460 private int insertionPosition(Item[] items, ViewerComparator comparator, | |
461 int lastInsertion, Object element, TreePath parentPath) { | |
462 | |
463 int size = items.length; | |
464 if (comparator is null) { | |
465 return size; | |
466 } | |
467 int min = lastInsertion, max = size - 1; | |
468 | |
469 while (min <= max) { | |
470 int mid = (min + max) / 2; | |
471 Object data = items[mid].getData(); | |
472 int compare = internalCompare(comparator, parentPath, data, element); | |
473 if (compare is 0) { | |
474 return mid;// Return if we already match | |
475 } | |
476 if (compare < 0) { | |
477 min = mid + 1; | |
478 } else { | |
479 max = mid - 1; | |
480 } | |
481 } | |
482 return min; | |
483 | |
484 } | |
485 | |
486 /** | |
487 * Returns the index where the item should be inserted. It uses sorter to | |
488 * determine the correct position, if sorter is not assigned, returns the | |
489 * index of the element after the last. | |
490 * | |
491 * @param parent | |
492 * The parent widget | |
493 * @param sorter | |
494 * The sorter to use. | |
495 * @param startIndex | |
496 * the start index to start search for position from this allows | |
497 * optimizing search for multiple elements that are sorted | |
498 * themselves. | |
499 * @param element | |
500 * element to find position for. | |
501 * @param currentSize | |
502 * the current size of the collection | |
503 * @return the index to use when inserting the element. | |
504 * | |
505 */ | |
506 | |
507 /** | |
508 * Returns the index where the item should be inserted. | |
509 * | |
510 * @param parent | |
511 * The parent widget the element will be inserted into. | |
512 * @param element | |
513 * The element to insert. | |
514 * @return the index of the element | |
515 */ | |
516 protected int indexForElement(Widget parent, Object element) { | |
517 ViewerComparator comparator = getComparator(); | |
518 TreePath parentPath = internalGetSorterParentPath(parent, comparator); | |
519 | |
520 Item[] items = getChildren(parent); | |
521 int count = items.length; | |
522 | |
523 if (comparator is null) { | |
524 return count; | |
525 } | |
526 int min = 0, max = count - 1; | |
527 | |
528 while (min <= max) { | |
529 int mid = (min + max) / 2; | |
530 Object data = items[mid].getData(); | |
531 int compare = internalCompare(comparator, parentPath, data, element); | |
532 if (compare is 0) { | |
533 // find first item > element | |
534 while (compare is 0) { | |
535 ++mid; | |
536 if (mid >= count) { | |
537 break; | |
538 } | |
539 data = items[mid].getData(); | |
540 compare = internalCompare(comparator, parentPath, data, | |
541 element); | |
542 } | |
543 return mid; | |
544 } | |
545 if (compare < 0) { | |
546 min = mid + 1; | |
547 } else { | |
548 max = mid - 1; | |
549 } | |
550 } | |
551 return min; | |
552 } | |
553 | |
554 /** | |
555 * Return the tree path that should be used as the parent path for the given | |
556 * widget and sorter. A <code>null</code> is returned if either the sorter | |
557 * is not a {@link TreePathViewerSorter} or if the parent widget is not an | |
558 * {@link Item} (i.e. is the root of the tree). | |
559 * | |
560 * @param parent | |
561 * the parent widget | |
562 * @param comparator | |
563 * the sorter | |
564 * @return the tree path that should be used as the parent path for the | |
565 * given widget and sorter | |
566 */ | |
567 private TreePath internalGetSorterParentPath(Widget parent, | |
568 ViewerComparator comparator) { | |
569 TreePath path; | |
570 if ( null !is cast(TreePathViewerSorter)comparator | |
571 && null !is cast(Item)parent ) { | |
572 Item item = cast(Item) parent; | |
573 path = getTreePathFromItem(item); | |
574 } else { | |
575 path = null; | |
576 } | |
577 return path; | |
578 } | |
579 | |
580 /** | |
581 * Compare the two elements using the given sorter. If the sorter is a | |
582 * {@link TreePathViewerSorter}, the provided tree path will be used. If | |
583 * the tree path is null and the sorter is a tree path sorter, then the | |
584 * elements are root elements | |
585 * | |
586 * @param comparator | |
587 * the sorter | |
588 * @param parentPath | |
589 * the path of the elements' parent | |
590 * @param e1 | |
591 * the first element | |
592 * @param e2 | |
593 * the second element | |
594 * @return the result of comparing the two elements | |
595 */ | |
596 private int internalCompare(ViewerComparator comparator, | |
597 TreePath parentPath, Object e1, Object e2) { | |
598 if ( auto tpvs = cast(TreePathViewerSorter) comparator ) { | |
599 return tpvs.compare(this, parentPath, e1, e2); | |
600 } | |
601 return comparator.compare(this, e1, e2); | |
602 } | |
603 | |
604 /* | |
605 * (non-Javadoc) | |
606 * | |
607 * @see dwtx.jface.viewers.StructuredViewer#getSortedChildren(java.lang.Object) | |
608 */ | |
609 protected Object[] getSortedChildren(Object parentElementOrTreePath) { | |
610 Object[] result = getFilteredChildren(parentElementOrTreePath); | |
611 ViewerComparator comparator = getComparator(); | |
612 if (parentElementOrTreePath !is null | |
613 && null !is cast(TreePathViewerSorter) comparator ) { | |
614 TreePathViewerSorter tpvs = cast(TreePathViewerSorter) comparator; | |
615 | |
616 // be sure we're not modifying the original array from the model | |
617 result = result.dup; | |
618 | |
619 TreePath path = null; | |
620 if ( auto p = cast(TreePath) parentElementOrTreePath ) { | |
621 path = p; | |
622 } else { | |
623 Object parent = parentElementOrTreePath; | |
624 Widget w = internalGetWidgetToSelect(parent); | |
625 if (w !is null) { | |
626 path = internalGetSorterParentPath(w, comparator); | |
627 } | |
628 } | |
629 tpvs.sort(this, path, result); | |
630 } else if (comparator !is null) { | |
631 // be sure we're not modifying the original array from the model | |
632 result = result.dup; | |
633 comparator.sort(this, result); | |
634 } | |
635 return result; | |
636 } | |
637 | |
638 /* | |
639 * (non-Javadoc) | |
640 * | |
641 * @see dwtx.jface.viewers.StructuredViewer#getFilteredChildren(java.lang.Object) | |
642 */ | |
643 protected Object[] getFilteredChildren(Object parentElementOrTreePath) { | |
644 Object[] result = getRawChildren(parentElementOrTreePath); | |
645 ViewerFilter[] filters = getFilters(); | |
646 for (int i = 0; i < filters.length; i++) { | |
647 ViewerFilter filter = filters[i]; | |
648 result = filter.filter(this, parentElementOrTreePath, result); | |
649 } | |
650 return result; | |
651 } | |
652 | |
653 /** | |
654 * Adds the given child element to this viewer as a child of the given | |
655 * parent element. If this viewer does not have a sorter, the element is | |
656 * added at the end of the parent's list of children; otherwise, the element | |
657 * is inserted at the appropriate position. | |
658 * <p> | |
659 * This method should be called (by the content provider) when a single | |
660 * element has been added to the model, in order to cause the viewer to | |
661 * accurately reflect the model. This method only affects the viewer, not | |
662 * the model. Note that there is another method for efficiently processing | |
663 * the simultaneous addition of multiple elements. | |
664 * </p> | |
665 * | |
666 * @param parentElementOrTreePath | |
667 * the parent element or path | |
668 * @param childElement | |
669 * the child element | |
670 */ | |
671 public void add(Object parentElementOrTreePath, Object childElement) { | |
672 add(parentElementOrTreePath, [ childElement ]); | |
673 } | |
674 | |
675 /** | |
676 * Adds the given DWT selection listener to the given DWT control. | |
677 * | |
678 * @param control | |
679 * the DWT control | |
680 * @param listener | |
681 * the DWT selection listener | |
682 * @deprecated | |
683 */ | |
684 protected void addSelectionListener(Control control, | |
685 SelectionListener listener) { | |
686 // do nothing | |
687 } | |
688 | |
689 /** | |
690 * Adds a listener for expand and collapse events in this viewer. Has no | |
691 * effect if an identical listener is already registered. | |
692 * | |
693 * @param listener | |
694 * a tree viewer listener | |
695 */ | |
696 public void addTreeListener(ITreeViewerListener listener) { | |
697 treeListeners.add(cast(Object)listener); | |
698 } | |
699 | |
700 /** | |
701 * Adds the given DWT tree listener to the given DWT control. | |
702 * | |
703 * @param control | |
704 * the DWT control | |
705 * @param listener | |
706 * the DWT tree listener | |
707 */ | |
708 protected abstract void addTreeListener(Control control, | |
709 TreeListener listener); | |
710 | |
711 /* | |
712 * (non-Javadoc) | |
713 * | |
714 * @see StructuredViewer#associate(Object, Item) | |
715 */ | |
716 protected void associate(Object element, Item item) { | |
717 Object data = item.getData(); | |
718 if (data !is null && data !is element && opEquals(data, element)) { | |
719 // workaround for PR 1FV62BT | |
720 // assumption: elements are equal but not identical | |
721 // -> remove from map but don't touch children | |
722 unmapElement(data, item); | |
723 item.setData(element); | |
724 mapElement(element, item); | |
725 } else { | |
726 // recursively disassociate all | |
727 super.associate(element, item); | |
728 } | |
729 } | |
730 | |
731 /** | |
732 * Collapses all nodes of the viewer's tree, starting with the root. This | |
733 * method is equivalent to <code>collapseToLevel(ALL_LEVELS)</code>. | |
734 */ | |
735 public void collapseAll() { | |
736 Object root = getRoot(); | |
737 if (root !is null) { | |
738 collapseToLevel(root, ALL_LEVELS); | |
739 } | |
740 } | |
741 | |
742 /** | |
743 * Collapses the subtree rooted at the given element or tree path to the | |
744 * given level. | |
745 * | |
746 * @param elementOrTreePath | |
747 * the element or tree path | |
748 * @param level | |
749 * non-negative level, or <code>ALL_LEVELS</code> to collapse | |
750 * all levels of the tree | |
751 */ | |
752 public void collapseToLevel(Object elementOrTreePath, int level) { | |
753 Assert.isNotNull(elementOrTreePath); | |
754 Widget w = internalGetWidgetToSelect(elementOrTreePath); | |
755 if (w !is null) { | |
756 internalCollapseToLevel(w, level); | |
757 } | |
758 } | |
759 | |
760 /** | |
761 * Creates all children for the given widget. | |
762 * <p> | |
763 * The default implementation of this framework method assumes that | |
764 * <code>widget.getData()</code> returns the element corresponding to the | |
765 * node. Note: the node is not visually expanded! You may have to call | |
766 * <code>parent.setExpanded(true)</code>. | |
767 * </p> | |
768 * | |
769 * @param widget | |
770 * the widget | |
771 */ | |
772 protected void createChildren(Widget widget) { | |
773 bool oldBusy = busy; | |
774 busy = true; | |
775 try { | |
776 final Item[] tis = getChildren(widget); | |
777 if (tis !is null && tis.length > 0) { | |
778 Object data = tis[0].getData(); | |
779 if (data !is null) { | |
780 return; // children already there! | |
781 } | |
782 } | |
783 | |
39 | 784 BusyIndicator.showWhile(widget.getDisplay(), new class(widget,tis) Runnable { |
10 | 785 Widget widget_; |
786 Item[] tis_; | |
39 | 787 this( Widget a, Item[] b){ |
788 widget_ = a; | |
789 tis_=b; | |
10 | 790 } |
791 public void run() { | |
792 // fix for PR 1FW89L7: | |
793 // don't complain and remove all "dummies" ... | |
794 if (tis_ !is null) { | |
795 for (int i = 0; i < tis_.length; i++) { | |
796 if (tis_[i].getData() !is null) { | |
797 disassociate(tis_[i]); | |
798 Assert.isTrue(tis_[i].getData() is null, | |
799 "Second or later child is non -null");//$NON-NLS-1$ | |
800 | |
801 } | |
802 tis_[i].dispose(); | |
803 } | |
804 } | |
805 Object d = widget_.getData(); | |
806 if (d !is null) { | |
807 Object parentElement = d; | |
808 Object[] children; | |
39 | 809 if (isTreePathContentProvider() && (null !is cast(Item)widget_) ) { |
10 | 810 TreePath path = getTreePathFromItem(cast(Item) widget_); |
811 children = getSortedChildren(path); | |
812 } else { | |
813 children = getSortedChildren(parentElement); | |
814 } | |
815 for (int i = 0; i < children.length; i++) { | |
816 createTreeItem(widget_, children[i], -1); | |
817 } | |
818 } | |
819 } | |
820 | |
821 }); | |
822 } finally { | |
823 busy = oldBusy; | |
824 } | |
825 } | |
826 | |
827 /** | |
828 * Creates a single item for the given parent and synchronizes it with the | |
829 * given element. | |
830 * | |
831 * @param parent | |
832 * the parent widget | |
833 * @param element | |
834 * the element | |
835 * @param index | |
836 * if non-negative, indicates the position to insert the item | |
837 * into its parent | |
838 */ | |
839 protected void createTreeItem(Widget parent, Object element, int index) { | |
840 Item item = newItem(parent, DWT.NULL, index); | |
841 updateItem(item, element); | |
842 updatePlus(item, element); | |
843 } | |
844 | |
845 /** | |
846 * The <code>AbstractTreeViewer</code> implementation of this method also | |
847 * recurses over children of the corresponding element. | |
848 */ | |
849 protected void disassociate(Item item) { | |
850 super.disassociate(item); | |
851 // recursively unmapping the items is only required when | |
852 // the hash map is used. In the other case disposing | |
853 // an item will recursively dispose its children. | |
854 if (usingElementMap()) { | |
855 disassociateChildren(item); | |
856 } | |
857 } | |
858 | |
859 /** | |
860 * Disassociates the children of the given DWT item from their corresponding | |
861 * elements. | |
862 * | |
863 * @param item | |
864 * the widget | |
865 */ | |
866 private void disassociateChildren(Item item) { | |
867 Item[] items = getChildren(item); | |
868 for (int i = 0; i < items.length; i++) { | |
869 if (items[i].getData() !is null) { | |
870 disassociate(items[i]); | |
871 } | |
872 } | |
873 } | |
874 | |
875 /* (non-Javadoc) Method declared on StructuredViewer. */ | |
876 protected Widget doFindInputItem(Object element) { | |
877 // compare with root | |
878 Object root = getRoot(); | |
879 if (root is null) { | |
880 return null; | |
881 } | |
882 | |
883 if (opEquals(root, element)) { | |
884 return getControl(); | |
885 } | |
886 return null; | |
887 } | |
888 | |
889 /* (non-Javadoc) Method declared on StructuredViewer. */ | |
890 protected Widget doFindItem(Object element) { | |
891 // compare with root | |
892 Object root = getRoot(); | |
893 if (root is null) { | |
894 return null; | |
895 } | |
896 | |
897 Item[] items = getChildren(getControl()); | |
898 if (items !is null) { | |
899 for (int i = 0; i < items.length; i++) { | |
900 Widget o = internalFindItem(items[i], element); | |
901 if (o !is null) { | |
902 return o; | |
903 } | |
904 } | |
905 } | |
906 return null; | |
907 } | |
908 | |
909 /** | |
910 * Copies the attributes of the given element into the given DWT item. | |
911 * | |
912 * @param item | |
913 * the DWT item | |
914 * @param element | |
915 * the element | |
916 */ | |
917 protected void doUpdateItem(Item item, Object element) { | |
40
da5ad8eedf5d
debug prints, dwt.dwthelper restructure, ...
Frank Benoit <benoit@tionex.de>
parents:
39
diff
changeset
|
918 Trace.formatln( "{} {}: doUpdateItem", __FILE__, __LINE__ ); |
10 | 919 if (item.isDisposed()) { |
920 unmapElement(element, item); | |
921 return; | |
922 } | |
923 | |
924 int columnCount = doGetColumnCount(); | |
925 if (columnCount is 0)// If no columns are created then fake one | |
926 columnCount = 1; | |
927 | |
928 ViewerRow viewerRowFromItem = getViewerRowFromItem(item); | |
929 | |
930 bool isVirtual = (getControl().getStyle() & DWT.VIRTUAL) !is 0; | |
931 | |
932 // If the control is virtual, we cannot use the cached viewer row object. See bug 188663. | |
933 if (isVirtual) { | |
40
da5ad8eedf5d
debug prints, dwt.dwthelper restructure, ...
Frank Benoit <benoit@tionex.de>
parents:
39
diff
changeset
|
934 Trace.formatln( "{} {}: doUpdateItem", __FILE__, __LINE__ ); |
10 | 935 viewerRowFromItem = cast(ViewerRow) viewerRowFromItem.clone(); |
936 } | |
937 | |
938 for (int column = 0; column < columnCount; column++) { | |
40
da5ad8eedf5d
debug prints, dwt.dwthelper restructure, ...
Frank Benoit <benoit@tionex.de>
parents:
39
diff
changeset
|
939 Trace.formatln( "{} {}: doUpdateItem", __FILE__, __LINE__ ); |
10 | 940 ViewerColumn columnViewer = getViewerColumn(column); |
941 ViewerCell cellToUpdate = updateCell(viewerRowFromItem, column, | |
942 element); | |
943 | |
944 // If the control is virtual, we cannot use the cached cell object. See bug 188663. | |
945 if (isVirtual) { | |
946 cellToUpdate = new ViewerCell(cellToUpdate.getViewerRow(), cellToUpdate.getColumnIndex(), element); | |
947 } | |
948 | |
949 columnViewer.refresh(cellToUpdate); | |
950 | |
951 // clear cell (see bug 201280) | |
952 updateCell(null, 0, null); | |
953 | |
954 // As it is possible for user code to run the event | |
955 // loop check here. | |
956 if (item.isDisposed()) { | |
957 unmapElement(element, item); | |
958 return; | |
959 } | |
960 | |
961 } | |
962 } | |
963 | |
964 /** | |
965 * Returns <code>true</code> if the given list and array of items refer to | |
966 * the same model elements. Order is unimportant. | |
967 * <p> | |
968 * This method is not intended to be overridden by subclasses. | |
969 * </p> | |
970 * | |
971 * @param items | |
972 * the list of items | |
973 * @param current | |
974 * the array of items | |
975 * @return <code>true</code> if the refer to the same elements, | |
976 * <code>false</code> otherwise | |
977 * | |
978 * @since 3.1 in TreeViewer, moved to AbstractTreeViewer in 3.3 | |
979 */ | |
980 protected bool isSameSelection(SeqView!(Item) items, Item[] current) { | |
981 // If they are not the same size then they are not equivalent | |
982 int n = items.size(); | |
983 if (n !is current.length) { | |
984 return false; | |
985 } | |
986 | |
987 CustomHashtable itemSet = newHashtable(n * 2 + 1); | |
988 foreach( item; items ){ | |
989 Object element = item.getData(); | |
990 itemSet.put(element, element); | |
991 } | |
992 | |
993 // Go through the items of the current collection | |
994 // If there is a mismatch return false | |
995 for (int i = 0; i < current.length; i++) { | |
996 if (current[i].getData() is null | |
997 || !itemSet.containsKey(current[i].getData())) { | |
998 return false; | |
999 } | |
1000 } | |
1001 | |
1002 return true; | |
1003 } | |
1004 | |
1005 | |
1006 | |
1007 /* (non-Javadoc) Method declared on StructuredViewer. */ | |
1008 protected void doUpdateItem(Widget widget, Object element, bool fullMap) { | |
1009 bool oldBusy = busy; | |
1010 busy = true; | |
1011 try { | |
1012 if ( auto item = cast(Item)widget ) { | |
1013 | |
1014 // ensure that back pointer is correct | |
1015 if (fullMap) { | |
1016 associate(element, item); | |
1017 } else { | |
1018 Object data = item.getData(); | |
1019 if (data !is null) { | |
1020 unmapElement(data, item); | |
1021 } | |
1022 item.setData(element); | |
1023 mapElement(element, item); | |
1024 } | |
1025 | |
1026 // update icon and label | |
1027 SafeRunnable.run(new UpdateItemSafeRunnable(item, element)); | |
1028 } | |
1029 } finally { | |
1030 busy = oldBusy; | |
1031 } | |
1032 } | |
1033 | |
1034 /** | |
1035 * Expands all nodes of the viewer's tree, starting with the root. This | |
1036 * method is equivalent to <code>expandToLevel(ALL_LEVELS)</code>. | |
1037 */ | |
1038 public void expandAll() { | |
1039 expandToLevel(ALL_LEVELS); | |
1040 } | |
1041 | |
1042 /** | |
1043 * Expands the root of the viewer's tree to the given level. | |
1044 * | |
1045 * @param level | |
1046 * non-negative level, or <code>ALL_LEVELS</code> to expand all | |
1047 * levels of the tree | |
1048 */ | |
1049 public void expandToLevel(int level) { | |
1050 expandToLevel(getRoot(), level); | |
1051 } | |
1052 | |
1053 /** | |
1054 * Expands all ancestors of the given element or tree path so that the given | |
1055 * element becomes visible in this viewer's tree control, and then expands | |
1056 * the subtree rooted at the given element to the given level. | |
1057 * | |
1058 * @param elementOrTreePath | |
1059 * the element | |
1060 * @param level | |
1061 * non-negative level, or <code>ALL_LEVELS</code> to expand all | |
1062 * levels of the tree | |
1063 */ | |
1064 public void expandToLevel(Object elementOrTreePath, int level) { | |
1065 if (isBusy()) | |
1066 return; | |
1067 Widget w = internalExpand(elementOrTreePath, true); | |
1068 if (w !is null) { | |
1069 internalExpandToLevel(w, level); | |
1070 } | |
1071 } | |
1072 | |
1073 /** | |
1074 * Fires a tree collapsed event. Only listeners registered at the time this | |
1075 * method is called are notified. | |
1076 * | |
1077 * @param event | |
1078 * the tree expansion event | |
1079 * @see ITreeViewerListener#treeCollapsed | |
1080 */ | |
1081 protected void fireTreeCollapsed(TreeExpansionEvent event) { | |
1082 Object[] listeners = treeListeners.getListeners(); | |
1083 for (int i = 0; i < listeners.length; ++i) { | |
39 | 1084 SafeRunnable.run(new class(event,cast(ITreeViewerListener) listeners[i]) SafeRunnable { |
10 | 1085 TreeExpansionEvent event_; |
1086 ITreeViewerListener l; | |
39 | 1087 this(TreeExpansionEvent a,ITreeViewerListener b){ |
1088 event_=a; | |
1089 l = b; | |
10 | 1090 } |
1091 public void run() { | |
1092 l.treeCollapsed(event_); | |
1093 } | |
1094 }); | |
1095 } | |
1096 } | |
1097 | |
1098 /** | |
1099 * Fires a tree expanded event. Only listeners registered at the time this | |
1100 * method is called are notified. | |
1101 * | |
1102 * @param event | |
1103 * the tree expansion event | |
1104 * @see ITreeViewerListener#treeExpanded | |
1105 */ | |
1106 protected void fireTreeExpanded(TreeExpansionEvent event) { | |
1107 Object[] listeners = treeListeners.getListeners(); | |
1108 for (int i = 0; i < listeners.length; ++i) { | |
39 | 1109 SafeRunnable.run(new class( event, cast(ITreeViewerListener) listeners[i]) SafeRunnable { |
10 | 1110 TreeExpansionEvent event_; |
1111 ITreeViewerListener l; | |
39 | 1112 this(TreeExpansionEvent a,ITreeViewerListener b){ |
1113 event_=a; | |
1114 l = b; | |
10 | 1115 } |
1116 public void run() { | |
1117 l.treeExpanded(event_); | |
1118 } | |
1119 }); | |
1120 } | |
1121 | |
1122 } | |
1123 | |
1124 /** | |
1125 * Returns the auto-expand level. | |
1126 * | |
1127 * @return non-negative level, or <code>ALL_LEVELS</code> if all levels of | |
1128 * the tree are expanded automatically | |
1129 * @see #setAutoExpandLevel | |
1130 */ | |
1131 public int getAutoExpandLevel() { | |
1132 return expandToLevel_; | |
1133 } | |
1134 | |
1135 /** | |
1136 * Returns the DWT child items for the given DWT widget. | |
1137 * | |
1138 * @param widget | |
1139 * the widget | |
1140 * @return the child items | |
1141 */ | |
1142 protected abstract Item[] getChildren(Widget widget); | |
1143 | |
1144 /** | |
1145 * Get the child for the widget at index. Note that the default | |
1146 * implementation is not very efficient and should be overridden if this | |
1147 * class is implemented. | |
1148 * | |
1149 * @param widget | |
1150 * the widget to check | |
1151 * @param index | |
1152 * the index of the widget | |
1153 * @return Item or <code>null</code> if widget is not a type that can | |
1154 * contain items. | |
1155 * | |
1156 * @throws ArrayIndexOutOfBoundsException | |
1157 * if the index is not valid. | |
1158 * @since 3.1 | |
1159 */ | |
1160 protected Item getChild(Widget widget, int index) { | |
1161 return getChildren(widget)[index]; | |
1162 } | |
1163 | |
1164 /** | |
1165 * Returns whether the given DWT item is expanded or collapsed. | |
1166 * | |
1167 * @param item | |
1168 * the item | |
1169 * @return <code>true</code> if the item is considered expanded and | |
1170 * <code>false</code> if collapsed | |
1171 */ | |
1172 protected abstract bool getExpanded(Item item); | |
1173 | |
1174 /** | |
1175 * Returns a list of elements corresponding to expanded nodes in this | |
1176 * viewer's tree, including currently hidden ones that are marked as | |
1177 * expanded but are under a collapsed ancestor. | |
1178 * <p> | |
1179 * This method is typically used when preserving the interesting state of a | |
1180 * viewer; <code>setExpandedElements</code> is used during the restore. | |
1181 * </p> | |
1182 * | |
1183 * @return the array of expanded elements | |
1184 * @see #setExpandedElements | |
1185 */ | |
1186 public Object[] getExpandedElements() { | |
1187 auto items = new ArraySeq!(Item); | |
1188 internalCollectExpandedItems(items, getControl()); | |
1189 auto result = new ArraySeq!(Object); | |
1190 result.capacity(items.size()); | |
1191 foreach ( item; items ) { | |
1192 Object data = item.getData(); | |
1193 if (data !is null) { | |
1194 result.append(data); | |
1195 } | |
1196 } | |
1197 return result.toArray(); | |
1198 } | |
1199 | |
1200 /** | |
1201 * Returns whether the node corresponding to the given element or tree path | |
1202 * is expanded or collapsed. | |
1203 * | |
1204 * @param elementOrTreePath | |
1205 * the element | |
1206 * @return <code>true</code> if the node is expanded, and | |
1207 * <code>false</code> if collapsed | |
1208 */ | |
1209 public bool getExpandedState(Object elementOrTreePath) { | |
1210 Assert.isNotNull(elementOrTreePath); | |
1211 Widget item = internalGetWidgetToSelect(elementOrTreePath); | |
1212 if ( auto i = cast(Item)item ) { | |
1213 return getExpanded(i); | |
1214 } | |
1215 return false; | |
1216 } | |
1217 | |
1218 /** | |
1219 * Returns the number of child items of the given DWT control. | |
1220 * | |
1221 * @param control | |
1222 * the control | |
1223 * @return the number of children | |
1224 */ | |
1225 protected abstract int getItemCount(Control control); | |
1226 | |
1227 /** | |
1228 * Returns the number of child items of the given DWT item. | |
1229 * | |
1230 * @param item | |
1231 * the item | |
1232 * @return the number of children | |
1233 */ | |
1234 protected abstract int getItemCount(Item item); | |
1235 | |
1236 /** | |
1237 * Returns the child items of the given DWT item. | |
1238 * | |
1239 * @param item | |
1240 * the item | |
1241 * @return the child items | |
1242 */ | |
1243 protected abstract Item[] getItems(Item item); | |
1244 | |
1245 /** | |
1246 * Returns the item after the given item in the tree, or <code>null</code> | |
1247 * if there is no next item. | |
1248 * | |
1249 * @param item | |
1250 * the item | |
1251 * @param includeChildren | |
1252 * <code>true</code> if the children are considered in | |
1253 * determining which item is next, and <code>false</code> if | |
1254 * subtrees are ignored | |
1255 * @return the next item, or <code>null</code> if none | |
1256 */ | |
1257 protected Item getNextItem(Item item, bool includeChildren) { | |
1258 if (item is null) { | |
1259 return null; | |
1260 } | |
1261 if (includeChildren && getExpanded(item)) { | |
1262 Item[] children = getItems(item); | |
1263 if (children !is null && children.length > 0) { | |
1264 return children[0]; | |
1265 } | |
1266 } | |
1267 | |
1268 // next item is either next sibling or next sibling of first | |
1269 // parent that has a next sibling. | |
1270 Item parent = getParentItem(item); | |
1271 if (parent is null) { | |
1272 return null; | |
1273 } | |
1274 Item[] siblings = getItems(parent); | |
1275 if (siblings !is null) { | |
1276 if (siblings.length <= 1) { | |
1277 return getNextItem(parent, false); | |
1278 } | |
1279 | |
1280 for (int i = 0; i < siblings.length; i++) { | |
1281 if (siblings[i] is item && i < (siblings.length - 1)) { | |
1282 return siblings[i + 1]; | |
1283 } | |
1284 } | |
1285 } | |
1286 return getNextItem(parent, false); | |
1287 } | |
1288 | |
1289 /** | |
1290 * Returns the parent item of the given item in the tree, or | |
1291 * <code>null</code> if there is no parent item. | |
1292 * | |
1293 * @param item | |
1294 * the item | |
1295 * @return the parent item, or <code>null</code> if none | |
1296 */ | |
1297 protected abstract Item getParentItem(Item item); | |
1298 | |
1299 /** | |
1300 * Returns the item before the given item in the tree, or <code>null</code> | |
1301 * if there is no previous item. | |
1302 * | |
1303 * @param item | |
1304 * the item | |
1305 * @return the previous item, or <code>null</code> if none | |
1306 */ | |
1307 protected Item getPreviousItem(Item item) { | |
1308 // previous item is either right-most visible descendent of previous | |
1309 // sibling or parent | |
1310 Item parent = getParentItem(item); | |
1311 if (parent is null) { | |
1312 return null; | |
1313 } | |
1314 Item[] siblings = getItems(parent); | |
1315 if (siblings.length is 0 || siblings[0] is item) { | |
1316 return parent; | |
1317 } | |
1318 Item previous = siblings[0]; | |
1319 for (int i = 1; i < siblings.length; i++) { | |
1320 if (siblings[i] is item) { | |
1321 return rightMostVisibleDescendent(previous); | |
1322 } | |
1323 previous = siblings[i]; | |
1324 } | |
1325 return null; | |
1326 } | |
1327 | |
1328 /* (non-Javadoc) Method declared on StructuredViewer. */ | |
1329 protected Object[] getRawChildren(Object parentElementOrTreePath) { | |
1330 bool oldBusy = busy; | |
1331 busy = true; | |
1332 try { | |
1333 Object parent; | |
1334 TreePath path; | |
1335 if ( auto p = cast(TreePath)parentElementOrTreePath ) { | |
1336 path = p; | |
1337 parent = path.getLastSegment(); | |
1338 } else { | |
1339 parent = parentElementOrTreePath; | |
1340 path = null; | |
1341 } | |
1342 if (parent !is null) { | |
1343 if (opEquals(parent, getRoot())) { | |
1344 return super.getRawChildren(parent); | |
1345 } | |
1346 IContentProvider cp = getContentProvider(); | |
1347 if ( auto tpcp = cast(ITreePathContentProvider)cp ) { | |
1348 if (path is null) { | |
1349 // A path was not provided so try and find one | |
1350 Widget w = findItem(parent); | |
1351 if ( auto item = cast(Item)w ) { | |
1352 path = getTreePathFromItem(item); | |
1353 } | |
1354 if (path is null) { | |
1355 path = new TreePath([parent ]); | |
1356 } | |
1357 } | |
1358 Object[] result = tpcp.getChildren(path); | |
1359 if (result !is null) { | |
1360 return result; | |
1361 } | |
1362 } else if ( auto tcp = cast(ITreeContentProvider)cp ) { | |
1363 Object[] result = tcp.getChildren(parent); | |
1364 if (result !is null) { | |
1365 return result; | |
1366 } | |
1367 } | |
1368 } | |
1369 return null; | |
1370 } finally { | |
1371 busy = oldBusy; | |
1372 } | |
1373 } | |
1374 | |
1375 /** | |
1376 * Returns all selected items for the given DWT control. | |
1377 * | |
1378 * @param control | |
1379 * the control | |
1380 * @return the list of selected items | |
1381 */ | |
1382 protected abstract Item[] getSelection(Control control); | |
1383 | |
1384 /* | |
1385 * (non-Javadoc) | |
1386 * | |
1387 * @see dwtx.jface.viewers.StructuredViewer#getSelectionFromWidget() | |
1388 */ | |
1389 protected SeqView!(Object) getSelectionFromWidget() { | |
1390 Widget[] items = getSelection(getControl()); | |
1391 ArraySeq!(Object) list = new ArraySeq!(Object); | |
1392 list.capacity(items.length); | |
1393 for (int i = 0; i < items.length; i++) { | |
1394 Widget item = items[i]; | |
1395 Object e = item.getData(); | |
1396 if (e !is null) { | |
1397 list.append(e); | |
1398 } | |
1399 } | |
1400 return list; | |
1401 } | |
1402 | |
1403 /* | |
1404 * Overridden in AbstractTreeViewer to fix bug 108102 (code copied from | |
1405 * StructuredViewer to avoid introducing new API) (non-Javadoc) | |
1406 * | |
1407 * @see dwtx.jface.viewers.StructuredViewer#handleDoubleSelect(dwt.events.SelectionEvent) | |
1408 */ | |
1409 protected void handleDoubleSelect(SelectionEvent event) { | |
1410 // handle case where an earlier selection listener disposed the control. | |
1411 Control control = getControl(); | |
1412 if (control !is null && !control.isDisposed()) { | |
1413 // If the double-clicked element can be obtained from the event, use | |
1414 // it | |
1415 // otherwise get it from the control. Some controls like List do | |
1416 // not have the notion of item. | |
1417 // For details, see bug 90161 [Navigator] DefaultSelecting folders | |
1418 // shouldn't always expand first one | |
1419 ISelection selection; | |
1420 if (event.item !is null && event.item.getData() !is null) { | |
1421 | |
1422 // changes to fix bug 108102 follow | |
1423 TreePath treePath = getTreePathFromItem(cast(Item) event.item); | |
1424 selection = new TreeSelection(treePath); | |
1425 // end of changes | |
1426 | |
1427 } else { | |
1428 selection = getSelection(); | |
1429 updateSelection(selection); | |
1430 } | |
1431 fireDoubleClick(new DoubleClickEvent(this, selection)); | |
1432 } | |
1433 } | |
1434 | |
1435 /** | |
1436 * Handles a tree collapse event from the DWT widget. | |
1437 * | |
1438 * @param event | |
1439 * the DWT tree event | |
1440 */ | |
1441 protected void handleTreeCollapse(TreeEvent event) { | |
1442 if (event.item.getData() !is null) { | |
1443 fireTreeCollapsed(new TreeExpansionEvent(this, event.item.getData())); | |
1444 } | |
1445 } | |
1446 | |
1447 /** | |
1448 * Handles a tree expand event from the DWT widget. | |
1449 * | |
1450 * @param event | |
1451 * the DWT tree event | |
1452 */ | |
1453 protected void handleTreeExpand(TreeEvent event) { | |
1454 createChildren(event.item); | |
1455 if (event.item.getData() !is null) { | |
1456 fireTreeExpanded(new TreeExpansionEvent(this, event.item.getData())); | |
1457 } | |
1458 } | |
1459 | |
1460 /* (non-Javadoc) Method declared on Viewer. */ | |
1461 protected void hookControl(Control control) { | |
1462 super.hookControl(control); | |
1463 addTreeListener(control, new class TreeListener { | |
1464 public void treeExpanded(TreeEvent event) { | |
1465 handleTreeExpand(event); | |
1466 } | |
1467 | |
1468 public void treeCollapsed(TreeEvent event) { | |
1469 handleTreeCollapse(event); | |
1470 } | |
1471 }); | |
1472 } | |
1473 | |
1474 /* | |
1475 * (non-Javadoc) Method declared on StructuredViewer. Builds the initial | |
1476 * tree and handles the automatic expand feature. | |
1477 */ | |
1478 protected void inputChanged(Object input, Object oldInput) { | |
1479 preservingSelection(new class Runnable { | |
1480 public void run() { | |
1481 Control tree = getControl(); | |
1482 bool useRedraw = true; | |
1483 // (size > REDRAW_THRESHOLD) || (table.getItemCount() > | |
1484 // REDRAW_THRESHOLD); | |
1485 if (useRedraw) { | |
1486 tree.setRedraw(false); | |
1487 } | |
1488 removeAll(tree); | |
1489 tree.setData(getRoot()); | |
1490 internalInitializeTree(tree); | |
1491 if (useRedraw) { | |
1492 tree.setRedraw(true); | |
1493 } | |
1494 } | |
1495 | |
1496 }); | |
1497 } | |
1498 | |
1499 /** | |
1500 * Initializes the tree with root items, expanding to the appropriate | |
1501 * level if necessary. | |
1502 * | |
1503 * @param tree the tree control | |
1504 * @since 3.3 | |
1505 */ | |
1506 protected void internalInitializeTree(Control tree) { | |
1507 createChildren(tree); | |
1508 internalExpandToLevel(tree, expandToLevel_); | |
1509 } | |
1510 | |
1511 /** | |
1512 * Recursively collapses the subtree rooted at the given widget to the given | |
1513 * level. | |
1514 * <p> | |
1515 * </p> | |
1516 * Note that the default implementation of this method does not call | |
1517 * <code>setRedraw</code>. | |
1518 * | |
1519 * @param widget | |
1520 * the widget | |
1521 * @param level | |
1522 * non-negative level, or <code>ALL_LEVELS</code> to collapse | |
1523 * all levels of the tree | |
1524 */ | |
1525 protected void internalCollapseToLevel(Widget widget, int level) { | |
1526 if (level is ALL_LEVELS || level > 0) { | |
1527 | |
1528 if ( auto i = cast(Item)widget ) { | |
1529 setExpanded(i, false); | |
1530 } | |
1531 | |
1532 if (level is ALL_LEVELS || level > 1) { | |
1533 Item[] children = getChildren(widget); | |
1534 if (children !is null) { | |
1535 int nextLevel = (level is ALL_LEVELS ? ALL_LEVELS | |
1536 : level - 1); | |
1537 for (int i = 0; i < children.length; i++) { | |
1538 internalCollapseToLevel(children[i], nextLevel); | |
1539 } | |
1540 } | |
1541 } | |
1542 } | |
1543 } | |
1544 | |
1545 /** | |
1546 * Recursively collects all expanded items from the given widget. | |
1547 * | |
1548 * @param result | |
1549 * a list (element type: <code>Item</code>) into which to | |
1550 * collect the elements | |
1551 * @param widget | |
1552 * the widget | |
1553 */ | |
1554 private void internalCollectExpandedItems(Seq!(Item) result, Widget widget) { | |
1555 Item[] items = getChildren(widget); | |
1556 for (int i = 0; i < items.length; i++) { | |
1557 Item item = items[i]; | |
1558 if (getExpanded(item)) { | |
1559 result.append(item); | |
1560 } | |
1561 internalCollectExpandedItems(result, item); | |
1562 } | |
1563 } | |
1564 | |
1565 /** | |
1566 * Tries to create a path of tree items for the given element or tree path. | |
1567 * This method recursively walks up towards the root of the tree and in the | |
1568 * case of an element (rather than a tree path) assumes that | |
1569 * <code>getParent</code> returns the correct parent of an element. | |
1570 * | |
1571 * @param elementOrPath | |
1572 * the element | |
1573 * @param expand | |
1574 * <code>true</code> if all nodes on the path should be | |
1575 * expanded, and <code>false</code> otherwise | |
1576 * @return Widget | |
1577 */ | |
1578 protected Widget internalExpand(Object elementOrPath, bool expand) { | |
1579 | |
1580 if (elementOrPath is null) { | |
1581 return null; | |
1582 } | |
1583 | |
1584 Widget w = internalGetWidgetToSelect(elementOrPath); | |
1585 if (w is null) { | |
1586 if (opEquals(elementOrPath, getRoot())) { // stop at root | |
1587 return null; | |
1588 } | |
1589 // my parent has to create me | |
1590 Object parent = getParentElement(elementOrPath); | |
1591 if (parent !is null) { | |
1592 Widget pw = internalExpand(parent, false); | |
1593 if (pw !is null) { | |
1594 // let my parent create me | |
1595 createChildren(pw); | |
1596 Object element = internalToElement(elementOrPath); | |
1597 w = internalFindChild(pw, element); | |
1598 if (expand && null !is cast(Item)pw ) { | |
1599 // expand parent items top-down | |
1600 Item item = cast(Item) pw; | |
1601 auto toExpandList = new LinkSeq!(Item); | |
1602 while (item !is null && !getExpanded(item)) { | |
1603 toExpandList.prepend(item); | |
1604 item = getParentItem(item); | |
1605 } | |
1606 foreach( toExpand; toExpandList ){ | |
1607 setExpanded(toExpand, true); | |
1608 } | |
1609 } | |
1610 } | |
1611 } | |
1612 } | |
1613 return w; | |
1614 } | |
1615 | |
1616 /** | |
1617 * If the argument is a tree path, returns its last segment, otherwise | |
1618 * return the argument | |
1619 * | |
1620 * @param elementOrPath | |
1621 * an element or a tree path | |
1622 * @return the element, or the last segment of the tree path | |
1623 */ | |
1624 private Object internalToElement(Object elementOrPath) { | |
1625 if (auto tp = cast(TreePath)elementOrPath ) { | |
1626 return tp.getLastSegment(); | |
1627 } | |
1628 return elementOrPath; | |
1629 } | |
1630 | |
1631 /** | |
1632 * This method takes a tree path or an element. If the argument is not a | |
1633 * tree path, returns the parent of the given element or <code>null</code> | |
1634 * if the parent is not known. If the argument is a tree path with more than | |
1635 * one segment, returns its parent tree path, otherwise returns | |
1636 * <code>null</code>. | |
1637 * | |
1638 * @param elementOrTreePath | |
1639 * @return the parent element, or parent path, or <code>null</code> | |
1640 * | |
1641 * @since 3.2 | |
1642 */ | |
1643 protected Object getParentElement(Object elementOrTreePath) { | |
1644 if (auto tp = cast(TreePath)elementOrTreePath) { | |
1645 return tp.getParentPath(); | |
1646 } | |
1647 IContentProvider cp = getContentProvider(); | |
1648 if ( auto tpcp = cast(ITreePathContentProvider)cp ) { | |
1649 TreePath[] paths = tpcp.getParents(elementOrTreePath); | |
1650 if (paths.length > 0) { | |
1651 if (paths[0].getSegmentCount() is 0) { | |
1652 return getInput(); | |
1653 } | |
1654 return paths[0].getLastSegment(); | |
1655 } | |
1656 } | |
1657 if ( auto tcp = cast(ITreeContentProvider) cp ) { | |
1658 return tcp.getParent(elementOrTreePath); | |
1659 } | |
1660 return null; | |
1661 } | |
1662 | |
1663 /** | |
1664 * Returns the widget to be selected for the given element or tree path. | |
1665 * | |
1666 * @param elementOrTreePath | |
1667 * the element or tree path to select | |
1668 * @return the widget to be selected, or <code>null</code> if not found | |
1669 * | |
1670 * @since 3.1 | |
1671 */ | |
1672 protected Widget internalGetWidgetToSelect(Object elementOrTreePath) { | |
1673 if ( auto treePath = cast(TreePath) elementOrTreePath ) { | |
1674 if (treePath.getSegmentCount() is 0) { | |
1675 return getControl(); | |
1676 } | |
1677 Widget[] candidates = findItems(treePath.getLastSegment()); | |
1678 for (int i = 0; i < candidates.length; i++) { | |
1679 Widget candidate = candidates[i]; | |
1680 if (!(cast(Item)candidate )) { | |
1681 continue; | |
1682 } | |
1683 if (treePath.opEquals(getTreePathFromItem(cast(Item) candidate), | |
1684 getComparer())) { | |
1685 return candidate; | |
1686 } | |
1687 } | |
1688 return null; | |
1689 } | |
1690 return findItem(elementOrTreePath); | |
1691 } | |
1692 | |
1693 /** | |
1694 * Recursively expands the subtree rooted at the given widget to the given | |
1695 * level. | |
1696 * <p> | |
1697 * </p> | |
1698 * Note that the default implementation of this method does not call | |
1699 * <code>setRedraw</code>. | |
1700 * | |
1701 * @param widget | |
1702 * the widget | |
1703 * @param level | |
1704 * non-negative level, or <code>ALL_LEVELS</code> to collapse | |
1705 * all levels of the tree | |
1706 */ | |
1707 protected void internalExpandToLevel(Widget widget, int level) { | |
1708 if (level is ALL_LEVELS || level > 0) { | |
1709 if ( cast(Item)widget && widget.getData() !is null | |
1710 && !isExpandable(cast(Item) widget, null, widget.getData())) { | |
1711 return; | |
1712 } | |
1713 createChildren(widget); | |
1714 if ( auto i = cast(Item)widget ) { | |
1715 setExpanded(i, true); | |
1716 } | |
1717 if (level is ALL_LEVELS || level > 1) { | |
1718 Item[] children = getChildren(widget); | |
1719 if (children !is null) { | |
1720 int newLevel = (level is ALL_LEVELS ? ALL_LEVELS | |
1721 : level - 1); | |
1722 for (int i = 0; i < children.length; i++) { | |
1723 internalExpandToLevel(children[i], newLevel); | |
1724 } | |
1725 } | |
1726 } | |
1727 } | |
1728 } | |
1729 | |
1730 /** | |
1731 * Non-recursively tries to find the given element as a child of the given | |
1732 * parent (item or tree). | |
1733 * | |
1734 * @param parent | |
1735 * the parent item | |
1736 * @param element | |
1737 * the element | |
1738 * @return Widget | |
1739 */ | |
1740 private Widget internalFindChild(Widget parent, Object element) { | |
1741 Item[] items = getChildren(parent); | |
1742 for (int i = 0; i < items.length; i++) { | |
1743 Item item = items[i]; | |
1744 Object data = item.getData(); | |
1745 if (data !is null && opEquals(data, element)) { | |
1746 return item; | |
1747 } | |
1748 } | |
1749 return null; | |
1750 } | |
1751 | |
1752 /** | |
1753 * Recursively tries to find the given element. | |
1754 * | |
1755 * @param parent | |
1756 * the parent item | |
1757 * @param element | |
1758 * the element | |
1759 * @return Widget | |
1760 */ | |
1761 private Widget internalFindItem(Item parent, Object element) { | |
1762 | |
1763 // compare with node | |
1764 Object data = parent.getData(); | |
1765 if (data !is null) { | |
1766 if (opEquals(data, element)) { | |
1767 return parent; | |
1768 } | |
1769 } | |
1770 // recurse over children | |
1771 Item[] items = getChildren(parent); | |
1772 for (int i = 0; i < items.length; i++) { | |
1773 Item item = items[i]; | |
1774 Widget o = internalFindItem(item, element); | |
1775 if (o !is null) { | |
1776 return o; | |
1777 } | |
1778 } | |
1779 return null; | |
1780 } | |
1781 | |
1782 /* (non-Javadoc) Method declared on StructuredViewer. */ | |
1783 protected void internalRefresh(Object element) { | |
1784 internalRefresh(element, true); | |
1785 } | |
1786 | |
1787 /* (non-Javadoc) Method declared on StructuredViewer. */ | |
1788 protected void internalRefresh(Object element, bool updateLabels) { | |
40
da5ad8eedf5d
debug prints, dwt.dwthelper restructure, ...
Frank Benoit <benoit@tionex.de>
parents:
39
diff
changeset
|
1789 Trace.formatln( "{} {}: ", __FILE__, __LINE__ ); |
da5ad8eedf5d
debug prints, dwt.dwthelper restructure, ...
Frank Benoit <benoit@tionex.de>
parents:
39
diff
changeset
|
1790 PrintStackTrace(); |
10 | 1791 // If element is null, do a full refresh. |
1792 if (element is null) { | |
1793 internalRefresh(getControl(), getRoot(), true, updateLabels); | |
1794 return; | |
1795 } | |
1796 Widget[] items = findItems(element); | |
40
da5ad8eedf5d
debug prints, dwt.dwthelper restructure, ...
Frank Benoit <benoit@tionex.de>
parents:
39
diff
changeset
|
1797 Trace.formatln( "{} {}: ,items.length={}", __FILE__, __LINE__ ,items.length); |
10 | 1798 if (items.length !is 0) { |
1799 for (int i = 0; i < items.length; i++) { | |
1800 // pick up structure changes too | |
1801 internalRefresh(items[i], element, true, updateLabels); | |
1802 } | |
1803 } | |
1804 } | |
1805 | |
1806 /** | |
1807 * Refreshes the tree starting at the given widget. | |
1808 * <p> | |
1809 * EXPERIMENTAL. Not to be used except by JDT. This method was added to | |
1810 * support JDT's explorations into grouping by working sets, which requires | |
1811 * viewers to support multiple equal elements. See bug 76482 for more | |
1812 * details. This support will likely be removed in Eclipse 3.2 in favor of | |
1813 * proper support for multiple equal elements. | |
1814 * </p> | |
1815 * | |
1816 * @param widget | |
1817 * the widget | |
1818 * @param element | |
1819 * the element | |
1820 * @param doStruct | |
1821 * <code>true</code> if structural changes are to be picked up, | |
1822 * and <code>false</code> if only label provider changes are of | |
1823 * interest | |
1824 * @param updateLabels | |
1825 * <code>true</code> to update labels for existing elements, | |
1826 * <code>false</code> to only update labels as needed, assuming | |
1827 * that labels for existing elements are unchanged. | |
1828 * @since 3.1 | |
1829 */ | |
1830 protected void internalRefresh(Widget widget, Object element, | |
1831 bool doStruct, bool updateLabels) { | |
1832 | |
1833 if ( auto i = cast(Item)widget ) { | |
1834 if (doStruct) { | |
1835 updatePlus(i, element); | |
1836 } | |
1837 if (updateLabels || !opEquals(element, widget.getData())) { | |
1838 doUpdateItem(widget, element, true); | |
1839 } else { | |
1840 associate(element, cast(Item) widget); | |
1841 } | |
1842 } | |
1843 | |
1844 if (doStruct) { | |
1845 internalRefreshStruct(widget, element, updateLabels); | |
1846 } else { | |
1847 Item[] children = getChildren(widget); | |
1848 if (children !is null) { | |
1849 for (int i = 0; i < children.length; i++) { | |
1850 Widget item = children[i]; | |
1851 Object data = item.getData(); | |
1852 if (data !is null) { | |
1853 internalRefresh(item, data, doStruct, updateLabels); | |
1854 } | |
1855 } | |
1856 } | |
1857 } | |
1858 } | |
1859 | |
1860 /** | |
1861 * Update the structure and recurse. Items are updated in updateChildren, as | |
1862 * needed. | |
1863 * | |
1864 * @param widget | |
1865 * @param element | |
1866 * @param updateLabels | |
1867 */ | |
1868 /* package */void internalRefreshStruct(Widget widget, Object element, | |
1869 bool updateLabels) { | |
1870 updateChildren(widget, element, null, updateLabels); | |
1871 Item[] children = getChildren(widget); | |
1872 if (children !is null) { | |
1873 for (int i = 0; i < children.length; i++) { | |
1874 Widget item = children[i]; | |
1875 Object data = item.getData(); | |
1876 if (data !is null) { | |
1877 internalRefreshStruct(item, data, updateLabels); | |
1878 } | |
1879 } | |
1880 } | |
1881 } | |
1882 | |
1883 /** | |
1884 * Removes the given elements from this viewer. | |
1885 * <p> | |
1886 * EXPERIMENTAL. Not to be used except by JDT. This method was added to | |
1887 * support JDT's explorations into grouping by working sets, which requires | |
1888 * viewers to support multiple equal elements. See bug 76482 for more | |
1889 * details. This support will likely be removed in Eclipse 3.2 in favor of | |
1890 * proper support for multiple equal elements. | |
1891 * </p> | |
1892 * | |
1893 * @param elementsOrPaths | |
1894 * the elements or element paths to remove | |
1895 * @since 3.1 | |
1896 */ | |
1897 protected void internalRemove(Object[] elementsOrPaths) { | |
1898 Object input = getInput(); | |
1899 for (int i = 0; i < elementsOrPaths.length; ++i) { | |
1900 Object element = elementsOrPaths[i]; | |
1901 if (opEquals(element, input)) { | |
1902 setInput(null); | |
1903 return; | |
1904 } | |
1905 Widget[] childItems = internalFindItems(element); | |
1906 for (int j = 0; j < childItems.length; j++) { | |
1907 Widget childItem = childItems[j]; | |
1908 if ( auto it = cast(Item)childItem ) { | |
1909 disassociate(it); | |
1910 childItem.dispose(); | |
1911 } | |
1912 } | |
1913 } | |
1914 } | |
1915 | |
1916 /** | |
1917 * Removes the given elements from this viewer, whenever those elements | |
1918 * appear as children of the given parent. | |
1919 * | |
1920 * @param parent the parent element | |
1921 * @param elements | |
1922 * the elements to remove | |
1923 * @since 3.1 | |
1924 */ | |
1925 protected void internalRemove(Object parent, Object[] elements) { | |
1926 | |
1927 CustomHashtable toRemove = new CustomHashtable(getComparer()); | |
1928 for (int i = 0; i < elements.length; i++) { | |
1929 toRemove.put(elements[i], elements[i]); | |
1930 } | |
1931 | |
1932 // Find each place the parent appears in the tree | |
1933 Widget[] parentItemArray = findItems(parent); | |
1934 for (int i = 0; i < parentItemArray.length; i++) { | |
1935 Widget parentItem = parentItemArray[i]; | |
1936 | |
1937 // Iterate over the child items and remove each one | |
1938 Item[] children = getChildren(parentItem); | |
1939 | |
1940 for (int j = 0; j < children.length; j++) { | |
1941 Item child = children[j]; | |
1942 | |
1943 Object data = child.getData(); | |
1944 if (data !is null && toRemove.containsKey(data)) { | |
1945 disassociate(child); | |
1946 child.dispose(); | |
1947 } | |
1948 } | |
1949 } | |
1950 } | |
1951 | |
1952 /** | |
1953 * Sets the expanded state of all items to correspond to the given set of | |
1954 * expanded elements. | |
1955 * | |
1956 * @param expandedElements | |
1957 * the set (element type: <code>Object</code>) of elements | |
1958 * which are expanded | |
1959 * @param widget | |
1960 * the widget | |
1961 */ | |
1962 private void internalSetExpanded(CustomHashtable expandedElements, | |
1963 Widget widget) { | |
1964 Item[] items = getChildren(widget); | |
1965 for (int i = 0; i < items.length; i++) { | |
1966 Item item = items[i]; | |
1967 Object data = item.getData(); | |
1968 if (data !is null) { | |
1969 // remove the element to avoid an infinite loop | |
1970 // if the same element appears on a child item | |
1971 bool expanded = expandedElements.remove(data) !is null; | |
1972 if (expanded !is getExpanded(item)) { | |
1973 if (expanded) { | |
1974 createChildren(item); | |
1975 } | |
1976 setExpanded(item, expanded); | |
1977 } | |
1978 } | |
1979 if (expandedElements.size() > 0) { | |
1980 internalSetExpanded(expandedElements, item); | |
1981 } | |
1982 } | |
1983 } | |
1984 | |
1985 /** | |
1986 * Sets the expanded state of all items to correspond to the given set of | |
1987 * expanded tree paths. | |
1988 * | |
1989 * @param expandedTreePaths | |
1990 * the set (element type: <code>TreePath</code>) of elements | |
1991 * which are expanded | |
1992 * @param widget | |
1993 * the widget | |
1994 */ | |
1995 private void internalSetExpandedTreePaths( | |
1996 CustomHashtable expandedTreePaths, Widget widget, | |
1997 TreePath currentPath) { | |
1998 Item[] items = getChildren(widget); | |
1999 for (int i = 0; i < items.length; i++) { | |
2000 Item item = items[i]; | |
2001 Object data = item.getData(); | |
2002 TreePath childPath = data is null ? null : currentPath | |
2003 .createChildPath(data); | |
2004 if (data !is null && childPath !is null) { | |
2005 // remove the element to avoid an infinite loop | |
2006 // if the same element appears on a child item | |
2007 bool expanded = expandedTreePaths.remove(childPath) !is null; | |
2008 if (expanded !is getExpanded(item)) { | |
2009 if (expanded) { | |
2010 createChildren(item); | |
2011 } | |
2012 setExpanded(item, expanded); | |
2013 } | |
2014 } | |
2015 internalSetExpandedTreePaths(expandedTreePaths, item, childPath); | |
2016 } | |
2017 } | |
2018 | |
2019 /** | |
2020 * Return whether the tree node representing the given element or path can | |
2021 * be expanded. Clients should query expandability by path if the viewer's | |
2022 * content provider is an {@link ITreePathContentProvider}. | |
2023 * <p> | |
2024 * The default implementation of this framework method calls | |
2025 * <code>hasChildren</code> on this viewer's content provider. It may be | |
2026 * overridden if necessary. | |
2027 * </p> | |
2028 * | |
2029 * @param elementOrTreePath | |
2030 * the element or path | |
2031 * @return <code>true</code> if the tree node representing the given | |
2032 * element can be expanded, or <code>false</code> if not | |
2033 */ | |
2034 public bool isExpandable(Object elementOrTreePath) { | |
2035 Object element; | |
2036 TreePath path; | |
2037 if (auto p = cast(TreePath)elementOrTreePath) { | |
2038 path = p; | |
2039 element = path.getLastSegment(); | |
2040 } else { | |
2041 element = elementOrTreePath; | |
2042 path = null; | |
2043 } | |
2044 IContentProvider cp = getContentProvider(); | |
2045 if ( auto tpcp = cast(ITreePathContentProvider) cp ) { | |
2046 if (path is null) { | |
2047 // A path was not provided so try and find one | |
2048 Widget w = findItem(element); | |
2049 if ( auto item = cast(Item)w ) { | |
2050 path = getTreePathFromItem(item); | |
2051 } | |
2052 if (path is null) { | |
2053 path = new TreePath([ element ]); | |
2054 } | |
2055 } | |
2056 return tpcp.hasChildren(path); | |
2057 } | |
2058 if (auto tcp = cast(ITreeContentProvider)cp ) { | |
2059 return tcp.hasChildren(element); | |
2060 } | |
2061 return false; | |
2062 } | |
2063 | |
2064 /** | |
2065 * Return whether the given element is expandable. | |
2066 * | |
2067 * @param item | |
2068 * the tree item for the element | |
2069 * @param parentPath | |
2070 * the parent path if it is known or <code>null</code> if it | |
2071 * needs to be determines | |
2072 * @param element | |
2073 * the element | |
2074 * @return whether the given element is expandable | |
2075 */ | |
2076 private bool isExpandable(Item item, TreePath parentPath, Object element) { | |
2077 Object elementOrTreePath = element; | |
2078 if (isTreePathContentProvider()) { | |
2079 if (parentPath !is null) { | |
2080 elementOrTreePath = parentPath.createChildPath(element); | |
2081 } else { | |
2082 elementOrTreePath = getTreePathFromItem(item); | |
2083 } | |
2084 } | |
2085 return isExpandable(elementOrTreePath); | |
2086 } | |
2087 | |
2088 /* (non-Javadoc) Method declared on Viewer. */ | |
2089 protected void labelProviderChanged() { | |
2090 // we have to walk the (visible) tree and update every item | |
2091 Control tree = getControl(); | |
2092 tree.setRedraw(false); | |
2093 // don't pick up structure changes, but do force label updates | |
2094 internalRefresh(tree, getRoot(), false, true); | |
2095 tree.setRedraw(true); | |
2096 } | |
2097 | |
2098 /** | |
2099 * Creates a new item. | |
2100 * | |
2101 * @param parent | |
2102 * the parent widget | |
2103 * @param style | |
2104 * DWT style bits | |
2105 * @param index | |
2106 * if non-negative, indicates the position to insert the item | |
2107 * into its parent | |
2108 * @return the newly-created item | |
2109 */ | |
2110 protected abstract Item newItem(Widget parent, int style, int index); | |
2111 | |
2112 /** | |
2113 * Removes the given elements from this viewer. The selection is updated if | |
2114 * required. | |
2115 * <p> | |
2116 * This method should be called (by the content provider) when elements have | |
2117 * been removed from the model, in order to cause the viewer to accurately | |
2118 * reflect the model. This method only affects the viewer, not the model. | |
2119 * </p> | |
2120 * | |
2121 * @param elementsOrTreePaths | |
2122 * the elements to remove | |
2123 */ | |
2124 public void remove(Object[] elementsOrTreePaths) { | |
2125 assertElementsNotNull(elementsOrTreePaths); | |
2126 if (elementsOrTreePaths.length is 0) { | |
2127 return; | |
2128 } | |
2129 if (isBusy()) | |
2130 return; | |
39 | 2131 preservingSelection(new class(elementsOrTreePaths) Runnable { |
10 | 2132 Object[] elementsOrTreePaths_; |
39 | 2133 this(Object[] a){ |
2134 elementsOrTreePaths_=a; | |
10 | 2135 } |
2136 public void run() { | |
2137 internalRemove(elementsOrTreePaths_); | |
2138 } | |
2139 }); | |
2140 } | |
2141 | |
2142 /** | |
2143 * Removes the given elements from this viewer whenever they appear as | |
2144 * children of the given parent element. If the given elements also appear | |
2145 * as children of some other parent, the other parent will remain unchanged. | |
2146 * The selection is updated if required. | |
2147 * <p> | |
2148 * This method should be called (by the content provider) when elements have | |
2149 * been removed from the model, in order to cause the viewer to accurately | |
2150 * reflect the model. This method only affects the viewer, not the model. | |
2151 * </p> | |
2152 * | |
2153 * @param parent | |
2154 * the parent of the elements to remove | |
2155 * @param elements | |
2156 * the elements to remove | |
2157 * | |
2158 * @since 3.2 | |
2159 */ | |
2160 public void remove(Object parent, Object[] elements) { | |
2161 assertElementsNotNull(elements); | |
2162 if (elements.length is 0) { | |
2163 return; | |
2164 } | |
2165 if (isBusy()) | |
2166 return; | |
39 | 2167 preservingSelection(new class(parent,elements) Runnable { |
10 | 2168 Object parent_; |
2169 Object[] elements_; | |
39 | 2170 this(Object a,Object[] b){ |
2171 parent_=a; | |
2172 elements_=b; | |
10 | 2173 } |
2174 public void run() { | |
2175 internalRemove(parent_, elements_); | |
2176 } | |
2177 }); | |
2178 } | |
2179 | |
2180 /** | |
2181 * Removes the given element from the viewer. The selection is updated if | |
2182 * necessary. | |
2183 * <p> | |
2184 * This method should be called (by the content provider) when a single | |
2185 * element has been removed from the model, in order to cause the viewer to | |
2186 * accurately reflect the model. This method only affects the viewer, not | |
2187 * the model. Note that there is another method for efficiently processing | |
2188 * the simultaneous removal of multiple elements. | |
2189 * </p> | |
2190 * | |
2191 * @param elementsOrTreePaths | |
2192 * the element | |
2193 */ | |
2194 public void remove(Object elementsOrTreePaths) { | |
2195 remove([ elementsOrTreePaths ]); | |
2196 } | |
2197 | |
2198 /** | |
2199 * Removes all items from the given control. | |
2200 * | |
2201 * @param control | |
2202 * the control | |
2203 */ | |
2204 protected abstract void removeAll(Control control); | |
2205 | |
2206 /** | |
2207 * Removes a listener for expand and collapse events in this viewer. Has no | |
2208 * affect if an identical listener is not registered. | |
2209 * | |
2210 * @param listener | |
2211 * a tree viewer listener | |
2212 */ | |
2213 public void removeTreeListener(ITreeViewerListener listener) { | |
2214 treeListeners.remove(cast(Object)listener); | |
2215 } | |
2216 | |
2217 /** | |
2218 * This implementation of reveal() reveals the given element or tree path. | |
2219 */ | |
2220 public void reveal(Object elementOrTreePath) { | |
2221 Assert.isNotNull(elementOrTreePath); | |
2222 Widget w = internalExpand(elementOrTreePath, true); | |
2223 if ( auto item = cast(Item)w ) { | |
2224 showItem(item); | |
2225 } | |
2226 } | |
2227 | |
2228 /** | |
2229 * Returns the rightmost visible descendent of the given item. Returns the | |
2230 * item itself if it has no children. | |
2231 * | |
2232 * @param item | |
2233 * the item to compute the descendent of | |
2234 * @return the rightmost visible descendent or the item itself if it has no | |
2235 * children | |
2236 */ | |
2237 private Item rightMostVisibleDescendent(Item item) { | |
2238 Item[] children = getItems(item); | |
2239 if (getExpanded(item) && children !is null && children.length > 0) { | |
2240 return rightMostVisibleDescendent(children[children.length - 1]); | |
2241 } | |
2242 return item; | |
2243 } | |
2244 | |
2245 /* (non-Javadoc) Method declared on Viewer. */ | |
2246 public Item scrollDown(int x, int y) { | |
2247 Item current = getItem(x, y); | |
2248 if (current !is null) { | |
2249 Item next = getNextItem(current, true); | |
2250 showItem(next is null ? current : next); | |
2251 return next; | |
2252 } | |
2253 return null; | |
2254 } | |
2255 | |
2256 /* (non-Javadoc) Method declared on Viewer. */ | |
2257 public Item scrollUp(int x, int y) { | |
2258 Item current = getItem(x, y); | |
2259 if (current !is null) { | |
2260 Item previous = getPreviousItem(current); | |
2261 showItem(previous is null ? current : previous); | |
2262 return previous; | |
2263 } | |
2264 return null; | |
2265 } | |
2266 | |
2267 /** | |
2268 * Sets the auto-expand level. The value 0 means that there is no | |
2269 * auto-expand; 1 means that top-level elements are expanded, but not their | |
2270 * children; 2 means that top-level elements are expanded, and their | |
2271 * children, but not grandchildren; and so on. | |
2272 * <p> | |
2273 * The value <code>ALL_LEVELS</code> means that all subtrees should be | |
2274 * expanded. | |
2275 * </p> | |
2276 * | |
2277 * @param level | |
2278 * non-negative level, or <code>ALL_LEVELS</code> to expand all | |
2279 * levels of the tree | |
2280 */ | |
2281 public void setAutoExpandLevel(int level) { | |
2282 expandToLevel_ = level; | |
2283 } | |
2284 | |
2285 /** | |
2286 * The <code>AbstractTreeViewer</code> implementation of this method | |
2287 * checks to ensure that the content provider is an | |
2288 * <code>ITreeContentProvider</code>. | |
2289 */ | |
2290 public void setContentProvider(IContentProvider provider) { | |
2291 // the actual check is in assertContentProviderType | |
2292 super.setContentProvider(provider); | |
2293 } | |
2294 | |
2295 protected void assertContentProviderType(IContentProvider provider) { | |
2296 Assert.isTrue(cast(ITreeContentProvider)provider | |
2297 || cast(ITreePathContentProvider)provider ); | |
2298 } | |
2299 | |
2300 /** | |
2301 * Sets the expand state of the given item. | |
2302 * | |
2303 * @param item | |
2304 * the item | |
2305 * @param expand | |
2306 * the expand state of the item | |
2307 */ | |
2308 protected abstract void setExpanded(Item item, bool expand); | |
2309 | |
2310 /** | |
2311 * Sets which nodes are expanded in this viewer's tree. The given list | |
2312 * contains the elements that are to be expanded; all other nodes are to be | |
2313 * collapsed. | |
2314 * <p> | |
2315 * This method is typically used when restoring the interesting state of a | |
2316 * viewer captured by an earlier call to <code>getExpandedElements</code>. | |
2317 * </p> | |
2318 * | |
2319 * @param elements | |
2320 * the array of expanded elements | |
2321 * @see #getExpandedElements | |
2322 */ | |
2323 public void setExpandedElements(Object[] elements) { | |
2324 assertElementsNotNull(elements); | |
2325 if (isBusy()) { | |
2326 return; | |
2327 } | |
2328 CustomHashtable expandedElements = newHashtable(elements.length * 2 + 1); | |
2329 for (int i = 0; i < elements.length; ++i) { | |
2330 Object element = elements[i]; | |
2331 // Ensure item exists for element. This will materialize items for | |
2332 // each element and their parents, if possible. This is important | |
2333 // to support expanding of inner tree nodes without necessarily | |
2334 // expanding their parents. | |
2335 internalExpand(element, false); | |
2336 expandedElements.put(element, element); | |
2337 } | |
2338 // this will traverse all existing items, and create children for | |
2339 // elements that need to be expanded. If the tree contains multiple | |
2340 // equal elements, and those are in the set of elements to be expanded, | |
2341 // only the first item found for each element will be expanded. | |
2342 internalSetExpanded(expandedElements, getControl()); | |
2343 } | |
2344 | |
2345 /** | |
2346 * Sets which nodes are expanded in this viewer's tree. The given list | |
2347 * contains the tree paths that are to be expanded; all other nodes are to | |
2348 * be collapsed. | |
2349 * <p> | |
2350 * This method is typically used when restoring the interesting state of a | |
2351 * viewer captured by an earlier call to <code>getExpandedTreePaths</code>. | |
2352 * </p> | |
2353 * | |
2354 * @param treePaths | |
2355 * the array of expanded tree paths | |
2356 * @see #getExpandedTreePaths() | |
2357 * | |
2358 * @since 3.2 | |
2359 */ | |
2360 public void setExpandedTreePaths(TreePath[] treePaths) { | |
2361 assertElementsNotNull(treePaths); | |
2362 if (isBusy()) | |
2363 return; | |
39 | 2364 IElementComparer treePathComparer = new class(getComparer()) IElementComparer { |
10 | 2365 IElementComparer comparer; |
39 | 2366 this(IElementComparer c){ |
2367 comparer = c; | |
10 | 2368 } |
2369 public int opEquals(Object a, Object b) { | |
2370 return (cast(TreePath) a).opEquals((cast(TreePath) b), comparer); | |
2371 } | |
2372 | |
2373 public hash_t toHash(Object element) { | |
2374 return (cast(TreePath) element).toHash(comparer); | |
2375 } | |
2376 }; | |
2377 CustomHashtable expandedTreePaths = new CustomHashtable( | |
2378 treePaths.length * 2 + 1, treePathComparer); | |
2379 for (int i = 0; i < treePaths.length; ++i) { | |
2380 TreePath treePath = treePaths[i]; | |
2381 // Ensure item exists for element. This will materialize items for | |
2382 // each element and their parents, if possible. This is important | |
2383 // to support expanding of inner tree nodes without necessarily | |
2384 // expanding their parents. | |
2385 internalExpand(treePath, false); | |
2386 expandedTreePaths.put(treePath, treePath); | |
2387 } | |
2388 // this will traverse all existing items, and create children for | |
2389 // elements that need to be expanded. If the tree contains multiple | |
2390 // equal elements, and those are in the set of elements to be expanded, | |
2391 // only the first item found for each element will be expanded. | |
2392 internalSetExpandedTreePaths(expandedTreePaths, getControl(), | |
2393 new TreePath(new Object[0])); | |
2394 } | |
2395 | |
2396 /** | |
2397 * Sets whether the node corresponding to the given element or tree path is | |
2398 * expanded or collapsed. | |
2399 * | |
2400 * @param elementOrTreePath | |
2401 * the element | |
2402 * @param expanded | |
2403 * <code>true</code> if the node is expanded, and | |
2404 * <code>false</code> if collapsed | |
2405 */ | |
2406 public void setExpandedState(Object elementOrTreePath, bool expanded) { | |
2407 Assert.isNotNull(elementOrTreePath); | |
2408 if (isBusy()) | |
2409 return; | |
2410 Widget item = internalExpand(elementOrTreePath, false); | |
2411 if ( cast(Item)item ) { | |
2412 if (expanded) { | |
2413 createChildren(item); | |
2414 } | |
2415 setExpanded(cast(Item) item, expanded); | |
2416 } | |
2417 } | |
2418 | |
2419 /** | |
2420 * Sets the selection to the given list of items. | |
2421 * | |
2422 * @param items | |
2423 * list of items (element type: | |
2424 * <code>dwt.widgets.Item</code>) | |
2425 */ | |
2426 protected abstract void setSelection(SeqView!(Item) items); | |
2427 | |
2428 /** | |
2429 * This implementation of setSelectionToWidget accepts a list of elements or | |
2430 * a list of tree paths. | |
2431 */ | |
2432 protected void setSelectionToWidget(SeqView!(Object) v, bool reveal) { | |
2433 if (v is null) { | |
2434 setSelection(new ArraySeq!(Item)); | |
2435 return; | |
2436 } | |
2437 int size = v.size(); | |
2438 auto newSelection = new ArraySeq!(Item); | |
2439 newSelection.capacity(size); | |
2440 for (int i = 0; i < size; ++i) { | |
2441 Object elementOrTreePath = v.get(i); | |
2442 // Use internalExpand since item may not yet be created. See | |
2443 // 1G6B1AR. | |
2444 Widget w = internalExpand(elementOrTreePath, false); | |
2445 if ( auto it = cast(Item)w ) { | |
2446 newSelection.append(it); | |
2447 } else if (w is null && null !is cast(TreePath)elementOrTreePath ) { | |
2448 TreePath treePath = cast(TreePath) elementOrTreePath; | |
2449 Object element = treePath.getLastSegment(); | |
2450 if (element !is null) { | |
2451 w = internalExpand(element, false); | |
2452 if ( auto it = cast(Item)w ) { | |
2453 newSelection.append(it); | |
2454 } | |
2455 } | |
2456 } | |
2457 } | |
2458 setSelection(newSelection); | |
2459 | |
2460 // Although setting the selection in the control should reveal it, | |
2461 // setSelection may be a no-op if the selection is unchanged, | |
2462 // so explicitly reveal the first item in the selection here. | |
2463 // See bug 100565 for more details. | |
2464 if (reveal && newSelection.size() > 0) { | |
2465 showItem(cast(Item) newSelection.get(0)); | |
2466 } | |
2467 } | |
2468 | |
2469 /** | |
2470 * Shows the given item. | |
2471 * | |
2472 * @param item | |
2473 * the item | |
2474 */ | |
2475 protected abstract void showItem(Item item); | |
2476 | |
2477 /** | |
2478 * Updates the tree items to correspond to the child elements of the given | |
2479 * parent element. If null is passed for the children, this method obtains | |
2480 * them (only if needed). | |
2481 * | |
2482 * @param widget | |
2483 * the widget | |
2484 * @param parent | |
2485 * the parent element | |
2486 * @param elementChildren | |
2487 * the child elements, or null | |
2488 * @deprecated this is no longer called by the framework | |
2489 */ | |
2490 protected void updateChildren(Widget widget, Object parent, | |
2491 Object[] elementChildren) { | |
2492 updateChildren(widget, parent, elementChildren, true); | |
2493 } | |
2494 | |
2495 /** | |
2496 * Updates the tree items to correspond to the child elements of the given | |
2497 * parent element. If null is passed for the children, this method obtains | |
2498 * them (only if needed). | |
2499 * | |
2500 * @param widget | |
2501 * the widget | |
2502 * @param parent | |
2503 * the parent element | |
2504 * @param elementChildren | |
2505 * the child elements, or null | |
2506 * @param updateLabels | |
2507 * <code>true</code> to update labels for existing elements, | |
2508 * <code>false</code> to only update labels as needed, assuming | |
2509 * that labels for existing elements are unchanged. | |
2510 * @since 2.1 | |
2511 */ | |
2512 private void updateChildren(Widget widget, Object parent, | |
2513 Object[] elementChildren, bool updateLabels) { | |
2514 // optimization! prune collapsed subtrees | |
2515 if (auto ti = cast(Item)widget ) { | |
2516 if (!getExpanded(ti)) { | |
2517 // need a dummy node if element is expandable; | |
2518 // but try to avoid recreating the dummy node | |
2519 bool needDummy = isExpandable(ti, null, parent); | |
2520 bool haveDummy = false; | |
2521 // remove all children | |
2522 Item[] items = getItems(ti); | |
2523 for (int i = 0; i < items.length; i++) { | |
2524 if (items[i].getData() !is null) { | |
2525 disassociate(items[i]); | |
2526 items[i].dispose(); | |
2527 } else { | |
2528 if (needDummy && !haveDummy) { | |
2529 haveDummy = true; | |
2530 } else { | |
2531 items[i].dispose(); | |
2532 } | |
2533 } | |
2534 } | |
2535 if (needDummy && !haveDummy) { | |
2536 newItem(ti, DWT.NULL, -1); | |
2537 } | |
2538 | |
2539 return; | |
2540 } | |
2541 } | |
2542 | |
2543 // If the children weren't passed in, get them now since they're needed | |
2544 // below. | |
2545 if (elementChildren is null) { | |
2546 if (isTreePathContentProvider() && null !is cast(Item) widget ) { | |
2547 TreePath path = getTreePathFromItem(cast(Item) widget); | |
2548 elementChildren = getSortedChildren(path); | |
2549 } else { | |
2550 elementChildren = getSortedChildren(parent); | |
2551 } | |
2552 } | |
2553 | |
2554 Control tree = getControl(); | |
2555 | |
2556 // WORKAROUND | |
2557 int oldCnt = -1; | |
2558 if (widget is tree) { | |
2559 oldCnt = getItemCount(tree); | |
2560 } | |
2561 | |
2562 Item[] items = getChildren(widget); | |
2563 | |
2564 // save the expanded elements | |
2565 CustomHashtable expanded = newHashtable(CustomHashtable.DEFAULT_CAPACITY); // assume | |
2566 // num | |
2567 // expanded | |
2568 // is | |
2569 // small | |
2570 for (int i = 0; i < items.length; ++i) { | |
2571 if (getExpanded(items[i])) { | |
2572 Object element = items[i].getData(); | |
2573 if (element !is null) { | |
2574 expanded.put(element, element); | |
2575 } | |
2576 } | |
2577 } | |
2578 | |
2579 int min = Math.min(elementChildren.length, items.length); | |
2580 | |
2581 // dispose of surplus items, optimizing for the case where elements have | |
2582 // been deleted but not reordered, or all elements have been removed. | |
2583 int numItemsToDispose = items.length - min; | |
2584 if (numItemsToDispose > 0) { | |
2585 CustomHashtable children = newHashtable(elementChildren.length * 2); | |
2586 for (int i = 0; i < elementChildren.length; i++) { | |
2587 Object elementChild = elementChildren[i]; | |
2588 children.put(elementChild, elementChild); | |
2589 } | |
2590 int i = 0; | |
2591 while (numItemsToDispose > 0 && i < items.length) { | |
2592 Object data = items[i].getData(); | |
2593 if (data is null || items.length - i <= numItemsToDispose || !children.containsKey(data)) { | |
2594 if (data !is null) { | |
2595 disassociate(items[i]); | |
2596 } | |
2597 items[i].dispose(); | |
2598 if (i + 1 < items.length) { | |
2599 // The components at positions i+1 through | |
2600 // items.length-1 in the source array are copied into | |
2601 // positions i through items.length-2 | |
2602 System.arraycopy(items, i + 1, items, i, items.length - (i+1)); | |
2603 } | |
2604 numItemsToDispose--; | |
2605 } else { | |
2606 i++; | |
2607 } | |
2608 } | |
2609 } | |
2610 | |
2611 // compare first min items, and update item if necessary | |
2612 // need to do it in two passes: | |
2613 // 1: disassociate old items | |
2614 // 2: associate new items | |
2615 // because otherwise a later disassociate can remove a mapping made for | |
2616 // a previous associate, | |
2617 // making the map inconsistent | |
2618 for (int i = 0; i < min; ++i) { | |
2619 Item item = items[i]; | |
2620 Object oldElement = item.getData(); | |
2621 if (oldElement !is null) { | |
2622 Object newElement = elementChildren[i]; | |
2623 if (newElement !is oldElement) { | |
2624 if (opEquals(newElement, oldElement)) { | |
2625 // update the data to be the new element, since | |
2626 // although the elements | |
2627 // may be equal, they may still have different labels | |
2628 // or children | |
2629 Object data = item.getData(); | |
2630 if (data !is null) { | |
2631 unmapElement(data, item); | |
2632 } | |
2633 item.setData(newElement); | |
2634 mapElement(newElement, item); | |
2635 } else { | |
2636 disassociate(item); | |
2637 // Clear the text and image to force a label update | |
2638 item.setImage(null); | |
2639 item.setText("");//$NON-NLS-1$ | |
2640 | |
2641 } | |
2642 } | |
2643 } | |
2644 } | |
2645 | |
2646 for (int i = 0; i < min; ++i) { | |
2647 Item item = items[i]; | |
2648 Object newElement = elementChildren[i]; | |
2649 if (item.getData() is null) { | |
2650 // old and new elements are not equal | |
2651 associate(newElement, item); | |
2652 updatePlus(item, newElement); | |
2653 updateItem(item, newElement); | |
2654 } else { | |
2655 // old and new elements are equal | |
2656 updatePlus(item, newElement); | |
2657 if (updateLabels) { | |
2658 updateItem(item, newElement); | |
2659 } | |
2660 } | |
2661 } | |
2662 | |
2663 // Restore expanded state for items that changed position. | |
2664 // Make sure setExpanded is called after updatePlus, since | |
2665 // setExpanded(false) fails if item has no children. | |
2666 // Need to call setExpanded for both expanded and unexpanded | |
2667 // cases since the expanded state can change either way. | |
2668 // This needs to be done in a second loop, see bug 148025. | |
2669 for (int i = 0; i < min; ++i) { | |
2670 Item item = items[i]; | |
2671 Object newElement = elementChildren[i]; | |
2672 setExpanded(item, expanded.containsKey(newElement)); | |
2673 } | |
2674 | |
2675 // add any remaining elements | |
2676 if (min < elementChildren.length) { | |
2677 for (int i = min; i < elementChildren.length; ++i) { | |
2678 createTreeItem(widget, elementChildren[i], i); | |
2679 } | |
2680 | |
2681 // Need to restore expanded state in a separate pass | |
2682 // because createTreeItem does not return the new item. | |
2683 // Avoid doing this unless needed. | |
2684 if (expanded.size() > 0) { | |
2685 // get the items again, to include the new items | |
2686 items = getChildren(widget); | |
2687 for (int i = min; i < elementChildren.length; ++i) { | |
2688 // Restore expanded state for items that changed position. | |
2689 // Make sure setExpanded is called after updatePlus (called | |
2690 // in createTreeItem), since | |
2691 // setExpanded(false) fails if item has no children. | |
2692 // Only need to call setExpanded if element was expanded | |
2693 // since new items are initially unexpanded. | |
2694 if (expanded.containsKey(elementChildren[i])) { | |
2695 setExpanded(items[i], true); | |
2696 } | |
2697 } | |
2698 } | |
2699 } | |
2700 | |
2701 // WORKAROUND | |
2702 if (widget is tree && oldCnt is 0 && getItemCount(tree) !is 0) { | |
2703 // System.out.println("WORKAROUND setRedraw"); | |
2704 tree.setRedraw(false); | |
2705 tree.setRedraw(true); | |
2706 } | |
2707 } | |
2708 | |
2709 /** | |
2710 * Updates the "+"/"-" icon of the tree node from the given element. It | |
2711 * calls <code>isExpandable</code> to determine whether an element is | |
2712 * expandable. | |
2713 * | |
2714 * @param item | |
2715 * the item | |
2716 * @param element | |
2717 * the element | |
2718 */ | |
2719 protected void updatePlus(Item item, Object element) { | |
2720 bool hasPlus = getItemCount(item) > 0; | |
2721 bool needsPlus = isExpandable(item, null, element); | |
2722 bool removeAll = false; | |
2723 bool addDummy = false; | |
2724 Object data = item.getData(); | |
2725 if (data !is null && opEquals(element, data)) { | |
2726 // item shows same element | |
2727 if (hasPlus !is needsPlus) { | |
2728 if (needsPlus) { | |
2729 addDummy = true; | |
2730 } else { | |
2731 removeAll = true; | |
2732 } | |
2733 } | |
2734 } else { | |
2735 // item shows different element | |
2736 removeAll = true; | |
2737 addDummy = needsPlus; | |
2738 | |
2739 // we cannot maintain expand state so collapse it | |
2740 setExpanded(item, false); | |
2741 } | |
2742 if (removeAll) { | |
2743 // remove all children | |
2744 Item[] items = getItems(item); | |
2745 for (int i = 0; i < items.length; i++) { | |
2746 if (items[i].getData() !is null) { | |
2747 disassociate(items[i]); | |
2748 } | |
2749 items[i].dispose(); | |
2750 } | |
2751 } | |
2752 if (addDummy) { | |
2753 newItem(item, DWT.NULL, -1); // append a dummy | |
2754 } | |
2755 } | |
2756 | |
2757 /** | |
2758 * Gets the expanded elements that are visible to the user. An expanded | |
2759 * element is only visible if the parent is expanded. | |
2760 * | |
2761 * @return the visible expanded elements | |
2762 * @since 2.0 | |
2763 */ | |
2764 public Object[] getVisibleExpandedElements() { | |
2765 auto v = new ArraySeq!(Object); | |
2766 internalCollectVisibleExpanded(v, getControl()); | |
2767 return v.toArray(); | |
2768 } | |
2769 | |
2770 private void internalCollectVisibleExpanded(ArraySeq!(Object) result, Widget widget) { | |
2771 Item[] items = getChildren(widget); | |
2772 for (int i = 0; i < items.length; i++) { | |
2773 Item item = items[i]; | |
2774 if (getExpanded(item)) { | |
2775 Object data = item.getData(); | |
2776 if (data !is null) { | |
2777 result.append(data); | |
2778 } | |
2779 // Only recurse if it is expanded - if | |
2780 // not then the children aren't visible | |
2781 internalCollectVisibleExpanded(result, item); | |
2782 } | |
2783 } | |
2784 } | |
2785 | |
2786 /** | |
2787 * Returns the tree path for the given item. | |
2788 * @param item | |
2789 * @return {@link TreePath} | |
2790 * | |
2791 * @since 3.2 | |
2792 */ | |
2793 protected TreePath getTreePathFromItem(Item item) { | |
2794 auto segments = new LinkSeq!(Object); | |
2795 while (item !is null) { | |
2796 Object segment = item.getData(); | |
2797 Assert.isNotNull(segment); | |
2798 segments.prepend(segment); | |
2799 item = getParentItem(item); | |
2800 } | |
2801 return new TreePath(segments.toArray()); | |
2802 } | |
2803 package TreePath getTreePathFromItem_package(Item item) { | |
2804 return getTreePathFromItem_package(item); | |
2805 } | |
2806 | |
2807 /** | |
2808 * This implementation of getSelection() returns an instance of | |
2809 * ITreeSelection. | |
2810 * | |
2811 * @since 3.2 | |
2812 */ | |
2813 public ISelection getSelection() { | |
2814 Control control = getControl(); | |
2815 if (control is null || control.isDisposed()) { | |
2816 return TreeSelection.EMPTY; | |
2817 } | |
2818 Widget[] items = getSelection(getControl()); | |
2819 auto list = new ArraySeq!(TreePath); | |
2820 list.capacity(items.length); | |
2821 for (int i = 0; i < items.length; i++) { | |
2822 Widget item = items[i]; | |
2823 if (item.getData() !is null) { | |
2824 list.append(getTreePathFromItem(cast(Item) item)); | |
2825 } | |
2826 } | |
2827 return new TreeSelection( list.toArray(), getComparer()); | |
2828 } | |
2829 | |
2830 protected void setSelectionToWidget(ISelection selection, bool reveal) { | |
2831 if ( auto treeSelection = cast(ITreeSelection)selection ) { | |
2832 auto list = new ArraySeq!(Object); | |
2833 auto paths = treeSelection.getPaths(); | |
2834 list.capacity(paths.length); | |
2835 foreach( path; paths ){ | |
2836 list.append(path); | |
2837 } | |
2838 setSelectionToWidget(list, reveal); | |
2839 } else { | |
2840 super.setSelectionToWidget(selection, reveal); | |
2841 } | |
2842 } | |
2843 | |
2844 /** | |
2845 * Returns a list of tree paths corresponding to expanded nodes in this | |
2846 * viewer's tree, including currently hidden ones that are marked as | |
2847 * expanded but are under a collapsed ancestor. | |
2848 * <p> | |
2849 * This method is typically used when preserving the interesting state of a | |
2850 * viewer; <code>setExpandedElements</code> is used during the restore. | |
2851 * </p> | |
2852 * | |
2853 * @return the array of expanded tree paths | |
2854 * @see #setExpandedElements | |
2855 * | |
2856 * @since 3.2 | |
2857 */ | |
2858 public TreePath[] getExpandedTreePaths() { | |
2859 auto items = new ArraySeq!(Item); | |
2860 internalCollectExpandedItems(items, getControl()); | |
2861 auto result = new ArraySeq!(TreePath); | |
2862 result.capacity(items.size()); | |
2863 foreach( item; items ){ | |
2864 TreePath treePath = getTreePathFromItem(item); | |
2865 if (treePath !is null) { | |
2866 result.append(treePath); | |
2867 } | |
2868 } | |
2869 return result.toArray(); | |
2870 } | |
2871 | |
2872 private bool isTreePathContentProvider() { | |
2873 return null !is cast(ITreePathContentProvider)getContentProvider() ; | |
2874 } | |
2875 | |
2876 /** | |
2877 * Inserts the given element as a new child element of the given parent | |
2878 * element at the given position. If this viewer has a sorter, the position | |
2879 * is ignored and the element is inserted at the correct position in the | |
2880 * sort order. | |
2881 * <p> | |
2882 * This method should be called (by the content provider) when elements have | |
2883 * been added to the model, in order to cause the viewer to accurately | |
2884 * reflect the model. This method only affects the viewer, not the model. | |
2885 * </p> | |
2886 * | |
2887 * @param parentElementOrTreePath | |
2888 * the parent element, or the tree path to the parent | |
2889 * @param element | |
2890 * the element | |
2891 * @param position | |
2892 * a 0-based position relative to the model, or -1 to indicate | |
2893 * the last position | |
2894 * | |
2895 * @since 3.2 | |
2896 */ | |
2897 public void insert(Object parentElementOrTreePath, Object element, | |
2898 int position) { | |
2899 Assert.isNotNull(parentElementOrTreePath); | |
2900 Assert.isNotNull(element); | |
2901 if (isBusy()) | |
2902 return; | |
2903 if (getComparator() !is null || hasFilters()) { | |
2904 add(parentElementOrTreePath, [ element ]); | |
2905 return; | |
2906 } | |
2907 Widget[] items; | |
2908 if (internalIsInputOrEmptyPath(parentElementOrTreePath)) { | |
2909 items = [ getControl() ]; | |
2910 } else { | |
2911 items = internalFindItems(parentElementOrTreePath); | |
2912 } | |
2913 | |
2914 for (int i = 0; i < items.length; i++) { | |
2915 Widget widget = items[i]; | |
2916 if (auto item = cast(Item)widget ) { | |
2917 | |
2918 Item[] childItems = getChildren(item); | |
2919 if (getExpanded(item) | |
2920 || (childItems.length > 0 && childItems[0].getData() !is null)) { | |
2921 // item has real children, go ahead and add | |
2922 int insertionPosition = position; | |
2923 if (insertionPosition is -1) { | |
2924 insertionPosition = getItemCount(item); | |
2925 } | |
2926 | |
2927 createTreeItem(item, element, insertionPosition); | |
2928 } | |
2929 } else { | |
2930 int insertionPosition = position; | |
2931 if (insertionPosition is -1) { | |
2932 insertionPosition = getItemCount(cast(Control) widget); | |
2933 } | |
2934 | |
2935 createTreeItem(widget, element, insertionPosition); | |
2936 } | |
2937 } | |
2938 } | |
2939 | |
2940 /* | |
2941 * (non-Javadoc) | |
2942 * | |
2943 * @see dwtx.jface.viewers.ColumnViewer#getColumnViewerOwner(int) | |
2944 */ | |
2945 protected Widget getColumnViewerOwner(int columnIndex) { | |
2946 // Return null by default | |
2947 return null; | |
2948 } | |
2949 | |
2950 /** | |
2951 * This implementation of {@link #getItemAt(Point)} returns null to ensure | |
2952 * API backwards compatibility. Subclasses should override. | |
2953 * | |
2954 * @since 3.3 | |
2955 */ | |
2956 protected Item getItemAt(Point point) { | |
2957 return null; | |
2958 } | |
2959 | |
2960 /** | |
2961 * This implementation of {@link #createViewerEditor()} returns null to ensure | |
2962 * API backwards compatibility. Subclasses should override. | |
2963 * | |
2964 * @since 3.3 | |
2965 */ | |
2966 protected ColumnViewerEditor createViewerEditor() { | |
2967 return null; | |
2968 } | |
2969 | |
2970 /** | |
2971 * Returns the number of columns of this viewer. | |
2972 * <p><b>Subclasses should overwrite this method, which has a default | |
2973 * implementation (returning 0) for API backwards compatility reasons</b></p> | |
2974 * | |
2975 * @return the number of columns | |
2976 * | |
2977 * @since 3.3 | |
2978 */ | |
2979 protected int doGetColumnCount() { | |
2980 return 0; | |
2981 } | |
2982 | |
2983 | |
2984 /** | |
2985 * This implementation of buildLabel handles tree paths as well as elements. | |
2986 * | |
2987 * @param updateLabel | |
2988 * the ViewerLabel to collect the result in | |
2989 * @param elementOrPath | |
2990 * the element or tree path for which a label should be built | |
2991 * | |
2992 * @see dwtx.jface.viewers.StructuredViewer#buildLabel(dwtx.jface.viewers.ViewerLabel, | |
2993 * java.lang.Object) | |
2994 */ | |
2995 protected void buildLabel(ViewerLabel updateLabel, Object elementOrPath) { | |
40
da5ad8eedf5d
debug prints, dwt.dwthelper restructure, ...
Frank Benoit <benoit@tionex.de>
parents:
39
diff
changeset
|
2996 Trace.formatln( "{} {}:", __FILE__, __LINE__ ); |
10 | 2997 Object element; |
2998 if (auto path = cast(TreePath)elementOrPath ) { | |
2999 IBaseLabelProvider provider = getLabelProvider(); | |
3000 if ( auto pprov = cast(ITreePathLabelProvider) provider ) { | |
3001 buildLabel(updateLabel, path, pprov); | |
3002 return; | |
3003 } | |
3004 element = path.getLastSegment(); | |
3005 } else { | |
3006 element = elementOrPath; | |
3007 } | |
3008 super.buildLabel(updateLabel, element); | |
3009 } | |
3010 | |
3011 /** | |
3012 * Returns true if the given object is either the input or an empty tree path. | |
3013 * | |
3014 * @param elementOrTreePath an element which could either be the viewer's input, or a tree path | |
3015 * | |
3016 * @return <code>true</code> if the given object is either the input or an empty tree path, | |
3017 * <code>false</code> otherwise. | |
3018 * @since 3.3 | |
3019 */ | |
3020 final protected bool internalIsInputOrEmptyPath(Object elementOrTreePath) { | |
3021 if (elementOrTreePath.opEquals(getInput())) | |
3022 return true; | |
3023 if (!(cast(TreePath)elementOrTreePath )) | |
3024 return false; | |
3025 return (cast(TreePath) elementOrTreePath).getSegmentCount() is 0; | |
3026 } | |
3027 | |
3028 /* | |
3029 * Subclasses should implement | |
3030 */ | |
3031 protected ViewerRow getViewerRowFromItem(Widget item) { | |
3032 return null; | |
3033 } | |
3034 } |