KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > debug > ui > breakpoints > ExceptionInspector


1 /*******************************************************************************
2  * Copyright (c) 2006, 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.breakpoints;
12
13 import org.eclipse.core.runtime.IAdaptable;
14 import org.eclipse.core.runtime.Preferences;
15 import org.eclipse.core.runtime.Preferences.IPropertyChangeListener;
16 import org.eclipse.core.runtime.Preferences.PropertyChangeEvent;
17 import org.eclipse.debug.core.DebugException;
18 import org.eclipse.debug.core.model.IBreakpoint;
19 import org.eclipse.debug.core.model.IExpression;
20 import org.eclipse.debug.ui.DebugUITools;
21 import org.eclipse.debug.ui.IDebugUIConstants;
22 import org.eclipse.debug.ui.IDebugView;
23 import org.eclipse.debug.ui.InspectPopupDialog;
24 import org.eclipse.debug.ui.contexts.DebugContextEvent;
25 import org.eclipse.debug.ui.contexts.IDebugContextListener;
26 import org.eclipse.debug.ui.contexts.IDebugContextManager;
27 import org.eclipse.jdt.debug.core.IJavaExceptionBreakpoint;
28 import org.eclipse.jdt.debug.core.IJavaObject;
29 import org.eclipse.jdt.debug.core.IJavaStackFrame;
30 import org.eclipse.jdt.debug.core.IJavaThread;
31 import org.eclipse.jdt.internal.debug.core.breakpoints.JavaExceptionBreakpoint;
32 import org.eclipse.jdt.internal.debug.ui.IJDIPreferencesConstants;
33 import org.eclipse.jdt.internal.debug.ui.JDIDebugUIPlugin;
34 import org.eclipse.jdt.internal.debug.ui.actions.PopupInspectAction;
35 import org.eclipse.jdt.internal.debug.ui.display.JavaInspectExpression;
36 import org.eclipse.jface.viewers.ISelection;
37 import org.eclipse.jface.viewers.IStructuredSelection;
38 import org.eclipse.swt.graphics.Point;
39 import org.eclipse.swt.graphics.Rectangle;
40 import org.eclipse.swt.widgets.Tree;
41 import org.eclipse.swt.widgets.TreeItem;
42 import org.eclipse.ui.IWorkbenchPage;
43 import org.eclipse.ui.IWorkbenchPart;
44 import org.eclipse.ui.IWorkbenchPartSite;
45
46 /**
47  * This class is used to show the inspect popup when a thread is suspended due to an exception being thrown
48  */

49 public class ExceptionInspector implements IDebugContextListener, IPropertyChangeListener {
50     
51     /**
52      * Constructor
53      */

54     public ExceptionInspector() {
55         Preferences pluginPreferences = JDIDebugUIPlugin.getDefault().getPluginPreferences();
56         pluginPreferences.addPropertyChangeListener(this);
57         if (pluginPreferences.getBoolean(IJDIPreferencesConstants.PREF_OPEN_INSPECT_POPUP_ON_EXCEPTION)) {
58             DebugUITools.getDebugContextManager().addDebugContextListener(this);
59         }
60     }
61
62     /**
63      * @see org.eclipse.debug.ui.contexts.IDebugContextListener#debugContextChanged(org.eclipse.debug.ui.contexts.DebugContextEvent)
64      */

65     public void debugContextChanged(DebugContextEvent event) {
66         if ((event.getFlags() & DebugContextEvent.ACTIVATED) > 0) {
67             IWorkbenchPart part = event.getDebugContextProvider().getPart();
68             if (part != null) {
69                 IWorkbenchPartSite site = part.getSite();
70                 if (site != null && IDebugUIConstants.ID_DEBUG_VIEW.equals(site.getId())) {
71                     IWorkbenchPage page = site.getWorkbenchWindow().getActivePage();
72                     if (page != null && page.isPartVisible(part)) {
73                         ISelection selection = event.getContext();
74                         if (selection instanceof IStructuredSelection) {
75                             IStructuredSelection ss = (IStructuredSelection) selection;
76                             if (ss.size() == 1) {
77                                 Object JavaDoc firstElement = ss.getFirstElement();
78                                 if (firstElement instanceof IAdaptable) {
79                                     IJavaStackFrame frame = (IJavaStackFrame) ((IAdaptable)firstElement).getAdapter(IJavaStackFrame.class);
80                                     if (frame != null) {
81                                         IJavaThread thread = (IJavaThread)frame.getThread();
82                                         try {
83                                             if (frame.equals(thread.getTopStackFrame())) {
84                                                 IBreakpoint[] breakpoints = thread.getBreakpoints();
85                                                 if (breakpoints.length == 1) {
86                                                     if (breakpoints[0] instanceof IJavaExceptionBreakpoint) {
87                                                         IJavaExceptionBreakpoint exception = (IJavaExceptionBreakpoint) breakpoints[0];
88                                                         IJavaObject lastException = ((JavaExceptionBreakpoint)exception).getLastException();
89                                                         if (lastException != null) {
90                                                             IExpression exp = new JavaInspectExpression(exception.getExceptionTypeName(), lastException);
91                                                             Tree tree = (Tree) ((IDebugView)part).getViewer().getControl();
92                                                             TreeItem[] selection2 = tree.getSelection();
93                                                             Rectangle bounds = selection2[0].getBounds();
94                                                             Point point = tree.toDisplay(bounds.x, bounds.y + bounds.height);
95                                                             InspectPopupDialog dialog = new InspectPopupDialog(part.getSite().getShell(),
96                                                                     point, PopupInspectAction.ACTION_DEFININITION_ID, exp);
97                                                             dialog.open();
98                                                         }
99                                                     }
100                                                 }
101                                             }
102                                         } catch (DebugException e) {}
103                                     }
104                                 }
105                             }
106                         }
107                     }
108                 }
109             }
110         }
111     }
112
113     /* (non-Javadoc)
114      * @see org.eclipse.debug.internal.ui.contexts.provisional.IDebugContextListener#contextChanged(org.eclipse.jface.viewers.ISelection, org.eclipse.ui.IWorkbenchPart)
115      */

116     public void contextChanged(ISelection selection, IWorkbenchPart part) {}
117
118     /* (non-Javadoc)
119      * @see org.eclipse.core.runtime.Preferences.IPropertyChangeListener#propertyChange(org.eclipse.core.runtime.Preferences.PropertyChangeEvent)
120      */

121     public void propertyChange(PropertyChangeEvent event) {
122         if (IJDIPreferencesConstants.PREF_OPEN_INSPECT_POPUP_ON_EXCEPTION.equals(event.getProperty())) {
123             IDebugContextManager manager = DebugUITools.getDebugContextManager();
124             if (JDIDebugUIPlugin.getDefault().getPluginPreferences().getBoolean(IJDIPreferencesConstants.PREF_OPEN_INSPECT_POPUP_ON_EXCEPTION)) {
125                 manager.addDebugContextListener(this);
126             } else {
127                 manager.removeDebugContextListener(this);
128             }
129         }
130         
131     }
132
133 }
134
Popular Tags