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.HashSet ; 16 import java.util.Iterator ; 17 import java.util.List ; 18 import java.util.Map ; 19 import java.util.Set ; 20 import java.util.Map.Entry; 21 22 import org.eclipse.core.resources.IMarker; 23 import org.eclipse.core.resources.IMarkerDelta; 24 import org.eclipse.core.runtime.CoreException; 25 import org.eclipse.core.runtime.IAdaptable; 26 import org.eclipse.debug.core.DebugPlugin; 27 import org.eclipse.debug.core.IBreakpointManager; 28 import org.eclipse.debug.core.IBreakpointsListener; 29 import org.eclipse.debug.core.model.IBreakpoint; 30 import org.eclipse.debug.internal.ui.DebugUIPlugin; 31 import org.eclipse.debug.internal.ui.IInternalDebugUIConstants; 32 import org.eclipse.debug.internal.ui.importexport.breakpoints.IImportExportConstants; 33 import org.eclipse.debug.ui.AbstractBreakpointOrganizerDelegate; 34 import org.eclipse.debug.ui.IBreakpointOrganizerDelegateExtension; 35 import org.eclipse.jface.preference.IPreferenceStore; 36 import org.eclipse.jface.util.IPropertyChangeListener; 37 import org.eclipse.jface.util.PropertyChangeEvent; 38 import org.eclipse.ui.IWorkingSet; 39 import org.eclipse.ui.IWorkingSetManager; 40 import org.eclipse.ui.PlatformUI; 41 42 47 public class BreakpointSetOrganizer extends AbstractBreakpointOrganizerDelegate implements IBreakpointOrganizerDelegateExtension, IPropertyChangeListener, IBreakpointsListener { 48 49 private IWorkingSetManager fWorkingSetManager = PlatformUI.getWorkbench().getWorkingSetManager(); 50 51 55 private BreakpointWorkingSetCache fCache = null; 56 57 private static IWorkingSet fDefaultWorkingSet = null; 59 60 61 65 public BreakpointSetOrganizer() { 66 fWorkingSetManager.addPropertyChangeListener(this); 67 fCache = new BreakpointWorkingSetCache(); 68 DebugUIPlugin.getDefault().getPreferenceStore().addPropertyChangeListener(this); 69 DebugPlugin.getDefault().getBreakpointManager().addBreakpointListener(this); 70 fDefaultWorkingSet = getDefaultWorkingSet(); 71 } 72 73 78 public IAdaptable[] getCategories(IBreakpoint breakpoint) { 79 List result = new ArrayList (); 80 IWorkingSet[] workingSets = fWorkingSetManager.getWorkingSets(); 81 for (int i = 0; i < workingSets.length; i++) { 82 IWorkingSet set = workingSets[i]; 83 if (IInternalDebugUIConstants.ID_BREAKPOINT_WORKINGSET.equals(set.getId())) { 84 IAdaptable[] elements = set.getElements(); 85 for (int j = 0; j < elements.length; j++) { 86 IAdaptable adaptable = elements[j]; 87 if (adaptable.equals(breakpoint)) { 88 result.add(new WorkingSetCategory(set)); 89 break; 90 } 91 } 92 } 93 } 94 return (IAdaptable[]) result.toArray(new IAdaptable[result.size()]); 95 } 96 97 102 public void dispose() { 103 fWorkingSetManager.removePropertyChangeListener(this); 104 fWorkingSetManager = null; 105 DebugPlugin.getDefault().getBreakpointManager().removeBreakpointListener(this); 106 DebugUIPlugin.getDefault().getPreferenceStore().removePropertyChangeListener(this); 107 super.dispose(); 108 } 109 110 115 public void propertyChange(PropertyChangeEvent event) { 116 IWorkingSet set = null; 117 Object newValue = event.getNewValue(); 118 if (newValue instanceof IWorkingSet) { 119 set = (IWorkingSet) newValue; 120 } 121 else if (event.getOldValue() instanceof IWorkingSet) { 122 set = (IWorkingSet) event.getOldValue(); 123 } 124 String property = event.getProperty(); 125 if (property.equals(IWorkingSetManager.CHANGE_WORKING_SET_NAME_CHANGE)) { 127 if (newValue.equals(fDefaultWorkingSet)) { 128 setDefaultWorkingSet((IWorkingSet) newValue); 129 } 130 } 131 if (property.equals(IWorkingSetManager.CHANGE_WORKING_SET_REMOVE)) { 132 if (event.getOldValue().equals(fDefaultWorkingSet)) { 133 setDefaultWorkingSet(null); 134 } 135 } 136 if(property.equals(IWorkingSetManager.CHANGE_WORKING_SET_ADD)) { 137 IAdaptable[] breakpoints = set.getElements(); 138 for (int i = 0; i < breakpoints.length; i++) { 139 if (breakpoints[i] instanceof IBreakpoint) { 140 IMarker marker = ((IBreakpoint)breakpoints[i]).getMarker(); 141 fCache.addEntry(marker, set.getName()); 142 fCache.flushMarkerCache(marker); 143 } 144 } 145 } 146 if (set != null && IInternalDebugUIConstants.ID_BREAKPOINT_WORKINGSET.equals(set.getId())) { 147 fireCategoryChanged(new WorkingSetCategory(set)); 148 } 149 if (property.equals(IInternalDebugUIConstants.MEMENTO_BREAKPOINT_WORKING_SET_NAME)) { 150 IWorkingSet defaultWorkingSet = getDefaultWorkingSet(); 151 if (defaultWorkingSet != null) { 152 fireCategoryChanged(new WorkingSetCategory(defaultWorkingSet)); 153 } else { 154 fireCategoryChanged(null); 155 } 156 } 157 } 158 159 164 public void breakpointsAdded(IBreakpoint[] breakpoints) { 165 Map setToBreakpoints = new HashMap (); 166 for (int i = 0; i < breakpoints.length; i++) { 167 IMarker marker = breakpoints[i].getMarker(); 168 String [] names = getWorkingsetAttributeFromMarker(marker, IInternalDebugUIConstants.WORKING_SET_NAME); 169 if (names.length == 0) { 171 queueToSet(breakpoints[i], getDefaultWorkingSet(), setToBreakpoints); 172 } else { 173 for (int j = 1; j < names.length; j++) { 174 IWorkingSet set = PlatformUI.getWorkbench().getWorkingSetManager().getWorkingSet(names[j]); 175 if (set == null) { 177 set = getDefaultWorkingSet(); 178 } 179 queueToSet(breakpoints[i], set, setToBreakpoints); 180 } 181 } 182 } 183 Iterator iterator = setToBreakpoints.entrySet().iterator(); 184 while (iterator.hasNext()) { 185 Entry entry = (Entry) iterator.next(); 186 IWorkingSet set = (IWorkingSet) entry.getKey(); 187 List list = (List ) entry.getValue(); 188 addBreakpointsToSet((IBreakpoint[]) list.toArray(new IBreakpoint[list.size()]), set); 189 } 190 } 191 192 private void queueToSet(IBreakpoint breakpoint, IWorkingSet set, Map queue) { 193 List list = (List ) queue.get(set); 194 if (list == null) { 195 list = new ArrayList (); 196 queue.put(set, list); 197 } 198 list.add(breakpoint); 199 } 200 201 208 private void addBreakpointsToSet(IBreakpoint[] breakpoints, IWorkingSet set) { 209 if (set != null) { 210 IAdaptable[] elements = set.getElements(); 211 Set collection = new HashSet (elements.length); 212 List list = new ArrayList (elements.length + breakpoints.length); 213 for(int i = 0; i < elements.length; i++) { 214 collection.add(elements[i]); 215 list.add(elements[i]); 216 } 217 for (int i = 0; i < breakpoints.length; i++) { 218 IBreakpoint breakpoint = breakpoints[i]; 219 if (!collection.contains(breakpoint)) { 220 list.add(breakpoint); 221 fCache.addEntry(breakpoint.getMarker(), set.getName()); fCache.flushMarkerCache(breakpoint.getMarker()); 223 } 224 } 225 set.setElements((IAdaptable[]) list.toArray(new IAdaptable[list.size()])); 226 } 227 } 228 229 235 public void breakpointsRemoved(IBreakpoint[] breakpoints, 236 IMarkerDelta[] deltas) { 237 IWorkingSet[] workingSets = fWorkingSetManager.getWorkingSets(); 238 IWorkingSet set = null; 239 for (int i = 0; i < workingSets.length; i++) { 240 set = workingSets[i]; 241 if (IInternalDebugUIConstants.ID_BREAKPOINT_WORKINGSET.equals(set.getId())) { 242 clean(set); 243 } 244 } 245 } 246 247 253 private void clean(IWorkingSet workingSet) { 254 IAdaptable[] elements = workingSet.getElements(); 255 IBreakpointManager manager = DebugPlugin.getDefault().getBreakpointManager(); 256 boolean update = false; 257 for (int i = 0; i < elements.length; i++) { 258 IAdaptable adaptable = elements[i]; 259 if (adaptable instanceof IBreakpoint) { 260 IBreakpoint breakpoint = (IBreakpoint) adaptable; 261 if (!manager.isRegistered(breakpoint)) { 262 update = true; 263 elements[i] = null; 264 } 265 } 266 } 267 if (update) { 268 List newElements = new ArrayList (elements.length); 269 for (int i = 0; i < elements.length; i++) { 270 IAdaptable adaptable = elements[i]; 271 if (adaptable != null) { 272 newElements.add(adaptable); 273 } 274 } 275 workingSet.setElements((IAdaptable[]) newElements.toArray(new IAdaptable[newElements.size()])); 276 } 277 } 278 279 285 public void breakpointsChanged(IBreakpoint[] breakpoints, IMarkerDelta[] deltas) { 286 } 287 288 294 public static IWorkingSet getDefaultWorkingSet() { 295 IPreferenceStore preferenceStore = DebugUIPlugin.getDefault().getPreferenceStore(); 296 String name = preferenceStore.getString(IInternalDebugUIConstants.MEMENTO_BREAKPOINT_WORKING_SET_NAME); 297 if (name != null) { 298 return PlatformUI.getWorkbench().getWorkingSetManager().getWorkingSet(name); 299 } 300 return null; 301 } 302 303 310 public static void setDefaultWorkingSet(IWorkingSet set) { 311 String name = ""; if (set != null) { 313 if (IInternalDebugUIConstants.ID_BREAKPOINT_WORKINGSET.equals(set.getId())) { 315 name = set.getName(); 316 } 317 } 318 fDefaultWorkingSet = set; 319 DebugUIPlugin.getDefault().getPluginPreferences().setValue(IInternalDebugUIConstants.MEMENTO_BREAKPOINT_WORKING_SET_NAME, name); 320 } 321 322 328 public boolean canRemove(IBreakpoint breakpoint, IAdaptable category) { 329 if (category instanceof WorkingSetCategory) { 330 WorkingSetCategory wsc = (WorkingSetCategory) category; 331 return IInternalDebugUIConstants.ID_BREAKPOINT_WORKINGSET.equals(wsc.getWorkingSet().getId()); 332 } 333 return super.canRemove(breakpoint, category); 334 } 335 336 342 public boolean canAdd(IBreakpoint breakpoint, IAdaptable category) { 343 if (category instanceof WorkingSetCategory) { 344 WorkingSetCategory wsc = (WorkingSetCategory) category; 345 return IInternalDebugUIConstants.ID_BREAKPOINT_WORKINGSET.equals(wsc.getWorkingSet().getId()); 346 } 347 return super.canAdd(breakpoint, category); 348 } 349 350 356 public void addBreakpoint(IBreakpoint breakpoint, IAdaptable category) { 357 addBreakpoints(new IBreakpoint[]{breakpoint}, category); 358 } 359 360 369 private String [] getWorkingsetAttributeFromMarker(IMarker marker, String type) { 370 try { 371 String name = (String ) marker.getAttribute(type); 372 if (name != null) { 373 return name.split("\\" + IImportExportConstants.DELIMITER); } 375 } 376 catch (CoreException e) {DebugPlugin.log(e);} 377 return new String [] {}; 378 } 379 380 386 public void removeBreakpoint(IBreakpoint breakpoint, IAdaptable category) { 387 if (category instanceof WorkingSetCategory) { 388 IWorkingSet set = ((WorkingSetCategory) category).getWorkingSet(); 389 IAdaptable[] elements = set.getElements(); 390 List list = new ArrayList (); 391 for (int i = 0; i < elements.length; i++) { 392 IAdaptable adaptable = elements[i]; 393 if (!adaptable.equals(breakpoint)) { 394 list.add(adaptable); 395 } 396 } 397 fCache.removeMappedEntry(breakpoint.getMarker(), set.getName()); 398 fCache.flushMarkerCache(breakpoint.getMarker()); 399 set.setElements((IAdaptable[]) list.toArray(new IAdaptable[list.size()])); 400 } 401 } 402 403 408 public IAdaptable[] getCategories() { 409 IWorkingSet[] workingSets = fWorkingSetManager.getWorkingSets(); 410 List all = new ArrayList (); 411 for (int i = 0; i < workingSets.length; i++) { 412 IWorkingSet set = workingSets[i]; 413 if (IInternalDebugUIConstants.ID_BREAKPOINT_WORKINGSET.equals(set 414 .getId())) { 415 all.add(new WorkingSetCategory(set)); 416 } 417 } 418 return (IAdaptable[]) all.toArray(new IAdaptable[all.size()]); 419 } 420 421 424 public void addBreakpoints(IBreakpoint[] breakpoints, IAdaptable category) { 425 if (category instanceof WorkingSetCategory) { 426 IWorkingSet set = ((WorkingSetCategory) category).getWorkingSet(); 427 addBreakpointsToSet(breakpoints, set); 428 } 429 } 430 431 434 public void removeBreakpoints(IBreakpoint[] breakpoints, IAdaptable category) { 435 if (category instanceof WorkingSetCategory) { 436 IWorkingSet set = ((WorkingSetCategory) category).getWorkingSet(); 437 IAdaptable[] elements = set.getElements(); 438 List list = new ArrayList (elements.length); 439 for (int i = 0; i < elements.length; i++) { 440 list.add(elements[i]); 441 } 442 for (int i = 0; i < breakpoints.length; i++) { 443 IBreakpoint breakpoint = breakpoints[i]; 444 fCache.removeMappedEntry(breakpoint.getMarker(), set.getName()); 445 fCache.flushMarkerCache(breakpoint.getMarker()); 446 list.remove(breakpoint); 447 } 448 set.setElements((IAdaptable[]) list.toArray(new IAdaptable[list.size()])); 449 } 450 } 451 } 452 | Popular Tags |