comparison org.eclipse.jface/src/org/eclipse/jface/viewers/CheckboxTreeViewer.d @ 12:bc29606a740c

Added dwt-addons in original directory structure of eclipse.org
author Frank Benoit <benoit@tionex.de>
date Sat, 14 Mar 2009 18:23:29 +0100
parents
children
comparison
equal deleted inserted replaced
11:43904fec5dca 12:bc29606a740c
1 /*******************************************************************************
2 * Copyright (c) 2000, 2008 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * IBM Corporation - initial API and implementation
10 * Port to the D programming language:
11 * Frank Benoit <benoit@tionex.de>
12 *******************************************************************************/
13 module org.eclipse.jface.viewers.CheckboxTreeViewer;
14
15 import org.eclipse.jface.viewers.TreeViewer;
16 import org.eclipse.jface.viewers.ICheckable;
17 import org.eclipse.jface.viewers.ICheckStateListener;
18 import org.eclipse.jface.viewers.CustomHashtable;
19 import org.eclipse.jface.viewers.CheckStateChangedEvent;
20
21
22 import org.eclipse.swt.SWT;
23 import org.eclipse.swt.events.SelectionEvent;
24 import org.eclipse.swt.widgets.Composite;
25 import org.eclipse.swt.widgets.Control;
26 import org.eclipse.swt.widgets.Item;
27 import org.eclipse.swt.widgets.Tree;
28 import org.eclipse.swt.widgets.TreeItem;
29 import org.eclipse.swt.widgets.Widget;
30 import org.eclipse.core.runtime.Assert;
31 import org.eclipse.core.runtime.ListenerList;
32 import org.eclipse.jface.util.SafeRunnable;
33
34 import java.lang.all;
35 import java.util.List;
36 import java.util.ArrayList;
37 import java.util.Set;
38
39 /**
40 * A concrete tree-structured viewer based on an SWT <code>Tree</code>
41 * control with checkboxes on each node.
42 * <p>
43 * This class is not intended to be subclassed outside the viewer framework.
44 * It is designed to be instantiated with a pre-existing SWT tree control and configured
45 * with a domain-specific content provider, label provider, element filter (optional),
46 * and element sorter (optional).
47 * </p>
48 * @noextend This class is not intended to be subclassed by clients.
49 */
50 public class CheckboxTreeViewer : TreeViewer, ICheckable {
51 alias TreeViewer.preservingSelection preservingSelection;
52
53 /**
54 * List of check state listeners (element type: <code>ICheckStateListener</code>).
55 */
56 private ListenerList checkStateListeners;
57
58 /**
59 * Last item clicked on, or <code>null</code> if none.
60 */
61 private TreeItem lastClickedItem = null;
62
63 /**
64 * Creates a tree viewer on a newly-created tree control under the given parent.
65 * The tree control is created using the SWT style bits: <code>CHECK</code> and <code>BORDER</code>.
66 * The viewer has no input, no content provider, a default label provider,
67 * no sorter, and no filters.
68 *
69 * @param parent the parent control
70 */
71 public this(Composite parent) {
72 this(parent, SWT.BORDER);
73 }
74
75 /**
76 * Creates a tree viewer on a newly-created tree control under the given parent.
77 * The tree control is created using the given SWT style bits, plus the <code>CHECK</code> style bit.
78 * The viewer has no input, no content provider, a default label provider,
79 * no sorter, and no filters.
80 *
81 * @param parent the parent control
82 * @param style the SWT style bits
83 */
84 public this(Composite parent, int style) {
85 this(new Tree(parent, SWT.CHECK | style));
86 }
87
88 /**
89 * Creates a tree viewer on the given tree control.
90 * The <code>SWT.CHECK</code> style bit must be set on the given tree control.
91 * The viewer has no input, no content provider, a default label provider,
92 * no sorter, and no filters.
93 *
94 * @param tree the tree control
95 */
96 public this(Tree tree) {
97 checkStateListeners = new ListenerList();
98 super(tree);
99 }
100
101 /* (non-Javadoc)
102 * Method declared on ICheckable.
103 */
104 public void addCheckStateListener(ICheckStateListener listener) {
105 checkStateListeners.add(cast(Object)listener);
106 }
107
108 /**
109 * Applies the checked and grayed states of the given widget and its
110 * descendents.
111 *
112 * @param checked a set of elements (element type: <code>Object</code>)
113 * @param grayed a set of elements (element type: <code>Object</code>)
114 * @param widget the widget
115 */
116 private void applyState(CustomHashtable checked, CustomHashtable grayed,
117 Widget widget) {
118 Item[] items = getChildren(widget);
119 for (int i = 0; i < items.length; i++) {
120 Item item = items[i];
121 if ( auto ti = cast(TreeItem) item ) {
122 Object data = item.getData();
123 if (data !is null) {
124 ti.setChecked(checked.containsKey(data));
125 ti.setGrayed(grayed.containsKey(data));
126 }
127 }
128 applyState(checked, grayed, item);
129 }
130 }
131
132 /**
133 * Notifies any check state listeners that the check state of an element has changed.
134 * Only listeners registered at the time this method is called are notified.
135 *
136 * @param event a check state changed event
137 *
138 * @see ICheckStateListener#checkStateChanged
139 */
140 protected void fireCheckStateChanged(CheckStateChangedEvent event) {
141 Object[] array = checkStateListeners.getListeners();
142 for (int i = 0; i < array.length; i++) {
143 SafeRunnable.run( dgSafeRunnable( (ICheckStateListener l){
144 l.checkStateChanged(event);
145 }, cast(ICheckStateListener) array[i]));
146 }
147
148 }
149
150 /**
151 * Gathers the checked and grayed states of the given widget and its
152 * descendents.
153 *
154 * @param checked a writable set of elements (element type: <code>Object</code>)
155 * @param grayed a writable set of elements (element type: <code>Object</code>)
156 * @param widget the widget
157 */
158 private void gatherState(CustomHashtable checked, CustomHashtable grayed,
159 Widget widget) {
160 Item[] items = getChildren(widget);
161 for (int i = 0; i < items.length; i++) {
162 Item item = items[i];
163 if ( auto ti = cast(TreeItem) item ) {
164 Object data = item.getData();
165 if (data !is null) {
166 if (ti.getChecked()) {
167 checked.put(data, data);
168 }
169 if (ti.getGrayed()) {
170 grayed.put(data, data);
171 }
172 }
173 }
174 gatherState(checked, grayed, item);
175 }
176 }
177
178 /* (non-Javadoc)
179 * Method declared on ICheckable.
180 */
181 public bool getChecked(Object element) {
182 Widget widget = findItem(element);
183 if ( auto ti = cast(TreeItem) widget ) {
184 return ti.getChecked();
185 }
186 return false;
187 }
188
189 /**
190 * Returns a list of checked elements in this viewer's tree,
191 * including currently hidden ones that are marked as
192 * checked but are under a collapsed ancestor.
193 * <p>
194 * This method is typically used when preserving the interesting
195 * state of a viewer; <code>setCheckedElements</code> is used during the restore.
196 * </p>
197 *
198 * @return the array of checked elements
199 *
200 * @see #setCheckedElements
201 */
202 public Object[] getCheckedElements() {
203 ArrayList v = new ArrayList();
204 Control tree = getControl();
205 internalCollectChecked(v, tree);
206 return v.toArray();
207 }
208
209 /**
210 * Returns the grayed state of the given element.
211 *
212 * @param element the element
213 * @return <code>true</code> if the element is grayed,
214 * and <code>false</code> if not grayed
215 */
216 public bool getGrayed(Object element) {
217 Widget widget = findItem(element);
218 if ( auto ti = cast(TreeItem) widget ) {
219 return ti.getGrayed();
220 }
221 return false;
222 }
223
224 /**
225 * Returns a list of grayed elements in this viewer's tree,
226 * including currently hidden ones that are marked as
227 * grayed but are under a collapsed ancestor.
228 * <p>
229 * This method is typically used when preserving the interesting
230 * state of a viewer; <code>setGrayedElements</code> is used during the restore.
231 * </p>
232 *
233 * @return the array of grayed elements
234 *
235 * @see #setGrayedElements
236 */
237 public Object[] getGrayedElements() {
238 List result = new ArrayList();
239 internalCollectGrayed(result, getControl());
240 return result.toArray();
241 }
242
243 /* (non-Javadoc)
244 * Method declared on StructuredViewer.
245 */
246 protected override void handleDoubleSelect(SelectionEvent event) {
247
248 if (lastClickedItem !is null) {
249 TreeItem item = lastClickedItem;
250 Object data = item.getData();
251 if (data !is null) {
252 bool state = item.getChecked();
253 setChecked(data, !state);
254 fireCheckStateChanged(new CheckStateChangedEvent(this, data,
255 !state));
256 }
257 lastClickedItem = null;
258 } else {
259 super.handleDoubleSelect(event);
260 }
261 }
262
263 /* (non-Javadoc)
264 * Method declared on StructuredViewer.
265 */
266 protected override void handleSelect(SelectionEvent event) {
267
268 lastClickedItem = null;
269 if (event.detail is SWT.CHECK) {
270 TreeItem item = cast(TreeItem) event.item;
271 lastClickedItem = item;
272 super.handleSelect(event);
273
274 Object data = item.getData();
275 if (data !is null) {
276 fireCheckStateChanged(new CheckStateChangedEvent(this, data,
277 item.getChecked()));
278 }
279 } else {
280 super.handleSelect(event);
281 }
282 }
283
284 /**
285 * Gathers the checked states of the given widget and its
286 * descendents, following a pre-order traversal of the tree.
287 *
288 * @param result a writable list of elements (element type: <code>Object</code>)
289 * @param widget the widget
290 */
291 private void internalCollectChecked(List result, Widget widget) {
292 Item[] items = getChildren(widget);
293 for (int i = 0; i < items.length; i++) {
294 Item item = items[i];
295 if ( null !is cast(TreeItem)item && (cast(TreeItem) item).getChecked()) {
296 Object data = item.getData();
297 if (data !is null) {
298 result.add(data);
299 }
300 }
301 internalCollectChecked(result, item);
302 }
303 }
304
305 /**
306 * Gathers the grayed states of the given widget and its
307 * descendents, following a pre-order traversal of the tree.
308 *
309 * @param result a writable list of elements (element type: <code>Object</code>)
310 * @param widget the widget
311 */
312 private void internalCollectGrayed(List result, Widget widget) {
313 Item[] items = getChildren(widget);
314 for (int i = 0; i < items.length; i++) {
315 Item item = items[i];
316 if (null !is cast(TreeItem)item && (cast(TreeItem) item).getGrayed()) {
317 Object data = item.getData();
318 if (data !is null) {
319 result.add(data);
320 }
321 }
322 internalCollectGrayed(result, item);
323 }
324 }
325
326 /**
327 * Sets the checked state of all items to correspond to the given set of checked elements.
328 *
329 * @param checkedElements the set (element type: <code>Object</code>) of elements which are checked
330 * @param widget the widget
331 */
332 private void internalSetChecked(CustomHashtable checkedElements,
333 Widget widget) {
334 Item[] items = getChildren(widget);
335 for (int i = 0; i < items.length; i++) {
336 TreeItem item = cast(TreeItem) items[i];
337 Object data = item.getData();
338 if (data !is null) {
339 bool checked = checkedElements.containsKey(data);
340 if (checked !is item.getChecked()) {
341 item.setChecked(checked);
342 }
343 }
344 internalSetChecked(checkedElements, item);
345 }
346 }
347
348 /**
349 * Sets the grayed state of all items to correspond to the given set of grayed elements.
350 *
351 * @param grayedElements the set (element type: <code>Object</code>) of elements which are grayed
352 * @param widget the widget
353 */
354 private void internalSetGrayed(CustomHashtable grayedElements, Widget widget) {
355 Item[] items = getChildren(widget);
356 for (int i = 0; i < items.length; i++) {
357 TreeItem item = cast(TreeItem) items[i];
358 Object data = item.getData();
359 if (data !is null) {
360 bool grayed = grayedElements.containsKey(data);
361 if (grayed !is item.getGrayed()) {
362 item.setGrayed(grayed);
363 }
364 }
365 internalSetGrayed(grayedElements, item);
366 }
367 }
368
369 /* (non-Javadoc)
370 * Method declared on Viewer.
371 */
372 protected override void preservingSelection(Runnable updateCode) {
373
374 int n = getItemCount(getControl());
375 CustomHashtable checkedNodes = newHashtable(n * 2 + 1);
376 CustomHashtable grayedNodes = newHashtable(n * 2 + 1);
377
378 gatherState(checkedNodes, grayedNodes, getControl());
379
380 super.preservingSelection(updateCode);
381
382 applyState(checkedNodes, grayedNodes, getControl());
383 }
384
385 /* (non-Javadoc)
386 * Method declared on ICheckable.
387 */
388 public void removeCheckStateListener(ICheckStateListener listener) {
389 checkStateListeners.remove(cast(Object)listener);
390 }
391
392 /* (non-Javadoc)
393 * Method declared on ICheckable.
394 */
395 public bool setChecked(Object element, bool state) {
396 Assert.isNotNull(element);
397 Widget widget = internalExpand(element, false);
398 if ( auto ti = cast(TreeItem) widget ) {
399 ti.setChecked(state);
400 return true;
401 }
402 return false;
403 }
404
405 /**
406 * Sets the checked state for the children of the given item.
407 *
408 * @param item the item
409 * @param state <code>true</code> if the item should be checked,
410 * and <code>false</code> if it should be unchecked
411 */
412 private void setCheckedChildren(Item item, bool state) {
413 createChildren(item);
414 Item[] items = getChildren(item);
415 if (items !is null) {
416 for (int i = 0; i < items.length; i++) {
417 Item it = items[i];
418 if (it.getData() !is null && (null !is cast(TreeItem)it )) {
419 TreeItem treeItem = cast(TreeItem) it;
420 treeItem.setChecked(state);
421 setCheckedChildren(treeItem, state);
422 }
423 }
424 }
425 }
426
427 /**
428 * Sets which elements are checked in this viewer's tree.
429 * The given list contains the elements that are to be checked;
430 * all other elements are to be unchecked.
431 * Does not fire events to check state listeners.
432 * <p>
433 * This method is typically used when restoring the interesting
434 * state of a viewer captured by an earlier call to <code>getCheckedElements</code>.
435 * </p>
436 *
437 * @param elements the array of checked elements
438 * @see #getCheckedElements
439 */
440 public void setCheckedElements(Object[] elements) {
441 assertElementsNotNull(elements);
442 CustomHashtable checkedElements = newHashtable(elements.length * 2 + 1);
443 for (int i = 0; i < elements.length; ++i) {
444 Object element = elements[i];
445 // Ensure item exists for element
446 internalExpand(element, false);
447 checkedElements.put(element, element);
448 }
449 Control tree = getControl();
450 tree.setRedraw(false);
451 internalSetChecked(checkedElements, tree);
452 tree.setRedraw(true);
453 }
454
455 /**
456 * Sets the grayed state for the given element in this viewer.
457 *
458 * @param element the element
459 * @param state <code>true</code> if the item should be grayed,
460 * and <code>false</code> if it should be ungrayed
461 * @return <code>true</code> if the gray state could be set,
462 * and <code>false</code> otherwise
463 */
464 public bool setGrayed(Object element, bool state) {
465 Assert.isNotNull(element);
466 Widget widget = internalExpand(element, false);
467 if ( auto ti = cast(TreeItem) widget ) {
468 ti.setGrayed(state);
469 return true;
470 }
471 return false;
472 }
473
474 /**
475 * Check and gray the selection rather than calling both
476 * setGrayed and setChecked as an optimization.
477 * Does not fire events to check state listeners.
478 * @param element the item being checked
479 * @param state a bool indicating selection or deselection
480 * @return bool indicating success or failure.
481 */
482 public bool setGrayChecked(Object element, bool state) {
483 Assert.isNotNull(element);
484 Widget widget = internalExpand(element, false);
485 if (auto item = cast(TreeItem)widget ) {
486 item.setChecked(state);
487 item.setGrayed(state);
488 return true;
489 }
490 return false;
491 }
492
493 /**
494 * Sets which elements are grayed in this viewer's tree.
495 * The given list contains the elements that are to be grayed;
496 * all other elements are to be ungrayed.
497 * <p>
498 * This method is typically used when restoring the interesting
499 * state of a viewer captured by an earlier call to <code>getGrayedElements</code>.
500 * </p>
501 *
502 * @param elements the array of grayed elements
503 *
504 * @see #getGrayedElements
505 */
506 public void setGrayedElements(Object[] elements) {
507 assertElementsNotNull(elements);
508 CustomHashtable grayedElements = newHashtable(elements.length * 2 + 1);
509 for (int i = 0; i < elements.length; ++i) {
510 Object element = elements[i];
511 // Ensure item exists for element
512 internalExpand(element, false);
513 grayedElements.put(element, element);
514 }
515 Control tree = getControl();
516 tree.setRedraw(false);
517 internalSetGrayed(grayedElements, tree);
518 tree.setRedraw(true);
519 }
520
521 /**
522 * Sets the grayed state for the given element and its parents
523 * in this viewer.
524 *
525 * @param element the element
526 * @param state <code>true</code> if the item should be grayed,
527 * and <code>false</code> if it should be ungrayed
528 * @return <code>true</code> if the element is visible and the gray
529 * state could be set, and <code>false</code> otherwise
530 * @see #setGrayed
531 */
532 public bool setParentsGrayed(Object element, bool state) {
533 Assert.isNotNull(element);
534 Widget widget = internalExpand(element, false);
535 if (auto item = cast(TreeItem) widget ) {
536 item.setGrayed(state);
537 item = item.getParentItem();
538 while (item !is null) {
539 item.setGrayed(state);
540 item = item.getParentItem();
541 }
542 return true;
543 }
544 return false;
545 }
546
547 /**
548 * Sets the checked state for the given element and its visible
549 * children in this viewer.
550 * Assumes that the element has been expanded before. To enforce
551 * that the item is expanded, call <code>expandToLevel</code>
552 * for the element.
553 * Does not fire events to check state listeners.
554 *
555 * @param element the element
556 * @param state <code>true</code> if the item should be checked,
557 * and <code>false</code> if it should be unchecked
558 * @return <code>true</code> if the checked state could be set,
559 * and <code>false</code> otherwise
560 */
561 public bool setSubtreeChecked(Object element, bool state) {
562 Widget widget = internalExpand(element, false);
563 if (auto item = cast(TreeItem) widget ) {
564 item.setChecked(state);
565 setCheckedChildren(item, state);
566 return true;
567 }
568 return false;
569 }
570
571 /**
572 * Sets to the given value the checked state for all elements in this viewer.
573 * Does not fire events to check state listeners.
574 * Assumes that the element has been expanded before. To enforce
575 * that the item is expanded, call <code>expandToLevel</code>
576 * for the element.
577 *
578 * @param state <code>true</code> if the element should be checked,
579 * and <code>false</code> if it should be unchecked
580 * @deprecated as this method only checks or unchecks visible items
581 * is is recommended that {@link #setSubtreeChecked(Object, bool)}
582 * is used instead.
583 * @see #setSubtreeChecked(Object, bool)
584 *
585 * @since 3.2
586 */
587 public void setAllChecked(bool state) {
588 setAllChecked(state, getTree().getItems());
589
590 }
591
592 /**
593 * Set the checked state of the visible items and their children to state.
594 * @param state
595 * @param items
596 * @deprecated
597 * @see #setAllChecked(bool)
598 */
599 private void setAllChecked(bool state, TreeItem[] items) {
600 for (int i = 0; i < items.length; i++) {
601 items[i].setChecked(state);
602 TreeItem[] children = items[i].getItems();
603 setAllChecked(state, children);
604 }
605 }
606 }