7f5935f7236a499ece09453bc52b3b9bbd93a828
[phpeclipse.git] / net.sourceforge.phpeclipse.debug.ui / src / net / sourceforge / phpdt / internal / debug / ui / actions / PHPManageBreakpointRulerAction.java
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
7
8  Contributors:
9  IBM Corporation - Initial implementation
10  Vicente Fernando - www.alfersoft.com.ar
11  **********************************************************************/
12 package net.sourceforge.phpdt.internal.debug.ui.actions;
13
14 import java.util.ArrayList;
15 import java.util.Iterator;
16 import java.util.List;
17
18 import net.sourceforge.phpdt.debug.core.PHPDebugModel;
19 import net.sourceforge.phpdt.internal.debug.ui.PHPDebugUiMessages;
20 import net.sourceforge.phpdt.internal.debug.ui.PHPDebugUiPlugin;
21
22 import org.eclipse.core.resources.IContainer;
23 import org.eclipse.core.resources.IFile;
24 import org.eclipse.core.resources.IMarker;
25 import org.eclipse.core.resources.IResource;
26 import org.eclipse.core.resources.IWorkspaceRoot;
27 import org.eclipse.core.resources.ResourcesPlugin;
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;
46
47 public class PHPManageBreakpointRulerAction extends Action implements IUpdate {
48
49         private IVerticalRulerInfo fRuler;
50
51         private ITextEditor fTextEditor;
52
53         private String fMarkerType;
54
55         private List fMarkers;
56
57         private String fAddLabel;
58
59         private String fRemoveLabel;
60
61         public PHPManageBreakpointRulerAction(IVerticalRulerInfo ruler,
62                         ITextEditor editor) {
63                 fRuler = ruler;
64                 fTextEditor = editor;
65                 fMarkerType = IBreakpoint.BREAKPOINT_MARKER;
66                 fAddLabel = PHPDebugUiMessages
67                                 .getString("PHPManageBreakpointRulerAction.ToggleBreakpoint"); //$NON-NLS-1$
68                 fRemoveLabel = PHPDebugUiMessages
69                                 .getString("PHPManageBreakpointRulerAction.ToggleBreakpoint"); //$NON-NLS-1$
70         }
71
72         /**
73          * Returns the resource for which to create the marker, or <code>null</code>
74          * if there is no applicable resource.
75          * 
76          * @return the resource for which to create the marker or <code>null</code>
77          */
78         protected IResource getResource() {
79                 IEditorInput input = fTextEditor.getEditorInput();
80
81                 IResource resource = (IResource) input.getAdapter(IFile.class);
82
83                 if (resource == null) {
84                         resource = (IResource) input.getAdapter(IResource.class);
85                 }
86
87                 return resource;
88         }
89
90         /**
91          * Checks whether a position includes the ruler's line of activity.
92          * 
93          * @param position
94          *            the position to be checked
95          * @param document
96          *            the document the position refers to
97          * @return <code>true</code> if the line is included by the given position
98          */
99         protected boolean includesRulerLine(Position position, IDocument document) {
100
101                 if (position != null) {
102                         try {
103                                 int markerLine = document.getLineOfOffset(position.getOffset());
104                                 int line = fRuler.getLineOfLastMouseButtonActivity();
105                                 if (line == markerLine) {
106                                         return true;
107                                 }
108                         } catch (BadLocationException x) {
109                         }
110                 }
111
112                 return false;
113         }
114
115         /**
116          * Returns this action's vertical ruler info.
117          * 
118          * @return this action's vertical ruler
119          */
120         protected IVerticalRulerInfo getVerticalRulerInfo() {
121                 return fRuler;
122         }
123
124         /**
125          * Returns this action's editor.
126          * 
127          * @return this action's editor
128          */
129         protected ITextEditor getTextEditor() {
130                 return fTextEditor;
131         }
132
133         /**
134          * Returns the <code>AbstractMarkerAnnotationModel</code> of the editor's
135          * input.
136          * 
137          * @return the marker annotation model
138          */
139         protected AbstractMarkerAnnotationModel getAnnotationModel() {
140                 IDocumentProvider provider = fTextEditor.getDocumentProvider();
141                 IAnnotationModel model = provider.getAnnotationModel(fTextEditor
142                                 .getEditorInput());
143                 if (model instanceof AbstractMarkerAnnotationModel) {
144                         return (AbstractMarkerAnnotationModel) model;
145                 }
146                 return null;
147         }
148
149         /**
150          * Returns the <code>IDocument</code> of the editor's input.
151          * 
152          * @return the document of the editor's input
153          */
154         protected IDocument getDocument() {
155                 IDocumentProvider provider = fTextEditor.getDocumentProvider();
156                 return provider.getDocument(fTextEditor.getEditorInput());
157         }
158
159         /**
160          * @see IUpdate#update()
161          */
162         public void update() {
163                 fMarkers = getMarkers();
164                 setText(fMarkers.isEmpty() ? fAddLabel : fRemoveLabel);
165         }
166
167         /**
168          * @see Action#run()
169          */
170         public void run() {
171                 if (fMarkers.isEmpty()) {
172                         addMarker();
173                 } else {
174                         removeMarkers(fMarkers);
175                 }
176         }
177
178         protected List getMarkers() {
179
180                 List breakpoints = new ArrayList();
181
182                 IResource resource = getResource();
183                 IDocument document = getDocument();
184                 AbstractMarkerAnnotationModel model = getAnnotationModel();
185
186                 if (model != null) {
187                         try {
188
189                                 IMarker[] markers = null;
190                                 if (resource instanceof IFile)
191                                         markers = resource.findMarkers(
192                                                         IBreakpoint.BREAKPOINT_MARKER, true,
193                                                         IResource.DEPTH_INFINITE);
194                                 else {
195                                         IWorkspaceRoot root = ResourcesPlugin.getWorkspace()
196                                                         .getRoot();
197                                         markers = root.findMarkers(IBreakpoint.BREAKPOINT_MARKER,
198                                                         true, IResource.DEPTH_INFINITE);
199                                 }
200
201                                 if (markers != null) {
202                                         IBreakpointManager breakpointManager = DebugPlugin
203                                                         .getDefault().getBreakpointManager();
204                                         int iFe = 0;
205                                         for (iFe = 0; iFe < markers.length; iFe++) {
206                                                 IBreakpoint breakpoint = breakpointManager
207                                                                 .getBreakpoint(markers[iFe]);
208                                                 if (breakpoint != null
209                                                                 && breakpointManager.isRegistered(breakpoint)
210                                                                 && includesRulerLine(model
211                                                                                 .getMarkerPosition(markers[iFe]),
212                                                                                 document))
213                                                         breakpoints.add(markers[iFe]);
214                                         }
215                                 }
216                         } catch (CoreException x) {
217                                 System.out.println(x.getStatus());
218                                 // JDIDebugUIPlugin.log(x.getStatus());
219                         }
220                 }
221                 return breakpoints;
222         }
223
224         protected void addMarker() {
225
226                 // IResource resource= getResource();
227                 IEditorInput editorInput = getTextEditor().getEditorInput();
228                 IDocument document = getDocument();
229                 // IBreakpointManager breakpointManager=
230                 // DebugPlugin.getDefault().getBreakpointManager();
231
232                 int rulerLine = getVerticalRulerInfo()
233                                 .getLineOfLastMouseButtonActivity();
234                 // create the marker
235                 try {
236                         // Falta verificar si la ubicaci?½n del Breakpoint es v?½lida
237                         int lineNumber = rulerLine + 1;
238
239                         if (lineNumber > 0) {
240                                 IResource resource = ((IFileEditorInput) editorInput).getFile();
241                                 if (PHPDebugModel.lineBreakpointExists(resource, lineNumber) == null) {
242                                         // Map attributes = new HashMap(10);
243                                         IRegion line = document.getLineInformation(lineNumber - 1);
244                                         int start = line.getOffset();
245                                         int lenline = line.getLength();
246                                         // int end= start + ((lenline > 0)?lenline:0);
247                                         int end = start + lenline;
248
249                                         // PHPDebugModel.createLineBreakpoint(getResource(),
250                                         // lineNumber, start, end, 0, true, attributes);
251                                         PHPDebugModel.createLineBreakpoint(
252                                                         resource, lineNumber, start, end, 0, true, null);
253                                         // PHPDebugModel.createLineBreakpoint(((IFileEditorInput)
254                                         // editorInput).getFile(), lineNumber, 0, true, attributes);
255
256                                 }
257                         }
258                 } catch (DebugException e) {
259                         System.out.println("Error");
260                 } catch (CoreException e) {
261                         System.out.println("Error");
262                 } catch (BadLocationException e) {
263                         System.out.println("Error");
264                 }
265         }
266
267         protected void removeMarkers(List markers) {
268                 IBreakpointManager breakpointManager = DebugPlugin.getDefault()
269                                 .getBreakpointManager();
270                 try {
271                         Iterator e = markers.iterator();
272                         while (e.hasNext()) {
273                                 IBreakpoint breakpoint = breakpointManager
274                                                 .getBreakpoint((IMarker) e.next());
275                                 breakpointManager.removeBreakpoint(breakpoint, true);
276                         }
277                 } catch (CoreException e) {
278                 }
279         }
280
281         public IResource getUnderlyingResource(String fName) {
282                 IResource parentResource = getResource(); // fParent.getUnderlyingResource();
283                 if (parentResource == null) {
284                         return null;
285                 }
286                 int type = parentResource.getType();
287                 if (type == IResource.FOLDER || type == IResource.PROJECT) {
288                         IContainer folder = (IContainer) parentResource;
289                         IResource resource = folder.findMember(fName);
290                         if (resource == null) {
291                                 // throw newNotPresentException();
292                                 return null;
293                         } else {
294                                 return resource;
295                         }
296                 } else {
297                         return parentResource;
298                 }
299         }
300
301 }