KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > debugger > jpda > ui > models > CallStackActionsProvider


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.models;
21
22 import com.sun.jdi.AbsentInformationException;
23 import java.awt.event.ActionEvent JavaDoc;
24 import java.util.Vector JavaDoc;
25 import javax.swing.Action JavaDoc;
26 import javax.swing.AbstractAction JavaDoc;
27 import javax.swing.SwingUtilities JavaDoc;
28
29 import org.netbeans.api.debugger.DebuggerEngine;
30 import org.netbeans.api.debugger.DebuggerManager;
31 import org.netbeans.spi.debugger.ContextProvider;
32 import org.netbeans.api.debugger.Session;
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 import org.netbeans.spi.viewmodel.NodeActionsProvider;
37 import org.netbeans.spi.viewmodel.ModelListener;
38 import org.netbeans.spi.viewmodel.UnknownTypeException;
39 import org.netbeans.spi.viewmodel.Models;
40 import org.netbeans.spi.viewmodel.TreeModel;
41
42 import org.netbeans.modules.debugger.jpda.ui.EditorContextBridge;
43 import org.netbeans.modules.debugger.jpda.ui.SourcePath;
44
45 import org.openide.ErrorManager;
46 import org.openide.util.NbBundle;
47 import org.openide.util.RequestProcessor;
48
49 import java.awt.Toolkit JavaDoc;
50 import java.awt.datatransfer.Clipboard JavaDoc;
51 import java.awt.datatransfer.StringSelection JavaDoc;
52 import java.awt.datatransfer.Transferable JavaDoc;
53
54
55 /**
56  * @author Jan Jancura
57  */

