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;
20 import org.eclipse.core.resources.IFile;
21 import org.eclipse.core.resources.IMarker;
22 import org.eclipse.core.resources.IResource;
23 import org.eclipse.core.resources.IWorkspaceRoot;
24 import org.eclipse.core.resources.ResourcesPlugin;
25 import org.eclipse.core.resources.IContainer;
26 import org.eclipse.core.runtime.CoreException;
27 import org.eclipse.debug.core.DebugException;
28 import org.eclipse.debug.core.DebugPlugin;
29 import org.eclipse.debug.core.IBreakpointManager;
30 import org.eclipse.debug.core.model.IBreakpoint;
31 import org.eclipse.jface.action.Action;
32 import org.eclipse.jface.text.BadLocationException;
33 import org.eclipse.jface.text.IDocument;
34 import org.eclipse.jface.text.IRegion;
35 import org.eclipse.jface.text.Position;
36 import org.eclipse.jface.text.source.IAnnotationModel;
37 import org.eclipse.jface.text.source.IVerticalRulerInfo;
38 import org.eclipse.ui.IEditorInput;
39 import org.eclipse.ui.IFileEditorInput;
40 import org.eclipse.ui.texteditor.AbstractMarkerAnnotationModel;
41 import org.eclipse.ui.texteditor.IDocumentProvider;
42 import org.eclipse.ui.texteditor.ITextEditor;
43 import org.eclipse.ui.texteditor.IUpdate;
45 public class PHPManageBreakpointRulerAction extends Action implements IUpdate {
47 private IVerticalRulerInfo fRuler;
48 private ITextEditor fTextEditor;
49 private String fMarkerType;
50 private List fMarkers;
52 private String fAddLabel;
53 private String fRemoveLabel;
55 public PHPManageBreakpointRulerAction(IVerticalRulerInfo ruler, ITextEditor editor) {
58 fMarkerType= IBreakpoint.BREAKPOINT_MARKER;
59 fAddLabel= PHPDebugUiMessages.getString("PHPManageBreakpointRulerAction.AddBreakpoint"); //$NON-NLS-1$
60 fRemoveLabel= PHPDebugUiMessages.getString("PHPManageBreakpointRulerAction.RemoveBreakpoint"); //$NON-NLS-1$
64 * Returns the resource for which to create the marker,
65 * or <code>null</code> if there is no applicable resource.
67 * @return the resource for which to create the marker or <code>null</code>
69 protected IResource getResource() {
70 IEditorInput input= fTextEditor.getEditorInput();
72 IResource resource= (IResource) input.getAdapter(IFile.class);
74 if (resource == null) {
75 resource= (IResource) input.getAdapter(IResource.class);
82 * Checks whether a position includes the ruler's line of activity.
84 * @param position the position to be checked
85 * @param document the document the position refers to
86 * @return <code>true</code> if the line is included by the given position
88 protected boolean includesRulerLine(Position position, IDocument document) {
90 if (position != null) {
92 int markerLine= document.getLineOfOffset(position.getOffset());
93 int line= fRuler.getLineOfLastMouseButtonActivity();
94 if (line == markerLine) {
97 } catch (BadLocationException x) {
105 * Returns this action's vertical ruler info.
107 * @return this action's vertical ruler
109 protected IVerticalRulerInfo getVerticalRulerInfo() {
114 * Returns this action's editor.
116 * @return this action's editor
118 protected ITextEditor getTextEditor() {
123 * Returns the <code>AbstractMarkerAnnotationModel</code> of the editor's input.
125 * @return the marker annotation model
127 protected AbstractMarkerAnnotationModel getAnnotationModel() {
128 IDocumentProvider provider= fTextEditor.getDocumentProvider();
129 IAnnotationModel model= provider.getAnnotationModel(fTextEditor.getEditorInput());
130 if (model instanceof AbstractMarkerAnnotationModel) {
131 return (AbstractMarkerAnnotationModel) model;
137 * Returns the <code>IDocument</code> of the editor's input.
139 * @return the document of the editor's input
141 protected IDocument getDocument() {
142 IDocumentProvider provider= fTextEditor.getDocumentProvider();
143 return provider.getDocument(fTextEditor.getEditorInput());
147 * @see IUpdate#update()
149 public void update() {
150 fMarkers= getMarkers();
151 setText(fMarkers.isEmpty() ? fAddLabel : fRemoveLabel);
158 if (fMarkers.isEmpty()) {
161 removeMarkers(fMarkers);
165 protected List getMarkers() {
167 List breakpoints= new ArrayList();
169 IResource resource= getResource();
170 IDocument document= getDocument();
171 AbstractMarkerAnnotationModel model= getAnnotationModel();
176 IMarker[] markers= null;
177 if (resource instanceof IFile)
178 markers= resource.findMarkers(IBreakpoint.BREAKPOINT_MARKER, true, IResource.DEPTH_INFINITE);
180 IWorkspaceRoot root= ResourcesPlugin.getWorkspace().getRoot();
181 markers= root.findMarkers(IBreakpoint.BREAKPOINT_MARKER, true, IResource.DEPTH_INFINITE);
184 if (markers != null) {
185 IBreakpointManager breakpointManager= DebugPlugin.getDefault().getBreakpointManager();
187 for (iFe= 0; iFe < markers.length; iFe++) {
188 IBreakpoint breakpoint= breakpointManager.getBreakpoint(markers[iFe]);
189 if (breakpoint != null && breakpointManager.isRegistered(breakpoint) &&
190 includesRulerLine(model.getMarkerPosition(markers[iFe]), document))
191 breakpoints.add(markers[iFe]);
194 } catch (CoreException x) {
195 System.out.println(x.getStatus());
196 // JDIDebugUIPlugin.log(x.getStatus());
202 protected void addMarker() {
204 //IResource resource= getResource();
205 IEditorInput editorInput= getTextEditor().getEditorInput();
206 IDocument document= getDocument();
207 //IBreakpointManager breakpointManager= DebugPlugin.getDefault().getBreakpointManager();
209 int rulerLine= getVerticalRulerInfo().getLineOfLastMouseButtonActivity();
212 // Falta verificar si la ubicación del Breakpoint es válida
213 int lineNumber= rulerLine + 1;
215 if (lineNumber > 0) {
216 if (PHPDebugModel.lineBreakpointExists(lineNumber)==null) {
217 // Map attributes = new HashMap(10);
218 IRegion line= document.getLineInformation(lineNumber - 1);
219 int start= line.getOffset();
220 int lenline= line.getLength();
221 //int end= start + ((lenline > 0)?lenline:0);
222 int end= start + lenline;
224 //PHPDebugModel.createLineBreakpoint(getResource(), lineNumber, start, end, 0, true, attributes);
225 PHPDebugModel.createLineBreakpoint(((IFileEditorInput) editorInput).getFile(), lineNumber, start, end, 0, true, null);
226 // PHPDebugModel.createLineBreakpoint(((IFileEditorInput) editorInput).getFile(), lineNumber, 0, true, attributes);
230 } catch (DebugException e) {
231 System.out.println("Error");
232 } catch (CoreException e) {
233 System.out.println("Error");
234 } catch (BadLocationException e) {
235 System.out.println("Error");
239 protected void removeMarkers(List markers) {
240 IBreakpointManager breakpointManager= DebugPlugin.getDefault().getBreakpointManager();
242 Iterator e= markers.iterator();
243 while (e.hasNext()) {
244 IBreakpoint breakpoint= breakpointManager.getBreakpoint((IMarker) e.next());
245 breakpointManager.removeBreakpoint(breakpoint, true);
247 } catch (CoreException e) {
251 public IResource getUnderlyingResource(String fName) {
252 IResource parentResource = getResource(); //fParent.getUnderlyingResource();
253 if (parentResource == null) {
256 int type = parentResource.getType();
257 if (type == IResource.FOLDER || type == IResource.PROJECT) {
258 IContainer folder = (IContainer) parentResource;
259 IResource resource = folder.findMember(fName);
260 if (resource == null) {
261 //throw newNotPresentException();
267 return parentResource;