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;
49 private ITextEditor fTextEditor;
50 private String fMarkerType;
51 private List fMarkers;
53 private String fAddLabel;
54 private String fRemoveLabel;
56 public PHPManageBreakpointRulerAction(IVerticalRulerInfo ruler, ITextEditor editor) {
59 fMarkerType= IBreakpoint.BREAKPOINT_MARKER;
60 fAddLabel= PHPDebugUiMessages.getString("PHPManageBreakpointRulerAction.ToggleBreakpoint"); //$NON-NLS-1$
61 fRemoveLabel= PHPDebugUiMessages.getString("PHPManageBreakpointRulerAction.ToggleBreakpoint"); //$NON-NLS-1$
65 * Returns the resource for which to create the marker,
66 * or <code>null</code> if there is no applicable resource.
68 * @return the resource for which to create the marker or <code>null</code>
70 protected IResource getResource() {
71 IEditorInput input= fTextEditor.getEditorInput();
73 IResource resource= (IResource) input.getAdapter(IFile.class);
75 if (resource == null) {
76 resource= (IResource) input.getAdapter(IResource.class);
83 * Checks whether a position includes the ruler's line of activity.
85 * @param position the position to be checked
86 * @param document the document the position refers to
87 * @return <code>true</code> if the line is included by the given position
89 protected boolean includesRulerLine(Position position, IDocument document) {
91 if (position != null) {
93 int markerLine= document.getLineOfOffset(position.getOffset());
94 int line= fRuler.getLineOfLastMouseButtonActivity();
95 if (line == markerLine) {
98 } catch (BadLocationException x) {
106 * Returns this action's vertical ruler info.
108 * @return this action's vertical ruler
110 protected IVerticalRulerInfo getVerticalRulerInfo() {
115 * Returns this action's editor.
117 * @return this action's editor
119 protected ITextEditor getTextEditor() {
124 * Returns the <code>AbstractMarkerAnnotationModel</code> of the editor's input.
126 * @return the marker annotation model
128 protected AbstractMarkerAnnotationModel getAnnotationModel() {
129 IDocumentProvider provider= fTextEditor.getDocumentProvider();
130 IAnnotationModel model= provider.getAnnotationModel(fTextEditor.getEditorInput());
131 if (model instanceof AbstractMarkerAnnotationModel) {
132 return (AbstractMarkerAnnotationModel) model;
138 * Returns the <code>IDocument</code> of the editor's input.
140 * @return the document of the editor's input
142 protected IDocument getDocument() {
143 IDocumentProvider provider= fTextEditor.getDocumentProvider();
144 return provider.getDocument(fTextEditor.getEditorInput());
148 * @see IUpdate#update()
150 public void update() {
151 fMarkers= getMarkers();
152 setText(fMarkers.isEmpty() ? fAddLabel : fRemoveLabel);
159 if (fMarkers.isEmpty()) {
162 removeMarkers(fMarkers);
166 protected List getMarkers() {
168 List breakpoints= new ArrayList();
170 IResource resource= getResource();
171 IDocument document= getDocument();
172 AbstractMarkerAnnotationModel model= getAnnotationModel();
177 IMarker[] markers= null;
178 if (resource instanceof IFile)
179 markers= resource.findMarkers(IBreakpoint.BREAKPOINT_MARKER, true, IResource.DEPTH_INFINITE);
181 IWorkspaceRoot root= ResourcesPlugin.getWorkspace().getRoot();
182 markers= root.findMarkers(IBreakpoint.BREAKPOINT_MARKER, true, IResource.DEPTH_INFINITE);
185 if (markers != null) {
186 IBreakpointManager breakpointManager= DebugPlugin.getDefault().getBreakpointManager();
188 for (iFe= 0; iFe < markers.length; iFe++) {
189 IBreakpoint breakpoint= breakpointManager.getBreakpoint(markers[iFe]);
190 if (breakpoint != null && breakpointManager.isRegistered(breakpoint) &&
191 includesRulerLine(model.getMarkerPosition(markers[iFe]), document))
192 breakpoints.add(markers[iFe]);
195 } catch (CoreException x) {
196 System.out.println(x.getStatus());
197 // JDIDebugUIPlugin.log(x.getStatus());
203 protected void addMarker() {
205 //IResource resource= getResource();
206 IEditorInput editorInput= getTextEditor().getEditorInput();
207 IDocument document= getDocument();
208 //IBreakpointManager breakpointManager= DebugPlugin.getDefault().getBreakpointManager();
210 int rulerLine= getVerticalRulerInfo().getLineOfLastMouseButtonActivity();
213 // Falta verificar si la ubicación del Breakpoint es válida
214 int lineNumber= rulerLine + 1;
216 if (lineNumber > 0) {
217 if (PHPDebugModel.lineBreakpointExists(lineNumber)==null) {
218 // Map attributes = new HashMap(10);
219 IRegion line= document.getLineInformation(lineNumber - 1);
220 int start= line.getOffset();
221 int lenline= line.getLength();
222 //int end= start + ((lenline > 0)?lenline:0);
223 int end= start + lenline;
225 //PHPDebugModel.createLineBreakpoint(getResource(), lineNumber, start, end, 0, true, attributes);
226 PHPDebugModel.createLineBreakpoint(((IFileEditorInput) editorInput).getFile(), lineNumber, start, end, 0, true, null);
227 // PHPDebugModel.createLineBreakpoint(((IFileEditorInput) editorInput).getFile(), lineNumber, 0, true, attributes);
231 } catch (DebugException e) {
232 System.out.println("Error");
233 } catch (CoreException e) {
234 System.out.println("Error");
235 } catch (BadLocationException e) {
236 System.out.println("Error");
240 protected void removeMarkers(List markers) {
241 IBreakpointManager breakpointManager= DebugPlugin.getDefault().getBreakpointManager();
243 Iterator e= markers.iterator();
244 while (e.hasNext()) {
245 IBreakpoint breakpoint= breakpointManager.getBreakpoint((IMarker) e.next());
246 breakpointManager.removeBreakpoint(breakpoint, true);
248 } catch (CoreException e) {
252 public IResource getUnderlyingResource(String fName) {
253 IResource parentResource = getResource(); //fParent.getUnderlyingResource();
254 if (parentResource == null) {
257 int type = parentResource.getType();
258 if (type == IResource.FOLDER || type == IResource.PROJECT) {
259 IContainer folder = (IContainer) parentResource;
260 IResource resource = folder.findMember(fName);
261 if (resource == null) {
262 //throw newNotPresentException();
268 return parentResource;