KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > update > internal > ui > wizards > ContainerCheckedTreeViewer


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.update.internal.ui.wizards;
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  * Copied from ui internal package.
28  *
29  * CheckboxTreeViewer with special behaviour of the checked / gray state on
30  * container (non-leaf) nodes:
31  * The grayed state is used to visualize the checked state of its children.
32  * Containers are checked and non-gary if all contained leafs are checked. The
33  * container is grayed if some but not all leafs are checked.
34  *
35  */

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

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

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

60     public ContainerCheckedTreeViewer(Tree tree) {
61         super(tree);
62         initViewer();
63     }
64
65     private void initViewer() {
66         setUseHashlookup(true);
67         addCheckStateListener(new ICheckStateListener() {
68             public void checkStateChanged(CheckStateChangedEvent event) {
69                 doCheckStateChanged(event.getElement());
70             }
71         });
72         addTreeListener(new ITreeViewerListener() {
73             public void treeCollapsed(TreeExpansionEvent event) {
74             }
75
76             public void treeExpanded(TreeExpansionEvent event) {
77                 Widget item = findItem(event.getElement());
78                 if (item instanceof TreeItem) {
79                     initializeItem((TreeItem) item);
80                 }
81             }
82         });
83     }
84
85     protected void doCheckStateChanged(Object JavaDoc element) {
86         Widget item = findItem(element);
87         if (item instanceof TreeItem) {
88             TreeItem treeItem = (TreeItem) item;
89             treeItem.setGrayed(false);
90             updateChildrenItems(treeItem);
91             updateParentItems(treeItem.getParentItem());
92         }
93     }
94
95     /**
96      * The item has expanded. Updates the checked state of its children.
97      */

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

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

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

143     public boolean setChecked(Object JavaDoc element, boolean state) {
144         if (super.setChecked(element, state)) {
145             doCheckStateChanged(element);
146             return true;
147         }
148         return false;
149     }
150
151     /*
152      * @see CheckboxTreeViewer#setCheckedElements(Object[])
153      */

154     public void setCheckedElements(Object JavaDoc[] elements) {
155         super.setCheckedElements(elements);
156         for (int i = 0; i < elements.length; i++) {
157             doCheckStateChanged(elements[i]);
158         }
159     }
160
161     /*
162      * @see AbstractTreeViewer#setExpanded(Item, boolean)
163      */

164     protected void setExpanded(Item item, boolean expand) {
165         super.setExpanded(item, expand);
166         if (expand && item instanceof TreeItem) {
167             initializeItem((TreeItem) item);
168         }
169     }
170
171     /*
172      * @see CheckboxTreeViewer#getCheckedElements()
173      */

174     public Object JavaDoc[] getCheckedElements() {
175         Object JavaDoc[] checked = super.getCheckedElements();
176         // add all items that are children of a checked node but not created yet
177
ArrayList JavaDoc result = new ArrayList JavaDoc();
178         for (int i = 0; i < checked.length; i++) {
179             Object JavaDoc curr = checked[i];
180             result.add(curr);
181             Widget item = findItem(curr);
182             if (item != null) {
183                 Item[] children = getChildren(item);
184                 // check if contains the dummy node
185
if (children.length == 1 && children[0].getData() == null) {
186                     // not yet created
187
collectChildren(curr, result);
188                 }
189             }
190         }
191         return result.toArray();
192     }
193
194     private void collectChildren(Object JavaDoc element, ArrayList JavaDoc result) {
195         Object JavaDoc[] filteredChildren = getFilteredChildren(element);
196         for (int i = 0; i < filteredChildren.length; i++) {
197             Object JavaDoc curr = filteredChildren[i];
198             result.add(curr);
199             collectChildren(curr, result);
200         }
201     }
202
203     protected void createChildren(final Widget widget) {
204         super.createChildren(widget);
205     }
206 }
207
Popular Tags