KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > debug > ui > actions > ManageBreakpointRulerAction


1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.debug.ui.actions;
12
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17
18 import org.eclipse.core.resources.IFile;
19 import org.eclipse.core.resources.IMarker;
20 import org.eclipse.core.resources.IResource;
21 import org.eclipse.core.resources.IWorkspaceRoot;
22 import org.eclipse.core.resources.ResourcesPlugin;
23 import org.eclipse.core.runtime.CoreException;
24 import org.eclipse.debug.core.DebugPlugin;
25 import org.eclipse.debug.core.IBreakpointManager;
26 import org.eclipse.debug.core.model.IBreakpoint;
27 import org.eclipse.jdt.internal.debug.ui.JDIDebugUIPlugin;
28 import org.eclipse.jface.action.Action;
29 import org.eclipse.jface.text.BadLocationException;
30 import org.eclipse.jface.text.IDocument;
31 import org.eclipse.jface.text.IRegion;
32 import org.eclipse.jface.text.ITextSelection;
33 import org.eclipse.jface.text.Position;
34 import org.eclipse.jface.text.TextSelection;
35 import org.eclipse.jface.text.source.IAnnotationModel;
36 import org.eclipse.jface.text.source.IVerticalRulerInfo;
37 import org.eclipse.ui.texteditor.AbstractMarkerAnnotationModel;
38 import org.eclipse.ui.texteditor.IDocumentProvider;
39 import org.eclipse.ui.texteditor.ITextEditor;
40
41 public class ManageBreakpointRulerAction extends Action {
42     
43     private IVerticalRulerInfo fRuler;
44     private ITextEditor fTextEditor;
45     private ToggleBreakpointAdapter fBreakpointAdapter;
46
47     public ManageBreakpointRulerAction(IVerticalRulerInfo ruler, ITextEditor editor) {
48         super(ActionMessages.getString("ManageBreakpointRulerAction.label")); //$NON-NLS-1$
49
fRuler= ruler;
50         fTextEditor= editor;
51         fBreakpointAdapter = new ToggleBreakpointAdapter();
52     }
53     
54     /**
55      * Disposes this action
56      */

57     public void dispose() {
58         fTextEditor = null;
59         fRuler = null;
60     }
61         
62     /**
63      * Returns this action's vertical ruler info.
64      *
65      * @return this action's vertical ruler
66      */

67     protected IVerticalRulerInfo getVerticalRulerInfo() {
68         return fRuler;
69     }
70     
71     /**
72      * Returns this action's editor.
73      *
74      * @return this action's editor
75      */

76     protected ITextEditor getTextEditor() {
77         return fTextEditor;
78     }
79     
80     /**
81      * Returns the <code>IDocument</code> of the editor's input.
82      *
83      * @return the document of the editor's input
84      */

85     protected IDocument getDocument() {
86         IDocumentProvider provider= fTextEditor.getDocumentProvider();
87         return provider.getDocument(fTextEditor.getEditorInput());
88     }
89     
90     /* (non-Javadoc)
91      * @see org.eclipse.jface.action.IAction#run()
92      */

93     public void run() {
94         try {
95             List JavaDoc list = getMarkers();
96             if (list.isEmpty()) {
97                 // create new markers
98
IDocument document= getDocument();
99                 int lineNumber= getVerticalRulerInfo().getLineOfLastMouseButtonActivity();
100                 if (lineNumber >= document.getNumberOfLines()) {
101                     return;
102                 }
103                 try {
104                     IRegion line= document.getLineInformation(lineNumber);
105                     ITextSelection selection = new TextSelection(document, line.getOffset(), line.getLength());
106                     fBreakpointAdapter.toggleLineBreakpoints(fTextEditor, selection);
107                 } catch (BadLocationException e) {
108                     //likely document is folded so you cannot get the line information of the folded line
109
}
110             } else {
111                 // remove existing breakpoints of any type
112
IBreakpointManager manager = DebugPlugin.getDefault().getBreakpointManager();
113                 Iterator JavaDoc iterator = list.iterator();
114                 while (iterator.hasNext()) {
115                     IMarker marker = (IMarker) iterator.next();
116                     IBreakpoint breakpoint = manager.getBreakpoint(marker);
117                     if (breakpoint != null) {
118                         breakpoint.delete();
119                     }
120                 }
121             }
122         } catch (CoreException e) {
123             JDIDebugUIPlugin.errorDialog(ActionMessages.getString("ManageBreakpointRulerAction.error.adding.message1"), e); //$NON-NLS-1$
124
}
125     }
126     
127     /**
128      * Returns a list of markers that exist at the current ruler location.
129      *
130      * @return a list of markers that exist at the current ruler location
131      */

132     protected List JavaDoc getMarkers() {
133         
134         List JavaDoc breakpoints= new ArrayList JavaDoc();
135         
136         IResource resource= ToggleBreakpointAdapter.getResource(fTextEditor);
137         IDocument document= getDocument();
138         AbstractMarkerAnnotationModel model= getAnnotationModel();
139         
140         if (model != null) {
141             try {
142                 
143                 IMarker[] markers= null;
144                 if (resource instanceof IFile)
145                     markers= resource.findMarkers(IBreakpoint.BREAKPOINT_MARKER, true, IResource.DEPTH_INFINITE);
146                 else {
147                     IWorkspaceRoot root= ResourcesPlugin.getWorkspace().getRoot();
148                     markers= root.findMarkers(IBreakpoint.BREAKPOINT_MARKER, true, IResource.DEPTH_INFINITE);
149                 }
150                 
151                 if (markers != null) {
152                     IBreakpointManager breakpointManager= DebugPlugin.getDefault().getBreakpointManager();
153                     for (int i= 0; i < markers.length; i++) {
154                         IBreakpoint breakpoint= breakpointManager.getBreakpoint(markers[i]);
155                         if (breakpoint != null && breakpointManager.isRegistered(breakpoint) &&
156                                 includesRulerLine(model.getMarkerPosition(markers[i]), document))
157                             breakpoints.add(markers[i]);
158                     }
159                 }
160             } catch (CoreException x) {
161                 JDIDebugUIPlugin.log(x.getStatus());
162             }
163         }
164         return breakpoints;
165     }
166     
167     /**
168      * Returns the <code>AbstractMarkerAnnotationModel</code> of the editor's input.
169      *
170      * @return the marker annotation model
171      */

172     protected AbstractMarkerAnnotationModel getAnnotationModel() {
173         IDocumentProvider provider= fTextEditor.getDocumentProvider();
174         IAnnotationModel model= provider.getAnnotationModel(fTextEditor.getEditorInput());
175         if (model instanceof AbstractMarkerAnnotationModel) {
176             return (AbstractMarkerAnnotationModel) model;
177         }
178         return null;
179     }
180     
181     /**
182      * Checks whether a position includes the ruler's line of activity.
183      *
184      * @param position the position to be checked
185      * @param document the document the position refers to
186      * @return <code>true</code> if the line is included by the given position
187      */

188     protected boolean includesRulerLine(Position position, IDocument document) {
189
190         if (position != null) {
191             try {
192                 int markerLine= document.getLineOfOffset(position.getOffset());
193                 int line= fRuler.getLineOfLastMouseButtonActivity();
194                 if (line == markerLine) {
195                     return true;
196                 }
197             } catch (BadLocationException x) {
198             }
199         }
200         
201         return false;
202     }
203 }
204
Popular Tags