1 11 package org.eclipse.debug.internal.ui.views.breakpoints; 12 13 import java.util.ArrayList ; 14 import java.util.HashMap ; 15 import java.util.Iterator ; 16 import java.util.List ; 17 import java.util.Map ; 18 import java.util.Map.Entry; 19 20 import org.eclipse.core.runtime.CoreException; 21 import org.eclipse.debug.core.DebugPlugin; 22 import org.eclipse.debug.core.IBreakpointManager; 23 import org.eclipse.debug.core.model.IBreakpoint; 24 import org.eclipse.debug.internal.ui.DebugUIPlugin; 25 import org.eclipse.debug.ui.IBreakpointOrganizerDelegateExtension; 26 import org.eclipse.jface.viewers.CheckboxTreeViewer; 27 import org.eclipse.jface.viewers.IStructuredSelection; 28 import org.eclipse.jface.viewers.ITreeContentProvider; 29 import org.eclipse.jface.viewers.TreePath; 30 import org.eclipse.swt.widgets.Item; 31 import org.eclipse.swt.widgets.Tree; 32 import org.eclipse.swt.widgets.TreeItem; 33 import org.eclipse.swt.widgets.Widget; 34 35 38 public class BreakpointsViewer extends CheckboxTreeViewer { 39 40 45 public BreakpointsViewer(Tree tree) { 46 super(tree); 47 } 48 49 54 public Item[] getSelectedItems() { 55 return getSelection(getControl()); 56 } 57 58 64 public Widget searchItem(Object element) { 65 return findItem(element); 66 } 67 68 73 public void refreshItem(TreeItem item) { 74 updateItem(item, item.getData()); 75 } 76 77 82 public IBreakpoint[] getVisibleBreakpoints() { 83 IBreakpointManager manager= DebugPlugin.getDefault().getBreakpointManager(); 84 Object [] elements= ((ITreeContentProvider)getContentProvider()).getElements(manager); 85 List list = new ArrayList (); 86 for (int i = 0; i < elements.length; i++) { 87 TreeItem item = (TreeItem) searchItem(elements[i]); 88 if (item != null) { 89 collectExpandedBreakpoints(item, list); 90 } 91 } 92 return (IBreakpoint[]) list.toArray(new IBreakpoint[list.size()]); 93 } 94 95 102 private void collectExpandedBreakpoints(TreeItem item, List list) { 103 Object data = item.getData(); 104 if (data instanceof IBreakpoint) { 105 list.add(data); 106 return; 107 } 108 if (item.getExpanded()) { 109 TreeItem[] items = item.getItems(); 110 for (int i = 0; i < items.length; i++) { 111 collectExpandedBreakpoints(items[i], list); 112 } 113 } 114 } 115 116 121 protected void setSelection(TreeItem item) { 122 getTree().setSelection(new TreeItem[]{item}); 123 updateSelection(getSelection()); 124 } 125 126 132 public BreakpointContainer getRemovableContainer(Item item) { 133 if(item == null) { 134 return null; 135 } 136 if(item.getData() instanceof IBreakpoint) { 137 TreePath path = getTreePathFromItem(item); 138 if(path != null) { 139 IBreakpoint breakpoint = (IBreakpoint) path.getLastSegment(); 140 BreakpointContainer container = null; 141 for(int i = path.getSegmentCount()-2; i > -1; i--) { 142 container = (BreakpointContainer) path.getSegment(i); 143 if(container.contains(breakpoint) && container.getOrganizer().canRemove(breakpoint, container.getCategory())) { 144 return container; 145 } 146 } 147 } 148 } 149 return null; 150 } 151 152 158 public BreakpointContainer getAddableContainer(Item item) { 159 TreePath path = getTreePathFromItem(item); 160 if(path != null) { 161 Object element = path.getLastSegment(); 162 if(element instanceof IBreakpoint) { 163 BreakpointContainer container = null; 164 IBreakpoint breakpoint = (IBreakpoint) element; 165 for(int i = path.getSegmentCount()-2; i > -1; i--) { 166 container = (BreakpointContainer) path.getSegment(i); 167 if(container.contains(breakpoint) && container.getOrganizer().canAdd(breakpoint, container.getCategory())) { 168 return container; 169 } 170 } 171 } 172 } 173 return null; 174 } 175 176 189 public boolean canDrag(Item[] items) { 190 if(items == null) { 191 return false; 192 } 193 if(items.length == 0) { 194 return false; 195 } 196 for(int i = 0; i < items.length; i++) { 197 if(getRemovableContainer(items[i]) == null) { 198 return false; 199 } 200 } 201 return true; 202 } 203 204 209 public void performDrag(Item[] items) { 210 if(items == null) { 211 return; 212 } 213 Map containersToBreakpoints = new HashMap (); 214 BreakpointContainer container = null; 215 IBreakpoint breakpoint = null; 216 for(int i = 0; i < items.length; i++) { 217 if(!items[i].isDisposed()) { 218 breakpoint = (IBreakpoint)items[i].getData(); 219 container = getRemovableContainer(items[i]); 220 if(container != null) { 221 List list = (List ) containersToBreakpoints.get(container); 222 if (list == null) { 223 list = new ArrayList (); 224 containersToBreakpoints.put(container, list); 225 } 226 list.add(breakpoint); 227 } 228 } 229 } 230 Iterator iterator = containersToBreakpoints.entrySet().iterator(); 231 while (iterator.hasNext()) { 232 Entry entry = (Entry) iterator.next(); 233 container = (BreakpointContainer) entry.getKey(); 234 List list = (List ) entry.getValue(); 235 IBreakpointOrganizer organizer = container.getOrganizer(); 236 IBreakpoint[] breakpoints = (IBreakpoint[]) list.toArray(new IBreakpoint[list.size()]); 237 if (organizer instanceof IBreakpointOrganizerDelegateExtension) { 238 IBreakpointOrganizerDelegateExtension extension = (IBreakpointOrganizerDelegateExtension) organizer; 239 extension.removeBreakpoints(breakpoints, container.getCategory()); 240 } else { 241 for (int i = 0; i < breakpoints.length; i++) { 242 organizer.removeBreakpoint(breakpoints[i], container.getCategory()); 243 } 244 } 245 } 246 } 247 248 262 public boolean canDrop(Item target, IStructuredSelection selection) { 263 if(selection == null || target == null) { 264 return false; 265 } 266 for(Iterator iter = selection.iterator(); iter.hasNext();) { 267 if(!checkAddableParentContainers(target, (IBreakpoint) iter.next())) { 268 return false; 269 } 270 } 271 return true; 272 } 273 274 284 private boolean checkAddableParentContainers(Item target, IBreakpoint breakpoint) { 285 BreakpointContainer container = null; 286 TreePath path = getTreePathFromItem(target); 287 if(path != null) { 288 Object element = null; 289 for(int i = path.getSegmentCount()-1; i > -1; i--) { 290 element = path.getSegment(i); 291 if(element instanceof BreakpointContainer) { 292 container = (BreakpointContainer) element; 293 if(container.contains(breakpoint) || !container.getOrganizer().canAdd(breakpoint, container.getCategory())) { 294 return false; 295 } 296 } 297 } 298 } 299 return true; 300 } 301 302 309 public boolean performDrop(Item target, IStructuredSelection selection) { 310 if(target == null || selection == null) { 311 return false; 312 } 313 IBreakpoint breakpoint = null; 314 Object element = target.getData(); 315 BreakpointContainer container = (element instanceof BreakpointContainer ? (BreakpointContainer)element : getAddableContainer(target)); 316 if(container == null) { 317 return false; 318 } 319 IBreakpointOrganizer organizer = container.getOrganizer(); 320 if (organizer instanceof IBreakpointOrganizerDelegateExtension) { 321 IBreakpointOrganizerDelegateExtension extension = (IBreakpointOrganizerDelegateExtension) organizer; 322 Object [] array = selection.toArray(); 323 IBreakpoint[] breakpoints = new IBreakpoint[array.length]; 324 System.arraycopy(array, 0, breakpoints, 0, array.length); 325 extension.addBreakpoints(breakpoints, container.getCategory()); 326 } else { 327 for(Iterator iter = selection.iterator(); iter.hasNext();) { 328 breakpoint = (IBreakpoint) iter.next(); 329 organizer.addBreakpoint(breakpoint, container.getCategory()); 330 } 331 } 332 expandToLevel(target.getData(), ALL_LEVELS); 333 return true; 334 } 335 336 339 public void refresh() { 340 super.refresh(); 341 initializeCheckedState(); 342 } 343 344 347 private void initializeCheckedState() { 348 TreeItem[] items = getTree().getItems(); 349 for (int i = 0; i < items.length; i++) { 350 updateCheckedState(items[i]); 351 } 352 } 353 354 359 public void updateCheckedState(Object element) { 360 Widget[] widgets = searchItems(element); 361 for (int i = 0; i < widgets.length; i++) { 362 Widget widget = widgets[i]; 363 if (widget != null) { 364 updateCheckedState((TreeItem)widget); 365 } 366 } 367 } 368 369 374 private Widget[] searchItems(Object element) { 375 ArrayList list = new ArrayList (); 376 TreeItem[] items = getTree().getItems(); 377 for (int i = 0; i < items.length; i++) { 378 findAllOccurrences(items[i], element, list); 379 } 380 return (Widget[]) list.toArray(new Widget[0]); 381 } 382 383 389 private void findAllOccurrences(TreeItem item, Object element, ArrayList list) { 390 if (element.equals(item.getData())) { 391 list.add(item); 392 } 393 TreeItem[] items = item.getItems(); 394 for (int i = 0; i < items.length; i++) { 395 findAllOccurrences(items[i], element, list); 396 } 397 } 398 399 404 public void updateCheckedState(TreeItem item) { 405 Object element = item.getData(); 406 if (element instanceof IBreakpoint) { 407 try { 408 item.setChecked(((IBreakpoint) element).isEnabled()); 409 refreshItem(item); 410 } catch (CoreException e) { 411 DebugUIPlugin.log(e); 412 } 413 } else if (element instanceof BreakpointContainer) { 414 IBreakpoint[] breakpoints = ((BreakpointContainer) element).getBreakpoints(); 415 int enabledChildren= 0; 416 for (int i = 0; i < breakpoints.length; i++) { 417 IBreakpoint breakpoint = breakpoints[i]; 418 try { 419 if (breakpoint.isEnabled()) { 420 enabledChildren++; 421 } 422 } catch (CoreException e) { 423 DebugUIPlugin.log(e); 424 } 425 } 426 if (enabledChildren == 0) { 427 item.setGrayed(false); 429 item.setChecked(false); 430 } else if (enabledChildren == breakpoints.length) { 431 item.setGrayed(false); 433 item.setChecked(true); 434 } else { 435 item.setGrayed(true); 437 item.setChecked(true); 438 } 439 TreeItem[] items = item.getItems(); 441 for (int i = 0; i < items.length; i++) { 442 updateCheckedState(items[i]); 443 } 444 } 445 } 446 } 447 | Popular Tags |