1 /**********************************************************************
2 Copyright (c) 2000, 2002 IBM Corp. 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
9 IBM Corporation - Initial implementation
10 Vicente Fernando - www.alfersoft.com.ar
11 **********************************************************************/
12 package net.sourceforge.phpdt.internal.debug.ui.actions;
14 import java.util.ArrayList;
15 import java.util.Iterator;
16 import java.util.List;
18 import net.sourceforge.phpdt.debug.core.PHPDebugModel;
19 import net.sourceforge.phpdt.internal.debug.ui.PHPDebugUiMessages;
21 import org.eclipse.core.resources.IContainer;
22 import org.eclipse.core.resources.IFile;
23 import org.eclipse.core.resources.IMarker;
24 import org.eclipse.core.resources.IResource;
25 import org.eclipse.core.resources.IWorkspaceRoot;
26 import org.eclipse.core.resources.ResourcesPlugin;
27 import org.eclipse.core.runtime.CoreException;
28 import org.eclipse.debug.core.DebugException;
29 import org.eclipse.debug.core.DebugPlugin;
30 import org.eclipse.debug.core.IBreakpointManager;
31 import org.eclipse.debug.core.model.IBreakpoint;
32 import org.eclipse.jface.action.Action;
33 import org.eclipse.jface.text.BadLocationException;
34 import org.eclipse.jface.text.IDocument;
35 import org.eclipse.jface.text.IRegion;
36 import org.eclipse.jface.text.Position;
37 import org.eclipse.jface.text.source.IAnnotationModel;
38 import org.eclipse.jface.text.source.IVerticalRulerInfo;
39 import org.eclipse.ui.IEditorInput;
40 import org.eclipse.ui.IFileEditorInput;
41 import org.eclipse.ui.texteditor.AbstractMarkerAnnotationModel;
42 import org.eclipse.ui.texteditor.IDocumentProvider;
43 import org.eclipse.ui.texteditor.ITextEditor;
44 import org.eclipse.ui.texteditor.IUpdate;
46 public class PHPManageBreakpointRulerAction extends Action implements IUpdate {
48 private IVerticalRulerInfo fRuler;
50 private ITextEditor fTextEditor;
52 private String fMarkerType;
54 private List fMarkers;
56 private String fAddLabel;
58 private String fRemoveLabel;
60 public PHPManageBreakpointRulerAction(IVerticalRulerInfo ruler,
64 fMarkerType = IBreakpoint.BREAKPOINT_MARKER;
65 fAddLabel = PHPDebugUiMessages
66 .getString("PHPManageBreakpointRulerAction.ToggleBreakpoint"); //$NON-NLS-1$
67 fRemoveLabel = PHPDebugUiMessages
68 .getString("PHPManageBreakpointRulerAction.ToggleBreakpoint"); //$NON-NLS-1$
72 * Returns the resource for which to create the marker, or <code>null</code>
73 * if there is no applicable resource.
75 * @return the resource for which to create the marker or <code>null</code>
77 protected IResource getResource() {
78 IEditorInput input = fTextEditor.getEditorInput();
80 IResource resource = (IResource) input.getAdapter(IFile.class);
82 if (resource == null) {
83 resource = (IResource) input.getAdapter(IResource.class);
90 * Checks whether a position includes the ruler's line of activity.
93 * the position to be checked
95 * the document the position refers to
96 * @return <code>true</code> if the line is included by the given position
98 protected boolean includesRulerLine(Position position, IDocument document) {
100 if (position != null) {
102 int markerLine = document.getLineOfOffset(position.getOffset());
103 int line = fRuler.getLineOfLastMouseButtonActivity();
104 if (line == markerLine) {
107 } catch (BadLocationException x) {
115 * Returns this action's vertical ruler info.
117 * @return this action's vertical ruler
119 protected IVerticalRulerInfo getVerticalRulerInfo() {
124 * Returns this action's editor.
126 * @return this action's editor
128 protected ITextEditor getTextEditor() {
133 * Returns the <code>AbstractMarkerAnnotationModel</code> of the editor's
136 * @return the marker annotation model
138 protected AbstractMarkerAnnotationModel getAnnotationModel() {
139 IDocumentProvider provider = fTextEditor.getDocumentProvider();
140 IAnnotationModel model = provider.getAnnotationModel(fTextEditor
142 if (model instanceof AbstractMarkerAnnotationModel) {
143 return (AbstractMarkerAnnotationModel) model;
149 * Returns the <code>IDocument</code> of the editor's input.
151 * @return the document of the editor's input
153 protected IDocument getDocument() {
154 IDocumentProvider provider = fTextEditor.getDocumentProvider();
155 return provider.getDocument(fTextEditor.getEditorInput());
159 * @see IUpdate#update()
161 public void update() {
162 fMarkers = getMarkers();
163 setText(fMarkers.isEmpty() ? fAddLabel : fRemoveLabel);
170 if (fMarkers.isEmpty()) {
173 removeMarkers(fMarkers);
177 protected List getMarkers() {
179 List breakpoints = new ArrayList();
181 IResource resource = getResource();
182 IDocument document = getDocument();
183 AbstractMarkerAnnotationModel model = getAnnotationModel();
188 IMarker[] markers = null;
189 if (resource instanceof IFile)
190 markers = resource.findMarkers(
191 IBreakpoint.BREAKPOINT_MARKER, true,
192 IResource.DEPTH_INFINITE);
194 IWorkspaceRoot root = ResourcesPlugin.getWorkspace()
196 markers = root.findMarkers(IBreakpoint.BREAKPOINT_MARKER,
197 true, IResource.DEPTH_INFINITE);
200 if (markers != null) {
201 IBreakpointManager breakpointManager = DebugPlugin
202 .getDefault().getBreakpointManager();
204 for (iFe = 0; iFe < markers.length; iFe++) {
205 IBreakpoint breakpoint = breakpointManager
206 .getBreakpoint(markers[iFe]);
207 if (breakpoint != null
208 && breakpointManager.isRegistered(breakpoint)
209 && includesRulerLine(model
210 .getMarkerPosition(markers[iFe]),
212 breakpoints.add(markers[iFe]);
215 } catch (CoreException x) {
216 System.out.println(x.getStatus());
217 // JDIDebugUIPlugin.log(x.getStatus());
223 protected void addMarker() {
225 // IResource resource= getResource();
226 IEditorInput editorInput = getTextEditor().getEditorInput();
227 IDocument document = getDocument();
228 // IBreakpointManager breakpointManager=
229 // DebugPlugin.getDefault().getBreakpointManager();
231 int rulerLine = getVerticalRulerInfo()
232 .getLineOfLastMouseButtonActivity();
235 // Falta verificar si la ubicaci�n del Breakpoint es v�lida
236 int lineNumber = rulerLine + 1;
238 if (lineNumber > 0) {
239 if (PHPDebugModel.lineBreakpointExists(lineNumber) == null) {
240 // Map attributes = new HashMap(10);
241 IRegion line = document.getLineInformation(lineNumber - 1);
242 int start = line.getOffset();
243 int lenline = line.getLength();
244 // int end= start + ((lenline > 0)?lenline:0);
245 int end = start + lenline;
247 // PHPDebugModel.createLineBreakpoint(getResource(),
248 // lineNumber, start, end, 0, true, attributes);
249 PHPDebugModel.createLineBreakpoint(
250 ((IFileEditorInput) editorInput).getFile(),
251 lineNumber, start, end, 0, true, null);
252 // PHPDebugModel.createLineBreakpoint(((IFileEditorInput)
253 // editorInput).getFile(), lineNumber, 0, true, attributes);
257 } catch (DebugException e) {
258 System.out.println("Error");
259 } catch (CoreException e) {
260 System.out.println("Error");
261 } catch (BadLocationException e) {
262 System.out.println("Error");
266 protected void removeMarkers(List markers) {
267 IBreakpointManager breakpointManager = DebugPlugin.getDefault()
268 .getBreakpointManager();
270 Iterator e = markers.iterator();
271 while (e.hasNext()) {
272 IBreakpoint breakpoint = breakpointManager
273 .getBreakpoint((IMarker) e.next());
274 breakpointManager.removeBreakpoint(breakpoint, true);
276 } catch (CoreException e) {
280 public IResource getUnderlyingResource(String fName) {
281 IResource parentResource = getResource(); // fParent.getUnderlyingResource();
282 if (parentResource == null) {
285 int type = parentResource.getType();
286 if (type == IResource.FOLDER || type == IResource.PROJECT) {
287 IContainer folder = (IContainer) parentResource;
288 IResource resource = folder.findMember(fName);
289 if (resource == null) {
290 // throw newNotPresentException();
296 return parentResource;