2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 * Class AddToWatchActionHandler
21 package com.intellij.debugger.actions;
23 import com.intellij.debugger.DebuggerInvocationUtil;
24 import com.intellij.debugger.engine.evaluation.EvaluateException;
25 import com.intellij.debugger.engine.evaluation.TextWithImports;
26 import com.intellij.debugger.engine.events.DebuggerContextCommandImpl;
27 import com.intellij.debugger.impl.DebuggerContextImpl;
28 import com.intellij.debugger.impl.DebuggerSession;
29 import com.intellij.debugger.impl.DebuggerUtilsEx;
30 import com.intellij.debugger.ui.DebuggerPanelsManager;
31 import com.intellij.debugger.ui.impl.*;
32 import com.intellij.debugger.ui.impl.watch.*;
33 import com.intellij.idea.ActionsBundle;
34 import com.intellij.openapi.actionSystem.AnActionEvent;
35 import com.intellij.openapi.actionSystem.CommonDataKeys;
36 import com.intellij.openapi.actionSystem.DataContext;
37 import com.intellij.openapi.editor.Editor;
38 import com.intellij.openapi.project.Project;
39 import com.intellij.openapi.ui.Messages;
40 import com.intellij.xdebugger.impl.actions.DebuggerActionHandler;
41 import com.intellij.xdebugger.impl.actions.XDebuggerActions;
42 import org.jetbrains.annotations.NotNull;
44 public class AddToWatchActionHandler extends DebuggerActionHandler {
46 public boolean isEnabled(@NotNull Project project, AnActionEvent event) {
47 DataContext context = event.getDataContext();
48 DebuggerTreeNodeImpl[] selectedNodes = DebuggerAction.getSelectedNodes(context);
49 boolean enabled = false;
50 if (selectedNodes != null && selectedNodes.length > 0) {
51 DebuggerTreePanel panel = DebuggerAction.getPanel(context);
52 if (panel instanceof VariablesPanel || panel instanceof WatchPanel
53 || (panel == null && DebuggerAction.getTree(context) instanceof InspectDebuggerTree)) {
55 for (DebuggerTreeNodeImpl node : selectedNodes) {
56 NodeDescriptorImpl descriptor = node.getDescriptor();
57 if (!(descriptor instanceof ValueDescriptorImpl)) {
65 final Editor editor = event.getData(CommonDataKeys.EDITOR);
66 enabled = DebuggerUtilsEx.getEditorText(editor) != null;
72 public void perform(@NotNull Project project, AnActionEvent event) {
73 final DebuggerContextImpl debuggerContext = DebuggerAction.getDebuggerContext(event.getDataContext());
75 if(debuggerContext == null) return;
77 final DebuggerSession session = debuggerContext.getDebuggerSession();
81 final MainWatchPanel watchPanel = DebuggerPanelsManager.getInstance(debuggerContext.getProject()).getWatchPanel();
83 if(watchPanel == null) {
87 final DebuggerTreeNodeImpl[] selectedNodes = DebuggerAction.getSelectedNodes(event.getDataContext());
89 if(selectedNodes != null && selectedNodes.length > 0) {
90 addFromNodes(debuggerContext, watchPanel, selectedNodes);
93 final Editor editor = event.getData(CommonDataKeys.EDITOR);
95 final TextWithImports editorText = DebuggerUtilsEx.getEditorText(editor);
96 if (editorText != null) {
97 doAddWatch(watchPanel, editorText, null);
103 public static void addFromNodes(final DebuggerContextImpl debuggerContext, final MainWatchPanel watchPanel, final DebuggerTreeNodeImpl[] selectedNodes) {
104 debuggerContext.getDebugProcess().getManagerThread().schedule(new AddToWatchesCommand(debuggerContext, selectedNodes, watchPanel));
107 public static void doAddWatch(final MainWatchPanel watchPanel, final TextWithImports expression, final NodeDescriptorImpl descriptor) {
108 final WatchDebuggerTree watchTree = watchPanel.getWatchTree();
109 final DebuggerTreeNodeImpl node = watchTree.addWatch(expression, null);
110 if (descriptor != null) {
111 node.getDescriptor().displayAs(descriptor);
116 private static class AddToWatchesCommand extends DebuggerContextCommandImpl {
118 private final DebuggerContextImpl myDebuggerContext;
119 private final DebuggerTreeNodeImpl[] mySelectedNodes;
120 private final MainWatchPanel myWatchPanel;
122 public AddToWatchesCommand(DebuggerContextImpl debuggerContext, DebuggerTreeNodeImpl[] selectedNodes, MainWatchPanel watchPanel) {
123 super(debuggerContext);
124 myDebuggerContext = debuggerContext;
125 mySelectedNodes = selectedNodes;
126 myWatchPanel = watchPanel;
130 public void threadAction() {
131 final DebuggerSession session = myDebuggerContext.getDebuggerSession();
132 if (session == null) {
135 final Project project = session.getProject();
136 for (final DebuggerTreeNodeImpl node : mySelectedNodes) {
137 final NodeDescriptorImpl descriptor = node.getDescriptor();
139 final TextWithImports expression = DebuggerTreeNodeExpression.createEvaluationText(node, myDebuggerContext);
140 if (expression != null) {
141 DebuggerInvocationUtil.swingInvokeLater(project, new Runnable() {
144 doAddWatch(myWatchPanel, expression, descriptor);
149 catch (final EvaluateException e) {
150 DebuggerInvocationUtil.swingInvokeLater(project, new Runnable() {
153 Messages.showErrorDialog(project, e.getMessage(), ActionsBundle.actionText(XDebuggerActions.ADD_TO_WATCH));
161 protected void commandCancelled() {
162 DebuggerInvocationUtil.swingInvokeLater(myDebuggerContext.getProject(), new Runnable() {
165 for (DebuggerTreeNodeImpl node : mySelectedNodes) {
166 final NodeDescriptorImpl descriptor = node.getDescriptor();
167 if (descriptor instanceof WatchItemDescriptor) {
168 final TextWithImports expression = ((WatchItemDescriptor)descriptor).getEvaluationText();
169 if (expression != null) {
170 doAddWatch(myWatchPanel, expression, descriptor);