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.HashMap;
16 import java.util.Iterator;
17 import java.util.List;
20 import net.sourceforge.phpdt.debug.core.PHPDebugModel;
21 import net.sourceforge.phpdt.internal.debug.ui.PHPDebugUiMessages;
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.resources.IContainer;
28 import org.eclipse.core.runtime.CoreException;
29 import org.eclipse.debug.core.DebugException;
30 import org.eclipse.debug.core.DebugPlugin;
31 import org.eclipse.debug.core.IBreakpointManager;
32 import org.eclipse.debug.core.model.IBreakpoint;
33 import org.eclipse.jface.action.Action;
34 import org.eclipse.jface.text.BadLocationException;
35 import org.eclipse.jface.text.IDocument;
36 import org.eclipse.jface.text.IRegion;
37 import org.eclipse.jface.text.Position;
38 import org.eclipse.jface.text.source.IAnnotationModel;
39 import org.eclipse.jface.text.source.IVerticalRulerInfo;
40 import org.eclipse.ui.IEditorInput;
41 import org.eclipse.ui.IFileEditorInput;
42 import org.eclipse.ui.texteditor.AbstractMarkerAnnotationModel;
43 import org.eclipse.ui.texteditor.IDocumentProvider;
44 import org.eclipse.ui.texteditor.ITextEditor;
45 import org.eclipse.ui.texteditor.IUpdate;
47 public class PHPManageBreakpointRulerAction extends Action implements IUpdate {
49 private IVerticalRulerInfo fRuler;
50 private ITextEditor fTextEditor;
51 private String fMarkerType;
52 private List fMarkers;
54 private String fAddLabel;
55 private String fRemoveLabel;
57 public PHPManageBreakpointRulerAction(IVerticalRulerInfo ruler, ITextEditor editor) {
60 fMarkerType= IBreakpoint.BREAKPOINT_MARKER;
61 fAddLabel= PHPDebugUiMessages.getString("PHPManageBreakpointRulerAction.AddBreakpoint"); //$NON-NLS-1$
62 fRemoveLabel= PHPDebugUiMessages.getString("PHPManageBreakpointRulerAction.RemoveBreakpoint"); //$NON-NLS-1$
66 * Returns the resource for which to create the marker,
67 * or <code>null</code> if there is no applicable resource.
69 * @return the resource for which to create the marker or <code>null</code>
71 protected IResource getResource() {
72 IEditorInput input= fTextEditor.getEditorInput();
74 IResource resource= (IResource) input.getAdapter(IFile.class);
76 if (resource == null) {
77 resource= (IResource) input.getAdapter(IResource.class);
84 * Checks whether a position includes the ruler's line of activity.
86 * @param position the position to be checked
87 * @param document the document the position refers to
88 * @return <code>true</code> if the line is included by the given position
90 protected boolean includesRulerLine(Position position, IDocument document) {
92 if (position != null) {
94 int markerLine= document.getLineOfOffset(position.getOffset());
95 int line= fRuler.getLineOfLastMouseButtonActivity();
96 if (line == markerLine) {
99 } catch (BadLocationException x) {
107 * Returns this action's vertical ruler info.
109 * @return this action's vertical ruler
111 protected IVerticalRulerInfo getVerticalRulerInfo() {
116 * Returns this action's editor.
118 * @return this action's editor
120 protected ITextEditor getTextEditor() {
125 * Returns the <code>AbstractMarkerAnnotationModel</code> of the editor's input.
127 * @return the marker annotation model
129 protected AbstractMarkerAnnotationModel getAnnotationModel() {
130 IDocumentProvider provider= fTextEditor.getDocumentProvider();
131 IAnnotationModel model= provider.getAnnotationModel(fTextEditor.getEditorInput());
132 if (model instanceof AbstractMarkerAnnotationModel) {
133 return (AbstractMarkerAnnotationModel) model;
139 * Returns the <code>IDocument</code> of the editor's input.
141 * @return the document of the editor's input
143 protected IDocument getDocument() {
144 IDocumentProvider provider= fTextEditor.getDocumentProvider();
145 return provider.getDocument(fTextEditor.getEditorInput());
149 * @see IUpdate#update()
151 public void update() {
152 fMarkers= getMarkers();
153 setText(fMarkers.isEmpty() ? fAddLabel : fRemoveLabel);
160 if (fMarkers.isEmpty()) {
163 removeMarkers(fMarkers);
167 protected List getMarkers() {
169 List breakpoints= new ArrayList();
171 IResource resource= getResource();
172 IDocument document= getDocument();
173 AbstractMarkerAnnotationModel model= getAnnotationModel();
178 IMarker[] markers= null;
179 if (resource instanceof IFile)
180 markers= resource.findMarkers(IBreakpoint.BREAKPOINT_MARKER, true, IResource.DEPTH_INFINITE);
182 IWorkspaceRoot root= ResourcesPlugin.getWorkspace().getRoot();
183 markers= root.findMarkers(IBreakpoint.BREAKPOINT_MARKER, true, IResource.DEPTH_INFINITE);
186 if (markers != null) {
187 IBreakpointManager breakpointManager= DebugPlugin.getDefault().getBreakpointManager();
189 for (iFe= 0; iFe < markers.length; iFe++) {
190 IBreakpoint breakpoint= breakpointManager.getBreakpoint(markers[iFe]);
191 if (breakpoint != null && breakpointManager.isRegistered(breakpoint) &&
192 includesRulerLine(model.getMarkerPosition(markers[iFe]), document))
193 breakpoints.add(markers[iFe]);
196 } catch (CoreException x) {
197 System.out.println(x.getStatus());
198 // JDIDebugUIPlugin.log(x.getStatus());
204 protected void addMarker() {
206 //IResource resource= getResource();
207 IEditorInput editorInput= getTextEditor().getEditorInput();
208 IDocument document= getDocument();
209 //IBreakpointManager breakpointManager= DebugPlugin.getDefault().getBreakpointManager();
211 int rulerLine= getVerticalRulerInfo().getLineOfLastMouseButtonActivity();
214 // Falta verificar si la ubicación del Breakpoint es válida
215 int lineNumber= rulerLine + 1;
217 if (lineNumber > 0) {
218 if (!PHPDebugModel.lineBreakpointExists(lineNumber)) {
219 Map attributes = new HashMap(10);
220 IRegion line= document.getLineInformation(lineNumber - 1);
221 int start= line.getOffset();
222 int lenline= line.getLength();
223 //int end= start + ((lenline > 0)?lenline:0);
224 int end= start + lenline;
226 //PHPDebugModel.createLineBreakpoint(getResource(), lineNumber, start, end, 0, true, attributes);
227 PHPDebugModel.createLineBreakpoint(((IFileEditorInput) editorInput).getFile(), lineNumber, start, end, 0, true, attributes);
231 } catch (DebugException e) {
232 System.out.println("Error");
233 // JDIDebugUIPlugin.errorDialog(ActionMessages.getString("ManageBreakpointRulerAction.error.adding.message1"), e); //$NON-NLS-1$
234 } catch (CoreException e) {
235 System.out.println("Error");
236 // JDIDebugUIPlugin.errorDialog(ActionMessages.getString("ManageBreakpointRulerAction.error.adding.message1"), e); //$NON-NLS-1$
237 } catch (BadLocationException e) {
238 System.out.println("Error");
239 // JDIDebugUIPlugin.errorDialog(ActionMessages.getString("ManageBreakpointRulerAction.error.adding.message1"), e); //$NON-NLS-1$
243 protected void removeMarkers(List markers) {
244 IBreakpointManager breakpointManager= DebugPlugin.getDefault().getBreakpointManager();
246 Iterator e= markers.iterator();
247 while (e.hasNext()) {
248 IBreakpoint breakpoint= breakpointManager.getBreakpoint((IMarker) e.next());
249 breakpointManager.removeBreakpoint(breakpoint, true);
251 } catch (CoreException e) {
255 public IResource getUnderlyingResource(String fName) {
256 IResource parentResource = getResource(); //fParent.getUnderlyingResource();
257 if (parentResource == null) {
260 int type = parentResource.getType();
261 if (type == IResource.FOLDER || type == IResource.PROJECT) {
262 IContainer folder = (IContainer) parentResource;
263 IResource resource = folder.findMember(fName);
264 if (resource == null) {
265 //throw newNotPresentException();
271 return parentResource;