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