1 11 package org.eclipse.ltk.internal.ui.refactoring; 12 13 import java.util.ArrayList ; 14 import java.util.Iterator ; 15 import java.util.List ; 16 17 import org.eclipse.swt.SWT; 18 import org.eclipse.swt.widgets.Composite; 19 import org.eclipse.swt.widgets.Item; 20 import org.eclipse.swt.widgets.TreeItem; 21 import org.eclipse.swt.widgets.Widget; 22 23 import org.eclipse.jface.viewers.CheckStateChangedEvent; 24 import org.eclipse.jface.viewers.CheckboxTreeViewer; 25 import org.eclipse.jface.viewers.ICheckStateListener; 26 import org.eclipse.jface.viewers.ISelection; 27 import org.eclipse.jface.viewers.IStructuredSelection; 28 import org.eclipse.jface.viewers.StructuredSelection; 29 import org.eclipse.jface.viewers.Viewer; 30 import org.eclipse.jface.viewers.ViewerFilter; 31 32 class ChangeElementTreeViewer extends CheckboxTreeViewer { 33 34 private static class GroupCategoryFilter extends ViewerFilter { 35 private List fGroupCategories; 36 public void setGroupCategory(List groupCategories) { 37 fGroupCategories= groupCategories; 38 } 39 public boolean select(Viewer viewer, Object parentElement, Object element) { 40 if (fGroupCategories == null) 41 return true; 42 return ((PreviewNode)element).hasOneGroupCategory(fGroupCategories); 43 } 44 } 45 46 private static class DerivedFilter extends ViewerFilter { 47 public boolean select(Viewer viewer, Object parentElement, Object element) { 48 return ! ((PreviewNode) element).hasDerived(); 49 } 50 } 51 52 private static final DerivedFilter DERIVED_FILTER= new DerivedFilter(); 53 54 55 private List fDeferredTreeItemUpdates; 57 58 public ChangeElementTreeViewer(Composite parentComposite) { 59 super(parentComposite, SWT.NONE); 60 addFilter(new GroupCategoryFilter()); 61 addCheckStateListener(new ICheckStateListener() { 62 public void checkStateChanged(CheckStateChangedEvent event){ 63 PreviewNode element= (PreviewNode)event.getElement(); 64 boolean checked= event.getChecked(); 65 66 element.setEnabled(checked); 67 setSubtreeChecked(element, checked); 68 setSubtreeGrayed(element, false); 69 PreviewNode parent= element.getParent(); 70 while(parent != null) { 71 int active= parent.getActive(); 72 parent.setEnabledShallow(active == PreviewNode.PARTLY_ACTIVE || active == PreviewNode.ACTIVE); 73 boolean grayed= (active == PreviewNode.PARTLY_ACTIVE); 74 setChecked(parent, checked ? true : grayed); 75 setGrayed(parent, grayed); 76 parent= parent.getParent(); 77 } 78 } 79 }); 80 } 81 82 public void setGroupCategory(List groupCategories) { 83 ((GroupCategoryFilter)(getFilters()[0])).setGroupCategory(groupCategories); 84 refresh(); 85 } 86 87 public void setHideDerived(boolean hide) { 88 if (hide) { 89 addFilter(DERIVED_FILTER); 90 } else { 91 removeFilter(DERIVED_FILTER); 92 } 93 } 94 95 public void refresh() { 96 try { 97 fDeferredTreeItemUpdates= new ArrayList (); 98 super.refresh(); 99 processDeferredTreeItemUpdates(); 100 } finally { 101 fDeferredTreeItemUpdates= null; 102 } 103 } 104 105 protected void handleInvalidSelection(ISelection invalidSelection, ISelection newSelection) { 106 PreviewNode next= getLeaf((PreviewNode)getInput(), true); 107 if (next != null) { 108 newSelection= new StructuredSelection(next); 109 setSelection(newSelection); 110 } 111 super.handleInvalidSelection(invalidSelection, newSelection); 112 } 113 114 protected void inputChanged(Object input, Object oldInput) { 115 try { 116 fDeferredTreeItemUpdates= new ArrayList (); 117 super.inputChanged(input, oldInput); 118 processDeferredTreeItemUpdates(); 119 } finally { 120 fDeferredTreeItemUpdates= null; 121 } 122 } 123 124 protected void doUpdateItem(Item item, Object element) { 125 super.doUpdateItem(item, element); 126 if (fDeferredTreeItemUpdates == null) { 127 applyCheckedState((TreeItem)item, (PreviewNode)element); 128 } else { 129 fDeferredTreeItemUpdates.add(item); 130 } 131 } 132 133 private void processDeferredTreeItemUpdates() { 134 for (Iterator iter= fDeferredTreeItemUpdates.iterator(); iter.hasNext();) { 135 TreeItem item= (TreeItem)iter.next(); 136 applyCheckedState(item, (PreviewNode)item.getData()); 137 } 138 } 139 140 private void applyCheckedState(TreeItem item, PreviewNode ce) { 141 int state= ce.getActive(); 142 boolean checked= state == PreviewNode.INACTIVE ? false : true; 143 item.setChecked(checked); 144 boolean grayed= state == PreviewNode.PARTLY_ACTIVE ? true : false; 145 item.setGrayed(grayed); 146 } 147 148 protected void revealNext() { 149 revealElement(true); 150 } 151 152 protected void revealPrevious() { 153 revealElement(false); 154 } 155 156 private void setSubtreeGrayed(Object element, boolean grayed) { 157 Widget widget= findItem(element); 158 if (widget instanceof TreeItem) { 159 TreeItem item= (TreeItem)widget; 160 if (item.getGrayed() != grayed) { 161 item.setGrayed(grayed); 162 grayChildren(getChildren(item), grayed); 163 } 164 } 165 } 166 167 private void grayChildren(Item[] items, boolean grayed) { 168 for (int i= 0; i < items.length; i++) { 169 Item element= items[i]; 170 if (element instanceof TreeItem) { 171 TreeItem item= (TreeItem)element; 172 if (item.getGrayed() != grayed) { 173 item.setGrayed(grayed); 174 grayChildren(getChildren(item), grayed); 175 } 176 } 177 } 178 } 179 180 private void revealElement(boolean next) { 181 PreviewNode current= (PreviewNode)getInput(); 182 IStructuredSelection selection= (IStructuredSelection)getSelection(); 183 if (!selection.isEmpty()) 184 current= (PreviewNode)selection.iterator().next(); 185 186 PreviewNode candidate= getLeaf(current, next); 187 if (candidate == null) { 188 candidate= getElement(current, next); 189 if (candidate != null) { 190 PreviewNode leaf= getLeaf(candidate, next); 191 if (leaf != null) 192 candidate= leaf; 193 } 194 } 195 if (candidate != null) 196 setSelection(new StructuredSelection(candidate), true); 197 else 198 getControl().getDisplay().beep(); 199 } 200 201 private PreviewNode getLeaf(PreviewNode element, boolean first) { 202 PreviewNode result= null; 203 PreviewNode[] children= getSortedChildrenAsPreviewNodes(element); 204 while(children != null && children.length > 0) { 205 result= children[first ? 0 : children.length - 1]; 206 children= getSortedChildrenAsPreviewNodes(result); 207 } 208 return result; 209 } 210 211 private PreviewNode getElement(PreviewNode element, boolean next) { 212 while(true) { 213 PreviewNode parent= element.getParent(); 214 if (parent == null) 215 return null; 216 PreviewNode candidate= getSibling( 217 getSortedChildrenAsPreviewNodes(parent), 218 element, next); 219 if (candidate != null) 220 return candidate; 221 element= parent; 222 } 223 } 224 225 private PreviewNode getSibling(PreviewNode[] children, PreviewNode element, boolean next) { 226 for (int i= 0; i < children.length; i++) { 227 if (children[i] == element) { 228 if (next) 229 if (i < children.length - 1) 230 return children[i + 1]; 231 else 232 return null; 233 else 234 if (i > 0) 235 return children[i - 1]; 236 else 237 return null; 238 } 239 } 240 return null; 241 } 242 243 private PreviewNode[] getSortedChildrenAsPreviewNodes(PreviewNode parent) { 244 Object [] sorted= getSortedChildren(parent); 245 PreviewNode[] result= new PreviewNode[sorted.length]; 246 for (int i= 0; i < result.length; i++) { 247 result[i]= (PreviewNode)sorted[i]; 248 } 249 return result; 250 } 251 } 252 | Popular Tags |