Posted on 2008-06-12 19:26
kooyee 阅读(1544)
评论(2) 编辑 收藏
在TreeContentProvider加入新的getAllChildren(Object arg0) method
public List getAllChildren(Object arg0){
List allChildren = new ArrayList();
//get all children and add to the List called allChildren
getAllChildren(arg0, allChildren);
return allChildren;
}
public void getAllChildren(Object arg0, List arg1){
if(this.hasChildren(arg0)){
for(Object child : this.getChildren(arg0)){
System.out.println("parent: "+ arg0 +" children: "+child);
//add node to the list
arg1.add(child);
//expand to next level of tree
getAllChildren(child, arg1);
}
}
}
以后直接调用getAllChildren(Object arg0) 的到含有all children的list
ContainerCheckedTreeViewer与 CheckboxTreeViewer区别
从ContainerCheckedTreeViewer的源代码可以看出它继承于CheckboxTreeViewer。但是当ContainerCheckedTreeViewer的父节点被选中时,对应的子节点会自动选中。 而起这个类还提供了父节点的灰选状态当不是全部子节点选中时。
(其他的区别还未发现,欢迎补充!)
import java.util.ArrayList ;
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 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 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 [] 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 [] getCheckedElements() {
175 Object [] checked = super.getCheckedElements();
176 // add all items that are children of a checked node but not created yet
177 ArrayList result = new ArrayList ();
178 for (int i = 0; i < checked.length; i++) {
179 Object 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 element, ArrayList result) {
195 Object [] filteredChildren = getFilteredChildren(element);
196 for (int i = 0; i < filteredChildren.length; i++) {
197 Object 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 }