8fbf2f32dac6f86e383b5e5c1f6b2021e03b2304
[idea/community.git] / platform / xdebugger-impl / src / com / intellij / xdebugger / impl / evaluate / ExpressionInputComponent.java
1 /*
2  * Copyright 2000-2015 JetBrains s.r.o.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package com.intellij.xdebugger.impl.evaluate;
17
18 import com.intellij.codeInsight.lookup.LookupManager;
19 import com.intellij.icons.AllIcons;
20 import com.intellij.openapi.actionSystem.AnAction;
21 import com.intellij.openapi.actionSystem.AnActionEvent;
22 import com.intellij.openapi.actionSystem.CustomShortcutSet;
23 import com.intellij.openapi.editor.ex.util.EditorUtil;
24 import com.intellij.openapi.project.Project;
25 import com.intellij.openapi.ui.popup.PopupStep;
26 import com.intellij.openapi.ui.popup.util.BaseListPopupStep;
27 import com.intellij.ui.ColoredListCellRenderer;
28 import com.intellij.ui.components.JBLabel;
29 import com.intellij.ui.popup.list.ListPopupImpl;
30 import com.intellij.util.ui.JBUI;
31 import com.intellij.util.ui.UIUtil;
32 import com.intellij.xdebugger.XDebuggerBundle;
33 import com.intellij.xdebugger.XExpression;
34 import com.intellij.xdebugger.XSourcePosition;
35 import com.intellij.xdebugger.evaluation.XDebuggerEditorsProvider;
36 import com.intellij.xdebugger.impl.breakpoints.XExpressionImpl;
37 import com.intellij.xdebugger.impl.ui.XDebuggerEditorBase;
38 import com.intellij.xdebugger.impl.ui.XDebuggerExpressionEditor;
39 import org.jetbrains.annotations.NotNull;
40 import org.jetbrains.annotations.Nullable;
41
42 import javax.swing.*;
43 import java.awt.*;
44 import java.awt.event.ActionEvent;
45 import java.awt.event.ActionListener;
46 import java.util.List;
47
48 /**
49  * @author nik
50  */
51 public class ExpressionInputComponent extends EvaluationInputComponent {
52   private final XDebuggerExpressionEditor myExpressionEditor;
53   private final JPanel myMainPanel;
54
55   public ExpressionInputComponent(final @NotNull Project project, @NotNull XDebuggerEditorsProvider editorsProvider, final @Nullable XSourcePosition sourcePosition,
56                                   @Nullable XExpression expression) {
57     super(XDebuggerBundle.message("xdebugger.dialog.title.evaluate.expression"));
58     myMainPanel = new JPanel(new BorderLayout());
59     //myMainPanel.add(new JLabel(XDebuggerBundle.message("xdebugger.evaluate.label.expression")), BorderLayout.WEST);
60     myExpressionEditor = new XDebuggerExpressionEditor(project, editorsProvider, "evaluateExpression", sourcePosition,
61                                                        expression != null ? expression : XExpressionImpl.EMPTY_EXPRESSION, false);
62     myMainPanel.add(myExpressionEditor.getComponent(), BorderLayout.CENTER);
63     JButton historyButton = new JButton(AllIcons.General.MessageHistory);
64     historyButton.setToolTipText(XDebuggerBundle.message("xdebugger.evaluate.history.hint"));
65     historyButton.addActionListener(new ActionListener() {
66       @Override
67       public void actionPerformed(ActionEvent e) {
68         showHistory();
69       }
70     });
71     myMainPanel.add(historyButton, BorderLayout.EAST);
72     final JBLabel help = new JBLabel(XDebuggerBundle.message("xdebugger.evaluate.addtowatches.hint"), SwingConstants.RIGHT);
73     help.setBorder(JBUI.Borders.empty(2, 0, 6, 0));
74     help.setComponentStyle(UIUtil.ComponentStyle.SMALL);
75     help.setFontColor(UIUtil.FontColor.BRIGHTER);
76     myMainPanel.add(help, BorderLayout.SOUTH);
77     if (expression != null) {
78       myExpressionEditor.setExpression(expression);
79     }
80     myExpressionEditor.selectAll();
81
82     new AnAction("XEvaluateDialog.ShowHistory") {
83       @Override
84       public void actionPerformed(AnActionEvent e) {
85         showHistory();
86       }
87
88       @Override
89       public void update(AnActionEvent e) {
90         e.getPresentation().setEnabled(LookupManager.getActiveLookup(myExpressionEditor.getEditor()) == null);
91       }
92     }.registerCustomShortcutSet(CustomShortcutSet.fromString("DOWN"), myMainPanel);
93   }
94
95   private void showHistory() {
96     List<XExpression> expressions = myExpressionEditor.getRecentExpressions();
97     if (!expressions.isEmpty()) {
98       ListPopupImpl popup = new ListPopupImpl(new BaseListPopupStep<XExpression>(null, expressions) {
99         @Override
100         public PopupStep onChosen(XExpression selectedValue, boolean finalChoice) {
101           myExpressionEditor.setExpression(selectedValue);
102           myExpressionEditor.requestFocusInEditor();
103           return FINAL_CHOICE;
104         }
105       }) {
106         @Override
107         protected ListCellRenderer getListElementRenderer() {
108           return new ColoredListCellRenderer<XExpression>() {
109             @Override
110             protected void customizeCellRenderer(JList list, XExpression value, int index, boolean selected, boolean hasFocus) {
111               append(value.getExpression());
112             }
113           };
114         }
115       };
116       popup.getList().setFont(EditorUtil.getEditorFont());
117       popup.showUnderneathOf(myExpressionEditor.getComponent());
118     }
119   }
120
121   @Override
122   public void addComponent(JPanel contentPanel, JPanel resultPanel) {
123     contentPanel.add(resultPanel, BorderLayout.CENTER);
124     contentPanel.add(myMainPanel, BorderLayout.NORTH);
125   }
126
127   @NotNull
128   protected XDebuggerEditorBase getInputEditor() {
129     return myExpressionEditor;
130   }
131 }