KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2003 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.internal.dialogs;
12
13
14 import java.util.ArrayList JavaDoc;
15
16 import org.eclipse.swt.widgets.Composite;
17 import org.eclipse.swt.widgets.Item;
18 import org.eclipse.swt.widgets.Tree;
19 import org.eclipse.swt.widgets.TreeItem;
20 import org.eclipse.swt.widgets.Widget;
21
22 import org.eclipse.jface.viewers.CheckStateChangedEvent;
23 import org.eclipse.jface.viewers.CheckboxTreeViewer;
24 import org.eclipse.jface.viewers.ICheckStateListener;
25 import org.eclipse.jface.viewers.ITreeViewerListener;
26 import org.eclipse.jface.viewers.TreeExpansionEvent;
27
28
29 /**
30  * CheckboxTreeViewer with special behaviour of the checked / gray state on
31  * container (non-leaf) nodes:
32  * The grayed state is used to visualize the checked state of its children.
33  * Containers are checked and non-gary if all contained leafs are checked. The
34  * container is grayed if some but not all leafs are checked.
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             public void treeExpanded(TreeExpansionEvent event) {
76                 Widget item= findItem(event.getElement());
77                 if (item instanceof TreeItem) {
78                     initializeItem((TreeItem) item);
79                 }
80             }
81         });
82     }
83     
84     protected void doCheckStateChanged(Object JavaDoc element) {
85         Widget item= findItem(element);
86         if (item instanceof TreeItem) {
87             TreeItem treeItem= (TreeItem) item;
88             treeItem.setGrayed(false);
89             updateChildrenItems(treeItem);
90             updateParentItems(treeItem.getParentItem());
91         }
92     }
93     
94     /**
95      * The item has expanded. Updates the checked state of its children.
96      */

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

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

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

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

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

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

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