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