KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > debugger > jpda > ui > actions > MakeCallerCurrentActionProvider


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.ui.actions;
21
22 import com.sun.jdi.AbsentInformationException;
23 import com.sun.jdi.ThreadReference;
24
25 import java.util.Collections JavaDoc;
26 import java.util.Set JavaDoc;
27 import javax.swing.SwingUtilities JavaDoc;
28 import org.netbeans.api.debugger.ActionsManager;
29
30
31 import org.netbeans.api.debugger.DebuggerManager;
32 import org.netbeans.spi.debugger.ContextProvider;
33 import org.netbeans.api.debugger.jpda.CallStackFrame;
34 import org.netbeans.api.debugger.jpda.JPDADebugger;
35 import org.netbeans.api.debugger.jpda.JPDAThread;
36
37 import org.netbeans.modules.debugger.jpda.ui.SourcePath;
38
39
40 /**
41 * Representation of a debugging session.
42 *
43 * @author Jan Jancura
44 */

45 public class MakeCallerCurrentActionProvider extends JPDADebuggerAction {
46     
47     private ContextProvider lookupProvider;
48
49     
50     public MakeCallerCurrentActionProvider (ContextProvider lookupProvider) {
51         super (
52             (JPDADebugger) lookupProvider.lookupFirst
53                 (null, JPDADebugger.class)
54         );
55         this.lookupProvider = lookupProvider;
56         getDebuggerImpl ().addPropertyChangeListener
57             (JPDADebugger.PROP_CURRENT_CALL_STACK_FRAME, this);
58     }
59     
60     public Set JavaDoc getActions () {
61         return Collections.singleton (ActionsManager.ACTION_MAKE_CALLER_CURRENT);
62     }
63
64     public void doAction (Object JavaDoc action) {
65         JPDAThread t = getDebuggerImpl ().getCurrentThread ();
66         if (t == null) return;
67         int i = getCurrentCallStackFrameIndex (getDebuggerImpl ());
68         if (i >= (t.getStackDepth () - 1)) return;
69         setCurrentCallStackFrameIndex (getDebuggerImpl (), ++i, lookupProvider);
70     }
71     
72     protected void checkEnabled (int debuggerState) {
73         if (debuggerState == getDebuggerImpl ().STATE_STOPPED) {
74             JPDAThread t = getDebuggerImpl ().getCurrentThread ();
75             if (t != null) {
76                 int i = getCurrentCallStackFrameIndex (getDebuggerImpl ());
77                 setEnabled (
78                     ActionsManager.ACTION_MAKE_CALLER_CURRENT,
79                     i < (t.getStackDepth () - 1)
80                 );
81                 return;
82             }
83         }
84         setEnabled (
85             ActionsManager.ACTION_MAKE_CALLER_CURRENT,
86             false
87         );
88     }
89     
90     static int getCurrentCallStackFrameIndex (JPDADebugger debuggerImpl) {
91         try {
92             JPDAThread t = debuggerImpl.getCurrentThread ();
93             if (t == null) return -1;
94             CallStackFrame csf = debuggerImpl.getCurrentCallStackFrame ();
95             if (csf == null) return -1;
96             CallStackFrame s[] = t.getCallStack ();
97             int i, k = s.length;
98             for (i = 0; i < k; i++)
99                 if (csf.equals (s [i])) return i;
100         } catch (AbsentInformationException e) {
101         }
102         return -1;
103     }
104     
105     static void setCurrentCallStackFrameIndex (
106         JPDADebugger debuggerImpl,
107         int index,
108         final ContextProvider lookupProvider
109     ) {
110         try {
111             JPDAThread t = debuggerImpl.getCurrentThread ();
112             if (t == null) return;
113             if (t.getStackDepth () <= index) return;
114             final CallStackFrame csf = t.getCallStack (index, index + 1) [0];
115             csf.makeCurrent ();
116         } catch (AbsentInformationException e) {
117         }
118     }
119 }
120
Popular Tags