e715589a518cba5a1a19a104f0a03de1bdbc9767
[phpeclipse.git] /
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 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;
44
45 public class PHPManageBreakpointRulerAction extends Action implements IUpdate { 
46         
47         private IVerticalRulerInfo fRuler;
48         private ITextEditor fTextEditor;
49         private String fMarkerType;
50         private List fMarkers;
51
52         private String fAddLabel;
53         private String fRemoveLabel;
54         
55         public PHPManageBreakpointRulerAction(IVerticalRulerInfo ruler, ITextEditor editor) {
56                 fRuler= ruler;
57                 fTextEditor= editor;
58                 fMarkerType= IBreakpoint.BREAKPOINT_MARKER;
59                 fAddLabel= PHPDebugUiMessages.getString("PHPManageBreakpointRulerAction.AddBreakpoint"); //$NON-NLS-1$
60                 fRemoveLabel= PHPDebugUiMessages.getString("PHPManageBreakpointRulerAction.RemoveBreakpoint"); //$NON-NLS-1$
61         }
62         
63         /** 
64          * Returns the resource for which to create the marker, 
65          * or <code>null</code> if there is no applicable resource.
66          *
67          * @return the resource for which to create the marker or <code>null</code>
68          */
69         protected IResource getResource() {
70                 IEditorInput input= fTextEditor.getEditorInput();
71                 
72                 IResource resource= (IResource) input.getAdapter(IFile.class);
73                 
74                 if (resource == null) {
75                         resource= (IResource) input.getAdapter(IResource.class);
76                 }
77                         
78                 return resource;
79         }
80         
81         /**
82          * Checks whether a position includes the ruler's line of activity.
83          *
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
87          */
88         protected boolean includesRulerLine(Position position, IDocument document) {
89
90                 if (position != null) {
91                         try {
92                                 int markerLine= document.getLineOfOffset(position.getOffset());
93                                 int line= fRuler.getLineOfLastMouseButtonActivity();
94                                 if (line == markerLine) {
95                                         return true;
96                                 }
97                         } catch (BadLocationException x) {
98                         }
99                 }
100                 
101                 return false;
102         }
103         
104         /**
105          * Returns this action's vertical ruler info.
106          *
107          * @return this action's vertical ruler
108          */
109         protected IVerticalRulerInfo getVerticalRulerInfo() {
110                 return fRuler;
111         }
112         
113         /**
114          * Returns this action's editor.
115          *
116          * @return this action's editor
117          */
118         protected ITextEditor getTextEditor() {
119                 return fTextEditor;
120         }
121         
122         /**
123          * Returns the <code>AbstractMarkerAnnotationModel</code> of the editor's input.
124          *
125          * @return the marker annotation model
126          */
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;
132                 }
133                 return null;
134         }
135
136         /**
137          * Returns the <code>IDocument</code> of the editor's input.
138          *
139          * @return the document of the editor's input
140          */
141         protected IDocument getDocument() {
142                 IDocumentProvider provider= fTextEditor.getDocumentProvider();
143                 return provider.getDocument(fTextEditor.getEditorInput());
144         }
145         
146         /**
147          * @see IUpdate#update()
148          */
149         public void update() {
150                 fMarkers= getMarkers();
151                 setText(fMarkers.isEmpty() ? fAddLabel : fRemoveLabel);
152         }
153
154         /**
155          * @see Action#run()
156          */
157         public void run() {
158                 if (fMarkers.isEmpty()) {
159                         addMarker();
160                 } else {
161                         removeMarkers(fMarkers);
162                 }
163         }
164         
165         protected List getMarkers() {
166
167                 List breakpoints= new ArrayList();
168                 
169                 IResource resource= getResource();
170                 IDocument document= getDocument();
171                 AbstractMarkerAnnotationModel model= getAnnotationModel();
172                 
173                 if (model != null) {
174                         try {
175                                 
176                                 IMarker[] markers= null;
177                                 if (resource instanceof IFile)
178                                         markers= resource.findMarkers(IBreakpoint.BREAKPOINT_MARKER, true, IResource.DEPTH_INFINITE);
179                                 else {
180                                         IWorkspaceRoot root= ResourcesPlugin.getWorkspace().getRoot();
181                                         markers= root.findMarkers(IBreakpoint.BREAKPOINT_MARKER, true, IResource.DEPTH_INFINITE);
182                                 }
183                                 
184                                 if (markers != null) {
185                                         IBreakpointManager breakpointManager= DebugPlugin.getDefault().getBreakpointManager();
186                                         int iFe =0;
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]);
192                                         }
193                                 }
194                         } catch (CoreException x) {
195                                 System.out.println(x.getStatus());
196 //                              JDIDebugUIPlugin.log(x.getStatus());
197                         }
198                 }
199                 return breakpoints;
200         }
201         
202         protected void addMarker() {
203
204                 //IResource resource= getResource();
205                 IEditorInput editorInput= getTextEditor().getEditorInput();     
206                 IDocument document= getDocument();
207                 //IBreakpointManager breakpointManager= DebugPlugin.getDefault().getBreakpointManager();
208
209                 int rulerLine= getVerticalRulerInfo().getLineOfLastMouseButtonActivity();
210                 // create the marker
211                 try {
212                         // Falta verificar si la ubicación del Breakpoint es válida
213                         int lineNumber= rulerLine + 1; 
214
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;
223                                         
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);
227                                         
228                                 }
229                         }
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");
236                 }
237         }
238         
239         protected void removeMarkers(List markers) {
240                 IBreakpointManager breakpointManager= DebugPlugin.getDefault().getBreakpointManager();
241                 try {
242                         Iterator e= markers.iterator();
243                         while (e.hasNext()) {
244                                 IBreakpoint breakpoint= breakpointManager.getBreakpoint((IMarker) e.next());
245                                 breakpointManager.removeBreakpoint(breakpoint, true);
246                         }
247                 } catch (CoreException e) {
248                 }
249         }
250
251         public IResource getUnderlyingResource(String fName) {
252                 IResource parentResource = getResource(); //fParent.getUnderlyingResource();
253                 if (parentResource == null) {
254                         return null;
255                 }
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();
262                                 return null;
263                         } else {
264                                 return resource;
265                         }
266                 } else {
267                         return parentResource;
268                 }
269         }
270
271 }