KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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 java.util.HashMap JavaDoc;
14 import java.util.Map JavaDoc;
15
16 import org.eclipse.core.resources.IResource;
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.core.runtime.IProgressMonitor;
19 import org.eclipse.core.runtime.IStatus;
20 import org.eclipse.core.runtime.Status;
21 import org.eclipse.core.runtime.jobs.Job;
22 import org.eclipse.debug.core.DebugPlugin;
23 import org.eclipse.debug.core.model.IBreakpoint;
24 import org.eclipse.jdt.core.ISourceRange;
25 import org.eclipse.jdt.core.IType;
26 import org.eclipse.jdt.core.search.SearchEngine;
27 import org.eclipse.jdt.debug.core.IJavaBreakpoint;
28 import org.eclipse.jdt.debug.core.IJavaClassPrepareBreakpoint;
29 import org.eclipse.jdt.debug.core.JDIDebugModel;
30 import org.eclipse.jdt.internal.debug.ui.BreakpointUtils;
31 import org.eclipse.jdt.internal.debug.ui.JDIDebugUIPlugin;
32 import org.eclipse.jdt.ui.IJavaElementSearchConstants;
33 import org.eclipse.jdt.ui.JavaUI;
34 import org.eclipse.jface.action.IAction;
35 import org.eclipse.jface.dialogs.IDialogConstants;
36 import org.eclipse.jface.viewers.ISelection;
37 import org.eclipse.swt.widgets.Shell;
38 import org.eclipse.ui.IWorkbenchWindow;
39 import org.eclipse.ui.IWorkbenchWindowActionDelegate;
40 import org.eclipse.ui.dialogs.SelectionDialog;
41
42 /**
43  * The workbench action for creating a class load breakpoint
44  */

45 public class AddClassPrepareBreakpointAction implements IWorkbenchWindowActionDelegate {
46
47     /**
48      * the current workbench window
49      */

50     private IWorkbenchWindow workbenchWindow;
51
52     /**
53      * Creates the breakpoints from the array of returned selections
54      * @param selection the selections form the dialog
55      * @since 3.2
56      */

57     private void createBreakpoints(final Object JavaDoc[] selection) {
58         try {
59             for (int i = 0; i < selection.length; i++) {
60                 final IType type = (IType) selection[i];
61                 final IResource resource = BreakpointUtils.getBreakpointResource(type);
62                 final Map JavaDoc map = new HashMap JavaDoc(10);
63                 BreakpointUtils.addJavaBreakpointAttributes(map, type);
64                 int kind = IJavaClassPrepareBreakpoint.TYPE_CLASS;
65                 if (!type.isClass()) {
66                     kind = IJavaClassPrepareBreakpoint.TYPE_INTERFACE;
67                 }
68                 IBreakpoint[] breakpoints = DebugPlugin.getDefault().getBreakpointManager().getBreakpoints(JDIDebugModel.getPluginIdentifier());
69                 boolean exists = false;
70                 for (int j = 0; j < breakpoints.length; j++) {
71                     IJavaBreakpoint breakpoint = (IJavaBreakpoint) breakpoints[j];
72                     if (breakpoint instanceof IJavaClassPrepareBreakpoint) {
73                         if (breakpoint.getTypeName().equals(type.getFullyQualifiedName())) {
74                             exists = true;
75                             break;
76                         }
77                     }
78                 }
79                 if (!exists) {
80                     ISourceRange range = type.getNameRange();
81                     int start = -1;
82                     int end = -1;
83                     if (range != null) {
84                         start = range.getOffset();
85                         end = start + range.getLength();
86                     }
87                     final int finalKind = kind;
88                     final int finalStart = start;
89                     final int finalEnd = end;
90                     new Job(BreakpointMessages.AddClassPrepareBreakpointAction_2) {
91                         protected IStatus run(IProgressMonitor monitor) {
92                             try {
93                                 JDIDebugModel.createClassPrepareBreakpoint(resource, type.getFullyQualifiedName(), finalKind, finalStart, finalEnd, true, map);
94                                 return Status.OK_STATUS;
95                             } catch (CoreException e) {
96                                 return e.getStatus();
97                             }
98                         }
99     
100                     }.schedule();
101                 }
102             }
103         } catch(CoreException e) {
104             JDIDebugUIPlugin.statusDialog(e.getStatus());
105         }
106     }
107     
108     /* (non-Javadoc)
109      * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
110      */

111     public void run(IAction action) {
112         Shell shell = JDIDebugUIPlugin.getActiveWorkbenchShell();
113         SelectionDialog dialog = null;
114         try {
115             dialog = JavaUI.createTypeDialog(shell, workbenchWindow,
116                                              SearchEngine.createWorkspaceScope(),
117                                              IJavaElementSearchConstants.CONSIDER_CLASSES,
118                                              true, "", null); //$NON-NLS-1$
119
dialog.setTitle(BreakpointMessages.AddClassPrepareBreakpointAction_0);
120             dialog.setMessage(BreakpointMessages.AddClassPrepareBreakpointAction_1);
121             if (dialog.open() == IDialogConstants.OK_ID) {
122                createBreakpoints(dialog.getResult());
123             }
124         } catch (CoreException e) {JDIDebugUIPlugin.log(e);}
125     }
126     
127     /* (non-Javadoc)
128      * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction, org.eclipse.jface.viewers.ISelection)
129      */

130     public void selectionChanged(IAction action, ISelection selection) {}
131
132     /* (non-Javadoc)
133      * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#dispose()
134      */

135     public void dispose() {
136         workbenchWindow = null;
137     }
138
139     /* (non-Javadoc)
140      * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#init(org.eclipse.ui.IWorkbenchWindow)
141      */

142     public void init(IWorkbenchWindow window) {
143         workbenchWindow = window;
144     }
145 }
146
Popular Tags