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