KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > jarpackager > 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.jdt.internal.ui.jarpackager;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.HashSet JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.List JavaDoc;
18 import java.util.Map JavaDoc;
19 import java.util.Set JavaDoc;
20
21 import org.eclipse.swt.SWT;
22 import org.eclipse.swt.custom.BusyIndicator;
23 import org.eclipse.swt.layout.GridData;
24 import org.eclipse.swt.layout.GridLayout;
25 import org.eclipse.swt.widgets.Composite;
26 import org.eclipse.swt.widgets.Table;
27 import org.eclipse.swt.widgets.Tree;
28
29 import org.eclipse.jface.viewers.CheckStateChangedEvent;
30 import org.eclipse.jface.viewers.CheckboxTableViewer;
31 import org.eclipse.jface.viewers.CheckboxTreeViewer;
32 import org.eclipse.jface.viewers.ICheckStateListener;
33 import org.eclipse.jface.viewers.ILabelProvider;
34 import org.eclipse.jface.viewers.ISelection;
35 import org.eclipse.jface.viewers.ISelectionChangedListener;
36 import org.eclipse.jface.viewers.IStructuredContentProvider;
37 import org.eclipse.jface.viewers.IStructuredSelection;
38 import org.eclipse.jface.viewers.ITreeContentProvider;
39 import org.eclipse.jface.viewers.ITreeViewerListener;
40 import org.eclipse.jface.viewers.SelectionChangedEvent;
41 import org.eclipse.jface.viewers.StructuredSelection;
42 import org.eclipse.jface.viewers.TreeExpansionEvent;
43 import org.eclipse.jface.viewers.ViewerComparator;
44 import org.eclipse.jface.viewers.ViewerFilter;
45
46 /**
47  * Combines a CheckboxTreeViewer and CheckboxListViewer.
48  * All viewer selection-driven interactions are handled within this viewer
49  */

