KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > ui > views > breakpoints > BreakpointSetOrganizer


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.debug.internal.ui.views.breakpoints;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.HashSet JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.List JavaDoc;
18 import java.util.Map JavaDoc;
19 import java.util.Set JavaDoc;
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 /**
43  * Breakpoint organizers for breakpoint working sets.
44  *
45  * @since 3.1
46  */

47 public class BreakpointSetOrganizer extends AbstractBreakpointOrganizerDelegate implements IBreakpointOrganizerDelegateExtension, IPropertyChangeListener, IBreakpointsListener {
48
49     private IWorkingSetManager fWorkingSetManager = PlatformUI.getWorkbench().getWorkingSetManager();
50     
51     /**
52      * A cache for mapping markers to the working set they belong to
53      * @since 3.2
54      */

55     private BreakpointWorkingSetCache fCache = null;
56     
57     // Cache of the default working set, so we can know when it changes name
58
private static IWorkingSet fDefaultWorkingSet = null;
59     
60     
61     /**
62      * Constructs a working set breakpoint organizer. Listens for changes in
63      * working sets and fires property change notification.
64      */

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     /*
74      * (non-Javadoc)
75      *
76      * @see org.eclipse.debug.ui.IBreakpointOrganizerDelegate#getCategories(org.eclipse.debug.core.model.IBreakpoint)
77      */

78     public IAdaptable[] getCategories(IBreakpoint breakpoint) {
79         List JavaDoc result = new ArrayList JavaDoc();
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     /*
98      * (non-Javadoc)
99      *
100      * @see org.eclipse.debug.ui.IBreakpointOrganizerDelegate#dispose()
101      */

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     /*
111      * (non-Javadoc)
112      *
113      * @see org.eclipse.jface.util.IPropertyChangeListener#propertyChange(org.eclipse.jface.util.PropertyChangeEvent)
114      */

115     public void propertyChange(PropertyChangeEvent event) {
116         IWorkingSet set = null;
117         Object JavaDoc 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 JavaDoc property = event.getProperty();
125         //fix for bug 103731
126
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     /*
160      * (non-Javadoc)
161      *
162      * @see org.eclipse.debug.core.IBreakpointsListener#breakpointsAdded(org.eclipse.debug.core.model.IBreakpoint[])
163      */

164     public void breakpointsAdded(IBreakpoint[] breakpoints) {
165         Map JavaDoc setToBreakpoints = new HashMap JavaDoc();
166         for (int i = 0; i < breakpoints.length; i++) {
167             IMarker marker = breakpoints[i].getMarker();
168             String JavaDoc[] names = getWorkingsetAttributeFromMarker(marker, IInternalDebugUIConstants.WORKING_SET_NAME);
169             //add it to the default set if the listing is empty
170
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 we cannot find the one we want, try to get the default
176
if (set == null) {
177                         set = getDefaultWorkingSet();
178                     }
179                     queueToSet(breakpoints[i], set, setToBreakpoints);
180                 }
181             }
182         }
183         Iterator JavaDoc iterator = setToBreakpoints.entrySet().iterator();
184         while (iterator.hasNext()) {
185             Entry entry = (Entry) iterator.next();
186             IWorkingSet set = (IWorkingSet) entry.getKey();
187             List JavaDoc list = (List JavaDoc) entry.getValue();
188             addBreakpointsToSet((IBreakpoint[]) list.toArray(new IBreakpoint[list.size()]), set);
189         }
190     }
191     
192     private void queueToSet(IBreakpoint breakpoint, IWorkingSet set, Map JavaDoc queue) {
193         List JavaDoc list = (List JavaDoc) queue.get(set);
194         if (list == null) {
195             list = new ArrayList JavaDoc();
196             queue.put(set, list);
197         }
198         list.add(breakpoint);
199     }
200     
201     /**
202      * Adds a breakpoint to a working set
203      * @param breakpoint the breakpoint to add
204      * @param set the set to add it to or <code>null</code> if none
205      *
206      * @since 3.2
207      */

208     private void addBreakpointsToSet(IBreakpoint[] breakpoints, IWorkingSet set) {
209         if (set != null) {
210             IAdaptable[] elements = set.getElements();
211             Set JavaDoc collection = new HashSet JavaDoc(elements.length);
212             List JavaDoc list = new ArrayList JavaDoc(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()); //fix for bug 103731
222
fCache.flushMarkerCache(breakpoint.getMarker());
223                 }
224             }
225             set.setElements((IAdaptable[]) list.toArray(new IAdaptable[list.size()]));
226         }
227     }
228     
229     /*
230      * (non-Javadoc)
231      *
232      * @see org.eclipse.debug.core.IBreakpointsListener#breakpointsRemoved(org.eclipse.debug.core.model.IBreakpoint[],
233      * org.eclipse.core.resources.IMarkerDelta[])
234      */

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     /**
248      * Removes deleted breakpoints from the given working set.
249      *
250      * @param workingSet
251      * breakpoint working set
252      */

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 JavaDoc newElements = new ArrayList JavaDoc(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     /*
280      * (non-Javadoc)
281      *
282      * @see org.eclipse.debug.core.IBreakpointsListener#breakpointsChanged(org.eclipse.debug.core.model.IBreakpoint[],
283      * org.eclipse.core.resources.IMarkerDelta[])
284      */

285     public void breakpointsChanged(IBreakpoint[] breakpoints, IMarkerDelta[] deltas) {
286     }
287
288     /**
289      * Returns the active default breakpoint working set, or <code>null</code>
290      * if none.
291      *
292      * @return the active default breakpoint working set, or <code>null</code>
293      */

294     public static IWorkingSet getDefaultWorkingSet() {
295         IPreferenceStore preferenceStore = DebugUIPlugin.getDefault().getPreferenceStore();
296         String JavaDoc 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     /**
304      * Sets the active default breakpoint working set, or <code>null</code> if
305      * none.
306      *
307      * @param set
308      * default working set or <code>null</code>
309      */

310     public static void setDefaultWorkingSet(IWorkingSet set) {
311         String JavaDoc name = ""; //$NON-NLS-1$
312
if (set != null) {
313             // only consider breakpoint working sets
314
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     /*
323      * (non-Javadoc)
324      *
325      * @see org.eclipse.debug.ui.IBreakpointOrganizerDelegate#canRemove(org.eclipse.debug.core.model.IBreakpoint,
326      * org.eclipse.core.runtime.IAdaptable)
327      */

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     /*
337      * (non-Javadoc)
338      *
339      * @see org.eclipse.debug.ui.IBreakpointOrganizerDelegate#canAdd(org.eclipse.debug.core.model.IBreakpoint,
340      * org.eclipse.core.runtime.IAdaptable)
341      */

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     /*
351      * (non-Javadoc)
352      *
353      * @see org.eclipse.debug.ui.IBreakpointOrganizerDelegate#addBreakpoint(org.eclipse.debug.core.model.IBreakpoint,
354      * org.eclipse.core.runtime.IAdaptable)
355      */

356     public void addBreakpoint(IBreakpoint breakpoint, IAdaptable category) {
357         addBreakpoints(new IBreakpoint[]{breakpoint}, category);
358     }
359     
360     /**
361      * Gets the working set names from the marker
362      *
363      * @param marker
364      * them marker to get the names from
365      * @return the listing of markers or an empty String array, never null
366      *
367      * @since 3.2
368      */

369     private String JavaDoc[] getWorkingsetAttributeFromMarker(IMarker marker, String JavaDoc type) {
370         try {
371             String JavaDoc name = (String JavaDoc) marker.getAttribute(type);
372             if (name != null) {
373                 return name.split("\\" + IImportExportConstants.DELIMITER); //$NON-NLS-1$
374
}
375         }
376         catch (CoreException e) {DebugPlugin.log(e);}
377         return new String JavaDoc[] {};
378     }
379
380     /*
381      * (non-Javadoc)
382      *
383      * @see org.eclipse.debug.ui.IBreakpointOrganizerDelegate#removeBreakpoint(org.eclipse.debug.core.model.IBreakpoint,
384      * org.eclipse.core.runtime.IAdaptable)
385      */

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 JavaDoc list = new ArrayList JavaDoc();
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     /*
404      * (non-Javadoc)
405      *
406      * @see org.eclipse.debug.ui.IBreakpointOrganizerDelegate#getCategories()
407      */

408     public IAdaptable[] getCategories() {
409         IWorkingSet[] workingSets = fWorkingSetManager.getWorkingSets();
410         List JavaDoc all = new ArrayList JavaDoc();
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     /* (non-Javadoc)
422      * @see org.eclipse.debug.ui.IBreakpointOrganizerDelegateExtension#addBreakpoints(org.eclipse.debug.core.model.IBreakpoint[], org.eclipse.core.runtime.IAdaptable)
423      */

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     /* (non-Javadoc)
432      * @see org.eclipse.debug.ui.IBreakpointOrganizerDelegateExtension#removeBreakpoints(org.eclipse.debug.core.model.IBreakpoint[], org.eclipse.core.runtime.IAdaptable)
433      */

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 JavaDoc list = new ArrayList JavaDoc(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