KickJava   Java API By Example, From Geeks To Geeks.

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


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.breakpoints;
12
13
14 import java.util.HashMap JavaDoc;
15 import java.util.Map JavaDoc;
16
17 import org.eclipse.core.resources.IResource;
18 import org.eclipse.core.runtime.CoreException;
19 import org.eclipse.core.runtime.IProgressMonitor;
20 import org.eclipse.core.runtime.IStatus;
21 import org.eclipse.core.runtime.NullProgressMonitor;
22 import org.eclipse.core.runtime.Status;
23 import org.eclipse.core.runtime.jobs.Job;
24 import org.eclipse.debug.core.DebugPlugin;
25 import org.eclipse.debug.core.model.IBreakpoint;
26 import org.eclipse.jdt.core.IType;
27 import org.eclipse.jdt.core.ITypeHierarchy;
28 import org.eclipse.jdt.core.JavaModelException;
29 import org.eclipse.jdt.core.search.TypeNameMatch;
30 import org.eclipse.jdt.debug.core.IJavaBreakpoint;
31 import org.eclipse.jdt.debug.core.IJavaExceptionBreakpoint;
32 import org.eclipse.jdt.debug.core.JDIDebugModel;
33 import org.eclipse.jdt.internal.debug.ui.BreakpointUtils;
34 import org.eclipse.jdt.internal.debug.ui.JDIDebugUIPlugin;
35 import org.eclipse.jface.action.IAction;
36 import org.eclipse.jface.dialogs.IDialogConstants;
37 import org.eclipse.jface.viewers.ISelection;
38 import org.eclipse.ui.IViewActionDelegate;
39 import org.eclipse.ui.IViewPart;
40 import org.eclipse.ui.IWorkbenchWindow;
41 import org.eclipse.ui.IWorkbenchWindowActionDelegate;
42
43 /**
44  * The workbench menu action for adding an exception breakpoint
45  */

46 public class AddExceptionAction implements IViewActionDelegate, IWorkbenchWindowActionDelegate {
47     
48     /* (non-Javadoc)
49      * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
50      */

51     public void run(IAction action) {
52         AddExceptionDialog dialog = new AddExceptionDialog();
53         if(dialog.open() == IDialogConstants.OK_ID) {
54             boolean caught = dialog.shouldHandleCaughtExceptions(),
55                     uncaught = dialog.shouldHandleUncaughtExceptions();
56             Object JavaDoc[] results = dialog.getResult();
57             if(results != null && results.length > 0) {
58                 try {
59                     createBreakpoint(caught, uncaught, ((TypeNameMatch)results[0]).getType());
60                 }
61                 catch (CoreException e) {JDIDebugUIPlugin.statusDialog(e.getStatus());}
62             }
63         }
64     }
65     
66     /**
67      * creates a single breakpoint of the specified type
68      * @param caught if the exception is caught
69      * @param uncaught if the exception is uncaught
70      * @param type the type of the exception
71      * @since 3.2
72      */

73     private void createBreakpoint(final boolean caught, final boolean uncaught, final IType type) throws CoreException {
74         final IResource resource = BreakpointUtils.getBreakpointResource(type);
75         final Map JavaDoc map = new HashMap JavaDoc(10);
76         BreakpointUtils.addJavaBreakpointAttributes(map, type);
77         IBreakpoint[] breakpoints = DebugPlugin.getDefault().getBreakpointManager().getBreakpoints(
78                         JDIDebugModel.getPluginIdentifier());
79         boolean exists = false;
80         for (int j = 0; j < breakpoints.length; j++) {
81             IJavaBreakpoint breakpoint = (IJavaBreakpoint) breakpoints[j];
82             if (breakpoint instanceof IJavaExceptionBreakpoint) {
83                 if (breakpoint.getTypeName().equals(type.getFullyQualifiedName())) {
84                     exists = true;
85                     break;
86                 }
87             }
88         }
89         if (!exists) {
90             new Job(BreakpointMessages.AddExceptionAction_0) {
91                 protected IStatus run(IProgressMonitor monitor) {
92                     try {
93                         JDIDebugModel.createExceptionBreakpoint(resource,
94                                 type.getFullyQualifiedName(), caught,
95                                 uncaught, isChecked(type), true, map);
96                         return Status.OK_STATUS;
97                     } catch (CoreException e) {
98                         return e.getStatus();
99                     }
100                 }
101
102             }.schedule();
103         }
104     }
105     
106     /**
107      * returns if the exception should be marked as 'checked' or not
108      * @param type the type of the exception
109      * @return true if the exception is to be 'checked' false otherwise
110      * @since 3.2
111      */

112     public static boolean isChecked(IType type) {
113          if(type != null) {
114             try {
115                 ITypeHierarchy hierarchy = type.newSupertypeHierarchy(new NullProgressMonitor());
116                 IType curr = type;
117                 while (curr != null) {
118                     String JavaDoc name = curr.getFullyQualifiedName('.');
119                     if ("java.lang.RuntimeException".equals(name) || "java.lang.Error".equals(name)) { //$NON-NLS-2$ //$NON-NLS-1$
120
return false;
121                     }
122                     curr = hierarchy.getSuperclass(curr);
123                 }
124             }
125             catch (JavaModelException e) {JDIDebugUIPlugin.log(e);}
126          }
127         return true;
128     }
129     
130     /* (non-Javadoc)
131      * @see org.eclipse.ui.IViewActionDelegate#init(org.eclipse.ui.IViewPart)
132      */

133     public void init(IViewPart view) {}
134
135     /* (non-Javadoc)
136      * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction, org.eclipse.jface.viewers.ISelection)
137      */

138     public void selectionChanged(IAction action, ISelection selection) {}
139     
140     /* (non-Javadoc)
141      * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#dispose()
142      */

143     public void dispose() {}
144
145     /* (non-Javadoc)
146      * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#init(org.eclipse.ui.IWorkbenchWindow)
147      */

148     public void init(IWorkbenchWindow window) {}
149 }
150
Popular Tags