50 public class CheckboxTreeAndListGroup implements ICheckStateListener, ISelectionChangedListener, ITreeViewerListener {
51
52     private Object JavaDoc fRoot;
53     private Object JavaDoc fCurrentTreeSelection;
54     private List JavaDoc fExpandedTreeNodes= new ArrayList JavaDoc();
55     private Map JavaDoc fCheckedStateStore= new HashMap JavaDoc(9);
56     private List JavaDoc fWhiteCheckedTreeItems= new ArrayList JavaDoc();
57     private List JavaDoc fListeners= new ArrayList JavaDoc();
58
59     private ITreeContentProvider fTreeContentProvider;
60     private IStructuredContentProvider fListContentProvider;
61     private ILabelProvider fTreeLabelProvider;
62     private ILabelProvider fListLabelProvider;
63
64     // widgets
65
private CheckboxTreeViewer fTreeViewer;
66     private CheckboxTableViewer fListViewer;
67
68     /**
69      * Creates an instance of this class. Use this constructor if you wish to specify
70      * the width and/or height of the combined widget (to only hardcode one of the
71      * sizing dimensions, specify the other dimension's value as -1)
72      */

73     public CheckboxTreeAndListGroup(
74             Composite parent,
75             Object JavaDoc rootObject,
76             ITreeContentProvider treeContentProvider,
77             ILabelProvider treeLabelProvider,
78             IStructuredContentProvider listContentProvider,
79             ILabelProvider listLabelProvider,
80             int style,
81             int width,
82             int height) {
83         fRoot= rootObject;
84         fTreeContentProvider= treeContentProvider;
85         fListContentProvider= listContentProvider;
86         fTreeLabelProvider= treeLabelProvider;
87         fListLabelProvider= listLabelProvider;
88         createContents(parent, width, height, style);
89     }
90     /**
91      * This method must be called just before this window becomes visible.
92      */

93     public void aboutToOpen() {
94         determineWhiteCheckedDescendents(fRoot);
95         checkNewTreeElements(getTreeChildren(fRoot));
96         fCurrentTreeSelection= null;
97
98         //select the first element in the list
99
Object JavaDoc[] elements= getTreeChildren(fRoot);
100         Object JavaDoc primary= elements.length > 0 ? elements[0] : null;
101         if (primary != null) {
102             fTreeViewer.setSelection(new StructuredSelection(primary));
103         }
104         fTreeViewer.getControl().setFocus();
105     }
106     /**
107      * Adds the passed listener to self's collection of clients
108      * that listen for changes to element checked states
109      *
110      * @param listener ICheckStateListener
111      */

112     public void addCheckStateListener(ICheckStateListener listener) {
113         fListeners.add(listener);
114     }
115     /**
116      * Adds the receiver and all of it's ancestors to the checkedStateStore if they
117      * are not already there.
118      */

119     private void addToHierarchyToCheckedStore(Object JavaDoc treeElement) {
120
121         // if this tree element is already gray then its ancestors all are as well
122
if (!fCheckedStateStore.containsKey(treeElement))
123             fCheckedStateStore.put(treeElement, new ArrayList JavaDoc());
124
125         Object JavaDoc parent= fTreeContentProvider.getParent(treeElement);
126         if (parent != null)
127             addToHierarchyToCheckedStore(parent);
128     }
129     /**
130      * Returns a boolean indicating whether all children of the passed tree element
131      * are currently white-checked
132      *
133      * @return boolean
134      * @param treeElement java.lang.Object
135      */

136     protected boolean areAllChildrenWhiteChecked(Object JavaDoc treeElement) {
137         Object JavaDoc[] children= getTreeChildren(treeElement);
138         for (int i= 0; i < children.length; ++i) {
139             if (!fWhiteCheckedTreeItems.contains(children[i]))
140                 return false;
141         }
142
143         return true;
144     }
145     /**
146      * Returns a boolean indicating whether all list elements associated with
147      * the passed tree element are currently checked
148      *
149      * @return boolean
150      * @param treeElement java.lang.Object
151      */

152     protected boolean areAllElementsChecked(Object JavaDoc treeElement) {
153         List JavaDoc checkedElements= (List JavaDoc)fCheckedStateStore.get(treeElement);
154         if (checkedElements == null) // ie.- tree item not even gray-checked
155
return false;
156
157         return getListItemsSize(treeElement) == checkedElements.size();
158     }
159     /**
160      * Iterates through the passed elements which are being realized for the first
161      * time and check each one in the tree viewer as appropriate
162      */

163     protected void checkNewTreeElements(Object JavaDoc[] elements) {
164         for (int i= 0; i < elements.length; ++i) {
165             Object JavaDoc currentElement= elements[i];
166             boolean checked= fCheckedStateStore.containsKey(currentElement);
167             fTreeViewer.setChecked(currentElement, checked);
168             fTreeViewer.setGrayed(
169                 currentElement,
170                 checked && !fWhiteCheckedTreeItems.contains(currentElement));
171         }
172     }
173     /**
174     * An item was checked in one of self's two views. Determine which
175     * view this occurred in and delegate appropriately
176     *
177     * @param event CheckStateChangedEvent
178     */

179     public void checkStateChanged(final CheckStateChangedEvent event) {
180
181         //Potentially long operation - show a busy cursor
182
BusyIndicator.showWhile(fTreeViewer.getControl().getDisplay(), new Runnable JavaDoc() {
183             public void run() {
184                 if (event.getCheckable().equals(fTreeViewer))
185                     treeItemChecked(event.getElement(), event.getChecked());
186                 else
187                     listItemChecked(event.getElement(), event.getChecked(), true);
188
189                 notifyCheckStateChangeListeners(event);
190             }
191         });
192     }
193     /**
194      * Lay out and initialize self's visual components.
195      *
196      * @param parent org.eclipse.swt.widgets.Composite
197      * @param width int
198      * @param height int
199      */

200     protected void createContents(
201         Composite parent,
202         int width,
203         int height,
204         int style) {
205         // group pane
206
Composite composite= new Composite(parent, style);
207         GridLayout layout= new GridLayout();
208         layout.numColumns= 2;
209         layout.makeColumnsEqualWidth= true;
210         layout.marginHeight= 0;
211         layout.marginWidth= 0;
212         composite.setLayout(layout);
213         composite.setLayoutData(new GridData(GridData.FILL_BOTH));
214
215         createTreeViewer(composite, width / 2, height);
216         createListViewer(composite, width / 2, height);
217
218         initialize();
219     }
220     /**
221      * Creates this group's list viewer.
222      */

223     protected void createListViewer(Composite parent, int width, int height) {
224         fListViewer= CheckboxTableViewer.newCheckList(parent, SWT.BORDER);
225         fListViewer.setUseHashlookup(true);
226         GridData data= new GridData(GridData.FILL_BOTH);
227         data.widthHint= width;
228         data.heightHint= height;
229         fListViewer.getTable().setLayoutData(data);
230         fListViewer.setContentProvider(fListContentProvider);
231         fListViewer.setLabelProvider(fListLabelProvider);
232         fListViewer.addCheckStateListener(this);
233     }
234     /**
235      * Creates this group's tree viewer.
236      */

237     protected void createTreeViewer(Composite parent, int width, int height) {
238         Tree tree= new Tree(parent, SWT.CHECK | SWT.BORDER);
239         GridData data= new GridData(GridData.FILL_BOTH);
240         data.widthHint= width;
241         data.heightHint= height;
242         tree.setLayoutData(data);
243
244         fTreeViewer= new CheckboxTreeViewer(tree);
245         fTreeViewer.setUseHashlookup(true);
246         fTreeViewer.setContentProvider(fTreeContentProvider);
247         fTreeViewer.setLabelProvider(fTreeLabelProvider);
248         fTreeViewer.addTreeListener(this);
249         fTreeViewer.addCheckStateListener(this);
250         fTreeViewer.addSelectionChangedListener(this);
251     }
252     /**
253      * Returns a boolean indicating whether the passed tree element should be
254      * at LEAST gray-checked. Note that this method does not consider whether
255      * it should be white-checked, so a specified tree item which should be
256      * white-checked will result in a <code>true</code> answer from this method.
257      * To determine whether a tree item should be white-checked use method
258      * #determineShouldBeWhiteChecked(Object).
259      *
260      * @param treeElement java.lang.Object
261      * @return boolean
262      * @see #determineShouldBeWhiteChecked(java.lang.Object)
263      */

264     protected boolean determineShouldBeAtLeastGrayChecked(Object JavaDoc treeElement) {
265         // if any list items associated with treeElement are checked then it
266
// retains its gray-checked status regardless of its children
267
List JavaDoc checked= (List JavaDoc) fCheckedStateStore.get(treeElement);
268         if (checked != null && (!checked.isEmpty()))
269             return true;
270
271         // if any children of treeElement are still gray-checked then treeElement
272
// must remain gray-checked as well
273
Object JavaDoc[] children= getTreeChildren(treeElement);
274         for (int i= 0; i < children.length; ++i) {
275             if (fCheckedStateStore.containsKey(children[i]))
276                 return true;
277         }
278
279         return false;
280     }
281     /**
282      * Returns a boolean indicating whether the passed tree item should be
283      * white-checked.
284      *
285      * @return boolean
286      * @param treeElement java.lang.Object
287      */

288     protected boolean determineShouldBeWhiteChecked(Object JavaDoc treeElement) {
289         return areAllChildrenWhiteChecked(treeElement)
290             && areAllElementsChecked(treeElement);
291     }
292     /**
293      * Recursively adds appropriate tree elements to the collection of
294      * known white-checked tree elements.
295      *
296      * @param treeElement java.lang.Object
297      */

298     protected void determineWhiteCheckedDescendents(Object JavaDoc treeElement) {
299         // always go through all children first since their white-checked
300
// statuses will be needed to determine the white-checked status for
301
// this tree element
302
Object JavaDoc[] children= getTreeChildren(treeElement);
303         for (int i= 0; i < children.length; ++i)
304             determineWhiteCheckedDescendents(children[i]);
305
306         // now determine the white-checked status for this tree element
307
if (determineShouldBeWhiteChecked(treeElement))
308             setWhiteChecked(treeElement, true);
309     }
310     /**
311      * Causes the tree viewer to expand all its items
312      */

313     public void expandAll() {
314         fTreeViewer.expandAll();
315     }
316     /**
317      * Answers a flat collection of all of the checked elements in the
318      * list portion of self
319      *
320      * @return java.util.Vector
321      */

322     public Iterator JavaDoc getAllCheckedListItems() {
323         Set JavaDoc result= new HashSet JavaDoc();
324         Iterator JavaDoc listCollectionsEnum= fCheckedStateStore.values().iterator();
325         while (listCollectionsEnum.hasNext())
326             result.addAll((List JavaDoc)listCollectionsEnum.next());
327         return result.iterator();
328     }
329     /**
330      * Answer a collection of all of the checked elements in the tree portion
331      * of self
332      *
333      * @return java.util.Vector
334      */

335     public Set JavaDoc getAllCheckedTreeItems() {
336         return new HashSet JavaDoc(fCheckedStateStore.keySet());
337     }
338     /**
339      * Answers the number of elements that have been checked by the
340      * user.
341      *
342      * @return int
343      */

344     public int getCheckedElementCount() {
345         return fCheckedStateStore.size();
346     }
347     /**
348      * Returns a count of the number of list items associated with a
349      * given tree item.
350      *
351      * @return int
352      * @param treeElement java.lang.Object
353      */

354     protected int getListItemsSize(Object JavaDoc treeElement) {
355         Object JavaDoc[] elements= getListElements(treeElement);
356         return elements.length;
357     }
358     /**
359      * Gets the table that displays the folder content
360      *
361      * @return the table used to show the list
362      */

363     public Table getTable() {
364         return fListViewer.getTable();
365     }
366     /**
367      * Gets the tree that displays the list for a folder
368      *
369      * @return the tree used to show the folders
370      */

371     public Tree getTree() {
372         return fTreeViewer.getTree();
373     }
374     /**
375      * Adds the given filter to the tree viewer and
376      * triggers refiltering and resorting of the elements.
377      *
378      * @param filter a viewer filter
379      */

380     public void addTreeFilter(ViewerFilter filter) {
381         fTreeViewer.addFilter(filter);
382     }
383     /**
384      * Adds the given filter to the list viewer and
385      * triggers refiltering and resorting of the elements.
386      *
387      * @param filter a viewer filter
388      */

389     public void addListFilter(ViewerFilter filter) {
390         fListViewer.addFilter(filter);
391     }
392     /**
393      * Logically gray-check all ancestors of treeItem by ensuring that they
394      * appear in the checked table
395      */

396     protected void grayCheckHierarchy(Object JavaDoc treeElement) {
397
398         // if this tree element is already gray then its ancestors all are as well
399
if (fCheckedStateStore.containsKey(treeElement))
400             return; // no need to proceed upwards from here
401

402         fCheckedStateStore.put(treeElement, new ArrayList JavaDoc());
403         if (determineShouldBeWhiteChecked(treeElement)) {
404             setWhiteChecked(treeElement, true);
405         }
406         Object JavaDoc parent= fTreeContentProvider.getParent(treeElement);
407         if (parent != null)
408             grayCheckHierarchy(parent);
409     }
410     /**
411      * Sets the initial checked state of the passed list element to true.
412      */

413     public void initialCheckListItem(Object JavaDoc element) {
414         Object JavaDoc parent= fTreeContentProvider.getParent(element);
415         fCurrentTreeSelection= parent;
416         //As this is not done from the UI then set the box for updating from the selection to false
417
listItemChecked(element, true, false);
418         updateHierarchy(parent);
419     }
420     /**
421      * Sets the initial checked state of the passed element to true,
422      * as well as to all of its children and associated list elements
423      */

424     public void initialCheckTreeItem(Object JavaDoc element) {
425         treeItemChecked(element, true);
426     }
427     /**
428      * Initializes this group's viewers after they have been laid out.
429      */

430     protected void initialize() {
431         fTreeViewer.setInput(fRoot);
432     }
433     /**
434      * Callback that's invoked when the checked status of an item in the list
435      * is changed by the user. Do not try and update the hierarchy if we are building the
436      * initial list.
437      */

438     protected void listItemChecked(
439         Object JavaDoc listElement,
440         boolean state,
441         boolean updatingFromSelection) {
442         List JavaDoc checkedListItems= (List JavaDoc) fCheckedStateStore.get(fCurrentTreeSelection);
443
444         if (state) {
445             if (checkedListItems == null) {
446                 // since the associated tree item has gone from 0 -> 1 checked
447
// list items, tree checking may need to be updated
448
grayCheckHierarchy(fCurrentTreeSelection);
449                 checkedListItems= (List JavaDoc) fCheckedStateStore.get(fCurrentTreeSelection);
450             }
451             checkedListItems.add(listElement);
452         } else {
453             checkedListItems.remove(listElement);
454             if (checkedListItems.isEmpty()) {
455                 // since the associated tree item has gone from 1 -> 0 checked
456
// list items, tree checking may need to be updated
457
ungrayCheckHierarchy(fCurrentTreeSelection);
458             }
459         }
460
461         if (updatingFromSelection)
462             updateHierarchy(fCurrentTreeSelection);
463     }
464     /**
465      * Notifies all checked state listeners that the passed element has had
466      * its checked state changed to the passed state
467      */

468     protected void notifyCheckStateChangeListeners(CheckStateChangedEvent event) {
469         Iterator JavaDoc listenersEnum= fListeners.iterator();
470         while (listenersEnum.hasNext())
471              ((ICheckStateListener) listenersEnum.next()).checkStateChanged(event);
472     }
473     /**
474      *Sets the contents of the list viewer based upon the specified selected
475      *tree element. This also includes checking the appropriate list items.
476      *
477      *@param treeElement java.lang.Object
478      */

479     protected void populateListViewer(final Object JavaDoc treeElement) {
480         if (treeElement == fCurrentTreeSelection)
481             return;
482         fCurrentTreeSelection= treeElement;
483         fListViewer.setInput(treeElement);
484         List JavaDoc listItemsToCheck= (List JavaDoc) fCheckedStateStore.get(treeElement);
485
486         if (listItemsToCheck != null) {
487             Iterator JavaDoc listItemsEnum= listItemsToCheck.iterator();
488             while (listItemsEnum.hasNext())
489                 fListViewer.setChecked(listItemsEnum.next(), true);
490         }
491     }
492     /**
493      * Removes the passed listener from self's collection of clients
494      * that listen for changes to element checked states
495      *
496      * @param listener ICheckStateListener
497      */

498     public void removeCheckStateListener(ICheckStateListener listener) {
499         fListeners.remove(listener);
500     }
501     /**
502      * Handles the selection of an item in the tree viewer
503      *
504      * @param event ISelection
505      */

506     public void selectionChanged(final SelectionChangedEvent event) {
507         BusyIndicator.showWhile(getTable().getShell().getDisplay(), new Runnable JavaDoc() {
508             public void run() {
509                 IStructuredSelection selection= (IStructuredSelection) event.getSelection();
510                 Object JavaDoc selectedElement= selection.getFirstElement();
511                 if (selectedElement == null) {
512                     fCurrentTreeSelection= null;
513                     fListViewer.setInput(fCurrentTreeSelection);
514                     return;
515                 }
516                 populateListViewer(selectedElement);
517             }
518         });
519     }
520
521     /**
522      * Selects or deselect all of the elements in the tree depending on the value of the selection
523      * boolean. Be sure to update the displayed files as well.
524      */

525     public void setAllSelections(final boolean selection) {
526
527         //Potentially long operation - show a busy cursor
528
BusyIndicator.showWhile(fTreeViewer.getControl().getDisplay(), new Runnable JavaDoc() {
529             public void run() {
530                 setTreeChecked(fRoot, selection);
531                 fListViewer.setAllChecked(selection);
532             }
533         });
534     }
535
536     /**
537      * Sets the list viewer's providers to those passed
538      *
539      * @param contentProvider ITreeContentProvider
540      * @param labelProvider ILabelProvider
541      */

542     public void setListProviders(
543         IStructuredContentProvider contentProvider,
544         ILabelProvider labelProvider) {
545         fListViewer.setContentProvider(contentProvider);
546         fListViewer.setLabelProvider(labelProvider);
547     }
548     /**
549      * Sets the sorter that is to be applied to self's list viewer
550      */

551     public void setListComparator(ViewerComparator comparator) {
552         fListViewer.setComparator(comparator);
553     }
554     /**
555      * Sets the root of the widget to be new Root. Regenerate all of the tables and lists from this
556      * value.
557      *
558      * @param newRoot
559      */

560     public void setRoot(Object JavaDoc newRoot) {
561         this.fRoot= newRoot;
562         initialize();
563     }
564     /**
565      * Sets the checked state of the passed tree element appropriately, and
566      * do so recursively to all of its child tree elements as well
567      */

568     protected void setTreeChecked(Object JavaDoc treeElement, boolean state) {
569
570         if (treeElement.equals(fCurrentTreeSelection)) {
571             fListViewer.setAllChecked(state);
572         }
573
574         if (state) {
575             Object JavaDoc[] listItems= getListElements(treeElement);
576             List JavaDoc listItemsChecked= new ArrayList JavaDoc();
577             for (int i= 0; i < listItems.length; ++i)
578                 listItemsChecked.add(listItems[i]);
579
580             fCheckedStateStore.put(treeElement, listItemsChecked);
581         } else
582             fCheckedStateStore.remove(treeElement);
583
584         setWhiteChecked(treeElement, state);
585         fTreeViewer.setChecked(treeElement, state);
586         fTreeViewer.setGrayed(treeElement, false);
587
588         // now logically check/uncheck all children as well
589
Object JavaDoc[] children= getTreeChildren(treeElement);
590         for (int i= 0; i < children.length; ++i) {
591             setTreeChecked(children[i], state);
592         }
593     }
594     /**
595      * Sets the tree viewer's providers to those passed
596      *
597      * @param contentProvider ITreeContentProvider
598      * @param labelProvider ILabelProvider
599      */

600     public void setTreeProviders(
601         ITreeContentProvider contentProvider,
602         ILabelProvider labelProvider) {
603         fTreeViewer.setContentProvider(contentProvider);
604         fTreeViewer.setLabelProvider(labelProvider);
605     }
606     /**
607      * Sets the sorter that is to be applied to self's tree viewer
608      */

609     public void setTreeComparator(ViewerComparator sorter) {
610         fTreeViewer.setComparator(sorter);
611     }
612     /**
613      * Adjusts the collection of references to white-checked tree elements appropriately.
614      *
615      * @param treeElement java.lang.Object
616      * @param isWhiteChecked boolean
617      */

618     protected void setWhiteChecked(Object JavaDoc treeElement, boolean isWhiteChecked) {
619         if (isWhiteChecked) {
620             if (!fWhiteCheckedTreeItems.contains(treeElement))
621                 fWhiteCheckedTreeItems.add(treeElement);
622         } else
623             fWhiteCheckedTreeItems.remove(treeElement);
624     }
625     /**
626      * Handle the collapsing of an element in a tree viewer
627      */

628     public void treeCollapsed(TreeExpansionEvent event) {
629         // We don't need to do anything with this
630
}
631
632     /**
633      * Handles the expansionsion of an element in a tree viewer
634      */

635     public void treeExpanded(TreeExpansionEvent event) {
636
637         Object JavaDoc item= event.getElement();
638
639         // First see if the children need to be given their checked state at all. If they've
640
// already been realized then this won't be necessary
641
if (!fExpandedTreeNodes.contains(item)) {
642             fExpandedTreeNodes.add(item);
643             checkNewTreeElements(getTreeChildren(item));
644         }
645     }
646
647     /**
648      * Callback that's invoked when the checked status of an item in the tree
649      * is changed by the user.
650      */

651     protected void treeItemChecked(Object JavaDoc treeElement, boolean state) {
652
653         // recursively adjust all child tree elements appropriately
654
setTreeChecked(treeElement, state);
655
656         Object JavaDoc parent= fTreeContentProvider.getParent(treeElement);
657         if (parent == null)
658             return;
659
660         // now update upwards in the tree hierarchy
661
if (state)
662             grayCheckHierarchy(parent);
663         else
664             ungrayCheckHierarchy(parent);
665
666         updateHierarchy(treeElement);
667     }
668     /**
669      * Logically un-gray-check all ancestors of treeItem iff appropriate.
670      */

671     protected void ungrayCheckHierarchy(Object JavaDoc treeElement) {
672         if (!determineShouldBeAtLeastGrayChecked(treeElement))
673             fCheckedStateStore.remove(treeElement);
674
675         Object JavaDoc parent= fTreeContentProvider.getParent(treeElement);
676         if (parent != null)
677             ungrayCheckHierarchy(parent);
678     }
679     /**
680      * Sets the checked state of self and all ancestors appropriately
681      */

682     protected void updateHierarchy(Object JavaDoc treeElement) {
683
684         boolean whiteChecked= determineShouldBeWhiteChecked(treeElement);
685         boolean shouldBeAtLeastGray= determineShouldBeAtLeastGrayChecked(treeElement);
686
687         fTreeViewer.setChecked(treeElement, whiteChecked || shouldBeAtLeastGray);
688         setWhiteChecked(treeElement, whiteChecked);
689         if (whiteChecked)
690             fTreeViewer.setGrayed(treeElement, false);
691         else
692             fTreeViewer.setGrayed(treeElement, shouldBeAtLeastGray);
693
694         // proceed up the tree element hierarchy
695
Object JavaDoc parent= fTreeContentProvider.getParent(treeElement);
696         if (parent != null) {
697             updateHierarchy(parent);
698         }
699     }
700     /**
701      * Update the selections of the tree elements in items to reflect the new
702      * selections provided.
703      *
704      * @param items with keys of Object (the tree element) and values of List (the selected
705      * list elements).
706      */

707     public void updateSelections(final Map JavaDoc items) {
708
709         //Potentially long operation - show a busy cursor
710
BusyIndicator.showWhile(fTreeViewer.getControl().getDisplay(), new Runnable JavaDoc() {
711             public void run() {
712                 handleUpdateSelection(items);
713             }
714         });
715     }
716     /**
717      * Returns the result of running the given elements through the filters.
718      *
719      * @param elements the elements to filter
720      * @return only the elements which all filters accept
721      */

722     protected Object JavaDoc[] filter(ViewerFilter[] filters, Object JavaDoc[] elements) {
723         if (filters != null) {
724             ArrayList JavaDoc filtered = new ArrayList JavaDoc(elements.length);
725             for (int i = 0; i < elements.length; i++) {
726                 boolean add = true;
727                 for (int j = 0; j < filters.length; j++) {
728                     add = filters[j].select(null, null, elements[i]);
729                     if (!add)
730                         break;
731                 }
732                 if (add)
733                     filtered.add(elements[i]);
734             }
735             return filtered.toArray();
736         }
737         return elements;
738     }
739
740     private Object JavaDoc[] getTreeChildren(Object JavaDoc element) {
741         return filter(fTreeViewer.getFilters(), fTreeContentProvider.getChildren(element));
742     }
743
744     private Object JavaDoc[] getListElements(Object JavaDoc element) {
745         return filter(fListViewer.getFilters(), fListContentProvider.getElements(element));
746     }
747
748     public Set JavaDoc getWhiteCheckedTreeItems() {
749         return new HashSet JavaDoc(fWhiteCheckedTreeItems);
750     }
751
752     private void handleUpdateSelection(Map JavaDoc items) {
753         Iterator JavaDoc keyIterator= items.keySet().iterator();
754
755         //Update the store before the hierarchy to prevent updating parents before all of the children are done
756
while (keyIterator.hasNext()) {
757             Object JavaDoc key= keyIterator.next();
758             //Replace the items in the checked state store with those from the supplied items
759
List JavaDoc selections= (List JavaDoc) items.get(key);
760             if (selections.size() == 0)
761                 //If it is empty remove it from the list
762
fCheckedStateStore.remove(key);
763             else {
764                 fCheckedStateStore.put(key, selections);
765                 // proceed up the tree element hierarchy
766
Object JavaDoc parent= fTreeContentProvider.getParent(key);
767                 if (parent != null) {
768                     addToHierarchyToCheckedStore(parent);
769                 }
770             }
771         }
772
773         //Now update hierarchies
774
keyIterator= items.keySet().iterator();
775
776         while (keyIterator.hasNext()) {
777             Object JavaDoc key= keyIterator.next();
778             updateHierarchy(key);
779             if (fCurrentTreeSelection != null && fCurrentTreeSelection.equals(key)) {
780                 fListViewer.setAllChecked(false);
781                 fListViewer.setCheckedElements(((List JavaDoc) items.get(key)).toArray());
782             }
783         }
784     }
785     
786     /**
787      * Checks if an element is grey checked.
788      */

789     public boolean isTreeItemGreyChecked(Object JavaDoc object) {
790         return fTreeViewer.getGrayed(object);
791     }
792
793     /**
794      * For a given element, expand its chidren to a level.
795      */

796     public void expandTreeToLevel(Object JavaDoc object, int level) {
797         fTreeViewer.expandToLevel(object, level);
798     }
799     /**
800      * @param selection
801      */

802     public void setTreeSelection(ISelection selection) {
803         fTreeViewer.setSelection(selection);
804     }
805 }
806
Popular Tags