KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > debugger > jpda > projects > RunToCursorActionProvider


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.debugger.jpda.projects;
21
22 import java.awt.event.ActionEvent JavaDoc;
23 import java.beans.PropertyChangeEvent JavaDoc;
24 import java.beans.PropertyChangeListener JavaDoc;
25 import java.beans.PropertyChangeSupport JavaDoc;
26 import java.util.Collections JavaDoc;
27 import java.util.Set JavaDoc;
28 import javax.swing.AbstractAction JavaDoc;
29 import javax.swing.Action JavaDoc;
30 import javax.swing.SwingUtilities JavaDoc;
31 import org.netbeans.api.debugger.ActionsManager;
32
33
34 import org.netbeans.api.debugger.Breakpoint;
35 import org.netbeans.api.debugger.DebuggerEngine;
36
37 import org.netbeans.api.debugger.DebuggerManager;
38 import org.netbeans.api.debugger.DebuggerManagerAdapter;
39
40 import org.netbeans.api.debugger.DebuggerManagerListener;
41 import org.netbeans.spi.debugger.ContextProvider;
42 import org.netbeans.api.debugger.Session;
43 import org.netbeans.api.debugger.Watch;
44 import org.netbeans.api.debugger.jpda.JPDADebugger;
45
46 import org.netbeans.api.debugger.jpda.LineBreakpoint;
47 import org.netbeans.api.java.project.JavaProjectConstants;
48 import org.netbeans.api.project.Project;
49 import org.netbeans.spi.debugger.ActionsProvider;
50 import org.netbeans.spi.debugger.ActionsProviderListener;
51 import org.netbeans.spi.debugger.ActionsProviderSupport;
52 import org.netbeans.spi.debugger.jpda.EditorContext;
53 import org.netbeans.spi.project.ActionProvider;
54 import org.openide.ErrorManager;
55 import org.openide.util.RequestProcessor;
56
57 import org.openide.windows.TopComponent;
58
59
60 /**
61 *
62 * @author Jan Jancura
63 */

