KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > ui > actions > ToggleBreakpointAction


1 /*******************************************************************************
2  * Copyright (c) 2005, 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.debug.ui.actions;
12
13 import org.eclipse.core.runtime.CoreException;
14 import org.eclipse.core.runtime.IAdapterManager;
15 import org.eclipse.core.runtime.Platform;
16 import org.eclipse.debug.internal.ui.DebugUIPlugin;
17 import org.eclipse.debug.internal.ui.actions.ActionMessages;
18 import org.eclipse.jface.action.Action;
19 import org.eclipse.jface.text.BadLocationException;
20 import org.eclipse.jface.text.IDocument;
21 import org.eclipse.jface.text.IRegion;
22 import org.eclipse.jface.text.ITextSelection;
23 import org.eclipse.jface.text.TextSelection;
24 import org.eclipse.jface.text.source.IVerticalRulerInfo;
25 import org.eclipse.ui.IWorkbenchPart;
26 import org.eclipse.ui.texteditor.IDocumentProvider;
27 import org.eclipse.ui.texteditor.ITextEditor;
28 import org.eclipse.ui.texteditor.IUpdate;
29
30 /**
31  * Action to toggle a breakpoint in a vertical ruler of a workbench part
32  * containing a document. The part must provide an <code>IToggleBreakpointsTarget</code>
33  * adapter which may optionally be an instance of an
34  * <code>IToggleBreakpointsTargetExtension</code>.
35  * <p>
36  * Clients may instantiate this class. This class is not intended to be subclassed.
37  * </p>
38  * @since 3.1
39  * @see org.eclipse.debug.ui.actions.RulerToggleBreakpointActionDelegate
40  */

