1 11 package org.eclipse.debug.internal.ui.launchConfigurations; 12 13 import java.util.ArrayList ; 14 import java.util.Iterator ; 15 16 import org.eclipse.jface.viewers.IStructuredSelection; 17 import org.eclipse.jface.viewers.StructuredSelection; 18 import org.eclipse.jface.viewers.TreePath; 19 import org.eclipse.jface.viewers.TreeViewer; 20 import org.eclipse.swt.widgets.Composite; 21 import org.eclipse.swt.widgets.Tree; 22 import org.eclipse.swt.widgets.TreeItem; 23 import org.eclipse.swt.widgets.Widget; 24 25 30 public class LaunchConfigurationViewer extends TreeViewer { 31 32 private int fTotalCount = 0; 33 private LaunchConfigurationView fView = null; 34 35 39 public LaunchConfigurationViewer(Tree tree) { 40 super(tree); 41 } 42 43 48 public LaunchConfigurationViewer(Composite parent, int style) { 49 this(new Tree(parent, style)); 50 } 51 52 55 protected void preservingSelection(Runnable updateCode) { 56 IStructuredSelection selection = (IStructuredSelection) getSelection(); 57 if(!selection.isEmpty()) { 58 int[] indices = collectIndices(selection.getFirstElement()); 59 updateCode.run(); 60 ArrayList set = new ArrayList (); 61 Object o = null; 62 for(Iterator iter = selection.iterator(); iter.hasNext();) { 63 o = iter.next(); 64 if(internalGetWidgetToSelect(o) != null) { 65 if(!set.contains(o)) { 66 set.add(o); 67 } 68 } 69 } 70 if(set.isEmpty()) { 71 Tree tree = getTree(); 73 if(tree.getItemCount() > 0) { 74 int index = selectIndex(tree.getItemCount(), indices[0]); 75 if(index > -1) { 76 TreeItem pitem = null; 77 if(indices[0] > tree.getItemCount()-1) { 78 pitem = tree.getItem(tree.getItemCount()-1); 79 } 80 else { 81 pitem = tree.getItem(indices[0]); 82 } 83 if(pitem != null) { 84 o = pitem.getData(); 85 if(indices[1] > -1) { 86 index = selectIndex(pitem.getItemCount(), indices[1]); 87 if(index > -1) { 88 Object d = pitem.getItem(index).getData(); 89 if(d != null) { 90 o = d; 91 } 92 } 93 else { 94 if(pitem.getItemCount() > 0) { 95 o = pitem.getItem((indices[1]-1 > -1 ? indices[1]-1 : 0)).getData(); 96 if(o == null) { 97 o = pitem.getData(); 98 } 99 } 100 } 101 } 102 } 103 } 104 if(!set.contains(o)) { 105 set.add(o); 106 } 107 } 108 } 109 setSelection(new StructuredSelection(set), true); 110 } 111 else { 112 super.preservingSelection(updateCode); 113 } 114 getTree().getHorizontalBar().setSelection(0); 115 } 116 117 123 private int selectIndex(int count, int index) { 124 if(index < count) { 125 return index; 126 } 127 if(index > count-1) { 128 return count-1; 129 } 130 if(index < 0) { 131 return 0; 132 } 133 return -1; 134 } 135 136 141 protected int getTotalChildCount() { 142 return fTotalCount; 143 } 144 145 148 public void remove(Object elementsOrTreePaths) { 149 super.remove(elementsOrTreePaths); 150 fTotalCount--; 151 } 152 153 156 protected void internalAdd(Widget widget, Object parentElement, Object [] childElements) { 157 super.internalAdd(widget, parentElement, childElements); 158 fTotalCount++; 159 } 160 161 164 protected void inputChanged(Object input, Object oldInput) { 165 super.inputChanged(input, oldInput); 166 LaunchConfigurationTreeContentProvider cp = (LaunchConfigurationTreeContentProvider) getContentProvider(); 168 Object [] types = cp.getElements(null); 169 LaunchGroupFilter filter = new LaunchGroupFilter(((LaunchConfigurationsDialog)LaunchConfigurationsDialog.getCurrentlyVisibleLaunchConfigurationDialog()).getLaunchGroup()); 170 for(int i = 0; i < types.length; i++) { 171 if(filter.select(this, types[i], null)) { 172 fTotalCount += cp.getChildren(types[i]).length + 1; } 174 } 175 } 176 177 184 protected int getNonFilteredChildCount() { 185 int count = 0; 186 getTree().setRedraw(false); 187 TreeItem[] items = getTree().getItems(); 188 count += items.length; 189 boolean expanded = false; 190 TreeItem item = null; 191 for(int i = 0; i < items.length; i++) { 192 item = items[i]; 193 expanded = item.getExpanded(); 194 setExpandedState(item.getData(), true); 195 count += item.getItems().length; 196 item.setExpanded(expanded); 197 } 198 getTree().setRedraw(true); 199 return count; 200 } 201 202 207 private int[] collectIndices(Object object) { 208 int[] indices = {-1, -1}; 209 if(object != null) { 210 TreeItem item = (TreeItem) findItem(object); 211 if(item != null) { 212 TreePath path = getTreePathFromItem(item); 213 item = (TreeItem) findItem(path.getFirstSegment()); 214 if(item != null) { 215 indices[0] = getTree().indexOf(item); 216 if(path.getSegmentCount() == 2) { 217 indices[1] = indexOf(item.getItems(), path.getLastSegment()); 218 } 219 } 220 } 221 } 222 return indices; 223 } 224 225 231 private int indexOf(TreeItem[] items, Object object) { 232 if(object != null) { 233 for(int i = 0; i < items.length; i++) { 234 if(object.equals(items[i].getData())) { 235 return i; 236 } 237 } 238 } 239 return -1; 240 } 241 242 245 protected void filterChanged() { 246 if (fView != null) { 247 fView.updateFilterLabel(); 248 } 249 250 } 251 252 255 protected void setLaunchConfigurationView(LaunchConfigurationView launchConfigurationView) { 256 fView = launchConfigurationView; 257 } 258 } 259 | Popular Tags |