Posted on 2008-06-12 19:26 
kooyee 阅读(1578) 
评论(2)  编辑  收藏  
			 
			
		 
		在TreeContentProvider加入新的getAllChildren(Object arg0) method

 public List getAllChildren(Object arg0)
public List getAllChildren(Object arg0) {
{
 List allChildren = new ArrayList();
      List allChildren = new ArrayList();
 //get all children and add to the List called allChildren
      //get all children and add to the List called allChildren 
 getAllChildren(arg0, allChildren);
      getAllChildren(arg0, allChildren);
 return allChildren;
      return allChildren;
 }
  }
 
  

 public void getAllChildren(Object arg0, List arg1)
  public void getAllChildren(Object arg0, List arg1) {
{

 if(this.hasChildren(arg0))
      if(this.hasChildren(arg0)) {
{

 for(Object child : this.getChildren(arg0))
          for(Object child : this.getChildren(arg0)) {
{
 System.out.println("parent: "+ arg0 +"    children: "+child);
              System.out.println("parent: "+ arg0 +"    children: "+child);
 //add node to the list
              //add node to the list
 arg1.add(child);
              arg1.add(child);
 //expand to next level of tree
              //expand to next level of tree
 getAllChildren(child, arg1);
              getAllChildren(child, arg1);
 }
          }
 }
      }
 }
  } 
以后直接调用getAllChildren(Object arg0) 的到含有all children的list
ContainerCheckedTreeViewer与 CheckboxTreeViewer区别
从ContainerCheckedTreeViewer的源代码可以看出它继承于CheckboxTreeViewer。但是当ContainerCheckedTreeViewer的父节点被选中时,对应的子节点会自动选中。 而起这个类还提供了父节点的灰选状态当不是全部子节点选中时。
(其他的区别还未发现,欢迎补充!)
 import java.util.ArrayList ;
import java.util.ArrayList ;
 14
14 
 15 import org.eclipse.jface.viewers.CheckStateChangedEvent;
15 import org.eclipse.jface.viewers.CheckStateChangedEvent;
 16 import org.eclipse.jface.viewers.CheckboxTreeViewer;
16 import org.eclipse.jface.viewers.CheckboxTreeViewer;
 17 import org.eclipse.jface.viewers.ICheckStateListener;
17 import org.eclipse.jface.viewers.ICheckStateListener;
 18 import org.eclipse.jface.viewers.ITreeViewerListener;
18 import org.eclipse.jface.viewers.ITreeViewerListener;
 19 import org.eclipse.jface.viewers.TreeExpansionEvent;
19 import org.eclipse.jface.viewers.TreeExpansionEvent;
 20 import org.eclipse.swt.widgets.Composite;
20 import org.eclipse.swt.widgets.Composite;
 21 import org.eclipse.swt.widgets.Item;
21 import org.eclipse.swt.widgets.Item;
 22 import org.eclipse.swt.widgets.Tree;
22 import org.eclipse.swt.widgets.Tree;
 23 import org.eclipse.swt.widgets.TreeItem;
23 import org.eclipse.swt.widgets.TreeItem;
 24 import org.eclipse.swt.widgets.Widget;
24 import org.eclipse.swt.widgets.Widget;
 25
25 

 26 /** *//**
26 /** *//**
 27  * Copied from ui internal package.
27  * Copied from ui internal package.
 28  *
28  * 
 29  * CheckboxTreeViewer with special behaviour of the checked / gray state on
29  * CheckboxTreeViewer with special behaviour of the checked / gray state on 
 30  * container (non-leaf) nodes:
30  * container (non-leaf) nodes:
 31  * The grayed state is used to visualize the checked state of its children.
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
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.
33  * container is grayed if some but not all leafs are checked.
 34  *
34  * 
 35  */
35  */

 36 public class ContainerCheckedTreeViewer extends CheckboxTreeViewer
