Update jediterm with fixes for IDEA-162497
[idea/community.git] / plugins / terminal / src / org / jetbrains / plugins / terminal / JBTerminalWidget.java
1 package org.jetbrains.plugins.terminal;
2
3 import com.intellij.openapi.Disposable;
4 import com.intellij.openapi.util.Disposer;
5 import com.intellij.ui.SearchTextField;
6 import com.intellij.ui.components.JBScrollBar;
7 import com.intellij.ui.components.JBScrollPane;
8 import com.intellij.util.ui.JBSwingUtilities;
9 import com.intellij.util.ui.RegionPainter;
10 import com.jediterm.terminal.SubstringFinder;
11 import com.jediterm.terminal.TerminalStarter;
12 import com.jediterm.terminal.TtyBasedArrayDataStream;
13 import com.jediterm.terminal.TtyConnector;
14 import com.jediterm.terminal.model.JediTerminal;
15 import com.jediterm.terminal.model.StyleState;
16 import com.jediterm.terminal.model.TerminalTextBuffer;
17 import com.jediterm.terminal.ui.JediTermWidget;
18 import com.jediterm.terminal.ui.settings.SettingsProvider;
19 import org.jetbrains.annotations.NotNull;
20
21 import javax.swing.*;
22 import javax.swing.event.DocumentListener;
23 import java.awt.*;
24 import java.awt.event.KeyListener;
25
26 public class JBTerminalWidget extends JediTermWidget implements Disposable{
27
28   public JBTerminalWidget(JBTerminalSystemSettingsProvider settingsProvider, Disposable parent) {
29     super(settingsProvider);
30     setName("terminal");
31     JBTabbedTerminalWidget.convertActions(this, getActions());
32
33     Disposer.register(parent, this);
34   }
35
36   @Override
37   protected JBTerminalPanel createTerminalPanel(@NotNull SettingsProvider settingsProvider,
38                                                 @NotNull StyleState styleState,
39                                                 @NotNull TerminalTextBuffer textBuffer) {
40     JBTerminalPanel panel = new JBTerminalPanel((JBTerminalSystemSettingsProvider)settingsProvider, textBuffer, styleState);
41     Disposer.register(this, panel);
42     return panel;
43   }
44
45   @Override
46   protected Graphics getComponentGraphics(Graphics graphics) {
47     return JBSwingUtilities.runGlobalCGTransform(this, super.getComponentGraphics(graphics));
48   }
49
50   @Override
51   protected TerminalStarter createTerminalStarter(JediTerminal terminal, TtyConnector connector) {
52     return new JBTerminalStarter(terminal, connector, new TtyBasedArrayDataStream(connector));
53   }
54
55   @Override
56   protected JScrollBar createScrollBar() {
57     JBScrollBar bar = new JBScrollBar();
58     bar.putClientProperty(JBScrollPane.Alignment.class, JBScrollPane.Alignment.RIGHT);
59     bar.putClientProperty(JBScrollBar.TRACK, new RegionPainter<Object>() {
60       @Override
61       public void paint(Graphics2D g, int x, int y, int width, int height, Object object) {
62         SubstringFinder.FindResult result = myTerminalPanel.getFindResult();
63         if (result != null) {
64           int modelHeight = bar.getModel().getMaximum() - bar.getModel().getMinimum();
65           int anchorHeight = Math.max(2, height / modelHeight);
66
67           Color color = mySettingsProvider.getTerminalColorPalette()
68             .getColor(mySettingsProvider.getFoundPatternColor().getBackground());
69           g.setColor(color);
70           for (SubstringFinder.FindResult.FindItem r : result.getItems()) {
71             int where = height * r.getStart().y / modelHeight;
72             g.fillRect(x, y + where, width, anchorHeight);
73           }
74         }
75       }
76     });
77     return bar;
78   }
79   
80   @Override
81   public void dispose() {
82   }
83
84   @Override
85   protected SearchComponent createSearchComponent() {
86     return new SearchComponent() {
87       private final SearchTextField myTextField = new SearchTextField(false);
88       @Override
89       public String getText() {
90         return myTextField.getText();
91       }
92
93       @Override
94       public JComponent getComponent() {
95         myTextField.setOpaque(false);
96         return myTextField;
97       }
98
99       @Override
100       public void addDocumentChangeListener(DocumentListener listener) {
101         myTextField.addDocumentListener(listener);
102       }
103
104       @Override
105       public void addKeyListener(KeyListener listener) {
106         myTextField.addKeyboardListener(listener);
107       }
108
109       @Override
110       public void onResultUpdated(SubstringFinder.FindResult result) {
111       }
112
113       @Override
114       public void nextFindResultItem(SubstringFinder.FindResult.FindItem item) {
115       }
116
117       @Override
118       public void prevFindResultItem(SubstringFinder.FindResult.FindItem item) {
119       }
120     };
121   }
122 }