KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > debug > ui > JavaDebugOptionsManager


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.jdt.internal.debug.ui;
12
13  
14 import java.util.ArrayList JavaDoc;
15 import java.util.HashSet JavaDoc;
16 import java.util.List JavaDoc;
17 import java.util.Set JavaDoc;
18 import java.util.StringTokenizer JavaDoc;
19
20 import org.eclipse.core.resources.IMarker;
21 import org.eclipse.core.resources.IMarkerDelta;
22 import org.eclipse.core.resources.IResource;
23 import org.eclipse.core.resources.IWorkspaceRunnable;
24 import org.eclipse.core.resources.ResourcesPlugin;
25 import org.eclipse.core.runtime.CoreException;
26 import org.eclipse.core.runtime.IAdaptable;
27 import org.eclipse.core.runtime.IProgressMonitor;
28 import org.eclipse.core.runtime.IStatus;
29 import org.eclipse.core.runtime.MultiStatus;
30 import org.eclipse.core.runtime.Status;
31 import org.eclipse.core.runtime.jobs.Job;
32 import org.eclipse.debug.core.DebugEvent;
33 import org.eclipse.debug.core.DebugException;
34 import org.eclipse.debug.core.DebugPlugin;
35 import org.eclipse.debug.core.IBreakpointsListener;
36 import org.eclipse.debug.core.IDebugEventSetListener;
37 import org.eclipse.debug.core.ILaunch;
38 import org.eclipse.debug.core.ILaunchConfiguration;
39 import org.eclipse.debug.core.ILaunchListener;
40 import org.eclipse.debug.core.model.IBreakpoint;
41 import org.eclipse.debug.core.model.IDebugTarget;
42 import org.eclipse.debug.ui.DebugUITools;
43 import org.eclipse.debug.ui.sourcelookup.ISourceLookupResult;
44 import org.eclipse.jdt.core.dom.Message;
45 import org.eclipse.jdt.debug.core.IJavaBreakpoint;
46 import org.eclipse.jdt.debug.core.IJavaBreakpointListener;
47 import org.eclipse.jdt.debug.core.IJavaDebugTarget;
48 import org.eclipse.jdt.debug.core.IJavaExceptionBreakpoint;
49 import org.eclipse.jdt.debug.core.IJavaLineBreakpoint;
50 import org.eclipse.jdt.debug.core.IJavaMethodBreakpoint;
51 import org.eclipse.jdt.debug.core.IJavaMethodEntryBreakpoint;
52 import org.eclipse.jdt.debug.core.IJavaStackFrame;
53 import org.eclipse.jdt.debug.core.IJavaThread;
54 import org.eclipse.jdt.debug.core.IJavaType;
55 import org.eclipse.jdt.debug.core.IJavaWatchpoint;
56 import org.eclipse.jdt.debug.core.JDIDebugModel;
57 import org.eclipse.jdt.debug.ui.IJavaDebugUIConstants;
58 import org.eclipse.jdt.internal.debug.core.breakpoints.JavaExceptionBreakpoint;
59 import org.eclipse.jdt.internal.debug.core.logicalstructures.IJavaStructuresListener;
60 import org.eclipse.jdt.internal.debug.core.logicalstructures.JavaLogicalStructures;
61 import org.eclipse.jdt.internal.debug.core.model.JDIDebugTarget;
62 import org.eclipse.jdt.internal.debug.ui.actions.JavaBreakpointPropertiesAction;
63 import org.eclipse.jdt.internal.debug.ui.snippeteditor.ScrapbookLauncher;
64 import org.eclipse.jface.preference.IPreferenceStore;
65 import org.eclipse.jface.util.IPropertyChangeListener;
66 import org.eclipse.jface.util.PropertyChangeEvent;
67 import org.eclipse.jface.viewers.ILabelProvider;
68 import org.eclipse.jface.viewers.StructuredSelection;
69 import org.eclipse.jface.window.Window;
70 import org.eclipse.swt.widgets.Display;
71 import org.eclipse.swt.widgets.Shell;
72
73 import com.ibm.icu.text.MessageFormat;
74 import com.sun.jdi.InvocationException;
75 import com.sun.jdi.ObjectReference;
76
77 /**
78  * Manages options for the Java Debugger:<ul>
79  * <li>Suspend on compilation errors</li>
80  * <li>Suspend on uncaught exceptions</li>
81  * <li>Step filters</li>
82  * <li>Sets a system property that the Java debugger is active if
83  * there are launches that contain running debug targets. Used for Java
84  * debug action visibility.
85  * </ul>
86  */

