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