1 11 package org.eclipse.update.internal.ui.wizards; 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 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 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 98 private void initializeItem(TreeItem item) { 99 if (item.getChecked() && !item.getGrayed()) { 100 updateChildrenItems(item); 101 } 102 } 103 104 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 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 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 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 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 174 public Object [] getCheckedElements() { 175 Object [] checked = super.getCheckedElements(); 176 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 if (children.length == 1 && children[0].getData() == null) { 186 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 } 207 | Popular Tags |