41 public class ToggleBreakpointAction extends Action implements IUpdate {
42     
43     private IWorkbenchPart fPart;
44     private IDocument fDocument;
45     private IVerticalRulerInfo fRulerInfo;
46
47     /**
48      * Constructs a new action to toggle a breakpoint in the given
49      * part containing the given document and ruler.
50      *
51      * @param part the part in which to toggle the breakpoint - provides
52      * an <code>IToggleBreakpointsTarget</code> adapter
53      * @param document the document breakpoints are being set in or
54      * <code>null</code> when the document should be derived from the
55      * given part
56      * @param rulerInfo specifies location the user has double-clicked
57      */

58     public ToggleBreakpointAction(IWorkbenchPart part, IDocument document, IVerticalRulerInfo rulerInfo) {
59         super(ActionMessages.ToggleBreakpointAction_0);
60         fPart = part;
61         fDocument = document;
62         fRulerInfo = rulerInfo;
63     }
64     /*
65      * (non-Javadoc)
66      * @see org.eclipse.jface.action.IAction#run()
67      */

68     public void run() {
69         IDocument document= getDocument();
70         if (document == null) {
71             return;
72         }
73         IToggleBreakpointsTarget adapter = (IToggleBreakpointsTarget) fPart.getAdapter(IToggleBreakpointsTarget.class);
74         if (adapter == null) {
75             // attempt to force load adapter
76
IAdapterManager manager = Platform.getAdapterManager();
77             if (manager.hasAdapter(fPart, IToggleBreakpointsTarget.class.getName())) {
78                 adapter = (IToggleBreakpointsTarget) manager.loadAdapter(fPart, IToggleBreakpointsTarget.class.getName());
79             }
80         }
81         if (adapter == null) {
82             return;
83         }
84         int line = fRulerInfo.getLineOfLastMouseButtonActivity();
85         
86         // Test if line is valid
87
if (line == -1)
88             return;
89
90         /*
91          * XXX: remove once the following bug is fixed:
92          * https://bugs.eclipse.org/bugs/show_bug.cgi?id=99234
93          */

94         if (line >= document.getNumberOfLines())
95             return;
96         
97         try {
98             IRegion region = document.getLineInformation(line);
99             ITextSelection selection = new TextSelection(document, region.getOffset(), 0);
100             if (adapter instanceof IToggleBreakpointsTargetExtension) {
101                 IToggleBreakpointsTargetExtension extension = (IToggleBreakpointsTargetExtension) adapter;
102                 if (extension.canToggleBreakpoints(fPart, selection)) {
103                     extension.toggleBreakpoints(fPart, selection);
104                     return;
105                 }
106             }
107             if (adapter.canToggleLineBreakpoints(fPart, selection)) {
108                 adapter.toggleLineBreakpoints(fPart, selection);
109             } else if (adapter.canToggleWatchpoints(fPart, selection)) {
110                 adapter.toggleWatchpoints(fPart, selection);
111             } else if (adapter.canToggleMethodBreakpoints(fPart, selection)) {
112                 adapter.toggleMethodBreakpoints(fPart, selection);
113             }
114         } catch (BadLocationException e) {
115             reportException(e);
116         } catch (CoreException e) {
117             reportException(e);
118         }
119     }
120     
121     /**
122      * Report an error to the user.
123      *
124      * @param e underlying exception
125      */

126     private void reportException(Exception JavaDoc e) {
127         DebugUIPlugin.errorDialog(fPart.getSite().getShell(), ActionMessages.ToggleBreakpointAction_1, ActionMessages.ToggleBreakpointAction_2, e); //
128
}
129     
130     /**
131      * Disposes this action. Clients must call this method when
132      * this action is no longer needed.
133      */

134     public void dispose() {
135         fDocument = null;
136         fPart = null;
137         fRulerInfo = null;
138     }
139
140     /**
141      * Returns the document on which this action operates.
142      *
143      * @return the document or <code>null</code> if none
144      */

145     private IDocument getDocument() {
146         if (fDocument != null)
147             return fDocument;
148         
149         if (fPart instanceof ITextEditor) {
150             ITextEditor editor= (ITextEditor)fPart;
151             IDocumentProvider provider = editor.getDocumentProvider();
152             if (provider != null)
153                 return provider.getDocument(editor.getEditorInput());
154         }
155         
156         IDocument doc = (IDocument) fPart.getAdapter(IDocument.class);
157         if (doc != null) {
158             return doc;
159         }
160         
161         return null;
162     }
163     
164     /* (non-Javadoc)
165      * @see org.eclipse.ui.texteditor.IUpdate#update()
166      */

167     public void update() {
168         IDocument document= getDocument();
169         if (document != null) {
170             IToggleBreakpointsTarget adapter = (IToggleBreakpointsTarget) fPart.getAdapter(IToggleBreakpointsTarget.class);
171             if (adapter == null) {
172                 // attempt to force load adapter
173
IAdapterManager manager = Platform.getAdapterManager();
174                 if (manager.hasAdapter(fPart, IToggleBreakpointsTarget.class.getName())) {
175                     adapter = (IToggleBreakpointsTarget) manager.loadAdapter(fPart, IToggleBreakpointsTarget.class.getName());
176                 }
177             }
178             if (adapter != null) {
179                 int line = fRulerInfo.getLineOfLastMouseButtonActivity();
180                 /*
181                  * XXX: remove once the following bug is fixed:
182                  * https://bugs.eclipse.org/bugs/show_bug.cgi?id=99234
183                  */

184                 if (line > -1 & line < document.getNumberOfLines()) {
185                     try {
186                         IRegion region = document.getLineInformation(line);
187                         ITextSelection selection = new TextSelection(document, region.getOffset(), 0);
188                         if (adapter instanceof IToggleBreakpointsTargetExtension) {
189                             IToggleBreakpointsTargetExtension extension = (IToggleBreakpointsTargetExtension) adapter;
190                             if (extension.canToggleBreakpoints(fPart, selection)) {
191                                 setEnabled(true);
192                                 return;
193                             }
194                         }
195                         if (adapter.canToggleLineBreakpoints(fPart, selection) |
196                             adapter.canToggleWatchpoints(fPart, selection) |
197                             adapter.canToggleMethodBreakpoints(fPart, selection)) {
198                                 setEnabled(true);
199                                 return;
200                         }
201                     } catch (BadLocationException e) {
202                         reportException(e);
203                     }
204                 }
205             }
206         }
207         setEnabled(false);
208     }
209
210 }
211
Popular Tags