(no commit message)
[phpeclipse.git] / net.sourceforge.phpeclipse.xdebug.ui / src / net / sourceforge / phpeclipse / xdebug / ui / actions / ToggleBreakpointRulerAction.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.phpeclipse.xdebug.ui.actions;
13
14 import net.sourceforge.phpeclipse.xdebug.ui.XDebugUIPlugin;
15 import net.sourceforge.phpeclipse.xdebug.ui.php.model.PHPLineBreakpointAdapter;
16
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;
29
30 public class ToggleBreakpointRulerAction extends Action {       
31         
32         static class EmptySelection implements ISelection {
33
34                 public boolean isEmpty() {
35                         return true;
36                 }               
37         }
38         
39         private IVerticalRulerInfo fRuler;
40         private IWorkbenchPart fTargetPart;
41         private PHPLineBreakpointAdapter fBreakpointAdapter;
42         private static final ISelection EMPTY_SELECTION = new EmptySelection();  
43         
44         public ToggleBreakpointRulerAction( IWorkbenchPart part, IVerticalRulerInfo ruler ) {
45                 super( "Toggle Breakpoint" ); //$NON-NLS-1$
46
47                 fRuler= ruler;
48                 fRuler = ruler;
49                 setTargetPart( part );
50                 fBreakpointAdapter = new PHPLineBreakpointAdapter();
51 //              part.getSite().getWorkbenchWindow().getWorkbench().getHelpSystem().setHelp( this, ICDebugHelpContextIds.TOGGLE_BREAKPOINT_ACTION );
52 //              setId( IInternalCDebugUIConstants.ACTION_TOGGLE_BREAKPOINT );
53         }
54         
55         
56         /**
57          * @see Action#run()
58          */
59         public void run() {
60                 try {
61                                 fBreakpointAdapter.toggleLineBreakpoints( getTargetPart(), getTargetSelection() );
62                 }
63                 catch( CoreException e ) {
64                         XDebugUIPlugin.errorDialog( getTargetPart().getSite().getShell(),"Error", "Operation failed" , e.getStatus() );
65                 }
66         }
67         
68         /**
69          * Returns this action's vertical ruler info.
70          *
71          * @return this action's vertical ruler
72          */
73         protected IVerticalRulerInfo getVerticalRulerInfo() {
74                 return fRuler;
75         }
76
77         private IWorkbenchPart getTargetPart() {
78                 return this.fTargetPart;
79         }
80
81         private void setTargetPart( IWorkbenchPart targetPart ) {
82                 this.fTargetPart = targetPart;
83         }
84
85         /**
86          * Returns the current selection in the active part, possibly
87          * and empty selection, but never <code>null</code>.
88          * 
89          * @return the selection in the active part, possibly empty
90          */
91         private ISelection getTargetSelection() {
92                 IDocument doc = getDocument();
93                 if ( doc != null ) {
94                         int line = getVerticalRulerInfo().getLineOfLastMouseButtonActivity();
95                         try {
96                                 IRegion region = doc.getLineInformation( line );
97                                 return new TextSelection( doc, region.getOffset(), region.getLength() );
98                         }
99                         catch( BadLocationException e ) {
100                                 DebugPlugin.log( e );
101                         } 
102                 }
103                 return EMPTY_SELECTION;
104         }
105
106         private IDocument getDocument() {
107                 IWorkbenchPart targetPart = getTargetPart();
108                 if ( targetPart instanceof ITextEditor ) {
109                         ITextEditor textEditor = (ITextEditor)targetPart; 
110                         IDocumentProvider provider = textEditor.getDocumentProvider();
111                         if ( provider != null )
112                                 return provider.getDocument( textEditor.getEditorInput() );
113                 }
114 //              else if ( targetPart instanceof DisassemblyView ) {
115 //                      DisassemblyView dv = (DisassemblyView)targetPart;
116 //                      IDocumentProvider provider = dv.getDocumentProvider();
117 //                      if ( provider != null )
118 //                              return provider.getDocument( dv.getInput() );
119 //              }
120                 return null;
121         }
122
123 }