36 public class ContainerCheckedTreeViewer extends CheckboxTreeViewer  {
{
 37
37 

 38     /** *//**
38     /** *//**
 39      * Constructor for ContainerCheckedTreeViewer.
39      * Constructor for ContainerCheckedTreeViewer.
 40      * @see CheckboxTreeViewer#CheckboxTreeViewer(Composite)
40      * @see CheckboxTreeViewer#CheckboxTreeViewer(Composite)
 41      */
41      */

 42     public ContainerCheckedTreeViewer(Composite parent)
42     public ContainerCheckedTreeViewer(Composite parent)  {
{
 43         super(parent);
43         super(parent);
 44         initViewer();
44         initViewer();
 45     }
45     }
 46
46 

 47     /** *//**
47     /** *//**
 48      * Constructor for ContainerCheckedTreeViewer.
48      * Constructor for ContainerCheckedTreeViewer.
 49      * @see CheckboxTreeViewer#CheckboxTreeViewer(Composite,int)
49      * @see CheckboxTreeViewer#CheckboxTreeViewer(Composite,int)
 50      */
50      */

 51     public ContainerCheckedTreeViewer(Composite parent, int style)
51     public ContainerCheckedTreeViewer(Composite parent, int style)  {
{
 52         super(parent, style);
52         super(parent, style);
 53         initViewer();
53         initViewer();
 54     }
54     }
 55
55 

 56     /** *//**
56     /** *//**
 57      * Constructor for ContainerCheckedTreeViewer.
57      * Constructor for ContainerCheckedTreeViewer.
 58      * @see CheckboxTreeViewer#CheckboxTreeViewer(Tree)
58      * @see CheckboxTreeViewer#CheckboxTreeViewer(Tree)
 59      */
59      */

 60     public ContainerCheckedTreeViewer(Tree tree)
60     public ContainerCheckedTreeViewer(Tree tree)  {
{
 61         super(tree);
61         super(tree);
 62         initViewer();
62         initViewer();
 63     }
63     }
 64
64 

 65     private void initViewer()
65     private void initViewer()  {
{
 66         setUseHashlookup(true);
66         setUseHashlookup(true);

 67         addCheckStateListener(new ICheckStateListener()
67         addCheckStateListener(new ICheckStateListener()  {
{

 68             public void checkStateChanged(CheckStateChangedEvent event)
68             public void checkStateChanged(CheckStateChangedEvent event)  {
{
 69                 doCheckStateChanged(event.getElement());
69                 doCheckStateChanged(event.getElement());
 70             }
70             }
 71         });
71         });

 72         addTreeListener(new ITreeViewerListener()
72         addTreeListener(new ITreeViewerListener()  {
{

 73             public void treeCollapsed(TreeExpansionEvent event)
73             public void treeCollapsed(TreeExpansionEvent event)  {
{
 74             }
74             }
 75
75 

 76             public void treeExpanded(TreeExpansionEvent event)
76             public void treeExpanded(TreeExpansionEvent event)  {
{
 77                 Widget item = findItem(event.getElement());
77                 Widget item = findItem(event.getElement());

 78                 if (item instanceof TreeItem)
78                 if (item instanceof TreeItem)  {
{
 79                     initializeItem((TreeItem) item);
79                     initializeItem((TreeItem) item);
 80                 }
80                 }
 81             }
81             }
 82         });
82         });
 83     }
83     }
 84
84 

 85     protected void doCheckStateChanged(Object  element)
85     protected void doCheckStateChanged(Object  element)  {
{
 86         Widget item = findItem(element);
86         Widget item = findItem(element);

 87         if (item instanceof TreeItem)
87         if (item instanceof TreeItem)  {
{
 88             TreeItem treeItem = (TreeItem) item;
88             TreeItem treeItem = (TreeItem) item;
 89             treeItem.setGrayed(false);
89             treeItem.setGrayed(false);
 90             updateChildrenItems(treeItem);
90             updateChildrenItems(treeItem);
 91             updateParentItems(treeItem.getParentItem());
91             updateParentItems(treeItem.getParentItem());
 92         }
92         }
 93     }
93     }
 94
94 

 95     /** *//**
95     /** *//**
 96      * The item has expanded. Updates the checked state of its children.
96      * The item has expanded. Updates the checked state of its children. 
 97      */
97      */

 98     private void initializeItem(TreeItem item)
98     private void initializeItem(TreeItem item)  {
{

 99         if (item.getChecked() && !item.getGrayed())
99         if (item.getChecked() && !item.getGrayed())  {
{
 100             updateChildrenItems(item);
100             updateChildrenItems(item);
 101         }
101         }
 102     }
102     }
 103
103 

 104     /** *//**
104     /** *//**
 105      * Updates the check state of all created children
105      * Updates the check state of all created children
 106      */
106      */

 107     protected void updateChildrenItems(TreeItem parent)
107     protected void updateChildrenItems(TreeItem parent)  {
{
 108         Item[] children = getChildren(parent);
108         Item[] children = getChildren(parent);
 109         boolean state = parent.getChecked();
109         boolean state = parent.getChecked();

 110         for (int i = 0; i < children.length; i++)
110         for (int i = 0; i < children.length; i++)  {
{
 111             TreeItem curr = (TreeItem) children[i];
111             TreeItem curr = (TreeItem) children[i];
 112             if (curr.getData() != null
112             if (curr.getData() != null

 113                     && ((curr.getChecked() != state) || curr.getGrayed()))
113                     && ((curr.getChecked() != state) || curr.getGrayed()))  {
{
 114                 curr.setChecked(state);
114                 curr.setChecked(state);
 115                 curr.setGrayed(false);
115                 curr.setGrayed(false);
 116                 updateChildrenItems(curr);
116                 updateChildrenItems(curr);
 117             }
117             }
 118         }
118         }
 119     }
119     }
 120
120 

 121     /** *//**
121     /** *//**
 122      * Updates the check / gray state of all parent items
122      * Updates the check / gray state of all parent items
 123      */
123      */

 124     private void updateParentItems(TreeItem item)
124     private void updateParentItems(TreeItem item)  {
{

 125         if (item != null)
125         if (item != null)  {
{
 126             Item[] children = getChildren(item);
126             Item[] children = getChildren(item);
 127             boolean containsChecked = false;
127             boolean containsChecked = false;
 128             boolean containsUnchecked = false;
128             boolean containsUnchecked = false;

 129             for (int i = 0; i < children.length; i++)
129             for (int i = 0; i < children.length; i++)  {
{
 130                 TreeItem curr = (TreeItem) children[i];
130                 TreeItem curr = (TreeItem) children[i];
 131                 containsChecked |= curr.getChecked();
131                 containsChecked |= curr.getChecked();
 132                 containsUnchecked |= (!curr.getChecked() || curr.getGrayed());
132                 containsUnchecked |= (!curr.getChecked() || curr.getGrayed());
 133             }
133             }
 134             item.setChecked(containsChecked);
134             item.setChecked(containsChecked);
 135             item.setGrayed(containsChecked && containsUnchecked);
135             item.setGrayed(containsChecked && containsUnchecked);
 136             updateParentItems(item.getParentItem());
136             updateParentItems(item.getParentItem());
 137         }
137         }
 138     }
138     }
 139
139 

 140     /**//*
140     /**//*
 141      * @see ICheckable#setChecked(Object, boolean)
141      * @see ICheckable#setChecked(Object, boolean)
 142      */
142      */

 143     public boolean setChecked(Object  element, boolean state)
143     public boolean setChecked(Object  element, boolean state)  {
{

 144         if (super.setChecked(element, state))
144         if (super.setChecked(element, state))  {
{
 145             doCheckStateChanged(element);
145             doCheckStateChanged(element);
 146             return true;
146             return true;
 147         }
147         }
 148         return false;
148         return false;
 149     }
149     }
 150
150 

 151     /**//*
151     /**//*
 152      * @see CheckboxTreeViewer#setCheckedElements(Object[])
152      * @see CheckboxTreeViewer#setCheckedElements(Object[])
 153      */
153      */

 154     public void setCheckedElements(Object [] elements)
154     public void setCheckedElements(Object [] elements)  {
{
 155         super.setCheckedElements(elements);
155         super.setCheckedElements(elements);

 156         for (int i = 0; i < elements.length; i++)
156         for (int i = 0; i < elements.length; i++)  {
{
 157             doCheckStateChanged(elements[i]);
157             doCheckStateChanged(elements[i]);
 158         }
158         }
 159     }
159     }
 160
160 

 161     /**//*
161     /**//*
 162      * @see AbstractTreeViewer#setExpanded(Item, boolean)
162      * @see AbstractTreeViewer#setExpanded(Item, boolean)
 163      */
163      */

 164     protected void setExpanded(Item item, boolean expand)
164     protected void setExpanded(Item item, boolean expand)  {
{
 165         super.setExpanded(item, expand);
165         super.setExpanded(item, expand);

 166         if (expand && item instanceof TreeItem)
166         if (expand && item instanceof TreeItem)  {
{
 167             initializeItem((TreeItem) item);
167             initializeItem((TreeItem) item);
 168         }
168         }
 169     }
169     }
 170
170 

 171     /**//*
171     /**//*
 172      * @see CheckboxTreeViewer#getCheckedElements()
172      * @see CheckboxTreeViewer#getCheckedElements()
 173      */
173      */

 174     public Object [] getCheckedElements()
174     public Object [] getCheckedElements()  {
{
 175         Object [] checked = super.getCheckedElements();
175         Object [] checked = super.getCheckedElements();
 176         // add all items that are children of a checked node but not created yet
176         // add all items that are children of a checked node but not created yet
 177 ArrayList  result = new ArrayList ();
177 ArrayList  result = new ArrayList ();

 178         for (int i = 0; i < checked.length; i++)
178         for (int i = 0; i < checked.length; i++)  {
{
 179             Object  curr = checked[i];
179             Object  curr = checked[i];
 180             result.add(curr);
180             result.add(curr);
 181             Widget item = findItem(curr);
181             Widget item = findItem(curr);

 182             if (item != null)
182             if (item != null)  {
{
 183                 Item[] children = getChildren(item);
183                 Item[] children = getChildren(item);
 184                 // check if contains the dummy node
184                 // check if contains the dummy node

 185 if (children.length == 1 && children[0].getData() == null)
185 if (children.length == 1 && children[0].getData() == null)  {
{
 186                     // not yet created
186                     // not yet created
 187 collectChildren(curr, result);
187 collectChildren(curr, result);
 188                 }
188                 }
 189             }
189             }
 190         }
190         }
 191         return result.toArray();
191         return result.toArray();
 192     }
192     }
 193
193 

 194     private void collectChildren(Object  element, ArrayList  result)
194     private void collectChildren(Object  element, ArrayList  result)  {
{
 195         Object [] filteredChildren = getFilteredChildren(element);
195         Object [] filteredChildren = getFilteredChildren(element);

 196         for (int i = 0; i < filteredChildren.length; i++)
196         for (int i = 0; i < filteredChildren.length; i++)  {
{
 197             Object  curr = filteredChildren[i];
197             Object  curr = filteredChildren[i];
 198             result.add(curr);
198             result.add(curr);
 199             collectChildren(curr, result);
199             collectChildren(curr, result);
 200         }
200         }
 201     }
201     }
 202
202 

 203     protected void createChildren(final Widget widget)
203     protected void createChildren(final Widget widget)  {
{
 204         super.createChildren(widget);
204         super.createChildren(widget);
 205     }
205     }
 206 }
206 }
