KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > ide > misc > CheckboxTreeAndListGroup


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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  *******************************************************************************/

11 package org.eclipse.ui.internal.ide.misc;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17 import java.util.Map JavaDoc;
18 import java.util.Set JavaDoc;
19
20 import org.eclipse.core.commands.common.EventManager;
21 import org.eclipse.core.runtime.SafeRunner;
22 import org.eclipse.jface.util.SafeRunnable;
23 import org.eclipse.jface.viewers.CheckStateChangedEvent;
24 import org.eclipse.jface.viewers.CheckboxTableViewer;
25 import org.eclipse.jface.viewers.CheckboxTreeViewer;
26 import org.eclipse.jface.viewers.ICheckStateListener;
27 import org.eclipse.jface.viewers.ILabelProvider;
28 import org.eclipse.jface.viewers.ISelectionChangedListener;
29 import org.eclipse.jface.viewers.IStructuredContentProvider;
30 import org.eclipse.jface.viewers.IStructuredSelection;
31 import org.eclipse.jface.viewers.ITreeContentProvider;
32 import org.eclipse.jface.viewers.ITreeViewerListener;
33 import org.eclipse.jface.viewers.SelectionChangedEvent;
34 import org.eclipse.jface.viewers.StructuredSelection;
35 import org.eclipse.jface.viewers.TreeExpansionEvent;
36 import org.eclipse.jface.viewers.ViewerComparator;
37 import org.eclipse.swt.SWT;
38 import org.eclipse.swt.custom.BusyIndicator;
39 import org.eclipse.swt.layout.GridData;
40 import org.eclipse.swt.layout.GridLayout;
41 import org.eclipse.swt.widgets.Composite;
42 import org.eclipse.swt.widgets.Table;
43 import org.eclipse.swt.widgets.Tree;
44
45 /**
46  * Workbench-level composite that combines a CheckboxTreeViewer and CheckboxListViewer.
47  * All viewer selection-driven interactions are handled within this object
48  */

