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.
16 package com
.intellij
.xdebugger
.impl
.evaluate
;
18 import com
.intellij
.openapi
.actionSystem
.AnAction
;
19 import com
.intellij
.openapi
.actionSystem
.AnActionEvent
;
20 import com
.intellij
.openapi
.actionSystem
.CustomShortcutSet
;
21 import com
.intellij
.openapi
.project
.Project
;
22 import com
.intellij
.openapi
.ui
.DialogWrapper
;
23 import com
.intellij
.openapi
.wm
.IdeFocusManager
;
24 import com
.intellij
.xdebugger
.XDebugSession
;
25 import com
.intellij
.xdebugger
.XDebuggerBundle
;
26 import com
.intellij
.xdebugger
.XSourcePosition
;
27 import com
.intellij
.xdebugger
.evaluation
.XDebuggerEditorsProvider
;
28 import com
.intellij
.xdebugger
.evaluation
.XDebuggerEvaluator
;
29 import com
.intellij
.xdebugger
.impl
.actions
.XDebuggerActions
;
30 import com
.intellij
.xdebugger
.impl
.ui
.XDebuggerEditorBase
;
31 import com
.intellij
.xdebugger
.impl
.ui
.tree
.XDebuggerTree
;
32 import com
.intellij
.xdebugger
.impl
.ui
.tree
.XDebuggerTreePanel
;
33 import com
.intellij
.xdebugger
.impl
.ui
.tree
.nodes
.EvaluatingExpressionRootNode
;
34 import org
.jetbrains
.annotations
.NotNull
;
38 import java
.awt
.event
.ActionEvent
;
39 import java
.awt
.event
.KeyEvent
;
44 public class XDebuggerEvaluationDialog
extends DialogWrapper
{
45 private final JPanel myMainPanel
;
46 private final JPanel myResultPanel
;
47 private final XDebuggerTreePanel myTreePanel
;
48 private EvaluationInputComponent myInputComponent
;
49 private final XDebuggerEvaluator myEvaluator
;
50 private final XDebugSession mySession
;
51 private final XDebuggerEditorsProvider myEditorsProvider
;
52 private EvaluationDialogMode myMode
;
53 private final XSourcePosition mySourcePosition
;
54 private final SwitchModeAction mySwitchModeAction
;
56 public XDebuggerEvaluationDialog(@NotNull XDebugSession session
, final @NotNull XDebuggerEditorsProvider editorsProvider
, @NotNull XDebuggerEvaluator evaluator
,
57 @NotNull String text
, EvaluationDialogMode mode
, final XSourcePosition sourcePosition
) {
58 super(session
.getProject(), true);
60 myEditorsProvider
= editorsProvider
;
61 mySourcePosition
= sourcePosition
;
63 setOKButtonText(XDebuggerBundle
.message("xdebugger.button.evaluate"));
64 setCancelButtonText(XDebuggerBundle
.message("xdebugger.evaluate.dialog.close"));
66 myTreePanel
= new XDebuggerTreePanel(session
, editorsProvider
, sourcePosition
, XDebuggerActions
.EVALUATE_DIALOG_TREE_POPUP_GROUP
);
67 myResultPanel
= new JPanel(new BorderLayout());
68 myResultPanel
.add(new JLabel(XDebuggerBundle
.message("xdebugger.evaluate.label.result")), BorderLayout
.NORTH
);
69 myResultPanel
.add(myTreePanel
.getMainPanel(), BorderLayout
.CENTER
);
70 myEvaluator
= evaluator
;
71 myMainPanel
= new JPanel(new BorderLayout());
73 mySwitchModeAction
= new SwitchModeAction();
76 public void actionPerformed(AnActionEvent e
) {
79 }.registerCustomShortcutSet(new CustomShortcutSet(KeyStroke
.getKeyStroke(KeyEvent
.VK_ENTER
, KeyEvent
.CTRL_DOWN_MASK
)), getRootPane(), myDisposable
);
81 public void actionPerformed(AnActionEvent e
) {
82 IdeFocusManager
.getInstance(mySession
.getProject()).requestFocus(myTreePanel
.getTree(), true);
84 }.registerCustomShortcutSet(new CustomShortcutSet(KeyStroke
.getKeyStroke(KeyEvent
.VK_R
, KeyEvent
.ALT_DOWN_MASK
)), getRootPane(), myDisposable
);
86 switchToMode(mode
, text
);
90 protected void doOKAction() {
95 protected Action
[] createActions() {
96 return new Action
[] {getOKAction(), mySwitchModeAction
, getCancelAction()};
100 protected JButton
createJButtonForAction(Action action
) {
101 final JButton button
= super.createJButtonForAction(action
);
102 if (action
== mySwitchModeAction
) {
103 int width1
= new JButton(getSwitchButtonText(EvaluationDialogMode
.EXPRESSION
)).getPreferredSize().width
;
104 int width2
= new JButton(getSwitchButtonText(EvaluationDialogMode
.CODE_FRAGMENT
)).getPreferredSize().width
;
105 final Dimension size
= new Dimension(Math
.max(width1
, width2
), button
.getPreferredSize().height
);
106 button
.setMinimumSize(size
);
107 button
.setPreferredSize(size
);
112 private static String
getSwitchButtonText(EvaluationDialogMode mode
) {
113 return mode
!= EvaluationDialogMode
.EXPRESSION
114 ? XDebuggerBundle
.message("button.text.expression.mode")
115 : XDebuggerBundle
.message("button.text.code.fragment.mode");
118 private void switchToMode(EvaluationDialogMode mode
, String text
) {
119 if (myMode
== mode
) return;
122 myInputComponent
= createInputComponent(mode
, text
);
123 myMainPanel
.removeAll();
124 myInputComponent
.addComponent(myMainPanel
, myResultPanel
);
126 setTitle(myInputComponent
.getTitle());
127 mySwitchModeAction
.putValue(Action
.NAME
, getSwitchButtonText(mode
));
128 final JComponent preferredFocusedComponent
= myInputComponent
.getInputEditor().getPreferredFocusedComponent();
129 if (preferredFocusedComponent
!= null) {
130 IdeFocusManager
.getInstance(mySession
.getProject()).requestFocus(preferredFocusedComponent
, true);
134 private EvaluationInputComponent
createInputComponent(EvaluationDialogMode mode
, String text
) {
135 final Project project
= mySession
.getProject();
136 if (mode
== EvaluationDialogMode
.EXPRESSION
) {
137 return new ExpressionInputComponent(project
, myEditorsProvider
, mySourcePosition
, text
);
140 return new CodeFragmentInputComponent(project
, myEditorsProvider
, mySourcePosition
, text
, myDisposable
);
144 private void evaluate() {
145 final XDebuggerTree tree
= myTreePanel
.getTree();
146 final EvaluatingExpressionRootNode root
= new EvaluatingExpressionRootNode(this, tree
);
147 tree
.setRoot(root
, false);
148 myResultPanel
.invalidate();
149 myInputComponent
.getInputEditor().selectAll();
152 protected void dispose() {
153 myTreePanel
.dispose();
157 protected String
getDimensionServiceKey() {
158 return "#xdebugger.evaluate";
161 protected JComponent
createCenterPanel() {
165 public void startEvaluation(XDebuggerEvaluator
.XEvaluationCallback evaluationCallback
) {
166 final XDebuggerEditorBase inputEditor
= myInputComponent
.getInputEditor();
167 inputEditor
.saveTextInHistory();
168 String expression
= inputEditor
.getText();
169 myEvaluator
.evaluate(expression
, evaluationCallback
, null);
172 public JComponent
getPreferredFocusedComponent() {
173 return myInputComponent
.getInputEditor().getPreferredFocusedComponent();
176 private class SwitchModeAction
extends AbstractAction
{
177 public void actionPerformed(ActionEvent e
) {
178 String text
= myInputComponent
.getInputEditor().getText();
179 if (myMode
== EvaluationDialogMode
.EXPRESSION
) {
180 switchToMode(EvaluationDialogMode
.CODE_FRAGMENT
, text
);
183 if (text
.indexOf('\n') != -1) text
= "";
184 switchToMode(EvaluationDialogMode
.EXPRESSION
, text
);