org.chromium.debug.ui/src/org/chromium/debug/ui/actions/OpenFunctionAction.java
changeset 355 8726e95bcbba
parent 52 f577ea64429e
equal deleted inserted replaced
354:0bceeb415e7f 355:8726e95bcbba
     1 package org.chromium.debug.ui.actions;
     1 package org.chromium.debug.ui.actions;
     2 
     2 
       
     3 import org.chromium.debug.core.model.DebugTargetImpl;
       
     4 import org.chromium.debug.core.model.Value;
     3 import org.chromium.debug.core.model.Variable;
     5 import org.chromium.debug.core.model.Variable;
     4 import org.chromium.debug.ui.JsDebugModelPresentation;
     6 import org.chromium.debug.ui.JsDebugModelPresentation;
     5 import org.chromium.debug.ui.editors.JsEditor;
     7 import org.chromium.debug.ui.editors.JsEditor;
     6 import org.chromium.sdk.JsFunction;
     8 import org.chromium.sdk.JsFunction;
     7 import org.chromium.sdk.JsObject;
     9 import org.chromium.sdk.JsObject;
     8 import org.chromium.sdk.JsValue;
    10 import org.chromium.sdk.JsValue;
     9 import org.chromium.sdk.JsVariable;
    11 import org.chromium.sdk.JsVariable;
    10 import org.chromium.sdk.Script;
    12 import org.chromium.sdk.Script;
    11 import org.eclipse.core.resources.IFile;
    13 import org.eclipse.core.resources.IFile;
       
    14 import org.eclipse.debug.core.model.IDebugTarget;
       
    15 import org.eclipse.debug.core.model.ISourceLocator;
       
    16 import org.eclipse.debug.core.model.IValue;
       
    17 import org.eclipse.debug.core.model.IWatchExpression;
       
    18 import org.eclipse.debug.core.sourcelookup.ISourceLookupDirector;
    12 import org.eclipse.jface.action.IAction;
    19 import org.eclipse.jface.action.IAction;
    13 import org.eclipse.jface.viewers.ISelection;
    20 import org.eclipse.jface.viewers.ISelection;
    14 import org.eclipse.jface.viewers.IStructuredSelection;
    21 import org.eclipse.jface.viewers.IStructuredSelection;
    15 import org.eclipse.swt.widgets.Event;
    22 import org.eclipse.swt.widgets.Event;
    16 import org.eclipse.ui.IActionDelegate2;
    23 import org.eclipse.ui.IActionDelegate2;
    25 import org.eclipse.ui.texteditor.ITextEditor;
    32 import org.eclipse.ui.texteditor.ITextEditor;
    26 
    33 
    27 /**
    34 /**
    28  * The action for context view in Variable view that opens selected function source text in editor.
    35  * The action for context view in Variable view that opens selected function source text in editor.
    29  */
    36  */
    30 public class OpenFunctionAction implements IObjectActionDelegate, IActionDelegate2 {
    37 public abstract class OpenFunctionAction<ELEMENT> implements IObjectActionDelegate,
       
    38     IActionDelegate2 {
       
    39   public static class ForVariable extends OpenFunctionAction<Variable> {
       
    40     @Override
       
    41     protected Variable castElement(Object element) {
       
    42       if (element instanceof Variable == false) {
       
    43         return null;
       
    44       }
       
    45       return (Variable) element;
       
    46     }
       
    47     @Override
       
    48     protected JsValue getJsValue(Variable variable) {
       
    49       JsVariable jsVariable = variable.getJsVariable();
       
    50       return jsVariable.getValue();
       
    51     }
       
    52     @Override
       
    53     protected DebugTargetImpl getDebugTarget(Variable variable) {
       
    54       return variable.getDebugTarget();
       
    55     }
       
    56   }
       
    57   public static class ForExpression extends OpenFunctionAction<IWatchExpression> {
       
    58     @Override
       
    59     protected IWatchExpression castElement(Object element) {
       
    60       if (element instanceof IWatchExpression == false) {
       
    61         return null;
       
    62       }
       
    63       return (IWatchExpression) element;
       
    64     }
       
    65     @Override
       
    66     protected DebugTargetImpl getDebugTarget(IWatchExpression expression) {
       
    67       IDebugTarget debugTarget = expression.getDebugTarget();
       
    68       if (debugTarget instanceof DebugTargetImpl == false) {
       
    69         return null;
       
    70       }
       
    71       return (DebugTargetImpl) debugTarget;
       
    72     }
       
    73     @Override
       
    74     protected JsValue getJsValue(IWatchExpression expression) {
       
    75       IValue value = expression.getValue();
       
    76       if (value instanceof Value == false) {
       
    77         return null;
       
    78       }
       
    79       Value chromiumValue = (Value) value;
       
    80       return chromiumValue.getJsValue();
       
    81     }
       
    82   }
       
    83 
    31   private Runnable currentRunnable = null;
    84   private Runnable currentRunnable = null;
    32 
    85 
    33   public void setActivePart(IAction action, IWorkbenchPart targetPart) {
    86   public void setActivePart(IAction action, IWorkbenchPart targetPart) {
    34   }
    87   }
    35 
    88 
    39     }
    92     }
    40     currentRunnable.run();
    93     currentRunnable.run();
    41   }
    94   }
    42 
    95 
    43   public void selectionChanged(IAction action, ISelection selection) {
    96   public void selectionChanged(IAction action, ISelection selection) {
    44     final Variable variable = getVariableFromSelection(selection);
    97     final ELEMENT variable = getElementFromSelection(selection);
    45     final JsFunction jsFunction = getJsFunctionFromVariable(variable);
    98     final JsFunction jsFunction = getJsFunctionFromElement(variable);
    46 
    99 
    47     currentRunnable = createRunnable(variable, jsFunction);
   100     currentRunnable = createRunnable(variable, jsFunction);
    48     action.setEnabled(currentRunnable != null);
   101     action.setEnabled(currentRunnable != null);
    49   }
   102   }
    50 
   103 
    51   private Runnable createRunnable(final Variable variable, final JsFunction jsFunction) {
   104   private Runnable createRunnable(final ELEMENT element, final JsFunction jsFunction) {
    52     if (jsFunction == null) {
   105     if (jsFunction == null) {
    53       return null;
   106       return null;
    54     }
   107     }
    55     return new Runnable() {
   108     return new Runnable() {
    56 
   109 
    61 
   114 
    62         Script script = jsFunction.getScript();
   115         Script script = jsFunction.getScript();
    63         if (script == null) {
   116         if (script == null) {
    64           return;
   117           return;
    65         }
   118         }
    66         IFile resource = variable.getDebugTarget().getScriptResource(script);
   119         DebugTargetImpl debugTarget = getDebugTarget(element);
       
   120         if (debugTarget == null) {
       
   121           return;
       
   122         }
       
   123         ISourceLocator sourceLocator = debugTarget.getLaunch().getSourceLocator();
       
   124         if (sourceLocator instanceof ISourceLookupDirector == false) {
       
   125           return;
       
   126         }
       
   127         ISourceLookupDirector director = (ISourceLookupDirector) sourceLocator;
       
   128         Object sourceObject = director.getSourceElement(script);
       
   129         if (sourceObject instanceof IFile == false) {
       
   130           return;
       
   131         }
       
   132         IFile resource = (IFile) sourceObject;
    67         IEditorInput input = JsDebugModelPresentation.toEditorInput(resource);
   133         IEditorInput input = JsDebugModelPresentation.toEditorInput(resource);
    68         IEditorPart editor;
   134         IEditorPart editor;
    69         try {
   135         try {
    70           editor = activeWorkbenchWindow.getActivePage().openEditor(input, JsEditor.EDITOR_ID);
   136           editor = activeWorkbenchWindow.getActivePage().openEditor(input, JsEditor.EDITOR_ID);
    71         } catch (PartInitException e) {
   137         } catch (PartInitException e) {
    92       return;
   158       return;
    93     }
   159     }
    94     currentRunnable.run();
   160     currentRunnable.run();
    95   }
   161   }
    96 
   162 
    97   private JsFunction getJsFunctionFromVariable(Variable variable) {
   163   private JsFunction getJsFunctionFromElement(ELEMENT element) {
    98     if (variable == null) {
   164     if (element == null) {
    99       return null;
   165       return null;
   100     }
   166     }
   101     JsVariable jsVariable = variable.getJsVariable();
   167     JsValue jsValue = getJsValue(element);
   102     JsValue jsValue = jsVariable.getValue();
   168     if (jsValue == null) {
       
   169       return null;
       
   170     }
   103     JsObject jsObject = jsValue.asObject();
   171     JsObject jsObject = jsValue.asObject();
   104     if (jsObject == null) {
   172     if (jsObject == null) {
   105       return null;
   173       return null;
   106     }
   174     }
   107     return jsObject.asFunction();
   175     return jsObject.asFunction();
   108   }
   176   }
   109 
   177 
   110   private Variable getVariableFromSelection(ISelection selection) {
   178   private ELEMENT getElementFromSelection(ISelection selection) {
   111     if (selection instanceof IStructuredSelection == false) {
   179     if (selection instanceof IStructuredSelection == false) {
   112       return null;
   180       return null;
   113     }
   181     }
   114     IStructuredSelection structuredSelection = (IStructuredSelection) selection;
   182     IStructuredSelection structuredSelection = (IStructuredSelection) selection;
   115     if (structuredSelection.size() != 1) {
   183     if (structuredSelection.size() != 1) {
   116       // We do not support multiple selection.
   184       // We do not support multiple selection.
   117       return null;
   185       return null;
   118     }
   186     }
   119     Object element = structuredSelection.getFirstElement();
   187     Object element = structuredSelection.getFirstElement();
   120     if (element instanceof Variable == false) {
   188     ELEMENT typedElement = castElement(element);
   121       return null;
   189     return typedElement;
   122     }
       
   123     return (Variable) element;
       
   124   }
   190   }
       
   191 
       
   192   protected abstract ELEMENT castElement(Object element);
       
   193 
       
   194   protected abstract JsValue getJsValue(ELEMENT element);
       
   195 
       
   196   protected abstract DebugTargetImpl getDebugTarget(ELEMENT element);
   125 }
   197 }