KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > dialogs > ContainerCheckedTreeViewer


1 /*******************************************************************************
2  * Copyright (c) 2005 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.dialogs;
12
13 import java.util.ArrayList JavaDoc;
14
15 import org.eclipse.jface.viewers.CheckStateChangedEvent;
16 import org.eclipse.jface.viewers.CheckboxTreeViewer;
17 import org.eclipse.jface.viewers.ICheckStateListener;
18 import org.eclipse.jface.viewers.ITreeViewerListener;
19 import org.eclipse.jface.viewers.TreeExpansionEvent;
20 import org.eclipse.swt.widgets.Composite;
21 import org.eclipse.swt.widgets.Item;
22 import org.eclipse.swt.widgets.Tree;
23 import org.eclipse.swt.widgets.TreeItem;
24 import org.eclipse.swt.widgets.Widget;
25
26 /**
27  * CheckboxTreeViewer with special behaviour of the checked / gray state on
28  * container (non-leaf) nodes:
29  * The grayed state is used to visualize the checked state of its children.
30  * Containers are checked and non-gray if all contained leafs are checked. The
31  * container is grayed if some but not all leafs are checked.
32  * @since 3.1
33  */

34 public class ContainerCheckedTreeViewer extends CheckboxTreeViewer {
35
36     /**
37      * Constructor for ContainerCheckedTreeViewer.
38      * @see CheckboxTreeViewer#CheckboxTreeViewer(Composite)
39      */

40     public ContainerCheckedTreeViewer(Composite parent) {
41         super(parent);
42         initViewer();
43     }
44
45     /**
46      * Constructor for ContainerCheckedTreeViewer.
47      * @see CheckboxTreeViewer#CheckboxTreeViewer(Composite,int)
48      */

49     public ContainerCheckedTreeViewer(Composite parent, int style) {
50         super(parent, style);
51         initViewer();
52     }
53
54     /**
55      * Constructor for ContainerCheckedTreeViewer.
56      * @see CheckboxTreeViewer#CheckboxTreeViewer(Tree)
57      */

58     public ContainerCheckedTreeViewer(Tree tree) {
59         super(tree);
60         initViewer();
61     }
62
63     private void initViewer() {
64         setUseHashlookup(true);
65         addCheckStateListener(new ICheckStateListener() {
66             public void checkStateChanged(CheckStateChangedEvent event) {
67                 doCheckStateChanged(event.getElement());
68             }
69         });
70         addTreeListener(new ITreeViewerListener() {
71             public void treeCollapsed(TreeExpansionEvent event) {
72             }
73
74             public void treeExpanded(TreeExpansionEvent event) {
75                 Widget item = findItem(event.getElement());
76                 if (item instanceof TreeItem) {
77                     initializeItem((TreeItem) item);
78                 }
79             }
80         });
81     }
82
83     /**
84      * Update element after a checkstate change.
85      * @param element
86      */

87     protected void doCheckStateChanged(Object JavaDoc element) {
88         Widget item = findItem(element);
89         if (item instanceof TreeItem) {
90             TreeItem treeItem = (TreeItem) item;
91             treeItem.setGrayed(false);
92             updateChildrenItems(treeItem);
93             updateParentItems(treeItem.getParentItem());
94         }
95     }
96
97     /**
98      * The item has expanded. Updates the checked state of its children.
99      */

100     private void initializeItem(TreeItem item) {
101         if (item.getChecked() && !item.getGrayed()) {
102             updateChildrenItems(item);
103         }
104     }
105
106     /**
107      * Updates the check state of all created children
108      */

109     private void updateChildrenItems(TreeItem parent) {
110         Item[] children = getChildren(parent);
111         boolean state = parent.getChecked();
112         for (int i = 0; i < children.length; i++) {
113             TreeItem curr = (TreeItem) children[i];
114             if (curr.getData() != null
115                     && ((curr.getChecked() != state) || curr.getGrayed())) {
116                 curr.setChecked(state);
117                 curr.setGrayed(false);
118                 updateChildrenItems(curr);
119             }
120         }
121     }
122
123     /**
124      * Updates the check / gray state of all parent items
125      */

126     private void updateParentItems(TreeItem item) {
127         if (item != null) {
128             Item[] children = getChildren(item);
129             boolean containsChecked = false;
130             boolean containsUnchecked = false;
131             for (int i = 0; i < children.length; i++) {
132                 TreeItem curr = (TreeItem) children[i];
133                 containsChecked |= curr.getChecked();
134                 containsUnchecked |= (!curr.getChecked() || curr.getGrayed());
135             }
136             item.setChecked(containsChecked);
137             item.setGrayed(containsChecked && containsUnchecked);
138             updateParentItems(item.getParentItem());
139         }
140     }
141
142     
143     /* (non-Javadoc)
144      * @see org.eclipse.jface.viewers.ICheckable#setChecked(java.lang.Object, boolean)
145      */

146     public boolean setChecked(Object JavaDoc element, boolean state) {
147         if (super.setChecked(element, state)) {
148             doCheckStateChanged(element);
149             return true;
150         }
151         return false;
152     }
153
154  
155     /* (non-Javadoc)
156      * @see org.eclipse.jface.viewers.CheckboxTreeViewer#setCheckedElements(java.lang.Object[])
157      */

158     public void setCheckedElements(Object JavaDoc[] elements) {
159         super.setCheckedElements(elements);
160         for (int i = 0; i < elements.length; i++) {
161             doCheckStateChanged(elements[i]);
162         }
163     }
164
165
166     /* (non-Javadoc)
167      * @see org.eclipse.jface.viewers.AbstractTreeViewer#setExpanded(org.eclipse.swt.widgets.Item, boolean)
168      */

169     protected void setExpanded(Item item, boolean expand) {
170         super.setExpanded(item, expand);
171         if (expand && item instanceof TreeItem) {
172             initializeItem((TreeItem) item);
173         }
174     }
175
176    
177     /* (non-Javadoc)
178      * @see org.eclipse.jface.viewers.CheckboxTreeViewer#getCheckedElements()
179      */

180     public Object JavaDoc[] getCheckedElements() {
181         Object JavaDoc[] checked = super.getCheckedElements();
182         // add all items that are children of a checked node but not created yet
183
ArrayList JavaDoc result = new ArrayList JavaDoc();
184         for (int i = 0; i < checked.length; i++) {
185             Object JavaDoc curr = checked[i];
186             result.add(curr);
187             Widget item = findItem(curr);
188             if (item != null) {
189                 Item[] children = getChildren(item);
190                 // check if contains the dummy node
191
if (children.length == 1 && children[0].getData() == null) {
192                     // not yet created
193
collectChildren(curr, result);
194                 }
195             }
196         }
197         return result.toArray();
198     }
199
200     /**
201      * Recursively add the filtered children of element to the result.
202      * @param element
203      * @param result
204      */

205     private void collectChildren(Object JavaDoc element, ArrayList JavaDoc result) {
206         Object JavaDoc[] filteredChildren = getFilteredChildren(element);
207         for (int i = 0; i < filteredChildren.length; i++) {
208             Object JavaDoc curr = filteredChildren[i];
209             result.add(curr);
210             collectChildren(curr, result);
211         }
212     }
213
214 }
215
Popular Tags