-
Notifications
You must be signed in to change notification settings - Fork 721
/
ExOutputPanel.java
331 lines (285 loc) · 9.68 KB
/
ExOutputPanel.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/*
* IdeaVim - Vim emulator for IDEs based on the IntelliJ platform
* Copyright (C) 2003-2016 The IdeaVim authors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.maddyhome.idea.vim.ui;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.ui.components.JBScrollPane;
import com.maddyhome.idea.vim.VimPlugin;
import com.maddyhome.idea.vim.helper.EditorData;
import com.maddyhome.idea.vim.helper.EditorDataContext;
import com.maddyhome.idea.vim.helper.UiHelper;
import com.maddyhome.idea.vim.option.Options;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.List;
/**
* This panel displays text in a <code>more</code> like window.
*/
public class ExOutputPanel extends JPanel {
@NotNull private final Editor myEditor;
@NotNull private final JLabel myLabel = new JLabel("more");
@NotNull private final JTextArea myText = new JTextArea();
@NotNull private final JScrollPane myScrollPane =
new JBScrollPane(myText, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
@NotNull private final ComponentAdapter myAdapter;
private boolean myAtEnd = false;
private int myLineHeight = 0;
@Nullable private JComponent myOldGlass = null;
@Nullable private LayoutManager myOldLayout = null;
private boolean myWasOpaque = false;
private boolean myActive = false;
private ExOutputPanel(@NotNull Editor editor) {
myEditor = editor;
// Create a text editor for the text and a label for the prompt
BorderLayout layout = new BorderLayout(0, 0);
setLayout(layout);
add(myScrollPane, BorderLayout.CENTER);
add(myLabel, BorderLayout.SOUTH);
setFontForElements();
myText.setBorder(null);
myScrollPane.setBorder(null);
myLabel.setForeground(myText.getForeground());
myLabel.setBackground(myText.getBackground());
setForeground(myText.getForeground());
setBackground(myText.getBackground());
myText.setEditable(false);
setBorder(BorderFactory.createEtchedBorder());
myAdapter = new ComponentAdapter() {
public void componentResized(ComponentEvent e) {
positionPanel();
}
};
// Setup some listeners to handle keystrokes
MoreKeyListener moreKeyListener = new MoreKeyListener(this);
addKeyListener(moreKeyListener);
myText.addKeyListener(moreKeyListener);
}
@NotNull
public static ExOutputPanel getInstance(@NotNull Editor editor) {
ExOutputPanel panel = EditorData.getMorePanel(editor);
if (panel == null) {
panel = new ExOutputPanel(editor);
EditorData.setMorePanel(editor, panel);
}
return panel;
}
public void setText(@NotNull String data) {
if (data.length() > 0 && data.charAt(data.length() - 1) == '\n') {
data = data.substring(0, data.length() - 1);
}
myText.setText(data);
myText.setCaretPosition(0);
if (data.length() > 0) {
activate();
}
}
/**
* Turns off the ex entry field and optionally puts the focus back to the original component
*/
public void deactivate(boolean refocusOwningEditor) {
if (!myActive) return;
myActive = false;
myText.setText("");
if (refocusOwningEditor) {
UiHelper.requestFocus(myEditor.getContentComponent());
}
if (myOldGlass != null) {
myOldGlass.removeComponentListener(myAdapter);
myOldGlass.setVisible(false);
myOldGlass.remove(this);
myOldGlass.setOpaque(myWasOpaque);
myOldGlass.setLayout(myOldLayout);
}
}
/**
* Turns on the more window for the given editor
*/
private void activate() {
JRootPane root = SwingUtilities.getRootPane(myEditor.getContentComponent());
myOldGlass = (JComponent)root.getGlassPane();
if (myOldGlass != null) {
myOldLayout = myOldGlass.getLayout();
myWasOpaque = myOldGlass.isOpaque();
myOldGlass.setLayout(null);
myOldGlass.setOpaque(false);
myOldGlass.add(this);
myOldGlass.addComponentListener(myAdapter);
}
setFontForElements();
positionPanel();
if (myOldGlass != null) {
myOldGlass.setVisible(true);
}
myActive = true;
UiHelper.requestFocus(myText);
}
private void setFontForElements() {
final Font font = UiHelper.getEditorFont();
myText.setFont(font);
myLabel.setFont(font);
}
private static int countLines(@NotNull String text) {
if (text.length() == 0) {
return 0;
}
int count = 0;
int pos = -1;
while ((pos = text.indexOf('\n', pos + 1)) != -1) {
count++;
}
if (text.charAt(text.length() - 1) != '\n') {
count++;
}
return count;
}
private void scrollLine() {
scrollOffset(myLineHeight);
}
private void scrollPage() {
scrollOffset(myScrollPane.getVerticalScrollBar().getVisibleAmount());
}
private void scrollHalfPage() {
double sa = myScrollPane.getVerticalScrollBar().getVisibleAmount() / 2.0;
double offset = Math.ceil(sa / myLineHeight) * myLineHeight;
scrollOffset((int)offset);
}
private void handleEnter() {
if (myAtEnd) {
close();
}
else {
scrollLine();
}
}
private void badKey() {
myLabel.setText("-- MORE -- (RET: line, SPACE: page, d: half page, q: quit)");
}
private void scrollOffset(int more) {
myAtEnd = false;
int val = myScrollPane.getVerticalScrollBar().getValue();
myScrollPane.getVerticalScrollBar().setValue(val + more);
myScrollPane.getHorizontalScrollBar().setValue(0);
if (val + more >=
myScrollPane.getVerticalScrollBar().getMaximum() - myScrollPane.getVerticalScrollBar().getVisibleAmount()) {
myAtEnd = true;
myLabel.setText("Hit ENTER or type command to continue");
}
else {
myLabel.setText("-- MORE --");
}
}
private void positionPanel() {
final JComponent contentComponent = myEditor.getContentComponent();
Container scroll = SwingUtilities.getAncestorOfClass(JScrollPane.class, contentComponent);
setSize(scroll.getSize());
myLineHeight = myText.getFontMetrics(myText.getFont()).getHeight();
int count = countLines(myText.getText());
int visLines = getSize().height / myLineHeight - 1;
int lines = Math.min(count, visLines);
setSize(getSize().width, lines * myLineHeight + myLabel.getPreferredSize().height +
getBorder().getBorderInsets(this).top * 2);
int height = getSize().height;
Rectangle bounds = scroll.getBounds();
bounds.translate(0, scroll.getHeight() - height);
bounds.height = height;
Point pos = SwingUtilities.convertPoint(scroll.getParent(), bounds.getLocation(),
SwingUtilities.getRootPane(contentComponent).getGlassPane());
bounds.setLocation(pos);
setBounds(bounds);
myScrollPane.getVerticalScrollBar().setValue(0);
if (!Options.getInstance().isSet("more")) {
// FIX
scrollOffset(100000);
}
else {
scrollOffset(0);
}
}
private void close() {
close(null);
}
private void close(@Nullable final KeyEvent e) {
ApplicationManager.getApplication().invokeLater(new Runnable() {
public void run() {
deactivate(true);
final Project project = myEditor.getProject();
if (project != null && e != null && e.getKeyChar() != '\n') {
final KeyStroke key = KeyStroke.getKeyStrokeForEvent(e);
final List<KeyStroke> keys = new ArrayList<KeyStroke>(1);
keys.add(key);
VimPlugin.getMacro().playbackKeys(myEditor, new EditorDataContext(myEditor), project, keys, 0, 0, 1);
}
}
});
}
private static class MoreKeyListener extends KeyAdapter {
private final ExOutputPanel myExOutputPanel;
public MoreKeyListener(ExOutputPanel panel) {
this.myExOutputPanel = panel;
}
/**
* Invoked when a key has been pressed.
*/
public void keyTyped(@NotNull KeyEvent e) {
if (myExOutputPanel.myAtEnd) {
myExOutputPanel.close(e);
}
else {
switch (e.getKeyChar()) {
case ' ':
myExOutputPanel.scrollPage();
break;
case 'd':
myExOutputPanel.scrollHalfPage();
break;
case 'q':
myExOutputPanel.close();
break;
case '\n':
myExOutputPanel.handleEnter();
break;
case '\u001b':
myExOutputPanel.close();
break;
case KeyEvent.CHAR_UNDEFINED: {
switch (e.getKeyCode()) {
case KeyEvent.VK_ENTER:
myExOutputPanel.handleEnter();
break;
case KeyEvent.VK_ESCAPE:
myExOutputPanel.close();
break;
default:
myExOutputPanel.badKey();
}
}
default:
myExOutputPanel.badKey();
}
}
}
}
}