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