kooyee ‘s blog

          開源軟件, 眾人努力的結晶, 全人類的共同財富
          posts - 103, comments - 55, trackbacks - 0, articles - 66
             :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          『SWT』Get all children from TreeView

          Posted on 2008-06-12 19:26 kooyee 閱讀(1555) 評論(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 }


          評論

          # re: 『SWT』Get all children from TreeView  回復  更多評論   

          2009-03-10 16:01 by fy_kenny
          你有沒有發現,當打開一個CheckboxTreeViewer的同時,有對很多節點進行setChecked時(比如2000個節點),這樣就需要很長時間才能夠勾選完成.

          這個問題你遇到過嗎? 怎么去解決呢?

          # re: 『SWT』Get all children from TreeView  回復  更多評論   

          2009-03-29 02:35 by javawind
          我是用線程,然后在后臺處理的。

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 黔西县| 宜兰县| 墨竹工卡县| 密山市| 团风县| 兰州市| 雅江县| 繁昌县| 麻城市| 方正县| 吴江市| 吉隆县| 辉县市| 承德县| 澄迈县| 当涂县| 巴青县| 漳州市| 垫江县| 陵川县| 横峰县| 泽普县| 大名县| 德安县| 博爱县| 绥德县| 普兰店市| 东乡县| 龙川县| 康马县| 临泽县| 靖远县| 新源县| 乐都县| 舟曲县| 天门市| 绍兴市| 辽中县| 庄浪县| 广州市| 泰兴市|