d6cadcd0a41ea66cbfa84cc7de520377f21959c3
[phpeclipse.git] /
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;
22
23 import net.sourceforge.phpdt.internal.debug.ui.PHPDebugUiPlugin;
24
25 /* (non-Javadoc)
26  * @see IWorkbenchWindowActionDelegate
27  */
28 public class ManageBreakpointActionDelegate implements IWorkbenchWindowActionDelegate, IPartListener {
29
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;
35
36
37         /* (non-Javadoc)
38          * @see org.eclipse.ui.IPartListener#partActivated(IWorkbenchPart)
39          */
40         public void partActivated( IWorkbenchPart part )
41         {
42                 if ( part instanceof ITextEditor )
43                 {
44                         setTextEditor( (ITextEditor)part );
45                 }
46         }
47
48         /* (non-Javadoc)
49          * @see org.eclipse.ui.IPartListener#partBroughtToTop(IWorkbenchPart)
50          */
51         public void partBroughtToTop( IWorkbenchPart part )
52         {
53         }
54
55         /* (non-Javadoc)
56          * @see org.eclipse.ui.IPartListener#partClosed(IWorkbenchPart)
57          */
58         public void partClosed( IWorkbenchPart part )
59         {
60                 if ( part == getTextEditor() )
61                 {
62                         setTextEditor( null );
63                         if ( getAction() != null )
64                         {
65                                 getAction().setEnabled( false );
66                         }
67                 }
68         }
69
70         /* (non-Javadoc)
71          * @see org.eclipse.ui.IPartListener#partDeactivated(IWorkbenchPart)
72          */
73         public void partDeactivated( IWorkbenchPart part )
74         {
75         }
76
77         /* (non-Javadoc)
78          * @see org.eclipse.ui.IPartListener#partOpened(IWorkbenchPart)
79          */
80         public void partOpened( IWorkbenchPart part )
81         {
82                 if ( part instanceof ITextEditor )
83                 {
84                         if ( getTextEditor() == null )
85                         {
86                                 setTextEditor( (ITextEditor)part );
87                         }
88                 }
89         }
90
91
92         /**
93          * Manages a breakpoint.
94          */
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$
99                         return;
100                 }
101                 report(null);
102                 ISelection selection= sp.getSelection();
103                 if ( selection instanceof ITextSelection ) {
104                         if ( getFile() == null )
105                                 return;
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 ) {
111                                 
112                                 try     {
113                                         PHPLineBreakpoint breakpoint=PHPDebugModel.lineBreakpointExists(lineNumber);    
114                                         if (breakpoint==null)   
115                                                 PHPDebugModel.createLineBreakpoint(getFile(), lineNumber, 0, true, null);
116                                         else
117                                                 DebugPlugin.getDefault().getBreakpointManager().removeBreakpoint( breakpoint, true );;  
118                                         
119                                 }       catch( CoreException ce )       {
120                                         PHPDebugUiPlugin.errorDialog( "Cannot add breakpoint", ce );
121                                 }
122                         }
123                 }               
124         }
125         
126         public ManageBreakpointActionDelegate() {
127         }
128
129         /* (non-Javadoc)
130          * @see IWorkbenchWindowActionDelegate#run
131          */
132         public void run(IAction action)  {
133                 if ( getTextEditor() != null ) {
134                         update();
135                         manageBreakpoint( getTextEditor().getEditorInput() );
136                 }               
137         }
138
139         /* (non-Javadoc)
140          * @see IWorkbenchWindowActionDelegate#selectionChanged
141          */
142         public void selectionChanged(IAction action, ISelection selection)  {
143                 if (!fInitialized) {
144                         initialize(action);
145                 } 
146         }
147         
148         protected void update( ISelection selection )
149         {
150                 setEnabledState( getTextEditor() );
151         }
152         
153         protected void initialize(IAction action) {
154                 setAction( action );
155                 if (getWorkbenchWindow() != null) {
156                         IWorkbenchPage page= getWorkbenchWindow().getActivePage();
157                         if (page != null) {
158                                 IEditorPart part= page.getActiveEditor();
159                                 if (part instanceof ITextEditor) {
160                                         setTextEditor((ITextEditor)part);
161                                         update( getTextEditor().getSelectionProvider().getSelection() );                                                
162                                 }
163                         }
164                 }
165                 fInitialized= true;
166         }
167
168         /* (non-Javadoc)
169          * @see IWorkbenchWindowActionDelegate#dispose
170          */
171         public void dispose()  {
172                 getWorkbenchWindow().getPartService().removePartListener( this );               
173         }
174
175         /* (non-Javadoc)
176          * @see IWorkbenchWindowActionDelegate#init
177          */
178         public void init(IWorkbenchWindow window)  {
179                 setWorkbenchWindow( window );
180                 window.getPartService().addPartListener( this );                
181         }
182         
183         protected ITextEditor getTextEditor() {
184                 return fTextEditor;
185         }
186         
187         
188         protected IAction getAction() {
189                 return fAction;
190         }
191
192         protected void setAction(IAction action) {
193                 fAction = action;
194         }
195
196         protected IWorkbenchWindow getWorkbenchWindow() {
197                 return fWorkbenchWindow;
198         }
199
200         protected void setWorkbenchWindow(IWorkbenchWindow workbenchWindow) {
201                 fWorkbenchWindow = workbenchWindow;
202         }
203
204         protected IFile getFile()
205         {
206                 return fFile;
207         }
208
209         protected void setFile( IFile file ) 
210         {
211                 fFile = file;
212         }       
213
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 );
219                 }
220                 setEnabledState(editor);
221         }
222         protected void setEnabledState(ITextEditor editor) {
223                 if ( getAction() != null ) {
224                         getAction().setEnabled( editor != null );
225                 }
226         }
227         
228         protected void update() {
229                 IAction action= getAction();
230                 if (action != null) {
231                         if (getTextEditor() != null) {
232                                 breakpointExists(getTextEditor().getEditorInput());
233                         }
234                 }
235         }
236         
237         protected boolean breakpointExists(IEditorInput editorInput){
238                 return false;
239         }       
240         
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);
247                                 } else {
248                                         statusLine.setMessage(true, null, null);
249                                 }
250                         }
251                 }               
252                 if (message != null && PHPDebugUiPlugin.getActiveWorkbenchShell() != null) {
253                         PHPDebugUiPlugin.getActiveWorkbenchShell().getDisplay().beep();
254                 }
255         }
256 }