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
 
   9  IBM Corporation - Initial implementation
 
  10  Vicente Fernando - www.alfersoft.com.ar
 
  11  **********************************************************************/
 
  12 package net.sourceforge.phpdt.internal.debug.ui.actions;
 
  14 import java.util.ArrayList;
 
  15 import java.util.Iterator;
 
  16 import java.util.List;
 
  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;
 
  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;
 
  47 public class PHPManageBreakpointRulerAction extends Action implements IUpdate {
 
  49         private IVerticalRulerInfo fRuler;
 
  51         private ITextEditor fTextEditor;
 
  53         private String fMarkerType;
 
  55         private List fMarkers;
 
  57         private String fAddLabel;
 
  59         private String fRemoveLabel;
 
  61         public PHPManageBreakpointRulerAction(IVerticalRulerInfo ruler,
 
  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$
 
  73          * Returns the resource for which to create the marker, or <code>null</code>
 
  74          * if there is no applicable resource.
 
  76          * @return the resource for which to create the marker or <code>null</code>
 
  78         protected IResource getResource() {
 
  79                 IEditorInput input = fTextEditor.getEditorInput();
 
  81                 IResource resource = (IResource) input.getAdapter(IFile.class);
 
  83                 if (resource == null) {
 
  84                         resource = (IResource) input.getAdapter(IResource.class);
 
  91          * Checks whether a position includes the ruler's line of activity.
 
  94          *            the position to be checked
 
  96          *            the document the position refers to
 
  97          * @return <code>true</code> if the line is included by the given position
 
  99         protected boolean includesRulerLine(Position position, IDocument document) {
 
 101                 if (position != null) {
 
 103                                 int markerLine = document.getLineOfOffset(position.getOffset());
 
 104                                 int line = fRuler.getLineOfLastMouseButtonActivity();
 
 105                                 if (line == markerLine) {
 
 108                         } catch (BadLocationException x) {
 
 116          * Returns this action's vertical ruler info.
 
 118          * @return this action's vertical ruler
 
 120         protected IVerticalRulerInfo getVerticalRulerInfo() {
 
 125          * Returns this action's editor.
 
 127          * @return this action's editor
 
 129         protected ITextEditor getTextEditor() {
 
 134          * Returns the <code>AbstractMarkerAnnotationModel</code> of the editor's
 
 137          * @return the marker annotation model
 
 139         protected AbstractMarkerAnnotationModel getAnnotationModel() {
 
 140                 IDocumentProvider provider = fTextEditor.getDocumentProvider();
 
 141                 IAnnotationModel model = provider.getAnnotationModel(fTextEditor
 
 143                 if (model instanceof AbstractMarkerAnnotationModel) {
 
 144                         return (AbstractMarkerAnnotationModel) model;
 
 150          * Returns the <code>IDocument</code> of the editor's input.
 
 152          * @return the document of the editor's input
 
 154         protected IDocument getDocument() {
 
 155                 IDocumentProvider provider = fTextEditor.getDocumentProvider();
 
 156                 return provider.getDocument(fTextEditor.getEditorInput());
 
 160          * @see IUpdate#update()
 
 162         public void update() {
 
 163                 fMarkers = getMarkers();
 
 164                 setText(fMarkers.isEmpty() ? fAddLabel : fRemoveLabel);
 
 171                 if (fMarkers.isEmpty()) {
 
 174                         removeMarkers(fMarkers);
 
 178         protected List getMarkers() {
 
 180                 List breakpoints = new ArrayList();
 
 182                 IResource resource = getResource();
 
 183                 IDocument document = getDocument();
 
 184                 AbstractMarkerAnnotationModel model = getAnnotationModel();
 
 189                                 IMarker[] markers = null;
 
 190                                 if (resource instanceof IFile)
 
 191                                         markers = resource.findMarkers(
 
 192                                                         IBreakpoint.BREAKPOINT_MARKER, true,
 
 193                                                         IResource.DEPTH_INFINITE);
 
 195                                         IWorkspaceRoot root = ResourcesPlugin.getWorkspace()
 
 197                                         markers = root.findMarkers(IBreakpoint.BREAKPOINT_MARKER,
 
 198                                                         true, IResource.DEPTH_INFINITE);
 
 201                                 if (markers != null) {
 
 202                                         IBreakpointManager breakpointManager = DebugPlugin
 
 203                                                         .getDefault().getBreakpointManager();
 
 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]),
 
 213                                                         breakpoints.add(markers[iFe]);
 
 216                         } catch (CoreException x) {
 
 217                                 System.out.println(x.getStatus());
 
 218                                 // JDIDebugUIPlugin.log(x.getStatus());
 
 224         protected void addMarker() {
 
 226                 // IResource resource= getResource();
 
 227                 IEditorInput editorInput = getTextEditor().getEditorInput();
 
 228                 IDocument document = getDocument();
 
 229                 // IBreakpointManager breakpointManager=
 
 230                 // DebugPlugin.getDefault().getBreakpointManager();
 
 232                 int rulerLine = getVerticalRulerInfo()
 
 233                                 .getLineOfLastMouseButtonActivity();
 
 236                         // Falta verificar si la ubicaci?½n del Breakpoint es v?½lida
 
 237                         int lineNumber = rulerLine + 1;
 
 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;
 
 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);
 
 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");
 
 267         protected void removeMarkers(List markers) {
 
 268                 IBreakpointManager breakpointManager = DebugPlugin.getDefault()
 
 269                                 .getBreakpointManager();
 
 271                         Iterator e = markers.iterator();
 
 272                         while (e.hasNext()) {
 
 273                                 IBreakpoint breakpoint = breakpointManager
 
 274                                                 .getBreakpoint((IMarker) e.next());
 
 275                                 breakpointManager.removeBreakpoint(breakpoint, true);
 
 277                 } catch (CoreException e) {
 
 281         public IResource getUnderlyingResource(String fName) {
 
 282                 IResource parentResource = getResource(); // fParent.getUnderlyingResource();
 
 283                 if (parentResource == null) {
 
 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();
 
 297                         return parentResource;