KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > debug > actions > JspRunToCursorActionProvider


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.web.debug.actions;
21
22 import java.beans.PropertyChangeEvent JavaDoc;
23 import java.beans.PropertyChangeListener JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.Set JavaDoc;
26 import org.netbeans.api.debugger.ActionsManager;
27 import org.netbeans.api.debugger.Breakpoint;
28 import org.netbeans.api.debugger.DebuggerEngine;
29 import org.netbeans.api.debugger.DebuggerManager;
30 import org.netbeans.api.debugger.DebuggerManagerListener;
31 import org.netbeans.api.debugger.Session;
32 import org.netbeans.api.debugger.Watch;
33 import org.netbeans.api.debugger.jpda.JPDADebugger;
34 import org.netbeans.api.project.Project;
35 import org.netbeans.spi.debugger.jpda.EditorContext;
36 import org.netbeans.spi.project.ActionProvider;
37 import org.openide.windows.TopComponent;
38 import org.netbeans.modules.web.debug.breakpoints.JspLineBreakpoint;
39 import org.netbeans.modules.web.debug.util.Utils;
40 import org.netbeans.spi.debugger.ActionsProviderSupport;
41
42 /**
43 *
44 * @author Martin Grebac, Libor Kotouc
45 */

46 public class JspRunToCursorActionProvider extends ActionsProviderSupport {
47     
48     private EditorContext editorContext;
49     private JspLineBreakpoint breakpoint;
50         
51     {
52         editorContext = (EditorContext) DebuggerManager.
53             getDebuggerManager().lookupFirst(null, EditorContext.class);
54         
55         Listener JavaDoc listener = new Listener JavaDoc ();
56         MainProjectManager.getDefault ().addPropertyChangeListener (listener);
57         TopComponent.getRegistry ().addPropertyChangeListener (listener);
58         DebuggerManager.getDebuggerManager ().addDebuggerListener (listener);
59
60         setEnabled(ActionsManager.ACTION_RUN_TO_CURSOR, shouldBeEnabled());
61     }
62     
63     public Set JavaDoc getActions() {
64         return Collections.singleton (ActionsManager.ACTION_RUN_TO_CURSOR);
65     }
66     
67     public void doAction (Object JavaDoc action) {
68         
69         // 1) set breakpoint
70
removeBreakpoint();
71         createBreakpoint();
72         
73         // 2) start debugging of project
74
((ActionProvider) MainProjectManager.getDefault().
75             getMainProject().getLookup().lookup(
76                 ActionProvider.class
77             )).invokeAction (
78                 ActionProvider.COMMAND_DEBUG,
79                 MainProjectManager.getDefault ().getMainProject ().getLookup ()
80             );
81     }
82     
83     private boolean shouldBeEnabled () {
84
85         if (/* some module disabled? */ editorContext == null || !Utils.isJsp(editorContext.getCurrentURL())) {
86             return false;
87         }
88         
89         // check if current project supports this action
90
Project p = MainProjectManager.getDefault ().getMainProject ();
91         if (p == null) return false;
92         ActionProvider actionProvider = (ActionProvider)p.getLookup ().lookup (ActionProvider.class);
93         if (actionProvider == null) return false;
94
95         String JavaDoc[] sa = actionProvider.getSupportedActions ();
96         int i, k = sa.length;
97         for (i = 0; i < k; i++) {
98             if (ActionProvider.COMMAND_DEBUG.equals (sa [i])) {
99                 break;
100             }
101         }
102         if (i == k) {
103             return false;
104         }
105
106         // check if this action should be enabled
107
return ((ActionProvider) p.getLookup ().lookup (
108                 ActionProvider.class
109             )).isActionEnabled (
110                 ActionProvider.COMMAND_DEBUG,
111                 p.getLookup ()
112             );
113     }
114
115     private void createBreakpoint() {
116         breakpoint = JspLineBreakpoint.create (
117             editorContext.getCurrentURL (),
118             editorContext.getCurrentLineNumber ()
119         );
120         breakpoint.setHidden (true);
121         DebuggerManager.getDebuggerManager ().addBreakpoint (breakpoint);
122     }
123     
124     private void removeBreakpoint() {
125         if (breakpoint != null) {
126             DebuggerManager.getDebuggerManager ().removeBreakpoint (breakpoint);
127             breakpoint = null;
128         }
129     }
130     
131     private class Listener implements PropertyChangeListener JavaDoc, DebuggerManagerListener {
132         public void propertyChange (PropertyChangeEvent JavaDoc e) {
133             if ((e == null) || (TopComponent.Registry.PROP_OPENED.equals(e.getPropertyName())))
134                 return;
135             if (e.getPropertyName () == JPDADebugger.PROP_STATE) {
136                 int state = ((Integer JavaDoc) e.getNewValue ()).intValue ();
137                 if (state == JPDADebugger.STATE_DISCONNECTED || state == JPDADebugger.STATE_STOPPED)
138                     removeBreakpoint ();
139                 return;
140             }
141
142             setEnabled (
143                 ActionsManager.ACTION_RUN_TO_CURSOR,
144                 shouldBeEnabled ()
145             );
146         }
147         
148         public void sessionRemoved (Session session) {
149             removeBreakpoint();
150         }
151         
152         public void breakpointAdded (Breakpoint breakpoint) {}
153         public void breakpointRemoved (Breakpoint breakpoint) {}
154         public Breakpoint[] initBreakpoints () {
155             return new Breakpoint [0];
156         }
157         public void initWatches () {}
158         public void sessionAdded (Session session) {}
159         public void watchAdded (Watch watch) {}
160         public void watchRemoved (Watch watch) {}
161
162         public void engineAdded (DebuggerEngine engine) {
163             JPDADebugger debugger = (JPDADebugger) engine.lookupFirst
164                 (null, JPDADebugger.class);
165             if (debugger == null) return;
166             debugger.addPropertyChangeListener (
167                 JPDADebugger.PROP_STATE,
168                 this
169             );
170         }
171         
172         public void engineRemoved (DebuggerEngine engine) {
173             JPDADebugger debugger = (JPDADebugger) engine.lookupFirst
174                 (null, JPDADebugger.class);
175             if (debugger == null) return;
176             debugger.removePropertyChangeListener (
177                 JPDADebugger.PROP_STATE,
178                 this
179             );
180         }
181
182     }
183 }
184
Popular Tags