2 * Copyright 2000-2015 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.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;
44 import java.awt.event.ActionEvent;
45 import java.awt.event.ActionListener;
46 import java.util.List;
51 public class ExpressionInputComponent extends EvaluationInputComponent {
52 private final XDebuggerExpressionEditor myExpressionEditor;
53 private final JPanel myMainPanel;
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() {
67 public void actionPerformed(ActionEvent e) {
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);
80 myExpressionEditor.selectAll();
82 new AnAction("XEvaluateDialog.ShowHistory") {
84 public void actionPerformed(AnActionEvent e) {
89 public void update(AnActionEvent e) {
90 e.getPresentation().setEnabled(LookupManager.getActiveLookup(myExpressionEditor.getEditor()) == null);
92 }.registerCustomShortcutSet(CustomShortcutSet.fromString("DOWN"), myMainPanel);
95 private void showHistory() {
96 List<XExpression> expressions = myExpressionEditor.getRecentExpressions();
97 if (!expressions.isEmpty()) {
98 ListPopupImpl popup = new ListPopupImpl(new BaseListPopupStep<XExpression>(null, expressions) {
100 public PopupStep onChosen(XExpression selectedValue, boolean finalChoice) {
101 myExpressionEditor.setExpression(selectedValue);
102 myExpressionEditor.requestFocusInEditor();
107 protected ListCellRenderer getListElementRenderer() {
108 return new ColoredListCellRenderer<XExpression>() {
110 protected void customizeCellRenderer(JList list, XExpression value, int index, boolean selected, boolean hasFocus) {
111 append(value.getExpression());
116 popup.getList().setFont(EditorUtil.getEditorFont());
117 popup.showUnderneathOf(myExpressionEditor.getComponent());
122 public void addComponent(JPanel contentPanel, JPanel resultPanel) {
123 contentPanel.add(resultPanel, BorderLayout.CENTER);
124 contentPanel.add(myMainPanel, BorderLayout.NORTH);
128 protected XDebuggerEditorBase getInputEditor() {
129 return myExpressionEditor;