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.phpeclipse.xdebug.ui.actions;
 
  14 import net.sourceforge.phpeclipse.xdebug.ui.XDebugUIPlugin;
 
  15 import net.sourceforge.phpeclipse.xdebug.ui.php.model.PHPLineBreakpointAdapter;
 
  17 import org.eclipse.core.runtime.CoreException;
 
  18 import org.eclipse.debug.core.DebugPlugin;
 
  19 import org.eclipse.jface.action.Action;
 
  20 import org.eclipse.jface.text.BadLocationException;
 
  21 import org.eclipse.jface.text.IDocument;
 
  22 import org.eclipse.jface.text.IRegion;
 
  23 import org.eclipse.jface.text.TextSelection;
 
  24 import org.eclipse.jface.text.source.IVerticalRulerInfo;
 
  25 import org.eclipse.jface.viewers.ISelection;
 
  26 import org.eclipse.ui.IWorkbenchPart;
 
  27 import org.eclipse.ui.texteditor.IDocumentProvider;
 
  28 import org.eclipse.ui.texteditor.ITextEditor;
 
  30 public class ToggleBreakpointRulerAction extends Action {
 
  32         static class EmptySelection implements ISelection {
 
  34                 public boolean isEmpty() {
 
  39         private IVerticalRulerInfo fRuler;
 
  41         private IWorkbenchPart fTargetPart;
 
  43         private PHPLineBreakpointAdapter fBreakpointAdapter;
 
  45         private static final ISelection EMPTY_SELECTION = new EmptySelection();
 
  47         public ToggleBreakpointRulerAction(IWorkbenchPart part,
 
  48                         IVerticalRulerInfo ruler) {
 
  49                 super("Toggle Breakpoint"); //$NON-NLS-1$
 
  54                 fBreakpointAdapter = new PHPLineBreakpointAdapter();
 
  55                 // part.getSite().getWorkbenchWindow().getWorkbench().getHelpSystem().setHelp(
 
  56                 // this, ICDebugHelpContextIds.TOGGLE_BREAKPOINT_ACTION );
 
  57                 // setId( IInternalCDebugUIConstants.ACTION_TOGGLE_BREAKPOINT );
 
  65                         fBreakpointAdapter.toggleLineBreakpoints(getTargetPart(),
 
  66                                         getTargetSelection());
 
  67                 } catch (CoreException e) {
 
  68                         XDebugUIPlugin.errorDialog(getTargetPart().getSite().getShell(),
 
  69                                         "Error", "Operation failed", e.getStatus());
 
  74          * Returns this action's vertical ruler info.
 
  76          * @return this action's vertical ruler
 
  78         protected IVerticalRulerInfo getVerticalRulerInfo() {
 
  82         private IWorkbenchPart getTargetPart() {
 
  83                 return this.fTargetPart;
 
  86         private void setTargetPart(IWorkbenchPart targetPart) {
 
  87                 this.fTargetPart = targetPart;
 
  91          * Returns the current selection in the active part, possibly and empty
 
  92          * selection, but never <code>null</code>.
 
  94          * @return the selection in the active part, possibly empty
 
  96         private ISelection getTargetSelection() {
 
  97                 IDocument doc = getDocument();
 
  99                         int line = getVerticalRulerInfo()
 
 100                                         .getLineOfLastMouseButtonActivity();
 
 102                                 IRegion region = doc.getLineInformation(line);
 
 103                                 return new TextSelection(doc, region.getOffset(), region
 
 105                         } catch (BadLocationException e) {
 
 109                 return EMPTY_SELECTION;
 
 112         private IDocument getDocument() {
 
 113                 IWorkbenchPart targetPart = getTargetPart();
 
 114                 if (targetPart instanceof ITextEditor) {
 
 115                         ITextEditor textEditor = (ITextEditor) targetPart;
 
 116                         IDocumentProvider provider = textEditor.getDocumentProvider();
 
 117                         if (provider != null)
 
 118                                 return provider.getDocument(textEditor.getEditorInput());
 
 120                 // else if ( targetPart instanceof DisassemblyView ) {
 
 121                 // DisassemblyView dv = (DisassemblyView)targetPart;
 
 122                 // IDocumentProvider provider = dv.getDocumentProvider();
 
 123                 // if ( provider != null )
 
 124                 // return provider.getDocument( dv.getInput() );