1 package net.sourceforge.phpdt.internal.debug.ui.actions;
2 import net.sourceforge.phpdt.debug.core.PHPDebugModel;
3 import org.eclipse.debug.core.DebugPlugin;
4 import net.sourceforge.phpdt.internal.debug.core.breakpoints.PHPLineBreakpoint;
5 import org.eclipse.jface.action.IAction;
6 import org.eclipse.jface.viewers.ISelection;
7 import org.eclipse.ui.IWorkbenchWindow;
8 import org.eclipse.ui.IWorkbenchWindowActionDelegate;
9 import org.eclipse.ui.texteditor.IEditorStatusLine;
10 import org.eclipse.ui.texteditor.ITextEditor;
11 import org.eclipse.core.resources.IFile;
12 import org.eclipse.ui.IEditorPart;
13 import org.eclipse.jface.text.IDocument;
14 import org.eclipse.ui.IWorkbenchPage;
15 import org.eclipse.ui.IPartListener;
16 import org.eclipse.jface.text.ITextSelection;
17 import org.eclipse.ui.IFileEditorInput;
18 import org.eclipse.jface.viewers.ISelectionProvider;
19 import org.eclipse.ui.IEditorInput;
20 import org.eclipse.ui.IWorkbenchPart;
21 import org.eclipse.core.runtime.CoreException;
23 import net.sourceforge.phpdt.internal.debug.ui.PHPDebugUiPlugin;
26 * @see IWorkbenchWindowActionDelegate
28 public class ManageBreakpointActionDelegate implements IWorkbenchWindowActionDelegate, IPartListener {
30 protected boolean fInitialized= false;
31 private ITextEditor fTextEditor= null;
32 private IAction fAction= null;
33 private IFile fFile = null;
34 private IWorkbenchWindow fWorkbenchWindow= null;
38 * @see org.eclipse.ui.IPartListener#partActivated(IWorkbenchPart)
40 public void partActivated( IWorkbenchPart part )
42 if ( part instanceof ITextEditor )
44 setTextEditor( (ITextEditor)part );
49 * @see org.eclipse.ui.IPartListener#partBroughtToTop(IWorkbenchPart)
51 public void partBroughtToTop( IWorkbenchPart part )
56 * @see org.eclipse.ui.IPartListener#partClosed(IWorkbenchPart)
58 public void partClosed( IWorkbenchPart part )
60 if ( part == getTextEditor() )
62 setTextEditor( null );
63 if ( getAction() != null )
65 getAction().setEnabled( false );
71 * @see org.eclipse.ui.IPartListener#partDeactivated(IWorkbenchPart)
73 public void partDeactivated( IWorkbenchPart part )
78 * @see org.eclipse.ui.IPartListener#partOpened(IWorkbenchPart)
80 public void partOpened( IWorkbenchPart part )
82 if ( part instanceof ITextEditor )
84 if ( getTextEditor() == null )
86 setTextEditor( (ITextEditor)part );
93 * Manages a breakpoint.
95 protected void manageBreakpoint(IEditorInput editorInput) {
96 ISelectionProvider sp= getTextEditor().getSelectionProvider();
97 if (sp == null || getFile() == null) {
98 report("ManageBreakpointActionDelegate.No_Breakpoint"); //$NON-NLS-1$
102 ISelection selection= sp.getSelection();
103 if ( selection instanceof ITextSelection ) {
104 if ( getFile() == null )
106 IDocument document = getTextEditor().getDocumentProvider().getDocument( editorInput );
107 // BreakpointLocationVerifier bv = new BreakpointLocationVerifier();
108 // int lineNumber = bv.getValidLineBreakpointLocation( document, ((ITextSelection)selection).getStartLine());
109 int lineNumber = ((ITextSelection)selection).getStartLine() +1;
110 if ( lineNumber > -1 ) {
113 PHPLineBreakpoint breakpoint=PHPDebugModel.lineBreakpointExists(lineNumber);
114 if (breakpoint==null)
115 PHPDebugModel.createLineBreakpoint(getFile(), lineNumber, 0, true, null);
117 DebugPlugin.getDefault().getBreakpointManager().removeBreakpoint( breakpoint, true );;
119 } catch( CoreException ce ) {
120 PHPDebugUiPlugin.errorDialog( "Cannot add breakpoint", ce );
126 public ManageBreakpointActionDelegate() {
130 * @see IWorkbenchWindowActionDelegate#run
132 public void run(IAction action) {
133 if ( getTextEditor() != null ) {
135 manageBreakpoint( getTextEditor().getEditorInput() );
140 * @see IWorkbenchWindowActionDelegate#selectionChanged
142 public void selectionChanged(IAction action, ISelection selection) {
148 protected void update( ISelection selection )
150 setEnabledState( getTextEditor() );
153 protected void initialize(IAction action) {
155 if (getWorkbenchWindow() != null) {
156 IWorkbenchPage page= getWorkbenchWindow().getActivePage();
158 IEditorPart part= page.getActiveEditor();
159 if (part instanceof ITextEditor) {
160 setTextEditor((ITextEditor)part);
161 update( getTextEditor().getSelectionProvider().getSelection() );
169 * @see IWorkbenchWindowActionDelegate#dispose
171 public void dispose() {
172 getWorkbenchWindow().getPartService().removePartListener( this );
176 * @see IWorkbenchWindowActionDelegate#init
178 public void init(IWorkbenchWindow window) {
179 setWorkbenchWindow( window );
180 window.getPartService().addPartListener( this );
183 protected ITextEditor getTextEditor() {
188 protected IAction getAction() {
192 protected void setAction(IAction action) {
196 protected IWorkbenchWindow getWorkbenchWindow() {
197 return fWorkbenchWindow;
200 protected void setWorkbenchWindow(IWorkbenchWindow workbenchWindow) {
201 fWorkbenchWindow = workbenchWindow;
204 protected IFile getFile()
209 protected void setFile( IFile file )
214 protected void setTextEditor(ITextEditor editor) {
215 fTextEditor = editor;
216 if ( fTextEditor != null ) {
217 IEditorInput input = fTextEditor.getEditorInput();
218 setFile( ( input != null && input instanceof IFileEditorInput ) ? ((IFileEditorInput)input).getFile() : null );
220 setEnabledState(editor);
222 protected void setEnabledState(ITextEditor editor) {
223 if ( getAction() != null ) {
224 getAction().setEnabled( editor != null );
228 protected void update() {
229 IAction action= getAction();
230 if (action != null) {
231 if (getTextEditor() != null) {
232 breakpointExists(getTextEditor().getEditorInput());
237 protected boolean breakpointExists(IEditorInput editorInput){
241 protected void report(String message) {
242 if (getTextEditor() != null) {
243 IEditorStatusLine statusLine= (IEditorStatusLine) getTextEditor().getAdapter(IEditorStatusLine.class);
244 if (statusLine != null) {
245 if (message != null) {
246 statusLine.setMessage(true, message, null);
248 statusLine.setMessage(true, null, null);
252 if (message != null && PHPDebugUiPlugin.getActiveWorkbenchShell() != null) {
253 PHPDebugUiPlugin.getActiveWorkbenchShell().getDisplay().beep();