87 public class JavaDebugOptionsManager implements IDebugEventSetListener, IPropertyChangeListener, IJavaBreakpointListener, ILaunchListener, IBreakpointsListener, IJavaStructuresListener {
88     
89     /**
90      * Singleton options manager
91      */

92     private static JavaDebugOptionsManager fgOptionsManager = null;
93     
94     /**
95      * Breakpoint used to suspend on uncaught exceptions
96      */

97     private IJavaExceptionBreakpoint fSuspendOnExceptionBreakpoint = null;
98     
99     /**
100      * Breakpoint used to suspend on compilation errors
101      */

102     private IJavaExceptionBreakpoint fSuspendOnErrorBreakpoint = null;
103     
104     /**
105      * A label provider
106      */

107     private static ILabelProvider fLabelProvider= DebugUITools.newDebugModelPresentation();
108     
109     /**
110      * Constants indicating whether a breakpoint
111      * is added, removed, or changed.
112      */

113     private static final int ADDED = 0;
114     private static final int REMOVED = 1;
115     private static final int CHANGED = 2;
116         
117     /**
118      * Local cache of active step filters.
119      */

120     private String JavaDoc[] fActiveStepFilters = null;
121     
122     /**
123      * Preferences that effect variable display options.
124      *
125      * @since 3.3
126      */

127     private static Set JavaDoc fgDisplayOptions;
128     
129     static {
130         fgDisplayOptions = new HashSet JavaDoc();
131         fgDisplayOptions.add(IJDIPreferencesConstants.PREF_SHOW_CHAR);
132         fgDisplayOptions.add(IJDIPreferencesConstants.PREF_SHOW_HEX);
133         fgDisplayOptions.add(IJDIPreferencesConstants.PREF_SHOW_UNSIGNED);
134     }
135     
136     /**
137      * Whether the manager has been activated
138      */

139     private boolean fActivated = false;
140     
141     class InitJob extends Job {
142         
143         public InitJob() {
144             super(DebugUIMessages.JavaDebugOptionsManager_0);
145         }
146         
147         protected IStatus run(IProgressMonitor monitor) {
148             MultiStatus status = new MultiStatus(JDIDebugUIPlugin.getUniqueIdentifier(), IJavaDebugUIConstants.INTERNAL_ERROR, DebugUIMessages.JavaDebugOptionsManager_1, null);
149             // compilation error breakpoint
150
try {
151                 IJavaExceptionBreakpoint bp = JDIDebugModel.createExceptionBreakpoint(ResourcesPlugin.getWorkspace().getRoot(),"java.lang.Error", true, true, false, false, null); //$NON-NLS-1$
152
bp.setPersisted(false);
153                 setSuspendOnCompilationErrorsBreakpoint(bp);
154             } catch (CoreException e) {
155                 status.add(e.getStatus());
156             }
157             
158             // uncaught exception breakpoint
159
try {
160                 IJavaExceptionBreakpoint bp = JDIDebugModel.createExceptionBreakpoint(ResourcesPlugin.getWorkspace().getRoot(),"java.lang.Throwable", false, true, false, false, null); //$NON-NLS-1$
161
((JavaExceptionBreakpoint)bp).setSuspendOnSubclasses(true);
162                 bp.setPersisted(false);
163                 setSuspendOnUncaughtExceptionBreakpoint(bp);
164             } catch (CoreException e) {
165                 status.add(e.getStatus());
166             }
167             
168             if (status.getChildren().length == 0) {
169                 return Status.OK_STATUS;
170             }
171             return status;
172         }
173     }
174     
175     /**
176      * Not to be instantiated
177      *
178      * @see JavaDebugOptionsManager#getDefault();
179      */

180     private JavaDebugOptionsManager() {
181     }
182     
183     /**
184      * Return the default options manager
185      */

186     public static JavaDebugOptionsManager getDefault() {
187         if (fgOptionsManager == null) {
188             fgOptionsManager = new JavaDebugOptionsManager();
189         }
190         return fgOptionsManager;
191     }
192     
193     /**
194      * Called at startup by the Java debug ui plug-in
195      */

196     public void startup() {
197         // lazy initialization will occur on the first launch
198
DebugPlugin debugPlugin = DebugPlugin.getDefault();
199         debugPlugin.getLaunchManager().addLaunchListener(this);
200         debugPlugin.getBreakpointManager().addBreakpointListener(this);
201         EvaluationContextManager.startup();
202     }
203     
204     /**
205      * Called at shutdown by the Java debug ui plug-in
206      */

207     public void shutdown() {
208         DebugPlugin debugPlugin = DebugPlugin.getDefault();
209         debugPlugin.removeDebugEventListener(this);
210         debugPlugin.getLaunchManager().removeLaunchListener(this);
211         debugPlugin.getBreakpointManager().removeBreakpointListener(this);
212         if (!JDIDebugUIPlugin.getDefault().isShuttingDown()) {
213             //avert restoring the preference store at shutdown
214
JDIDebugUIPlugin.getDefault().getPreferenceStore().removePropertyChangeListener(this);
215         }
216         JDIDebugModel.removeJavaBreakpointListener(this);
217         JavaLogicalStructures.removeStructuresListener(this);
218         System.getProperties().remove(JDIDebugUIPlugin.getUniqueIdentifier() + ".debuggerActive"); //$NON-NLS-1$
219
}
220
221     /**
222      * Initializes compilation error handling and suspending
223      * on uncaught exceptions.
224      */

225     protected void initializeProblemHandling() {
226         InitJob job = new InitJob();
227         job.setSystem(true);
228         job.schedule();
229     }
230                         
231     /**
232      * Notifies java debug targets of the given breakpoint
233      * addition or removal.
234      *
235      * @param breakpoint a breakpoint
236      * @param kind ADDED, REMOVED, or CHANGED
237      */

238     protected void notifyTargets(IBreakpoint breakpoint, int kind) {
239         IDebugTarget[] targets = DebugPlugin.getDefault().getLaunchManager().getDebugTargets();
240         for (int i = 0; i < targets.length; i++) {
241             if (targets[i] instanceof IJavaDebugTarget) {
242                 IJavaDebugTarget target = (IJavaDebugTarget)targets[i];
243                 notifyTarget(target, breakpoint, kind);
244             }
245         }
246     }
247     
248     /**
249      * Notifies the give debug target of filter specifications
250      *
251      * @param target Java debug target
252      */

253     protected void notifyTargetOfFilters(IJavaDebugTarget target) {
254
255         IPreferenceStore store = JDIDebugUIPlugin.getDefault().getPreferenceStore();
256         
257         target.setFilterConstructors(store.getBoolean(IJDIPreferencesConstants.PREF_FILTER_CONSTRUCTORS));
258         target.setFilterStaticInitializers(store.getBoolean(IJDIPreferencesConstants.PREF_FILTER_STATIC_INITIALIZERS));
259         target.setFilterSynthetics(store.getBoolean(IJDIPreferencesConstants.PREF_FILTER_SYNTHETICS));
260         if (target instanceof JDIDebugTarget) {
261             ((JDIDebugTarget)target).setStepThruFilters(store.getBoolean(IJDIPreferencesConstants.PREF_STEP_THRU_FILTERS));
262         }
263         target.setStepFilters(getActiveStepFilters());
264
265     }
266     
267     /**
268      * Notifies all targets of current filter specifications.
269      */

270     protected void notifyTargetsOfFilters() {
271         IDebugTarget[] targets = DebugPlugin.getDefault().getLaunchManager().getDebugTargets();
272         for (int i = 0; i < targets.length; i++) {
273             if (targets[i] instanceof IJavaDebugTarget) {
274                 IJavaDebugTarget target = (IJavaDebugTarget)targets[i];
275                 notifyTargetOfFilters(target);
276             }
277         }
278     }
279
280     /**
281      * Notifies the given target of the given breakpoint
282      * addition or removal.
283      *
284      * @param target Java debug target
285      * @param breakpoint a breakpoint
286      * @param kind ADDED, REMOVED, or CHANGED
287      */

288     protected void notifyTarget(IJavaDebugTarget target, IBreakpoint breakpoint, int kind) {
289         switch (kind) {
290             case ADDED:
291                 target.breakpointAdded(breakpoint);
292                 break;
293             case REMOVED:
294                 target.breakpointRemoved(breakpoint,null);
295                 break;
296             case CHANGED:
297                 target.breakpointChanged(breakpoint,null);
298                 break;
299         }
300     }
301     
302     /**
303      * @see IPropertyChangeListener#propertyChange(PropertyChangeEvent)
304      */

305     public void propertyChange(PropertyChangeEvent event) {
306         String JavaDoc property = event.getProperty();
307         if (property.equals(IJDIPreferencesConstants.PREF_SUSPEND_ON_COMPILATION_ERRORS)) {
308             IBreakpoint breakpoint = getSuspendOnCompilationErrorBreakpoint();
309             if (breakpoint != null) {
310                 int kind = REMOVED;
311                 if (isSuspendOnCompilationErrors()) {
312                     kind = ADDED;
313                 }
314                 notifyTargets(breakpoint, kind);
315             }
316         } else if (property.equals(IJDIPreferencesConstants.PREF_SUSPEND_ON_UNCAUGHT_EXCEPTIONS)) {
317             IBreakpoint breakpoint = getSuspendOnUncaughtExceptionBreakpoint();
318             if (breakpoint != null) {
319                 int kind = REMOVED;
320                 if (isSuspendOnUncaughtExceptions()) {
321                     kind = ADDED;
322                 }
323                 notifyTargets(breakpoint, kind);
324             }
325         } else if (fgDisplayOptions.contains(property)) {
326             variableViewSettingsChanged();
327         } else if (isUseFilterProperty(property)) {
328             notifyTargetsOfFilters();
329         } else if (isFilterListProperty(property)) {
330             updateActiveFilters();
331         }
332     }
333     
334     /**
335      * Returns whether the given property is a property that affects whether
336      * or not step filters are used.
337      */

338     private boolean isUseFilterProperty(String JavaDoc property) {
339         return property.equals(IJDIPreferencesConstants.PREF_FILTER_CONSTRUCTORS) ||
340             property.equals(IJDIPreferencesConstants.PREF_FILTER_STATIC_INITIALIZERS) ||
341             property.equals(IJDIPreferencesConstants.PREF_FILTER_SYNTHETICS) ||
342             property.equals(IJDIPreferencesConstants.PREF_STEP_THRU_FILTERS);
343     }
344     
345     /**
346      * Returns whether the given property is a property that affects
347      * the list of active or inactive step filters.
348      */

349     private boolean isFilterListProperty(String JavaDoc property) {
350         return property.equals(IJDIPreferencesConstants.PREF_ACTIVE_FILTERS_LIST) ||
351             property.equals(IJDIPreferencesConstants.PREF_INACTIVE_FILTERS_LIST);
352     }
353     
354     /**
355      * Enable/Disable the given breakpoint and notify
356      * targets of the change.
357      *
358      * @param breakpoint a breakpoint
359      * @param enabled whether enabeld
360      */

361     protected void setEnabled(IBreakpoint breakpoint, boolean enabled) {
362         try {
363             breakpoint.setEnabled(enabled);
364             notifyTargets(breakpoint, CHANGED);
365         } catch (CoreException e) {
366             JDIDebugUIPlugin.log(e);
367         }
368     }
369     
370     /**
371      * Returns whether suspend on compilation errors is
372      * enabled.
373      *
374      * @return whether suspend on compilation errors is
375      * enabled
376      */

377     protected boolean isSuspendOnCompilationErrors() {
378         return JDIDebugUIPlugin.getDefault().getPreferenceStore().getBoolean(IJDIPreferencesConstants.PREF_SUSPEND_ON_COMPILATION_ERRORS);
379     }
380     
381     /**
382      * Returns whether suspend on uncaught exception is
383      * enabled
384      *
385      * @return whether suspend on uncaught exception is
386      * enabled
387      */

388     protected boolean isSuspendOnUncaughtExceptions() {
389         return JDIDebugUIPlugin.getDefault().getPreferenceStore().getBoolean(IJDIPreferencesConstants.PREF_SUSPEND_ON_UNCAUGHT_EXCEPTIONS);
390     }
391
392
393     /**
394      * Sets the breakpoint used to suspend on uncaught exceptions
395      *
396      * @param breakpoint exception breakpoint
397      */

398     private void setSuspendOnUncaughtExceptionBreakpoint(IJavaExceptionBreakpoint breakpoint) {
399         fSuspendOnExceptionBreakpoint = breakpoint;
400     }
401     
402     /**
403      * Returns the breakpoint used to suspend on uncaught exceptions
404      *
405      * @return exception breakpoint
406      */

407     protected IJavaExceptionBreakpoint getSuspendOnUncaughtExceptionBreakpoint() {
408         return fSuspendOnExceptionBreakpoint;
409     }
410     
411     /**
412      * Sets the breakpoint used to suspend on compilation
413      * errors.
414      *
415      * @param breakpoint exception breakpoint
416      */

417     private void setSuspendOnCompilationErrorsBreakpoint(IJavaExceptionBreakpoint breakpoint) {
418         fSuspendOnErrorBreakpoint = breakpoint;
419     }
420     
421     /**
422      * Returns the breakpoint used to suspend on compilation
423      * errors
424      *
425      * @return exception breakpoint
426      */

427     protected IJavaExceptionBreakpoint getSuspendOnCompilationErrorBreakpoint() {
428         return fSuspendOnErrorBreakpoint;
429     }
430     
431     /**
432      * Parses the comma separated string into an array of strings
433      *
434      * @return list
435      */

436     public static String JavaDoc[] parseList(String JavaDoc listString) {
437         List JavaDoc list = new ArrayList JavaDoc(10);
438         StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(listString, ","); //$NON-NLS-1$
439
while (tokenizer.hasMoreTokens()) {
440             String JavaDoc token = tokenizer.nextToken();
441             list.add(token);
442         }
443         return (String JavaDoc[])list.toArray(new String JavaDoc[list.size()]);
444     }
445     
446     /**
447      * Serializes the array of strings into one comma
448      * separated string.
449      *
450      * @param list array of strings
451      * @return a single string composed of the given list
452      */

453     public static String JavaDoc serializeList(String JavaDoc[] list) {
454         if (list == null) {
455             return ""; //$NON-NLS-1$
456
}
457         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
458         for (int i = 0; i < list.length; i++) {
459             if (i > 0) {
460                 buffer.append(',');
461             }
462             buffer.append(list[i]);
463         }
464         return buffer.toString();
465     }
466     
467     /**
468      * Returns the current list of active step filters.
469      *
470      * @return current list of active step filters
471      */

472     protected String JavaDoc[] getActiveStepFilters() {
473         if (fActiveStepFilters == null) {
474             fActiveStepFilters= parseList(JDIDebugUIPlugin.getDefault().getPreferenceStore().getString(IJDIPreferencesConstants.PREF_ACTIVE_FILTERS_LIST));
475             // After active filters are cached, register to hear about future changes
476
JDIDebugUIPlugin.getDefault().getPreferenceStore().addPropertyChangeListener(this);
477         }
478         return fActiveStepFilters;
479     }
480     
481     /**
482      * Updates local copy of active step filters and
483      * notifies targets.
484      */

485     protected void updateActiveFilters() {
486         fActiveStepFilters= parseList(JDIDebugUIPlugin.getDefault().getPreferenceStore().getString(IJDIPreferencesConstants.PREF_ACTIVE_FILTERS_LIST));
487         notifyTargetsOfFilters();
488     }
489     
490     /**
491      * When a Java debug target is created, install options in
492      * the target and set that the Java debugger is active.
493      * When all Java debug targets are terminated set that that Java debugger is
494      * no longer active.
495      *
496      * @see IDebugEventSetListener#handleDebugEvents(DebugEvent[])
497      */

498     public void handleDebugEvents(DebugEvent[] events) {
499         for (int i = 0; i < events.length; i++) {
500             DebugEvent event = events[i];
501             if (event.getKind() == DebugEvent.CREATE) {
502                 Object JavaDoc source = event.getSource();
503                 if (source instanceof IJavaDebugTarget) {
504                     IJavaDebugTarget javaTarget = (IJavaDebugTarget)source;
505                     
506                     // compilation breakpoints
507
if (isSuspendOnCompilationErrors()) {
508                         notifyTarget(javaTarget, getSuspendOnCompilationErrorBreakpoint(), ADDED);
509                     }
510                     
511                     // uncaught exception breakpoint
512
if (isSuspendOnUncaughtExceptions()) {
513                         ILaunchConfiguration launchConfiguration = javaTarget.getLaunch().getLaunchConfiguration();
514                         boolean isSnippetEditor = false;
515                         
516                         try {
517                             isSnippetEditor = (launchConfiguration.getAttribute(ScrapbookLauncher.SCRAPBOOK_LAUNCH, (String JavaDoc)null) != null);
518                         } catch (CoreException e) {
519                         }
520                         
521                         if (!isSnippetEditor) {
522                             notifyTarget(javaTarget, getSuspendOnUncaughtExceptionBreakpoint(), ADDED);
523                         }
524                     }
525                     
526                     // step filters
527
notifyTargetOfFilters(javaTarget);
528                 }
529             }
530         }
531     }
532
533     /**
534      * @see IJavaBreakpointListener#addingBreakpoint(IJavaDebugTarget, IJavaBreakpoint)
535      */

536     public void addingBreakpoint(IJavaDebugTarget target, IJavaBreakpoint breakpoint) {
537     }
538
539     /**
540      * @see IJavaBreakpointListener#installingBreakpoint(IJavaDebugTarget, IJavaBreakpoint, IJavaType)
541      */

542     public int installingBreakpoint(IJavaDebugTarget target, IJavaBreakpoint breakpoint, IJavaType type) {
543         return DONT_CARE;
544     }
545     
546     /**
547      * @see IJavaBreakpointListener#breakpointHit(IJavaThread, IJavaBreakpoint)
548      */

549     public int breakpointHit(IJavaThread thread, IJavaBreakpoint breakpoint) {
550         if (breakpoint == getSuspendOnCompilationErrorBreakpoint()) {
551             IJavaExceptionBreakpoint exception = (IJavaExceptionBreakpoint) breakpoint;
552             if (exception.getExceptionTypeName().equals("java.lang.Error")) { //$NON-NLS-1$
553
// only lookup source if the exception actually is a java.lang.Error (which is used
554
// to indicate compilation errors by the Eclipse Java compiler).
555
try {
556                     return getProblem(thread) != null ? SUSPEND : DONT_SUSPEND;
557                 } catch (DebugException e) {
558                     JDIDebugUIPlugin.log(e);
559                     // don't suspend if we can't determine if there is a problem
560
return DONT_SUSPEND;
561                 }
562             }
563             return DONT_SUSPEND;
564         }
565         if (breakpoint == getSuspendOnUncaughtExceptionBreakpoint()) {
566             // the "uncaught" exceptions breakpoint subsumes the "compilation error" breakpoint
567
// since "Throwable" is a supertype of "Error". Thus, if there is actually a compilation
568
// error here, but the option to suspend on compilation errors is off, we should
569
// resume (i.e. do not suspend)
570
if (!isSuspendOnCompilationErrors()) {
571                 try {
572                     if (getProblem(thread) != null) {
573                         return DONT_SUSPEND;
574                     }
575                 } catch (DebugException e) {
576                     JDIDebugUIPlugin.log(e);
577                     // unable to determine if there was a compilation problem, so fall thru and suspend
578
}
579             }
580             return SUSPEND;
581         }
582         return DONT_CARE;
583     }
584     
585     /**
586      * Returns any problem for the top stack frame in the given thread, or
587      * <code>null</code> if none.
588      *
589      * @param thread thread to look for a compilation problem in
590      * @return problem or <code>null</code>
591      * @throws DebugException if an exception occurrs retrieveing the problem
592      */

593     private IMarker getProblem(IJavaThread thread) throws DebugException {
594         IJavaStackFrame frame = (IJavaStackFrame)thread.getTopStackFrame();
595         if (frame != null) {
596             return getProblem(frame);
597         }
598         return null;
599     }
600
601     /**
602      * @see IJavaBreakpointListener#breakpointInstalled(IJavaDebugTarget, IJavaBreakpoint)
603      */

604     public void breakpointInstalled(IJavaDebugTarget target, IJavaBreakpoint breakpoint) {
605     }
606
607     /**
608      * @see IJavaBreakpointListener#breakpointRemoved(IJavaDebugTarget, IJavaBreakpoint)
609      */

610     public void breakpointRemoved(IJavaDebugTarget target, IJavaBreakpoint breakpoint) {
611     }
612     
613     /**
614      * Returns any problem marker associated with the current location
615      * of the given stack frame, or <code>null</code> if none.
616      *
617      * @param frame stack frame
618      * @return marker representing compilation problem, or <code>null</code>
619      * @throws DebugException if an exception occurrs retrieveing the problem
620      */

621     protected IMarker getProblem(IJavaStackFrame frame) {
622         ILaunch launch = frame.getLaunch();
623         if (launch != null) {
624             ISourceLookupResult result = DebugUITools.lookupSource(frame, null);
625             Object JavaDoc sourceElement = result.getSourceElement();
626             if (sourceElement instanceof IResource) {
627                 try {
628                     IResource resource = (IResource) sourceElement;
629                     IMarker[] markers = resource.findMarkers("org.eclipse.jdt.core.problem", true, IResource.DEPTH_INFINITE); //$NON-NLS-1$
630
int line = frame.getLineNumber();
631                     for (int i = 0; i < markers.length; i++) {
632                         IMarker marker = markers[i];
633                         if (marker.getAttribute(IMarker.LINE_NUMBER, -1) == line && marker.getAttribute(IMarker.SEVERITY, -1) == IMarker.SEVERITY_ERROR) {
634                             return marker;
635                         }
636                     }
637                 } catch (CoreException e) {
638                 }
639             }
640         }
641         return null;
642     }
643     
644     /* (non-Javadoc)
645      * @see org.eclipse.jdt.debug.core.IJavaBreakpointListener#breakpointHasRuntimeException(org.eclipse.jdt.debug.core.IJavaLineBreakpoint, org.eclipse.debug.core.DebugException)
646      */

647     public void breakpointHasRuntimeException(final IJavaLineBreakpoint breakpoint, final DebugException exception) {
648         IStatus status;
649         Throwable JavaDoc wrappedException= exception.getStatus().getException();
650         if (wrappedException instanceof InvocationException) {
651             InvocationException ie= (InvocationException) wrappedException;
652             ObjectReference ref= ie.exception();
653             status= new Status(IStatus.ERROR,JDIDebugUIPlugin.getUniqueIdentifier(), IStatus.ERROR, ref.referenceType().name(), null);
654         } else {
655             status= exception.getStatus();
656         }
657         openConditionErrorDialog(breakpoint, DebugUIMessages.JavaDebugOptionsManager_Conditional_breakpoint_encountered_runtime_exception__1, status);
658     }
659
660     /* (non-Javadoc)
661      * @see org.eclipse.jdt.debug.core.IJavaBreakpointListener#breakpointHasCompilationErrors(org.eclipse.jdt.debug.core.IJavaLineBreakpoint, org.eclipse.jdt.core.dom.Message[])
662      */

663     public void breakpointHasCompilationErrors(final IJavaLineBreakpoint breakpoint, final Message[] errors) {
664         StringBuffer JavaDoc message= new StringBuffer JavaDoc();
665         Message error;
666         for (int i=0, numErrors= errors.length; i < numErrors; i++) {
667             error= errors[i];
668             message.append(error.getMessage());
669             message.append("\n "); //$NON-NLS-1$
670
}
671         IStatus status= new Status(IStatus.ERROR, JDIDebugUIPlugin.getUniqueIdentifier(), IStatus.ERROR, message.toString(), null);
672         openConditionErrorDialog(breakpoint, DebugUIMessages.JavaDebugOptionsManager_Conditional_breakpoint_has_compilation_error_s___2, status);
673     }
674     
675     private void openConditionErrorDialog(final IJavaLineBreakpoint breakpoint, final String JavaDoc errorMessage, final IStatus status) {
676         final Display display= JDIDebugUIPlugin.getStandardDisplay();
677         if (display.isDisposed()) {
678             return;
679         }
680         final String JavaDoc message= MessageFormat.format(errorMessage, new String JavaDoc[] {fLabelProvider.getText(breakpoint)});
681         display.asyncExec(new Runnable JavaDoc() {
682             public void run() {
683                 if (display.isDisposed()) {
684                     return;
685                 }
686                 Shell shell= JDIDebugUIPlugin.getActiveWorkbenchShell();
687                 ConditionalBreakpointErrorDialog dialog= new ConditionalBreakpointErrorDialog(shell, message, status);
688                 int result = dialog.open();
689                 if (result == Window.OK) {
690                     JavaBreakpointPropertiesAction action= new JavaBreakpointPropertiesAction();
691                     action.selectionChanged(null, new StructuredSelection(breakpoint));
692                     action.run(null);
693                 }
694             }
695         });
696     }
697     
698     /**
699      * Activates this debug options manager. When active, this
700      * manager becomes a listener to many notifications and updates
701      * running debug targets based on these notifications.
702      *
703      * A debug options manager does not need to be activated until
704      * there is a running debug target.
705      */

706     private void activate() {
707         if (fActivated) {
708             return;
709         }
710         fActivated = true;
711         initializeProblemHandling();
712         notifyTargetsOfFilters();
713         DebugPlugin.getDefault().addDebugEventListener(this);
714         JDIDebugModel.addJavaBreakpointListener(this);
715         JavaLogicalStructures.addStructuresListener(this);
716     }
717
718     /**
719      * Startup problem handling on the first launch.
720      *
721      * @see ILaunchListener#launchAdded(ILaunch)
722      */

723     public void launchAdded(ILaunch launch) {
724         launchChanged(launch);
725     }
726     /**
727      * @see ILaunchListener#launchChanged(ILaunch)
728      */

729     public void launchChanged(ILaunch launch) {
730         activate();
731         DebugPlugin.getDefault().getLaunchManager().removeLaunchListener(this);
732     }
733
734     /**
735      * @see ILaunchListener#launchRemoved(ILaunch)
736      */

737     public void launchRemoved(ILaunch launch) {
738     }
739     
740     /**
741      * Adds message attributes to java breakpoints.
742      *
743      * @see org.eclipse.debug.core.IBreakpointsListener#breakpointsAdded(org.eclipse.debug.core.model.IBreakpoint[])
744      */

745     public void breakpointsAdded(final IBreakpoint[] breakpoints) {
746         // if a breakpoint is added, but already has a message, do not update it
747
List JavaDoc update = new ArrayList JavaDoc();
748         for (int i = 0; i < breakpoints.length; i++) {
749             IBreakpoint breakpoint = breakpoints[i];
750             try {
751                 if (breakpoint instanceof IJavaBreakpoint && breakpoint.getMarker().getAttribute(IMarker.MESSAGE) == null) {
752                     update.add(breakpoint);
753                 }
754             } catch (CoreException e) {
755                 JDIDebugUIPlugin.log(e);
756             }
757         }
758         if (!update.isEmpty()) {
759             updateBreakpointMessages((IBreakpoint[])update.toArray(new IBreakpoint[update.size()]));
760         }
761     }
762     
763     /**
764      * Updates message attributes on the given java breakpoints.
765      *
766      * @see org.eclipse.debug.core.IBreakpointsListener#breakpointsAdded(org.eclipse.debug.core.model.IBreakpoint[])
767      */

768     private void updateBreakpointMessages(final IBreakpoint[] breakpoints) {
769         IWorkspaceRunnable runnable = new IWorkspaceRunnable() {
770             public void run(IProgressMonitor monitor) throws CoreException {
771                 for (int i = 0; i < breakpoints.length; i++) {
772                     IBreakpoint breakpoint = breakpoints[i];
773                     if (breakpoint instanceof IJavaBreakpoint) {
774                         String JavaDoc info = fLabelProvider.getText(breakpoint);
775                         String JavaDoc type = DebugUIMessages.JavaDebugOptionsManager_Breakpoint___1;
776                         if (breakpoint instanceof IJavaMethodBreakpoint || breakpoint instanceof IJavaMethodEntryBreakpoint) {
777                             type = DebugUIMessages.JavaDebugOptionsManager_Method_breakpoint___2;
778                         } else if (breakpoint instanceof IJavaWatchpoint) {
779                             type = DebugUIMessages.JavaDebugOptionsManager_Watchpoint___3;
780                         } else if (breakpoint instanceof IJavaLineBreakpoint) {
781                             type = DebugUIMessages.JavaDebugOptionsManager_Line_breakpoint___4;
782                         }
783                         breakpoint.getMarker().setAttribute(IMarker.MESSAGE, type + info);
784                     }
785                 }
786             }
787         };
788         try {
789             ResourcesPlugin.getWorkspace().run(runnable, null, 0, null);
790         } catch (CoreException e) {
791             JDIDebugUIPlugin.log(e);
792         }
793     }
794
795     /**
796      * Updates message attributes on java breakpoints.
797      *
798      * @see org.eclipse.debug.core.IBreakpointsListener#breakpointsChanged(org.eclipse.debug.core.model.IBreakpoint[], org.eclipse.core.resources.IMarkerDelta[])
799      */

800     public void breakpointsChanged(
801         IBreakpoint[] breakpoints,
802         IMarkerDelta[] deltas) {
803             updateBreakpointMessages(breakpoints);
804
805     }
806
807     /* (non-Javadoc)
808      * @see org.eclipse.debug.core.IBreakpointsListener#breakpointsRemoved(org.eclipse.debug.core.model.IBreakpoint[], org.eclipse.core.resources.IMarkerDelta[])
809      */

810     public void breakpointsRemoved(
811         IBreakpoint[] breakpoints,
812         IMarkerDelta[] deltas) {
813     }
814
815     /* (non-Javadoc)
816      * @see org.eclipse.jdt.internal.debug.core.logicalstructures.IJavaStructuresListener#logicalStructuresChanged()
817      */

818     public void logicalStructuresChanged() {
819         variableViewSettingsChanged();
820     }
821     
822     /**
823      * Refreshes the variables view by firing a change event on a stack frame (active
824      * debug context).
825      */

826     protected void variableViewSettingsChanged() {
827         // If a Java stack frame is selected in the Debug view, fire a change event on
828
// it so the variables view will update for any structure changes.
829
IAdaptable selected = DebugUITools.getDebugContext();
830         if (selected != null) {
831             IJavaStackFrame frame= (IJavaStackFrame) selected.getAdapter(IJavaStackFrame.class);
832             if (frame != null) {
833                 DebugPlugin.getDefault().fireDebugEventSet(new DebugEvent[] {
834                         new DebugEvent(frame, DebugEvent.CHANGE)
835                 });
836             }
837         }
838     }
839
840 }
841
Popular Tags