org.chromium.debug.ui/src/org/chromium/debug/ui/actions/OpenFunctionAction.java
changeset 2 e4420d2515f1
child 52 f577ea64429e
equal deleted inserted replaced
1:ef76fc2ac88c 2:e4420d2515f1
       
     1 package org.chromium.debug.ui.actions;
       
     2 
       
     3 import org.chromium.debug.core.model.Variable;
       
     4 import org.chromium.debug.ui.JsDebugModelPresentation;
       
     5 import org.chromium.debug.ui.editors.JsEditor;
       
     6 import org.chromium.sdk.JsFunction;
       
     7 import org.chromium.sdk.JsObject;
       
     8 import org.chromium.sdk.JsValue;
       
     9 import org.chromium.sdk.JsVariable;
       
    10 import org.chromium.sdk.Script;
       
    11 import org.eclipse.core.resources.IFile;
       
    12 import org.eclipse.jface.action.IAction;
       
    13 import org.eclipse.jface.viewers.ISelection;
       
    14 import org.eclipse.jface.viewers.IStructuredSelection;
       
    15 import org.eclipse.swt.widgets.Event;
       
    16 import org.eclipse.ui.IActionDelegate2;
       
    17 import org.eclipse.ui.IEditorInput;
       
    18 import org.eclipse.ui.IEditorPart;
       
    19 import org.eclipse.ui.IObjectActionDelegate;
       
    20 import org.eclipse.ui.IWorkbench;
       
    21 import org.eclipse.ui.IWorkbenchPart;
       
    22 import org.eclipse.ui.IWorkbenchWindow;
       
    23 import org.eclipse.ui.PartInitException;
       
    24 import org.eclipse.ui.PlatformUI;
       
    25 import org.eclipse.ui.texteditor.ITextEditor;
       
    26 
       
    27 /**
       
    28  * The action for context view in Variable view that opens selected function source text in editor.
       
    29  */
       
    30 public class OpenFunctionAction implements IObjectActionDelegate, IActionDelegate2 {
       
    31   private Runnable currentRunnable = null;
       
    32 
       
    33   public void setActivePart(IAction action, IWorkbenchPart targetPart) {
       
    34   }
       
    35 
       
    36   public void run(IAction action) {
       
    37     if (currentRunnable == null) {
       
    38       return;
       
    39     }
       
    40     currentRunnable.run();
       
    41   }
       
    42 
       
    43   public void selectionChanged(IAction action, ISelection selection) {
       
    44     final Variable variable = getVariableFromSelection(selection);
       
    45     final JsFunction jsFunction = getJsFunctionFromVariable(variable);
       
    46 
       
    47     currentRunnable = createRunnable(variable, jsFunction);
       
    48     action.setEnabled(currentRunnable != null);
       
    49   }
       
    50 
       
    51   private Runnable createRunnable(final Variable variable, final JsFunction jsFunction) {
       
    52     if (jsFunction == null) {
       
    53       return null;
       
    54     }
       
    55     return new Runnable() {
       
    56 
       
    57       public void run() {
       
    58         // This works in UI thread.
       
    59         IWorkbench workbench = PlatformUI.getWorkbench();
       
    60         final IWorkbenchWindow activeWorkbenchWindow = workbench.getActiveWorkbenchWindow();
       
    61 
       
    62         Script script = jsFunction.getScript();
       
    63         if (script == null) {
       
    64           return;
       
    65         }
       
    66         IFile resource = variable.getDebugTarget().getResourceManager().getResource(script);
       
    67         IEditorInput input = JsDebugModelPresentation.toEditorInput(resource);
       
    68         IEditorPart editor;
       
    69         try {
       
    70           editor = activeWorkbenchWindow.getActivePage().openEditor(input, JsEditor.EDITOR_ID);
       
    71         } catch (PartInitException e) {
       
    72           throw new RuntimeException(e);
       
    73         }
       
    74         if (editor instanceof ITextEditor == false) {
       
    75           return;
       
    76         }
       
    77         ITextEditor textEditor = (ITextEditor) editor;
       
    78         textEditor.selectAndReveal(jsFunction.getSourcePosition(), 0);
       
    79       }
       
    80     };
       
    81   }
       
    82 
       
    83   public void dispose() {
       
    84     currentRunnable = null;
       
    85   }
       
    86 
       
    87   public void init(IAction action) {
       
    88   }
       
    89 
       
    90   public void runWithEvent(IAction action, Event event) {
       
    91     if (currentRunnable == null) {
       
    92       return;
       
    93     }
       
    94     currentRunnable.run();
       
    95   }
       
    96 
       
    97   private JsFunction getJsFunctionFromVariable(Variable variable) {
       
    98     if (variable == null) {
       
    99       return null;
       
   100     }
       
   101     JsVariable jsVariable = variable.getJsVariable();
       
   102     JsValue jsValue = jsVariable.getValue();
       
   103     JsObject jsObject = jsValue.asObject();
       
   104     if (jsObject == null) {
       
   105       return null;
       
   106     }
       
   107     return jsObject.asFunction();
       
   108   }
       
   109 
       
   110   private Variable getVariableFromSelection(ISelection selection) {
       
   111     if (selection instanceof IStructuredSelection == false) {
       
   112       return null;
       
   113     }
       
   114     IStructuredSelection structuredSelection = (IStructuredSelection) selection;
       
   115     if (structuredSelection.size() != 1) {
       
   116       // We do not support multiple selection.
       
   117       return null;
       
   118     }
       
   119     Object element = structuredSelection.getFirstElement();
       
   120     if (element instanceof Variable == false) {
       
   121       return null;
       
   122     }
       
   123     return (Variable) element;
       
   124   }
       
   125 }