package net.sourceforge.phpdt.internal.debug.ui.actions; import net.sourceforge.phpdt.debug.core.PHPDebugModel; import org.eclipse.debug.core.DebugPlugin; import net.sourceforge.phpdt.internal.debug.core.breakpoints.PHPLineBreakpoint; import org.eclipse.jface.action.IAction; import org.eclipse.jface.viewers.ISelection; import org.eclipse.ui.IWorkbenchWindow; import org.eclipse.ui.IWorkbenchWindowActionDelegate; import org.eclipse.ui.texteditor.IEditorStatusLine; import org.eclipse.ui.texteditor.ITextEditor; import org.eclipse.core.resources.IFile; import org.eclipse.ui.IEditorPart; import org.eclipse.jface.text.IDocument; import org.eclipse.ui.IWorkbenchPage; import org.eclipse.ui.IPartListener; import org.eclipse.jface.text.ITextSelection; import org.eclipse.ui.IFileEditorInput; import org.eclipse.jface.viewers.ISelectionProvider; import org.eclipse.ui.IEditorInput; import org.eclipse.ui.IWorkbenchPart; import org.eclipse.core.runtime.CoreException; import net.sourceforge.phpdt.internal.debug.ui.PHPDebugUiPlugin; /* (non-Javadoc) * @see IWorkbenchWindowActionDelegate */ public class ManageBreakpointActionDelegate implements IWorkbenchWindowActionDelegate, IPartListener { protected boolean fInitialized= false; private ITextEditor fTextEditor= null; private IAction fAction= null; private IFile fFile = null; private IWorkbenchWindow fWorkbenchWindow= null; /* (non-Javadoc) * @see org.eclipse.ui.IPartListener#partActivated(IWorkbenchPart) */ public void partActivated( IWorkbenchPart part ) { if ( part instanceof ITextEditor ) { setTextEditor( (ITextEditor)part ); } } /* (non-Javadoc) * @see org.eclipse.ui.IPartListener#partBroughtToTop(IWorkbenchPart) */ public void partBroughtToTop( IWorkbenchPart part ) { } /* (non-Javadoc) * @see org.eclipse.ui.IPartListener#partClosed(IWorkbenchPart) */ public void partClosed( IWorkbenchPart part ) { if ( part == getTextEditor() ) { setTextEditor( null ); if ( getAction() != null ) { getAction().setEnabled( false ); } } } /* (non-Javadoc) * @see org.eclipse.ui.IPartListener#partDeactivated(IWorkbenchPart) */ public void partDeactivated( IWorkbenchPart part ) { } /* (non-Javadoc) * @see org.eclipse.ui.IPartListener#partOpened(IWorkbenchPart) */ public void partOpened( IWorkbenchPart part ) { if ( part instanceof ITextEditor ) { if ( getTextEditor() == null ) { setTextEditor( (ITextEditor)part ); } } } /** * Manages a breakpoint. */ protected void manageBreakpoint(IEditorInput editorInput) { ISelectionProvider sp= getTextEditor().getSelectionProvider(); if (sp == null || getFile() == null) { report("ManageBreakpointActionDelegate.No_Breakpoint"); //$NON-NLS-1$ return; } report(null); ISelection selection= sp.getSelection(); if ( selection instanceof ITextSelection ) { if ( getFile() == null ) return; IDocument document = getTextEditor().getDocumentProvider().getDocument( editorInput ); // BreakpointLocationVerifier bv = new BreakpointLocationVerifier(); // int lineNumber = bv.getValidLineBreakpointLocation( document, ((ITextSelection)selection).getStartLine()); int lineNumber = ((ITextSelection)selection).getStartLine() +1; if ( lineNumber > -1 ) { try { PHPLineBreakpoint breakpoint=PHPDebugModel.lineBreakpointExists(lineNumber); if (breakpoint==null) PHPDebugModel.createLineBreakpoint(getFile(), lineNumber, 0, true, null); else DebugPlugin.getDefault().getBreakpointManager().removeBreakpoint( breakpoint, true ); } catch( CoreException ce ) { PHPDebugUiPlugin.errorDialog( "Cannot add breakpoint", ce ); } } } } public ManageBreakpointActionDelegate() { } /* (non-Javadoc) * @see IWorkbenchWindowActionDelegate#run */ public void run(IAction action) { if ( getTextEditor() != null ) { update(); manageBreakpoint( getTextEditor().getEditorInput() ); } } /* (non-Javadoc) * @see IWorkbenchWindowActionDelegate#selectionChanged */ public void selectionChanged(IAction action, ISelection selection) { if (!fInitialized) { initialize(action); } } protected void update( ISelection selection ) { setEnabledState( getTextEditor() ); } protected void initialize(IAction action) { setAction( action ); if (getWorkbenchWindow() != null) { IWorkbenchPage page= getWorkbenchWindow().getActivePage(); if (page != null) { IEditorPart part= page.getActiveEditor(); if (part instanceof ITextEditor) { setTextEditor((ITextEditor)part); update( getTextEditor().getSelectionProvider().getSelection() ); } } } fInitialized= true; } /* (non-Javadoc) * @see IWorkbenchWindowActionDelegate#dispose */ public void dispose() { getWorkbenchWindow().getPartService().removePartListener( this ); } /* (non-Javadoc) * @see IWorkbenchWindowActionDelegate#init */ public void init(IWorkbenchWindow window) { setWorkbenchWindow( window ); window.getPartService().addPartListener( this ); } protected ITextEditor getTextEditor() { return fTextEditor; } protected IAction getAction() { return fAction; } protected void setAction(IAction action) { fAction = action; } protected IWorkbenchWindow getWorkbenchWindow() { return fWorkbenchWindow; } protected void setWorkbenchWindow(IWorkbenchWindow workbenchWindow) { fWorkbenchWindow = workbenchWindow; } protected IFile getFile() { return fFile; } protected void setFile( IFile file ) { fFile = file; } protected void setTextEditor(ITextEditor editor) { fTextEditor = editor; if ( fTextEditor != null ) { IEditorInput input = fTextEditor.getEditorInput(); setFile( ( input != null && input instanceof IFileEditorInput ) ? ((IFileEditorInput)input).getFile() : null ); } setEnabledState(editor); } protected void setEnabledState(ITextEditor editor) { if ( getAction() != null ) { getAction().setEnabled( editor != null ); } } protected void update() { IAction action= getAction(); if (action != null) { if (getTextEditor() != null) { breakpointExists(getTextEditor().getEditorInput()); } } } protected boolean breakpointExists(IEditorInput editorInput){ return false; } protected void report(String message) { if (getTextEditor() != null) { IEditorStatusLine statusLine= (IEditorStatusLine) getTextEditor().getAdapter(IEditorStatusLine.class); if (statusLine != null) { if (message != null) { statusLine.setMessage(true, message, null); } else { statusLine.setMessage(true, null, null); } } } if (message != null && PHPDebugUiPlugin.getActiveWorkbenchShell() != null) { PHPDebugUiPlugin.getActiveWorkbenchShell().getDisplay().beep(); } } }