1 19 20 package org.netbeans.modules.debugger.jpda.ui.models; 21 22 import com.sun.jdi.AbsentInformationException; 23 import java.awt.event.ActionEvent ; 24 import java.util.Vector ; 25 import javax.swing.Action ; 26 import javax.swing.AbstractAction ; 27 import javax.swing.SwingUtilities ; 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 ; 50 import java.awt.datatransfer.Clipboard ; 51 import java.awt.datatransfer.StringSelection ; 52 import java.awt.datatransfer.Transferable ; 53 54 55 58 public class CallStackActionsProvider implements NodeActionsProvider { 59 60 private final Action MAKE_CURRENT_ACTION = Models.createAction ( 61 NbBundle.getBundle(ThreadsActionsProvider.class).getString("CTL_CallstackAction_MakeCurrent_Label"), 62 new Models.ActionPerformer () { 63 public boolean isEnabled (Object node) { 64 return true; 66 } 67 public void perform (Object [] nodes) { 68 makeCurrent ((CallStackFrame) nodes [0]); 69 } 70 }, 71 Models.MULTISELECTION_TYPE_EXACTLY_ONE 72 ); 73 74 87 private final Action COPY_TO_CLBD_ACTION = new AbstractAction ( 88 NbBundle.getBundle(ThreadsActionsProvider.class).getString("CTL_CallstackAction_Copy2CLBD_Label")) { 89 public void actionPerformed (ActionEvent e) { 90 stackToCLBD (); 91 } 92 }; 93 94 private static final Action 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 node) { 98 return true; 100 } 101 public void perform (final Object [] nodes) { 102 RequestProcessor.getDefault().post(new Runnable () { 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 [] getActions (Object node) throws UnknownTypeException { 125 if (node == TreeModel.ROOT) { 126 return new Action [] { 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 [] { MAKE_CURRENT_ACTION, POP_TO_HERE_ACTION, COPY_TO_CLBD_ACTION }; 135 else 136 return new Action [] { MAKE_CURRENT_ACTION, COPY_TO_CLBD_ACTION }; 137 } 138 139 public void performDefaultAction (Object 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 181 CallStackFrame[] stack = t.getCallStack (); 182 int i, k = stack.length; 183 StringBuffer frameStr = new StringBuffer (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 systemClipboard = 194 Toolkit.getDefaultToolkit().getSystemClipboard(); 195 Transferable transferableText = 196 new StringSelection (new String (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 () { 211 public void run () { 212 String 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 |