Escape should work in terminal the same way as for other windows (IDEA-116221)
[idea/community.git] / plugins / terminal / src / org / jetbrains / plugins / terminal / JBTerminalWidget.java
1 package org.jetbrains.plugins.terminal;
2
3 import com.google.common.base.Predicate;
4 import com.intellij.openapi.Disposable;
5 import com.intellij.openapi.project.Project;
6 import com.intellij.openapi.util.Disposer;
7 import com.intellij.openapi.wm.ToolWindowManager;
8 import com.intellij.ui.SearchTextField;
9 import com.intellij.ui.components.JBScrollBar;
10 import com.intellij.ui.components.JBScrollPane;
11 import com.intellij.util.ui.JBSwingUtilities;
12 import com.intellij.util.ui.RegionPainter;
13 import com.jediterm.terminal.SubstringFinder;
14 import com.jediterm.terminal.TerminalStarter;
15 import com.jediterm.terminal.TtyBasedArrayDataStream;
16 import com.jediterm.terminal.TtyConnector;
17 import com.jediterm.terminal.model.JediTerminal;
18 import com.jediterm.terminal.model.StyleState;
19 import com.jediterm.terminal.model.TerminalTextBuffer;
20 import com.jediterm.terminal.ui.JediTermWidget;
21 import com.jediterm.terminal.ui.TerminalAction;
22 import com.jediterm.terminal.ui.settings.SettingsProvider;
23 import org.jetbrains.annotations.NotNull;
24
25 import javax.swing.*;
26 import javax.swing.event.DocumentListener;
27 import java.awt.*;
28 import java.awt.event.KeyEvent;
29 import java.awt.event.KeyListener;
30 import java.util.List;
31
32 public class JBTerminalWidget extends JediTermWidget implements Disposable{
33
34   private final Project myProject;
35
36   public JBTerminalWidget(Project project, JBTerminalSystemSettingsProvider settingsProvider, Disposable parent) {
37     super(settingsProvider);
38     myProject = project;
39     setName("terminal");
40     JBTabbedTerminalWidget.convertActions(this, getActions());
41
42     Disposer.register(parent, this);
43   }
44
45   @Override
46   protected JBTerminalPanel createTerminalPanel(@NotNull SettingsProvider settingsProvider,
47                                                 @NotNull StyleState styleState,
48                                                 @NotNull TerminalTextBuffer textBuffer) {
49     JBTerminalPanel panel = new JBTerminalPanel((JBTerminalSystemSettingsProvider)settingsProvider, textBuffer, styleState);
50     Disposer.register(this, panel);
51     return panel;
52   }
53
54   @Override
55   protected Graphics getComponentGraphics(Graphics graphics) {
56     return JBSwingUtilities.runGlobalCGTransform(this, super.getComponentGraphics(graphics));
57   }
58
59   @Override
60   protected TerminalStarter createTerminalStarter(JediTerminal terminal, TtyConnector connector) {
61     return new JBTerminalStarter(terminal, connector, new TtyBasedArrayDataStream(connector));
62   }
63
64   @Override
65   protected JScrollBar createScrollBar() {
66     JBScrollBar bar = new JBScrollBar();
67     bar.putClientProperty(JBScrollPane.Alignment.class, JBScrollPane.Alignment.RIGHT);
68     bar.putClientProperty(JBScrollBar.TRACK, new RegionPainter<Object>() {
69       @Override
70       public void paint(Graphics2D g, int x, int y, int width, int height, Object object) {
71         SubstringFinder.FindResult result = myTerminalPanel.getFindResult();
72         if (result != null) {
73           int modelHeight = bar.getModel().getMaximum() - bar.getModel().getMinimum();
74           int anchorHeight = Math.max(2, height / modelHeight);
75
76           Color color = mySettingsProvider.getTerminalColorPalette()
77             .getColor(mySettingsProvider.getFoundPatternColor().getBackground());
78           g.setColor(color);
79           for (SubstringFinder.FindResult.FindItem r : result.getItems()) {
80             int where = height * r.getStart().y / modelHeight;
81             g.fillRect(x, y + where, width, anchorHeight);
82           }
83         }
84       }
85     });
86     return bar;
87   }
88
89   @Override
90   public List<TerminalAction> getActions() {
91      List<TerminalAction> actions = super.getActions();
92      if (!TerminalOptionsProvider.getInstance().overrideIdeShortcuts()) {
93        actions
94          .add(new TerminalAction("EditorEscape", new KeyStroke[]{KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0)}, new Predicate<KeyEvent>() {
95            @Override
96            public boolean apply(KeyEvent input) {
97              if (!myTerminalPanel.getTerminalTextBuffer().isUsingAlternateBuffer()) {
98                ToolWindowManager.getInstance(myProject).activateEditorComponent();
99                return true;
100              }
101              else {
102                return false;
103              }
104            }
105          }).withHidden(true));
106      }
107      return actions;
108   };
109
110   @Override
111   public void dispose() {
112   }
113
114   @Override
115   protected SearchComponent createSearchComponent() {
116     return new SearchComponent() {
117       private final SearchTextField myTextField = new SearchTextField(false);
118       @Override
119       public String getText() {
120         return myTextField.getText();
121       }
122
123       @Override
124       public JComponent getComponent() {
125         myTextField.setOpaque(false);
126         return myTextField;
127       }
128
129       @Override
130       public void addDocumentChangeListener(DocumentListener listener) {
131         myTextField.addDocumentListener(listener);
132       }
133
134       @Override
135       public void addKeyListener(KeyListener listener) {
136         myTextField.addKeyboardListener(listener);
137       }
138
139       @Override
140       public void onResultUpdated(SubstringFinder.FindResult result) {
141       }
142
143       @Override
144       public void nextFindResultItem(SubstringFinder.FindResult.FindItem item) {
145       }
146
147       @Override
148       public void prevFindResultItem(SubstringFinder.FindResult.FindItem item) {
149       }
150     };
151   }
152 }