1 11 package org.eclipse.jdt.internal.debug.ui; 12 13 import java.util.ArrayList ; 14 import java.util.HashMap ; 15 import java.util.List ; 16 import java.util.Map ; 17 18 import org.eclipse.core.runtime.IAdaptable; 19 import org.eclipse.debug.ui.DebugUITools; 20 import org.eclipse.debug.ui.contexts.DebugContextEvent; 21 import org.eclipse.debug.ui.contexts.IDebugContextListener; 22 import org.eclipse.jdt.debug.core.IJavaDebugTarget; 23 import org.eclipse.jdt.debug.core.IJavaStackFrame; 24 import org.eclipse.jdt.debug.core.IJavaThread; 25 import org.eclipse.jdt.internal.debug.ui.snippeteditor.ScrapbookLauncher; 26 import org.eclipse.jface.viewers.ISelection; 27 import org.eclipse.jface.viewers.IStructuredSelection; 28 import org.eclipse.ui.IWindowListener; 29 import org.eclipse.ui.IWorkbench; 30 import org.eclipse.ui.IWorkbenchPage; 31 import org.eclipse.ui.IWorkbenchPart; 32 import org.eclipse.ui.IWorkbenchWindow; 33 import org.eclipse.ui.PlatformUI; 34 35 36 41 public class EvaluationContextManager implements IWindowListener, IDebugContextListener { 42 43 private static EvaluationContextManager fgManager; 44 45 49 private static final String DEBUGGER_ACTIVE = JDIDebugUIPlugin.getUniqueIdentifier() + ".debuggerActive"; 54 private static final String INSTANCE_OF_IJAVA_STACK_FRAME = JDIDebugUIPlugin.getUniqueIdentifier() + ".instanceof.IJavaStackFrame"; 58 private static final String SUPPORTS_FORCE_RETURN = JDIDebugUIPlugin.getUniqueIdentifier() + ".supportsForceReturn"; 62 private static final String SUPPORTS_INSTANCE_RETRIEVAL = JDIDebugUIPlugin.getUniqueIdentifier() + ".supportsInstanceRetrieval"; 64 private Map fContextsByPage = null; 65 66 private IWorkbenchWindow fActiveWindow; 67 68 private EvaluationContextManager() { 69 DebugUITools.getDebugContextManager().addDebugContextListener(this); 70 } 71 72 public static void startup() { 73 Runnable r = new Runnable () { 74 public void run() { 75 if (fgManager == null) { 76 fgManager = new EvaluationContextManager(); 77 IWorkbench workbench = PlatformUI.getWorkbench(); 78 IWorkbenchWindow[] windows = workbench.getWorkbenchWindows(); 79 for (int i = 0; i < windows.length; i++) { 80 fgManager.windowOpened(windows[i]); 81 } 82 workbench.addWindowListener(fgManager); 83 fgManager.fActiveWindow = workbench.getActiveWorkbenchWindow(); 84 } 85 } 86 }; 87 JDIDebugUIPlugin.getStandardDisplay().asyncExec(r); 88 } 89 90 93 public void windowActivated(IWorkbenchWindow window) { 94 fActiveWindow = window; 95 } 96 97 100 public void windowClosed(IWorkbenchWindow window) { 101 } 102 103 106 public void windowDeactivated(IWorkbenchWindow window) { 107 } 108 109 112 public void windowOpened(IWorkbenchWindow window) { 113 } 114 115 122 private void setContext(IWorkbenchPage page, IJavaStackFrame frame, boolean instOf) { 123 if (fContextsByPage == null) { 124 fContextsByPage = new HashMap (); 125 } 126 fContextsByPage.put(page, frame); 127 System.setProperty(DEBUGGER_ACTIVE, "true"); if (frame.canForceReturn()) { 129 System.setProperty(SUPPORTS_FORCE_RETURN, "true"); } else { 131 System.setProperty(SUPPORTS_FORCE_RETURN, "false"); } 133 if (((IJavaDebugTarget)frame.getDebugTarget()).supportsInstanceRetrieval()){ 134 System.setProperty(SUPPORTS_INSTANCE_RETRIEVAL, "true"); } else { 136 System.setProperty(SUPPORTS_INSTANCE_RETRIEVAL, "false"); } 138 if (instOf) { 139 System.setProperty(INSTANCE_OF_IJAVA_STACK_FRAME, "true"); } else { 141 System.setProperty(INSTANCE_OF_IJAVA_STACK_FRAME, "false"); } 143 } 144 145 151 private void removeContext(IWorkbenchPage page) { 152 if (fContextsByPage != null) { 153 fContextsByPage.remove(page); 154 if (fContextsByPage.isEmpty()) { 155 System.setProperty(DEBUGGER_ACTIVE, "false"); System.setProperty(INSTANCE_OF_IJAVA_STACK_FRAME, "false"); System.setProperty(SUPPORTS_FORCE_RETURN, "false"); System.setProperty(SUPPORTS_INSTANCE_RETRIEVAL, "false"); } 160 } 161 } 162 163 private static IJavaStackFrame getContext(IWorkbenchPage page) { 164 if (fgManager != null) { 165 if (fgManager.fContextsByPage != null) { 166 return (IJavaStackFrame)fgManager.fContextsByPage.get(page); 167 } 168 } 169 return null; 170 } 171 172 186 public static IJavaStackFrame getEvaluationContext(IWorkbenchPart part) { 187 IWorkbenchPage page = part.getSite().getPage(); 188 IJavaStackFrame frame = getContext(page); 189 if (frame == null) { 190 return getEvaluationContext(page.getWorkbenchWindow()); 191 } 192 return frame; 193 } 194 195 211 public static IJavaStackFrame getEvaluationContext(IWorkbenchWindow window) { 212 List alreadyVisited= new ArrayList (); 213 if (window == null) { 214 window = fgManager.fActiveWindow; 215 } 216 return getEvaluationContext(window, alreadyVisited); 217 } 218 219 private static IJavaStackFrame getEvaluationContext(IWorkbenchWindow window, List alreadyVisited) { 220 IWorkbenchPage activePage = window.getActivePage(); 221 IJavaStackFrame frame = null; 222 if (activePage != null) { 223 frame = getContext(activePage); 224 } 225 if (frame == null) { 226 IWorkbenchPage[] pages = window.getPages(); 227 for (int i = 0; i < pages.length; i++) { 228 if (activePage != pages[i]) { 229 frame = getContext(pages[i]); 230 if (frame != null) { 231 return frame; 232 } 233 } 234 } 235 236 alreadyVisited.add(window); 237 238 IWorkbenchWindow[] windows = PlatformUI.getWorkbench().getWorkbenchWindows(); 239 for (int i = 0; i < windows.length; i++) { 240 if (!alreadyVisited.contains(windows[i])) { 241 frame = getEvaluationContext(windows[i], alreadyVisited); 242 if (frame != null) { 243 return frame; 244 } 245 } 246 } 247 return null; 248 } 249 return frame; 250 } 251 252 public void debugContextChanged(DebugContextEvent event) { 253 if ((event.getFlags() & DebugContextEvent.ACTIVATED) > 0) { 254 IWorkbenchPart part = event.getDebugContextProvider().getPart(); 255 if (part != null) { 256 IWorkbenchPage page = part.getSite().getPage(); 257 ISelection selection = event.getContext(); 258 if (selection instanceof IStructuredSelection) { 259 IStructuredSelection ss = (IStructuredSelection)selection; 260 if (ss.size() == 1) { 261 Object element = ss.getFirstElement(); 262 if (element instanceof IAdaptable) { 263 IJavaStackFrame frame = (IJavaStackFrame)((IAdaptable)element).getAdapter(IJavaStackFrame.class); 264 boolean instOf = element instanceof IJavaStackFrame || element instanceof IJavaThread; 265 if (frame != null) { 266 if (frame.getLaunch().getAttribute(ScrapbookLauncher.SCRAPBOOK_LAUNCH) == null) { 268 setContext(page, frame, instOf); 269 return; 270 } 271 } 272 } 273 } 274 } 275 removeContext(page); 277 } 278 } 279 } 280 281 } 282 | Popular Tags |