Updated psf files to reflect new names and additional project files.
[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
41         private IWorkbenchPart fTargetPart;
42
43         private PHPLineBreakpointAdapter fBreakpointAdapter;
44
45         private static final ISelection EMPTY_SELECTION = new EmptySelection();
46
47         public ToggleBreakpointRulerAction(IWorkbenchPart part,
48                         IVerticalRulerInfo ruler) {
49                 super("Toggle Breakpoint"); //$NON-NLS-1$
50
51                 fRuler = ruler;
52                 fRuler = ruler;
53                 setTargetPart(part);
54                 fBreakpointAdapter = new PHPLineBreakpointAdapter();
55                 // part.getSite().getWorkbenchWindow().getWorkbench().getHelpSystem().setHelp(
56                 // this, ICDebugHelpContextIds.TOGGLE_BREAKPOINT_ACTION );
57                 // setId( IInternalCDebugUIConstants.ACTION_TOGGLE_BREAKPOINT );
58         }
59
60         /**
61          * @see Action#run()
62          */
63         public void run() {
64                 try {
65                         fBreakpointAdapter.toggleLineBreakpoints(getTargetPart(),
66                                         getTargetSelection());
67                 } catch (CoreException e) {
68                         XDebugUIPlugin.errorDialog(getTargetPart().getSite().getShell(),
69                                         "Error", "Operation failed", e.getStatus());
70                 }
71         }
72
73         /**
74          * Returns this action's vertical ruler info.
75          * 
76          * @return this action's vertical ruler
77          */
78         protected IVerticalRulerInfo getVerticalRulerInfo() {
79                 return fRuler;
80         }
81
82         private IWorkbenchPart getTargetPart() {
83                 return this.fTargetPart;
84         }
85
86         private void setTargetPart(IWorkbenchPart targetPart) {
87                 this.fTargetPart = targetPart;
88         }
89
90         /**
91          * Returns the current selection in the active part, possibly and empty
92          * selection, but never <code>null</code>.
93          * 
94          * @return the selection in the active part, possibly empty
95          */
96         private ISelection getTargetSelection() {
97                 IDocument doc = getDocument();
98                 if (doc != null) {
99                         int line = getVerticalRulerInfo()
100                                         .getLineOfLastMouseButtonActivity();
101                         try {
102                                 IRegion region = doc.getLineInformation(line);
103                                 return new TextSelection(doc, region.getOffset(), region
104                                                 .getLength());
105                         } catch (BadLocationException e) {
106                                 DebugPlugin.log(e);
107                         }
108                 }
109                 return EMPTY_SELECTION;
110         }
111
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());
119                 }
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() );
125                 // }
126                 return null;
127         }
128
129 }