1 11 package org.eclipse.ui.internal.dialogs; 12 13 14 import java.util.ArrayList ; 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 36 public class ContainerCheckedTreeViewer extends CheckboxTreeViewer { 37 38 42 public ContainerCheckedTreeViewer(Composite parent) { 43 super(parent); 44 initViewer(); 45 } 46 47 51 public ContainerCheckedTreeViewer(Composite parent, int style) { 52 super(parent, style); 53 initViewer(); 54 } 55 56 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 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 97 private void initializeItem(TreeItem item) { 98 if (item.getChecked() && !item.getGrayed()) { 99 updateChildrenItems((TreeItem) item); 100 } 101 } 102 103 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 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 141 public boolean setChecked(Object element, boolean state) { 142 if (super.setChecked(element, state)) { 143 doCheckStateChanged(element); 144 return true; 145 } 146 return false; 147 } 148 149 152 public void setCheckedElements(Object [] elements) { 153 super.setCheckedElements(elements); 154 for (int i= 0; i < elements.length; i++) { 155 doCheckStateChanged(elements[i]); 156 } 157 } 158 159 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 172 public Object [] getCheckedElements() { 173 Object [] checked= super.getCheckedElements(); 174 ArrayList result= new ArrayList (); 176 for (int i= 0; i < checked.length; i++) { 177 Object curr= checked[i]; 178 result.add(curr); 179 Widget item= findItem(curr); 180 if (item != null) { 181 Item[] children= getChildren(item); 182 if (children.length == 1 && children[0].getData() == null) { 184 collectChildren(curr, result); 186 } 187 } 188 } 189 return result.toArray(); 190 } 191 192 private void collectChildren(Object element, ArrayList result) { 193 Object [] filteredChildren= getFilteredChildren(element); 194 for (int i= 0; i < filteredChildren.length; i++) { 195 Object curr= filteredChildren[i]; 196 result.add(curr); 197 collectChildren(curr, result); 198 } 199 } 200 201 } 202 | Popular Tags |