49 public class CheckboxTreeAndListGroup extends EventManager implements
50         ICheckStateListener, ISelectionChangedListener, ITreeViewerListener {
51     private Object JavaDoc root;
52
53     private Object JavaDoc currentTreeSelection;
54
55     private List JavaDoc expandedTreeNodes = new ArrayList JavaDoc();
56
57     private Map JavaDoc checkedStateStore = new HashMap JavaDoc(9);
58
59     private List JavaDoc whiteCheckedTreeItems = new ArrayList JavaDoc();
60
61     private ITreeContentProvider treeContentProvider;
62
63     private IStructuredContentProvider listContentProvider;
64
65     private ILabelProvider treeLabelProvider;
66
67     private ILabelProvider listLabelProvider;
68
69     // widgets
70
private CheckboxTreeViewer treeViewer;
71
72     private CheckboxTableViewer listViewer;
73
74     /**
75      * Create an instance of this class. Use this constructor if you wish to specify
76      * the width and/or height of the combined widget (to only hardcode one of the
77      * sizing dimensions, specify the other dimension's value as -1)
78      * @param parent
79      * @param rootObject
80      * @param treeContentProvider
81      * @param treeLabelProvider
82      * @param listContentProvider
83      * @param listLabelProvider
84      * @param style
85      * @param width
86      * @param height
87      */

88     public CheckboxTreeAndListGroup(Composite parent, Object JavaDoc rootObject,
89             ITreeContentProvider treeContentProvider,
90             ILabelProvider treeLabelProvider,
91             IStructuredContentProvider listContentProvider,
92             ILabelProvider listLabelProvider, int style, int width, int height) {
93
94         root = rootObject;
95         this.treeContentProvider = treeContentProvider;
96         this.listContentProvider = listContentProvider;
97         this.treeLabelProvider = treeLabelProvider;
98         this.listLabelProvider = listLabelProvider;
99         createContents(parent, width, height, style);
100     }
101
102     /**
103      * This method must be called just before this window becomes visible.
104      */

105     public void aboutToOpen() {
106         determineWhiteCheckedDescendents(root);
107         checkNewTreeElements(treeContentProvider.getElements(root));
108         currentTreeSelection = null;
109
110         //select the first element in the list
111
Object JavaDoc[] elements = treeContentProvider.getElements(root);
112         Object JavaDoc primary = elements.length > 0 ? elements[0] : null;
113         if (primary != null) {
114             treeViewer.setSelection(new StructuredSelection(primary));
115         }
116         treeViewer.getControl().setFocus();
117     }
118
119     /**
120      * Add the passed listener to self's collection of clients
121      * that listen for changes to element checked states
122      *
123      * @param listener ICheckStateListener
124      */

125     public void addCheckStateListener(ICheckStateListener listener) {
126         addListenerObject(listener);
127     }
128
129     /**
130      * Add the receiver and all of it's ancestors to the checkedStateStore if they
131      * are not already there.
132      */

133     private void addToHierarchyToCheckedStore(Object JavaDoc treeElement) {
134
135         // if this tree element is already gray then its ancestors all are as well
136
if (!checkedStateStore.containsKey(treeElement)) {
137             checkedStateStore.put(treeElement, new ArrayList JavaDoc());
138         }
139
140         Object JavaDoc parent = treeContentProvider.getParent(treeElement);
141         if (parent != null) {
142             addToHierarchyToCheckedStore(parent);
143         }
144     }
145
146     /**
147      * Return a boolean indicating whether all children of the passed tree element
148      * are currently white-checked
149      *
150      * @return boolean
151      * @param treeElement java.lang.Object
152      */

153     protected boolean areAllChildrenWhiteChecked(Object JavaDoc treeElement) {
154         Object JavaDoc[] children = treeContentProvider.getChildren(treeElement);
155         for (int i = 0; i < children.length; ++i) {
156             if (!whiteCheckedTreeItems.contains(children[i])) {
157                 return false;
158             }
159         }
160
161         return true;
162     }
163
164     /**
165      * Return a boolean indicating whether all list elements associated with
166      * the passed tree element are currently checked
167      *
168      * @return boolean
169      * @param treeElement java.lang.Object
170      */

171     protected boolean areAllElementsChecked(Object JavaDoc treeElement) {
172         List JavaDoc checkedElements = (List JavaDoc) checkedStateStore.get(treeElement);
173         if (checkedElements == null) {
174             return false;
175         }
176
177         return getListItemsSize(treeElement) == checkedElements.size();
178     }
179
180     /**
181      * Iterate through the passed elements which are being realized for the first
182      * time and check each one in the tree viewer as appropriate
183      */

184     protected void checkNewTreeElements(Object JavaDoc[] elements) {
185         for (int i = 0; i < elements.length; ++i) {
186             Object JavaDoc currentElement = elements[i];
187             boolean checked = checkedStateStore.containsKey(currentElement);
188             treeViewer.setChecked(currentElement, checked);
189             treeViewer.setGrayed(currentElement, checked
190                     && !whiteCheckedTreeItems.contains(currentElement));
191         }
192     }
193
194     /**
195      * An item was checked in one of self's two views. Determine which
196      * view this occurred in and delegate appropriately
197      *
198      * @param event CheckStateChangedEvent
199      */

200     public void checkStateChanged(final CheckStateChangedEvent event) {
201
202         //Potentially long operation - show a busy cursor
203
BusyIndicator.showWhile(treeViewer.getControl().getDisplay(),
204                 new Runnable JavaDoc() {
205                     public void run() {
206                         if (event.getCheckable().equals(treeViewer)) {
207                             treeItemChecked(event.getElement(), event
208                                     .getChecked());
209                         } else {
210                             listItemChecked(event.getElement(), event
211                                     .getChecked(), true);
212                         }
213
214                         notifyCheckStateChangeListeners(event);
215                     }
216                 });
217     }
218
219     /**
220      * Lay out and initialize self's visual components.
221      *
222      * @param parent org.eclipse.swt.widgets.Composite
223      * @param width int
224      * @param height int
225      */

226     protected void createContents(Composite parent, int width, int height,
227             int style) {
228         // group pane
229
Composite composite = new Composite(parent, style);
230         GridLayout layout = new GridLayout();
231         layout.numColumns = 2;
232         layout.makeColumnsEqualWidth = true;
233         layout.marginHeight = 0;
234         layout.marginWidth = 0;
235         composite.setLayout(layout);
236         composite.setLayoutData(new GridData(GridData.FILL_BOTH));
237         composite.setFont(parent.getFont());
238
239         createTreeViewer(composite, width / 2, height);
240         createListViewer(composite, width / 2, height);
241
242         initialize();
243     }
244
245     /**
246      * Create this group's list viewer.
247      */

248     protected void createListViewer(Composite parent, int width, int height) {
249         listViewer = CheckboxTableViewer.newCheckList(parent, SWT.BORDER);
250         GridData data = new GridData(GridData.FILL_BOTH);
251         data.widthHint = width;
252         data.heightHint = height;
253         listViewer.getTable().setLayoutData(data);
254         listViewer.getTable().setFont(parent.getFont());
255         listViewer.setContentProvider(listContentProvider);
256         listViewer.setLabelProvider(listLabelProvider);
257         listViewer.addCheckStateListener(this);
258     }
259
260     /**
261      * Create this group's tree viewer.
262      */

263     protected void createTreeViewer(Composite parent, int width, int height) {
264         Tree tree = new Tree(parent, SWT.CHECK | SWT.BORDER);
265         GridData data = new GridData(GridData.FILL_BOTH);
266         data.widthHint = width;
267         data.heightHint = height;
268         tree.setLayoutData(data);
269         tree.setFont(parent.getFont());
270
271         treeViewer = new CheckboxTreeViewer(tree);
272         treeViewer.setContentProvider(treeContentProvider);
273         treeViewer.setLabelProvider(treeLabelProvider);
274         treeViewer.addTreeListener(this);
275         treeViewer.addCheckStateListener(this);
276         treeViewer.addSelectionChangedListener(this);
277     }
278
279     /**
280      * Returns a boolean indicating whether the passed tree element should be
281      * at LEAST gray-checked. Note that this method does not consider whether
282      * it should be white-checked, so a specified tree item which should be
283      * white-checked will result in a <code>true</code> answer from this method.
284      * To determine whether a tree item should be white-checked use method
285      * #determineShouldBeWhiteChecked(Object).
286      *
287      * @param treeElement java.lang.Object
288      * @return boolean
289      * @see #determineShouldBeWhiteChecked(java.lang.Object)
290      */

291     protected boolean determineShouldBeAtLeastGrayChecked(Object JavaDoc treeElement) {
292         // if any list items associated with treeElement are checked then it
293
// retains its gray-checked status regardless of its children
294
List JavaDoc checked = (List JavaDoc) checkedStateStore.get(treeElement);
295         if (checked != null && (!checked.isEmpty())) {
296             return true;
297         }
298
299         // if any children of treeElement are still gray-checked then treeElement
300
// must remain gray-checked as well
301
Object JavaDoc[] children = treeContentProvider.getChildren(treeElement);
302         for (int i = 0; i < children.length; ++i) {
303             if (checkedStateStore.containsKey(children[i])) {
304                 return true;
305             }
306         }
307
308         return false;
309     }
310
311     /**
312      * Returns a boolean indicating whether the passed tree item should be
313      * white-checked.
314      *
315      * @return boolean
316      * @param treeElement java.lang.Object
317      */

318     protected boolean determineShouldBeWhiteChecked(Object JavaDoc treeElement) {
319         return areAllChildrenWhiteChecked(treeElement)
320                 && areAllElementsChecked(treeElement);
321     }
322
323     /**
324      * Recursively add appropriate tree elements to the collection of
325      * known white-checked tree elements.
326      *
327      * @param treeElement java.lang.Object
328      */

329     protected void determineWhiteCheckedDescendents(Object JavaDoc treeElement) {
330         // always go through all children first since their white-checked
331
// statuses will be needed to determine the white-checked status for
332
// this tree element
333
Object JavaDoc[] children = treeContentProvider.getElements(treeElement);
334         for (int i = 0; i < children.length; ++i) {
335             determineWhiteCheckedDescendents(children[i]);
336         }
337
338         // now determine the white-checked status for this tree element
339
if (determineShouldBeWhiteChecked(treeElement)) {
340             setWhiteChecked(treeElement, true);
341         }
342     }
343
344     /**
345      * Cause the tree viewer to expand all its items
346      */

347     public void expandAll() {
348         treeViewer.expandAll();
349     }
350
351     /**
352      * Answer a flat collection of all of the checked elements in the
353      * list portion of self
354      *
355      * @return java.util.Vector
356      */

357     public Iterator JavaDoc getAllCheckedListItems() {
358         List JavaDoc result = new ArrayList JavaDoc();
359         Iterator JavaDoc listCollectionsEnum = checkedStateStore.values().iterator();
360
361         while (listCollectionsEnum.hasNext()) {
362             Iterator JavaDoc currentCollection = ((List JavaDoc) listCollectionsEnum.next())
363                     .iterator();
364             while (currentCollection.hasNext()) {
365                 result.add(currentCollection.next());
366             }
367         }
368
369         return result.iterator();
370     }
371
372     /**
373      * Answer a collection of all of the checked elements in the tree portion
374      * of self
375      *
376      * @return java.util.Vector
377      */

378     public Set JavaDoc getAllCheckedTreeItems() {
379         return checkedStateStore.keySet();
380     }
381
382     /**
383      * Answer the number of elements that have been checked by the
384      * user.
385      *
386      * @return int
387      */

388     public int getCheckedElementCount() {
389         return checkedStateStore.size();
390     }
391
392     /**
393      * Return a count of the number of list items associated with a
394      * given tree item.
395      *
396      * @return int
397      * @param treeElement java.lang.Object
398      */

399     protected int getListItemsSize(Object JavaDoc treeElement) {
400         Object JavaDoc[] elements = listContentProvider.getElements(treeElement);
401         return elements.length;
402     }
403
404     /**
405      * Get the table the list viewer uses.
406      * @return org.eclipse.swt.widgets.Table
407      */

408     public Table getListTable() {
409         return this.listViewer.getTable();
410     }
411
412     /**
413      * Logically gray-check all ancestors of treeItem by ensuring that they
414      * appear in the checked table
415      */

416     protected void grayCheckHierarchy(Object JavaDoc treeElement) {
417
418         // if this tree element is already gray then its ancestors all are as well
419
if (checkedStateStore.containsKey(treeElement)) {
420             return; // no need to proceed upwards from here
421
}
422
423         checkedStateStore.put(treeElement, new ArrayList JavaDoc());
424         if (determineShouldBeWhiteChecked(treeElement)) {
425             setWhiteChecked(treeElement, true);
426         }
427         Object JavaDoc parent = treeContentProvider.getParent(treeElement);
428         if (parent != null) {
429             grayCheckHierarchy(parent);
430         }
431     }
432
433     /**
434      * Set the initial checked state of the passed list element to true.
435      *
436      * @param element the element in the list to select
437      */

438     public void initialCheckListItem(Object JavaDoc element) {
439         Object JavaDoc parent = treeContentProvider.getParent(element);
440         currentTreeSelection = parent;
441         //As this is not done from the UI then set the box for updating from the selection to false
442
listItemChecked(element, true, false);
443         updateHierarchy(parent);
444     }
445
446     /**
447      * Set the initial checked state of the passed element to true,
448      * as well as to all of its children and associated list elements
449      *
450      * @param element the element in the tree to select
451      */

452     public void initialCheckTreeItem(Object JavaDoc element) {
453         treeItemChecked(element, true);
454     }
455
456     /**
457      * Initialize this group's viewers after they have been laid out.
458      */

459     protected void initialize() {
460         treeViewer.setInput(root);
461     }
462
463     /**
464      * Callback that's invoked when the checked status of an item in the list
465      * is changed by the user. Do not try and update the hierarchy if we are building the
466      * initial list.
467      */

468     protected void listItemChecked(Object JavaDoc listElement, boolean state,
469             boolean updatingFromSelection) {
470         List JavaDoc checkedListItems = (List JavaDoc) checkedStateStore
471                 .get(currentTreeSelection);
472
473         if (state) {
474             if (checkedListItems == null) {
475                 // since the associated tree item has gone from 0 -> 1 checked
476
// list items, tree checking may need to be updated
477
grayCheckHierarchy(currentTreeSelection);
478                 checkedListItems = (List JavaDoc) checkedStateStore
479                         .get(currentTreeSelection);
480             }
481             checkedListItems.add(listElement);
482         } else {
483             checkedListItems.remove(listElement);
484             if (checkedListItems.isEmpty()) {
485                 // since the associated tree item has gone from 1 -> 0 checked
486
// list items, tree checking may need to be updated
487
ungrayCheckHierarchy(currentTreeSelection);
488             }
489         }
490
491         if (updatingFromSelection) {
492             updateHierarchy(currentTreeSelection);
493         }
494     }
495
496     /**
497      * Notify all checked state listeners that the passed element has had
498      * its checked state changed to the passed state
499      */

500     protected void notifyCheckStateChangeListeners(
501             final CheckStateChangedEvent event) {
502         Object JavaDoc[] array = getListeners();
503         for (int i = 0; i < array.length; i++) {
504             final ICheckStateListener l = (ICheckStateListener) array[i];
505             SafeRunner.run(new SafeRunnable() {
506                 public void run() {
507                     l.checkStateChanged(event);
508                 }
509             });
510         }
511     }
512
513     /**
514      *Set the contents of the list viewer based upon the specified selected
515      *tree element. This also includes checking the appropriate list items.
516      *
517      *@param treeElement java.lang.Object
518      */

519     protected void populateListViewer(final Object JavaDoc treeElement) {
520         listViewer.setInput(treeElement);
521         List JavaDoc listItemsToCheck = (List JavaDoc) checkedStateStore.get(treeElement);
522
523         if (listItemsToCheck != null) {
524             Iterator JavaDoc listItemsEnum = listItemsToCheck.iterator();
525             while (listItemsEnum.hasNext()) {
526                 listViewer.setChecked(listItemsEnum.next(), true);
527             }
528         }
529     }
530
531     /**
532      * Remove the passed listener from self's collection of clients
533      * that listen for changes to element checked states
534      *
535      * @param listener ICheckStateListener
536      */

537     public void removeCheckStateListener(ICheckStateListener listener) {
538         removeListenerObject(listener);
539     }
540
541     /**
542      * Handle the selection of an item in the tree viewer
543      *
544      * @param event SelectionChangedEvent
545      */

546     public void selectionChanged(SelectionChangedEvent event) {
547         IStructuredSelection selection = (IStructuredSelection) event
548                 .getSelection();
549         Object JavaDoc selectedElement = selection.getFirstElement();
550         if (selectedElement == null) {
551             currentTreeSelection = null;
552             listViewer.setInput(currentTreeSelection);
553             return;
554         }
555
556         // ie.- if not an item deselection
557
if (selectedElement != currentTreeSelection) {
558             populateListViewer(selectedElement);
559         }
560
561         currentTreeSelection = selectedElement;
562     }
563
564     /**
565      * Select or deselect all of the elements in the tree depending on the value of the selection
566      * boolean. Be sure to update the displayed files as well.
567      *
568      * @param selection boolean indicating whether or not to select all elements
569      */

570     public void setAllSelections(final boolean selection) {
571
572         //Potentially long operation - show a busy cursor
573
BusyIndicator.showWhile(treeViewer.getControl().getDisplay(),
574                 new Runnable JavaDoc() {
575                     public void run() {
576                         setTreeChecked(root, selection);
577                         listViewer.setAllChecked(selection);
578                     }
579                 });
580     }
581
582     /**
583      * Set the list viewer's providers to those passed
584      *
585      * @param contentProvider ITreeContentProvider
586      * @param labelProvider ILabelProvider
587      */

588     public void setListProviders(IStructuredContentProvider contentProvider,
589             ILabelProvider labelProvider) {
590         listViewer.setContentProvider(contentProvider);
591         listViewer.setLabelProvider(labelProvider);
592     }
593
594     /**
595      * Set the comparator that is to be applied to self's list viewer
596      *
597      * @param comparator the comparator for the list viewer
598      */

599     public void setListComparator(ViewerComparator comparator) {
600         listViewer.setComparator(comparator);
601     }
602
603     /**
604      * Set the root of the widget to be new Root. Regenerate all of the tables and lists from this
605      * value.
606      * @param newRoot
607      */

608     public void setRoot(Object JavaDoc newRoot) {
609         this.root = newRoot;
610         initialize();
611     }
612
613     /**
614      * Set the checked state of the passed tree element appropriately, and
615      * do so recursively to all of its child tree elements as well
616      */

617     protected void setTreeChecked(Object JavaDoc treeElement, boolean state) {
618
619         if (treeElement.equals(currentTreeSelection)) {
620             listViewer.setAllChecked(state);
621         }
622
623         if (state) {
624             Object JavaDoc[] listItems = listContentProvider.getElements(treeElement);
625             List JavaDoc listItemsChecked = new ArrayList JavaDoc();
626             for (int i = 0; i < listItems.length; ++i) {
627                 listItemsChecked.add(listItems[i]);
628             }
629
630             checkedStateStore.put(treeElement, listItemsChecked);
631         } else {
632             checkedStateStore.remove(treeElement);
633         }
634
635         setWhiteChecked(treeElement, state);
636         treeViewer.setChecked(treeElement, state);
637         treeViewer.setGrayed(treeElement, false);
638
639         // now logically check/uncheck all children as well
640
Object JavaDoc[] children = treeContentProvider.getChildren(treeElement);
641         for (int i = 0; i < children.length; ++i) {
642             setTreeChecked(children[i], state);
643         }
644     }
645
646     /**
647      * Set the tree viewer's providers to those passed
648      *
649      * @param contentProvider ITreeContentProvider
650      * @param labelProvider ILabelProvider
651      */

652     public void setTreeProviders(ITreeContentProvider contentProvider,
653             ILabelProvider labelProvider) {
654         treeViewer.setContentProvider(contentProvider);
655         treeViewer.setLabelProvider(labelProvider);
656     }
657
658     /**
659      * Set the comparator that is to be applied to self's tree viewer
660      *
661      * @param comparator the comparator for the tree
662      */

663     public void setTreeComparator(ViewerComparator comparator) {
664         treeViewer.setComparator(comparator);
665     }
666
667     /**
668      * Adjust the collection of references to white-checked tree elements appropriately.
669      *
670      * @param treeElement java.lang.Object
671      * @param isWhiteChecked boolean
672      */

673     protected void setWhiteChecked(Object JavaDoc treeElement, boolean isWhiteChecked) {
674         if (isWhiteChecked) {
675             if (!whiteCheckedTreeItems.contains(treeElement)) {
676                 whiteCheckedTreeItems.add(treeElement);
677             }
678         } else {
679             whiteCheckedTreeItems.remove(treeElement);
680         }
681     }
682
683     /**
684      * Handle the collapsing of an element in a tree viewer
685      */

686     public void treeCollapsed(TreeExpansionEvent event) {
687         // We don't need to do anything with this
688
}
689
690     /**
691      * Handle the expansionsion of an element in a tree viewer
692      */

693     public void treeExpanded(TreeExpansionEvent event) {
694
695         Object JavaDoc item = event.getElement();
696
697         // First see if the children need to be given their checked state at all. If they've
698
// already been realized then this won't be necessary
699
if (!expandedTreeNodes.contains(item)) {
700             expandedTreeNodes.add(item);
701             checkNewTreeElements(treeContentProvider.getChildren(item));
702         }
703     }
704
705     /**
706      * Callback that's invoked when the checked status of an item in the tree
707      * is changed by the user.
708      */

709     protected void treeItemChecked(Object JavaDoc treeElement, boolean state) {
710
711         // recursively adjust all child tree elements appropriately
712
setTreeChecked(treeElement, state);
713
714         Object JavaDoc parent = treeContentProvider.getParent(treeElement);
715         if (parent == null) {
716             return;
717         }
718
719         // now update upwards in the tree hierarchy
720
if (state) {
721             grayCheckHierarchy(parent);
722         } else {
723             ungrayCheckHierarchy(parent);
724         }
725
726         updateHierarchy(treeElement);
727     }
728
729     /**
730      * Logically un-gray-check all ancestors of treeItem iff appropriate.
731      */

732     protected void ungrayCheckHierarchy(Object JavaDoc treeElement) {
733         if (!determineShouldBeAtLeastGrayChecked(treeElement)) {
734             checkedStateStore.remove(treeElement);
735         }
736
737         Object JavaDoc parent = treeContentProvider.getParent(treeElement);
738         if (parent != null) {
739             ungrayCheckHierarchy(parent);
740         }
741     }
742
743     /**
744      * Set the checked state of self and all ancestors appropriately
745      */

746     protected void updateHierarchy(Object JavaDoc treeElement) {
747
748         boolean whiteChecked = determineShouldBeWhiteChecked(treeElement);
749         boolean shouldBeAtLeastGray = determineShouldBeAtLeastGrayChecked(treeElement);
750
751         treeViewer.setChecked(treeElement, shouldBeAtLeastGray);
752         setWhiteChecked(treeElement, whiteChecked);
753         if (!whiteChecked) {
754             treeViewer.setGrayed(treeElement, shouldBeAtLeastGray);
755         }
756
757         // proceed up the tree element hierarchy
758
Object JavaDoc parent = treeContentProvider.getParent(treeElement);
759         if (parent != null) {
760             updateHierarchy(parent);
761         }
762     }
763
764     /**
765      * Update the selections of the tree elements in items to reflect the new
766      * selections provided.
767      * @param items Map with keys of Object (the tree element) and values of List (the selected
768      * list elements).
769      */

770     public void updateSelections(final Map JavaDoc items) {
771
772         //Potentially long operation - show a busy cursor
773
BusyIndicator.showWhile(treeViewer.getControl().getDisplay(),
774                 new Runnable JavaDoc() {
775                     public void run() {
776                         Iterator JavaDoc keyIterator = items.keySet().iterator();
777
778                         //Update the store before the hierarchy to prevent updating parents before all of the children are done
779
while (keyIterator.hasNext()) {
780                             Object JavaDoc key = keyIterator.next();
781                             //Replace the items in the checked state store with those from the supplied items
782
List JavaDoc selections = (List JavaDoc) items.get(key);
783                             if (selections.size() == 0) {
784                                 //If it is empty remove it from the list
785
checkedStateStore.remove(key);
786                             } else {
787                                 checkedStateStore.put(key, selections);
788                                 // proceed up the tree element hierarchy
789
Object JavaDoc parent = treeContentProvider
790                                         .getParent(key);
791                                 if (parent != null) {
792                                     addToHierarchyToCheckedStore(parent);
793                                 }
794                             }
795                         }
796
797                         //Now update hierarchies
798
keyIterator = items.keySet().iterator();
799
800                         while (keyIterator.hasNext()) {
801                             Object JavaDoc key = keyIterator.next();
802                             updateHierarchy(key);
803                             if (currentTreeSelection != null
804                                     && currentTreeSelection.equals(key)) {
805                                 listViewer.setAllChecked(false);
806                                 listViewer.setCheckedElements(((List JavaDoc) items
807                                         .get(key)).toArray());
808                             }
809                         }
810                     }
811                 });
812
813     }
814 }
815
Popular Tags