58 public class CallStackActionsProvider implements NodeActionsProvider {
59     
60     private final Action JavaDoc MAKE_CURRENT_ACTION = Models.createAction (
61         NbBundle.getBundle(ThreadsActionsProvider.class).getString("CTL_CallstackAction_MakeCurrent_Label"),
62         new Models.ActionPerformer () {
63             public boolean isEnabled (Object JavaDoc node) {
64                 // TODO: Check whether is not current - API change necessary
65
return true;
66             }
67             public void perform (Object JavaDoc[] nodes) {
68                 makeCurrent ((CallStackFrame) nodes [0]);
69             }
70         },
71         Models.MULTISELECTION_TYPE_EXACTLY_ONE
72     );
73         
74 // private final Action COPY_TO_CLBD_ACTION = Models.createAction (
75
// NbBundle.getBundle(ThreadsActionsProvider.class).getString("CTL_CallstackAction_Copy2CLBD_Label"),
76
// new Models.ActionPerformer () {
77
// public boolean isEnabled (Object node) {
78
// return true;
79
// }
80
// public void perform (Object[] nodes) {
81
// stackToCLBD (nodes[0]);
82
// }
83
// },
84
// Models.MULTISELECTION_TYPE_ANY
85
// );
86

87     private final Action JavaDoc COPY_TO_CLBD_ACTION = new AbstractAction JavaDoc (
88         NbBundle.getBundle(ThreadsActionsProvider.class).getString("CTL_CallstackAction_Copy2CLBD_Label")) {
89         public void actionPerformed (ActionEvent JavaDoc e) {
90             stackToCLBD ();
91         }
92     };
93         
94     private static final Action JavaDoc POP_TO_HERE_ACTION = Models.createAction (
95         NbBundle.getBundle(ThreadsActionsProvider.class).getString("CTL_CallstackAction_PopToHere_Label"),
96         new Models.ActionPerformer () {
97             public boolean isEnabled (Object JavaDoc node) {
98                 // TODO: Check whether this frame is deeper then the top-most
99
return true;
100             }
101             public void perform (final Object JavaDoc[] nodes) {
102                 // Do not do expensive actions in AWT,
103
// It can also block if it can not procceed for some reason
104
RequestProcessor.getDefault().post(new Runnable JavaDoc() {
105                     public void run() {
106                         popToHere ((CallStackFrame) nodes [0]);
107                     }
108                 });
109             }
110         },
111         Models.MULTISELECTION_TYPE_EXACTLY_ONE
112     );
113         
114     private JPDADebugger debugger;
115     private ContextProvider lookupProvider;
116
117
118     public CallStackActionsProvider (ContextProvider lookupProvider) {
119         this.lookupProvider = lookupProvider;
120         debugger = (JPDADebugger) lookupProvider.
121             lookupFirst (null, JPDADebugger.class);
122     }
123     
124     public Action JavaDoc[] getActions (Object JavaDoc node) throws UnknownTypeException {
125         if (node == TreeModel.ROOT) {
126             return new Action JavaDoc [] { COPY_TO_CLBD_ACTION };
127         }
128         
129         if (!(node instanceof CallStackFrame))
130             throw new UnknownTypeException (node);
131         
132         boolean popToHere = debugger.canPopFrames ();
133         if (popToHere)
134             return new Action JavaDoc [] { MAKE_CURRENT_ACTION, POP_TO_HERE_ACTION, COPY_TO_CLBD_ACTION };
135         else
136             return new Action JavaDoc [] { MAKE_CURRENT_ACTION, COPY_TO_CLBD_ACTION };
137     }
138     
139     public void performDefaultAction (Object JavaDoc node) throws UnknownTypeException {
140         if (node == TreeModel.ROOT)
141             return;
142         if (node instanceof CallStackFrame) {
143             makeCurrent ((CallStackFrame) node);
144             return;
145         }
146         throw new UnknownTypeException (node);
147     }
148
149     public void addModelListener (ModelListener l) {
150     }
151
152     public void removeModelListener (ModelListener l) {
153     }
154
155     private static void popToHere (final CallStackFrame frame) {
156         try {
157             JPDAThread t = frame.getThread ();
158             CallStackFrame[] stack = t.getCallStack ();
159             int i, k = stack.length;
160             if (k < 2) return ;
161             for (i = 0; i < k; i++)
162                 if (stack [i].equals (frame)) {
163                     if (i > 0) {
164                         stack [i - 1].popFrame ();
165                     }
166                     return;
167                 }
168         } catch (AbsentInformationException ex) {
169         }
170     }
171     
172     private void stackToCLBD() {
173         try {
174             JPDAThread t = debugger.getCurrentThread();;
175             
176 // if (frame instanceof CallStackFrame )
177
// t = ((CallStackFrame)frame).getThread ();
178
// else
179
// t = debugger.getCurrentThread();
180

181             CallStackFrame[] stack = t.getCallStack ();
182             int i, k = stack.length;
183             StringBuffer JavaDoc frameStr = new StringBuffer JavaDoc(50);
184             
185             for (i = 0; i < k; i++) {
186                 int index = stack[i].getClassName().lastIndexOf('.');
187                 frameStr.append(stack[i].getClassName().substring(index + 1));
188          
189                 frameStr.append("." + stack[i].getMethodName() +
190                         " line: " + stack[i].getLineNumber("java"));
191                 if (i != k - 1) frameStr.append('\n');
192             }
193             Clipboard JavaDoc systemClipboard =
194                     Toolkit.getDefaultToolkit().getSystemClipboard();
195             Transferable JavaDoc transferableText =
196                     new StringSelection JavaDoc(new String JavaDoc(frameStr));
197             systemClipboard.setContents(
198                     transferableText,
199                     null);
200             
201         } catch (AbsentInformationException ex) {
202             ErrorManager.getDefault().notify(ex);
203         }
204     }
205     
206     private void makeCurrent (final CallStackFrame frame) {
207         if (debugger.getCurrentCallStackFrame () != frame)
208             frame.makeCurrent ();
209         else
210             SwingUtilities.invokeLater (new Runnable JavaDoc () {
211                 public void run () {
212                     String JavaDoc language = DebuggerManager.getDebuggerManager ().
213                         getCurrentSession ().getCurrentLanguage ();
214                     SourcePath sp = (SourcePath) DebuggerManager.
215                         getDebuggerManager ().getCurrentEngine ().lookupFirst
216                         (null, SourcePath.class);
217                     sp.showSource (frame, language);
218                 }
219             });
220     }
221 }
222
Popular Tags