1 11 package org.eclipse.ui.dialogs; 12 13 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 34 public class ContainerCheckedTreeViewer extends CheckboxTreeViewer { 35 36 40 public ContainerCheckedTreeViewer(Composite parent) { 41 super(parent); 42 initViewer(); 43 } 44 45 49 public ContainerCheckedTreeViewer(Composite parent, int style) { 50 super(parent, style); 51 initViewer(); 52 } 53 54 58 public ContainerCheckedTreeViewer(Tree tree) { 59 super(tree); 60 initViewer(); 61 } 62 63 private void initViewer() { 64 setUseHashlookup(true); 65 addCheckStateListener(new ICheckStateListener() { 66 public void checkStateChanged(CheckStateChangedEvent event) { 67 doCheckStateChanged(event.getElement()); 68 } 69 }); 70 addTreeListener(new ITreeViewerListener() { 71 public void treeCollapsed(TreeExpansionEvent event) { 72 } 73 74 public void treeExpanded(TreeExpansionEvent event) { 75 Widget item = findItem(event.getElement()); 76 if (item instanceof TreeItem) { 77 initializeItem((TreeItem) item); 78 } 79 } 80 }); 81 } 82 83 87 protected void doCheckStateChanged(Object element) { 88 Widget item = findItem(element); 89 if (item instanceof TreeItem) { 90 TreeItem treeItem = (TreeItem) item; 91 treeItem.setGrayed(false); 92 updateChildrenItems(treeItem); 93 updateParentItems(treeItem.getParentItem()); 94 } 95 } 96 97 100 private void initializeItem(TreeItem item) { 101 if (item.getChecked() && !item.getGrayed()) { 102 updateChildrenItems(item); 103 } 104 } 105 106 109 private void updateChildrenItems(TreeItem parent) { 110 Item[] children = getChildren(parent); 111 boolean state = parent.getChecked(); 112 for (int i = 0; i < children.length; i++) { 113 TreeItem curr = (TreeItem) children[i]; 114 if (curr.getData() != null 115 && ((curr.getChecked() != state) || curr.getGrayed())) { 116 curr.setChecked(state); 117 curr.setGrayed(false); 118 updateChildrenItems(curr); 119 } 120 } 121 } 122 123 126 private void updateParentItems(TreeItem item) { 127 if (item != null) { 128 Item[] children = getChildren(item); 129 boolean containsChecked = false; 130 boolean containsUnchecked = false; 131 for (int i = 0; i < children.length; i++) { 132 TreeItem curr = (TreeItem) children[i]; 133 containsChecked |= curr.getChecked(); 134 containsUnchecked |= (!curr.getChecked() || curr.getGrayed()); 135 } 136 item.setChecked(containsChecked); 137 item.setGrayed(containsChecked && containsUnchecked); 138 updateParentItems(item.getParentItem()); 139 } 140 } 141 142 143 146 public boolean setChecked(Object element, boolean state) { 147 if (super.setChecked(element, state)) { 148 doCheckStateChanged(element); 149 return true; 150 } 151 return false; 152 } 153 154 155 158 public void setCheckedElements(Object [] elements) { 159 super.setCheckedElements(elements); 160 for (int i = 0; i < elements.length; i++) { 161 doCheckStateChanged(elements[i]); 162 } 163 } 164 165 166 169 protected void setExpanded(Item item, boolean expand) { 170 super.setExpanded(item, expand); 171 if (expand && item instanceof TreeItem) { 172 initializeItem((TreeItem) item); 173 } 174 } 175 176 177 180 public Object [] getCheckedElements() { 181 Object [] checked = super.getCheckedElements(); 182 ArrayList result = new ArrayList (); 184 for (int i = 0; i < checked.length; i++) { 185 Object curr = checked[i]; 186 result.add(curr); 187 Widget item = findItem(curr); 188 if (item != null) { 189 Item[] children = getChildren(item); 190 if (children.length == 1 && children[0].getData() == null) { 192 collectChildren(curr, result); 194 } 195 } 196 } 197 return result.toArray(); 198 } 199 200 205 private void collectChildren(Object element, ArrayList result) { 206 Object [] filteredChildren = getFilteredChildren(element); 207 for (int i = 0; i < filteredChildren.length; i++) { 208 Object curr = filteredChildren[i]; 209 result.add(curr); 210 collectChildren(curr, result); 211 } 212 } 213 214 } 215 | Popular Tags |