64 public class RunToCursorActionProvider extends ActionsProviderSupport {
65     
66     private EditorContext editor;
67     private LineBreakpoint breakpoint;
68     
69     {
70         editor = (EditorContext) DebuggerManager.
71             getDebuggerManager ().lookupFirst (null, EditorContext.class);
72         
73         Listener JavaDoc listener = new Listener JavaDoc ();
74         MainProjectManager.getDefault ().addPropertyChangeListener (listener);
75         TopComponent.getRegistry ().addPropertyChangeListener (listener);
76         DebuggerManager.getDebuggerManager ().addDebuggerListener (
77             DebuggerManager.PROP_DEBUGGER_ENGINES,
78             listener
79         );
80
81         //PATCH 57824: getOpenedPanes () calls from non AWT threads can
82
// lead to deadlock.
83
SwingUtilities.invokeLater (new Runnable JavaDoc () {
84             public void run () {
85                 setEnabled (
86                     ActionsManager.ACTION_RUN_TO_CURSOR,
87                     shouldBeEnabled ()
88                 );
89             }
90         });
91         //PATCH 57824
92
}
93     
94     public Set JavaDoc getActions () {
95         return Collections.singleton (ActionsManager.ACTION_RUN_TO_CURSOR);
96     }
97     
98     public void doAction (Object JavaDoc action) {
99         
100         // 1) set breakpoint
101
removeBreakpoint ();
102         createBreakpoint (LineBreakpoint.create (
103             editor.getCurrentURL (),
104             editor.getCurrentLineNumber ()
105         ));
106         
107         // 2) start debugging of project
108
invokeAction();
109     }
110     
111     public void postAction(Object JavaDoc action, final Runnable JavaDoc actionPerformedNotifier) {
112         final LineBreakpoint newBreakpoint = LineBreakpoint.create (
113             editor.getCurrentURL (),
114             editor.getCurrentLineNumber ()
115         );
116         RequestProcessor.getDefault().post(new Runnable JavaDoc() {
117             public void run() {
118                 // 1) set breakpoint
119
removeBreakpoint ();
120                 createBreakpoint (newBreakpoint);
121                 try {
122                     SwingUtilities.invokeAndWait(new Runnable JavaDoc() {
123                         public void run() {
124                             invokeAction();
125                         }
126                     });
127                 } catch (InterruptedException JavaDoc iex) {
128                     // Procceed
129
} catch (java.lang.reflect.InvocationTargetException JavaDoc itex) {
130                     ErrorManager.getDefault().notify(itex);
131                 } finally {
132                     actionPerformedNotifier.run();
133                 }
134             }
135         });
136     }
137     
138     private void invokeAction() {
139         ((ActionProvider) MainProjectManager.getDefault ().
140             getMainProject ().getLookup ().lookup (
141                 ActionProvider.class
142             )).invokeAction (
143                 ActionProvider.COMMAND_DEBUG,
144                 MainProjectManager.getDefault ().getMainProject ().getLookup ()
145             );
146     }
147     
148     private boolean shouldBeEnabled () {
149         if (editor.getCurrentLineNumber () < 0) return false;
150         if (!editor.getCurrentURL ().endsWith (".java")) return false;
151         
152         // check if current project supports this action
153
Project p = MainProjectManager.getDefault ().getMainProject ();
154         if (p == null) return false;
155         ActionProvider actionProvider = (ActionProvider) p.getLookup ().
156             lookup (ActionProvider.class);
157         if (actionProvider == null) return false;
158         String JavaDoc[] sa = actionProvider.getSupportedActions ();
159         int i, k = sa.length;
160         for (i = 0; i < k; i++)
161             if (ActionProvider.COMMAND_DEBUG.equals (sa [i]))
162                 break;
163         if (i == k) return false;
164
165         // check if this action should be enabled
166
return ((ActionProvider) p.getLookup ().lookup (
167                 ActionProvider.class
168             )).isActionEnabled (
169                 ActionProvider.COMMAND_DEBUG,
170                 MainProjectManager.getDefault ().getMainProject ().getLookup ()
171             );
172     }
173     
174     private void createBreakpoint (LineBreakpoint breakpoint) {
175         breakpoint.setHidden (true);
176         DebuggerManager.getDebuggerManager ().addBreakpoint (breakpoint);
177         this.breakpoint = breakpoint;
178     }
179     
180     private void removeBreakpoint () {
181         if (breakpoint != null) {
182             DebuggerManager.getDebuggerManager ().removeBreakpoint (breakpoint);
183             breakpoint = null;
184         }
185     }
186     
187     private class Listener extends DebuggerManagerAdapter {
188         public void propertyChange (PropertyChangeEvent JavaDoc e) {
189             if (e.getPropertyName () == JPDADebugger.PROP_STATE) {
190                 int state = ((Integer JavaDoc) e.getNewValue ()).intValue ();
191                 if ( (state == JPDADebugger.STATE_DISCONNECTED) ||
192                      (state == JPDADebugger.STATE_STOPPED)
193                 ) removeBreakpoint ();
194                 return;
195             }
196             setEnabled (
197                 ActionsManager.ACTION_RUN_TO_CURSOR,
198                 shouldBeEnabled ()
199             );
200         }
201         
202         public void engineAdded (DebuggerEngine engine) {
203             JPDADebugger debugger = (JPDADebugger) engine.lookupFirst
204                 (null, JPDADebugger.class);
205             if (debugger == null) return;
206             debugger.addPropertyChangeListener (
207                 JPDADebugger.PROP_STATE,
208                 this
209             );
210         }
211         
212         public void engineRemoved (DebuggerEngine engine) {
213             JPDADebugger debugger = (JPDADebugger) engine.lookupFirst
214                 (null, JPDADebugger.class);
215             if (debugger == null) return;
216             debugger.removePropertyChangeListener (
217                 JPDADebugger.PROP_STATE,
218                 this
219             );
220         }
221     }
222 }
223
Popular Tags