org.chromium.debug.ui/src/org/chromium/debug/ui/JsEvalContextManager.java
changeset 52 f577ea64429e
parent 2 e4420d2515f1
equal deleted inserted replaced
49:e64c52f5ee56 52:f577ea64429e
     7 import java.util.HashMap;
     7 import java.util.HashMap;
     8 import java.util.HashSet;
     8 import java.util.HashSet;
     9 import java.util.Map;
     9 import java.util.Map;
    10 import java.util.Set;
    10 import java.util.Set;
    11 
    11 
    12 import org.chromium.debug.core.model.StackFrame;
    12 import org.chromium.debug.core.model.EvaluateContext;
    13 import org.eclipse.core.runtime.IAdaptable;
    13 import org.eclipse.core.runtime.IAdaptable;
    14 import org.eclipse.debug.ui.DebugUITools;
    14 import org.eclipse.debug.ui.DebugUITools;
    15 import org.eclipse.debug.ui.contexts.DebugContextEvent;
    15 import org.eclipse.debug.ui.contexts.DebugContextEvent;
    16 import org.eclipse.debug.ui.contexts.IDebugContextListener;
    16 import org.eclipse.debug.ui.contexts.IDebugContextListener;
    17 import org.eclipse.jface.viewers.ISelection;
    17 import org.eclipse.jface.viewers.ISelection;
    33 
    33 
    34   private static JsEvalContextManager instance;
    34   private static JsEvalContextManager instance;
    35 
    35 
    36   private IWorkbenchWindow activeWindow;
    36   private IWorkbenchWindow activeWindow;
    37 
    37 
    38   private final Map<IWorkbenchPage, StackFrame> pageToFrame =
    38   private final Map<IWorkbenchPage, EvaluateContext> pageToFrame =
    39       new HashMap<IWorkbenchPage, StackFrame>();
    39       new HashMap<IWorkbenchPage, EvaluateContext>();
    40 
    40 
    41   protected JsEvalContextManager() {
    41   protected JsEvalContextManager() {
    42     DebugUITools.getDebugContextManager().addDebugContextListener(this);
    42     DebugUITools.getDebugContextManager().addDebugContextListener(this);
    43   }
    43   }
    44 
    44 
    81       IWorkbenchPage page = part.getSite().getPage();
    81       IWorkbenchPage page = part.getSite().getPage();
    82       ISelection selection = event.getContext();
    82       ISelection selection = event.getContext();
    83       if (selection instanceof IStructuredSelection) {
    83       if (selection instanceof IStructuredSelection) {
    84         Object firstElement = ((IStructuredSelection) selection).getFirstElement();
    84         Object firstElement = ((IStructuredSelection) selection).getFirstElement();
    85         if (firstElement instanceof IAdaptable) {
    85         if (firstElement instanceof IAdaptable) {
    86           StackFrame frame = (StackFrame) ((IAdaptable) firstElement).getAdapter(StackFrame.class);
    86           IAdaptable adaptable = (IAdaptable) firstElement;
       
    87 
       
    88           EvaluateContext frame = (EvaluateContext) adaptable.getAdapter(EvaluateContext.class);
    87           if (frame != null) {
    89           if (frame != null) {
    88             putStackFrame(page, frame);
    90             putStackFrame(page, frame);
    89             return;
    91             return;
    90           }
    92           }
    91         }
    93         }
   101    *
   103    *
   102    * @param part the active part
   104    * @param part the active part
   103    * @return the stack frame in whose context the evaluation is performed, or
   105    * @return the stack frame in whose context the evaluation is performed, or
   104    *         {@code null} if none
   106    *         {@code null} if none
   105    */
   107    */
   106   public static StackFrame getStackFrameFor(IWorkbenchPart part) {
   108   public static EvaluateContext getStackFrameFor(IWorkbenchPart part) {
   107     IWorkbenchPage page = part.getSite().getPage();
   109     IWorkbenchPage page = part.getSite().getPage();
   108     StackFrame frame = getStackFrameFor(page);
   110     EvaluateContext frame = getStackFrameFor(page);
   109     if (frame == null) {
   111     if (frame == null) {
   110       return getStackFrameFor(page.getWorkbenchWindow());
   112       return getStackFrameFor(page.getWorkbenchWindow());
   111     }
   113     }
   112     return frame;
   114     return frame;
   113   }
   115   }
   119    * @param window to find the StackFrame for. If {@code null}, the {@code
   121    * @param window to find the StackFrame for. If {@code null}, the {@code
   120    *        activeWindow} will be used instead
   122    *        activeWindow} will be used instead
   121    * @return the stack frame in whose the evaluation is performed, or {@code
   123    * @return the stack frame in whose the evaluation is performed, or {@code
   122    *         null} if none
   124    *         null} if none
   123    */
   125    */
   124   public static StackFrame getStackFrameFor(IWorkbenchWindow window) {
   126   public static EvaluateContext getStackFrameFor(IWorkbenchWindow window) {
   125     Set<IWorkbenchWindow> visitedWindows = new HashSet<IWorkbenchWindow>();
   127     Set<IWorkbenchWindow> visitedWindows = new HashSet<IWorkbenchWindow>();
   126     if (window == null) {
   128     if (window == null) {
   127       window = instance.activeWindow;
   129       window = instance.activeWindow;
   128     }
   130     }
   129     return getStackFrameFor(window, visitedWindows);
   131     return getStackFrameFor(window, visitedWindows);
   130   }
   132   }
   131 
   133 
   132   private static StackFrame getStackFrameFor(
   134   private static EvaluateContext getStackFrameFor(
   133       IWorkbenchWindow window, Set<IWorkbenchWindow> visitedWindows) {
   135       IWorkbenchWindow window, Set<IWorkbenchWindow> visitedWindows) {
   134     IWorkbenchPage activePage = window.getActivePage();
   136     IWorkbenchPage activePage = window.getActivePage();
   135     StackFrame frame = null;
   137     EvaluateContext frame = null;
   136     // Check the active page in the window
   138     // Check the active page in the window
   137     if (activePage != null) {
   139     if (activePage != null) {
   138       frame = getStackFrameFor(activePage);
   140       frame = getStackFrameFor(activePage);
   139       if (frame != null) {
   141       if (frame != null) {
   140         return frame;
   142         return frame;
   164 
   166 
   165     // Nothing found
   167     // Nothing found
   166     return null;
   168     return null;
   167   }
   169   }
   168 
   170 
   169   private static StackFrame getStackFrameFor(IWorkbenchPage page) {
   171   private static EvaluateContext getStackFrameFor(IWorkbenchPage page) {
   170     if (instance != null) {
   172     if (instance != null) {
   171       return instance.pageToFrame.get(page);
   173       return instance.pageToFrame.get(page);
   172     }
   174     }
   173     return null;
   175     return null;
   174   }
   176   }
   179       // No more available frames
   181       // No more available frames
   180       System.setProperty(DEBUGGER_ACTIVE, Boolean.FALSE.toString());
   182       System.setProperty(DEBUGGER_ACTIVE, Boolean.FALSE.toString());
   181     }
   183     }
   182   }
   184   }
   183 
   185 
   184   private void putStackFrame(IWorkbenchPage page, StackFrame frame) {
   186   private void putStackFrame(IWorkbenchPage page, EvaluateContext frame) {
   185     pageToFrame.put(page, frame);
   187     pageToFrame.put(page, frame);
   186     System.setProperty(DEBUGGER_ACTIVE, Boolean.TRUE.toString());
   188     System.setProperty(DEBUGGER_ACTIVE, Boolean.TRUE.toString());
   187   }
   189   }
   188 
   190 
   189 }
   191 }