KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > debugger > jpda > ui > actions > 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 package org.netbeans.modules.debugger.jpda.ui.actions;
20
21 import com.sun.jdi.VMDisconnectedException;
22 import com.sun.jdi.request.EventRequestManager;
23 import java.beans.PropertyChangeEvent JavaDoc;
24 import java.beans.PropertyChangeListener JavaDoc;
25
26 import java.util.Collections JavaDoc;
27 import java.util.Set JavaDoc;
28 import org.netbeans.api.debugger.ActionsManager;
29 import org.netbeans.api.debugger.ActionsManagerListener;
30
31
32 import org.netbeans.api.debugger.DebuggerManager;
33 import org.netbeans.spi.debugger.ContextProvider;
34 import org.netbeans.api.debugger.Session;
35 import org.netbeans.api.debugger.jpda.JPDADebugger;
36 import org.netbeans.api.debugger.jpda.LineBreakpoint;
37 import org.netbeans.modules.debugger.jpda.ui.EditorContextBridge;
38 import org.netbeans.spi.debugger.ActionsProviderSupport;
39
40
41 /**
42  *
43  * @author Jan Jancura
44  */

45 public class RunToCursorActionProvider extends ActionsProviderSupport
46                                        implements PropertyChangeListener JavaDoc,
47                                                   ActionsManagerListener {
48
49     private JPDADebugger debugger;
50     private Session session;
51     private LineBreakpoint breakpoint;
52     private ActionsManager lastActionsManager;
53     
54     
55     public RunToCursorActionProvider (ContextProvider lookupProvider) {
56         debugger = (JPDADebugger) lookupProvider.lookupFirst
57                 (null, JPDADebugger.class);
58         session = (Session) lookupProvider.lookupFirst
59                 (null, Session.class);
60         debugger.addPropertyChangeListener (debugger.PROP_STATE, this);
61         EditorContextBridge.addPropertyChangeListener (this);
62     }
63     
64     private void destroy () {
65         debugger.removePropertyChangeListener (debugger.PROP_STATE, this);
66         EditorContextBridge.removePropertyChangeListener (this);
67     }
68     
69     static ActionsManager getCurrentActionsManager () {
70         return DebuggerManager.getDebuggerManager ().
71             getCurrentEngine () == null ?
72             DebuggerManager.getDebuggerManager ().getActionsManager () :
73             DebuggerManager.getDebuggerManager ().getCurrentEngine ().
74                 getActionsManager ();
75     }
76     
77     private ActionsManager getActionsManager () {
78         ActionsManager current = getCurrentActionsManager();
79         if (current != lastActionsManager) {
80             if (lastActionsManager != null) {
81                 lastActionsManager.removeActionsManagerListener(
82                         ActionsManagerListener.PROP_ACTION_STATE_CHANGED, this);
83             }
84             current.addActionsManagerListener(
85                     ActionsManagerListener.PROP_ACTION_STATE_CHANGED, this);
86             lastActionsManager = current;
87         }
88         return current;
89     }
90
91     public void propertyChange (PropertyChangeEvent JavaDoc evt) {
92         setEnabled (
93             ActionsManager.ACTION_RUN_TO_CURSOR,
94             getActionsManager().isEnabled(ActionsManager.ACTION_CONTINUE) &&
95             (debugger.getState () == debugger.STATE_STOPPED) &&
96             (EditorContextBridge.getCurrentLineNumber () >= 0) &&
97             (EditorContextBridge.getCurrentURL ().endsWith (".java"))
98         );
99         if ( (debugger.getState () != debugger.STATE_RUNNING) &&
100              (breakpoint != null)
101         ) {
102             DebuggerManager.getDebuggerManager ().removeBreakpoint (breakpoint);
103             breakpoint = null;
104         }
105         if (debugger.getState () == debugger.STATE_DISCONNECTED)
106             destroy ();
107     }
108     
109     public Set JavaDoc getActions () {
110         return Collections.singleton (ActionsManager.ACTION_RUN_TO_CURSOR);
111     }
112     
113     public void doAction (Object JavaDoc action) {
114         if (breakpoint != null) {
115             DebuggerManager.getDebuggerManager ().removeBreakpoint (breakpoint);
116             breakpoint = null;
117         }
118         breakpoint = LineBreakpoint.create (
119             EditorContextBridge.getCurrentURL (),
120             EditorContextBridge.getCurrentLineNumber ()
121         );
122         breakpoint.setHidden (true);
123         DebuggerManager.getDebuggerManager ().addBreakpoint (breakpoint);
124         session.getEngineForLanguage ("Java").getActionsManager ().doAction (
125             ActionsManager.ACTION_CONTINUE
126         );
127     }
128
129     public void actionPerformed(Object JavaDoc action) {
130         // Is never called
131
}
132
133     /** Sync up with continue action state. */
134     public void actionStateChanged(Object JavaDoc action, boolean enabled) {
135         if (ActionsManager.ACTION_CONTINUE == action) {
136             setEnabled (
137                 ActionsManager.ACTION_RUN_TO_CURSOR,
138                 enabled &&
139                 (debugger.getState () == debugger.STATE_STOPPED) &&
140                 (EditorContextBridge.getCurrentLineNumber () >= 0) &&
141                 (EditorContextBridge.getCurrentURL ().endsWith (".java"))
142             );
143         }
144     }
145 }
146
Popular Tags