-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConsole.java
More file actions
586 lines (452 loc) · 18.5 KB
/
Copy pathConsole.java
File metadata and controls
586 lines (452 loc) · 18.5 KB
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
/*
* Copyright (C) 2020 Henrik Ankersø
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* https://www.sdu.dk/ - Software Engineering 2020 - Group T2-3; SDU Overflow
*
* @author Alican Erten, Henrik Ankersø, Simon Krüger Tagge, Stefan Profft Larsen and Thomas Christensen
* @version 2020.11.10
*/
import javax.swing.*;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultCaret;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
@SuppressWarnings({"unused"})
public class Console implements KeyListener, MouseListener {
private final Color FONT_COLOR = Color.WHITE;
private final Color BACKGROUND_COLOR = Color.BLACK;
private final int[] KEYS_WITHOUT_INTERESTS = {KeyEvent.VK_ACCEPT, KeyEvent.VK_ADD, KeyEvent.VK_AGAIN,
KeyEvent.VK_ALL_CANDIDATES, KeyEvent.VK_ALPHANUMERIC, KeyEvent.VK_ALT, KeyEvent.VK_ALT_GRAPH,
KeyEvent.VK_AMPERSAND, KeyEvent.VK_BEGIN, KeyEvent.VK_CANCEL, KeyEvent.VK_CAPS_LOCK, KeyEvent.VK_CODE_INPUT,
KeyEvent.VK_COMPOSE, KeyEvent.VK_CONTEXT_MENU, KeyEvent.VK_CONTROL, KeyEvent.VK_CONVERT, KeyEvent.VK_F1,
KeyEvent.VK_F2, KeyEvent.VK_F3, KeyEvent.VK_F4, KeyEvent.VK_F5, KeyEvent.VK_F6, KeyEvent.VK_F7,
KeyEvent.VK_F8, KeyEvent.VK_F9, KeyEvent.VK_F10, KeyEvent.VK_F11, KeyEvent.VK_F12, KeyEvent.VK_F13,
KeyEvent.VK_F14, KeyEvent.VK_F15, KeyEvent.VK_F16, KeyEvent.VK_F17, KeyEvent.VK_F18, KeyEvent.VK_F19,
KeyEvent.VK_F20, KeyEvent.VK_F21, KeyEvent.VK_F22, KeyEvent.VK_F23, KeyEvent.VK_F24, KeyEvent.VK_DECIMAL,
KeyEvent.VK_FINAL, KeyEvent.VK_FIND, KeyEvent.VK_FULL_WIDTH, KeyEvent.VK_HALF_WIDTH, KeyEvent.VK_HELP,
KeyEvent.VK_HIRAGANA, KeyEvent.VK_INPUT_METHOD_ON_OFF, KeyEvent.VK_JAPANESE_HIRAGANA,
KeyEvent.VK_JAPANESE_KATAKANA, KeyEvent.VK_JAPANESE_ROMAN, KeyEvent.VK_KANA, KeyEvent.VK_KANA_LOCK,
KeyEvent.VK_KANJI, KeyEvent.VK_KATAKANA, KeyEvent.VK_META, KeyEvent.VK_MODECHANGE, KeyEvent.VK_NONCONVERT,
KeyEvent.VK_NUM_LOCK, KeyEvent.VK_PAUSE, KeyEvent.VK_PREVIOUS_CANDIDATE, KeyEvent.VK_PRINTSCREEN,
KeyEvent.VK_PROPS, KeyEvent.VK_QUOTE, KeyEvent.VK_QUOTEDBL, KeyEvent.VK_ROMAN_CHARACTERS,
KeyEvent.VK_SCROLL_LOCK, KeyEvent.VK_SHIFT, KeyEvent.VK_STOP, KeyEvent.VK_TAB, KeyEvent.VK_UNDEFINED,
KeyEvent.VK_UNDO, KeyEvent.VK_WINDOWS};
private JTextArea textArea;
private JFrame frame;
private final PipedOutputStream outputToInputStream = new PipedOutputStream();
private int initialCaretPosition = 0;
private int curCaretPosition = 0;
private int curCmdHistoryIndex = 0;
private ArrayList<String> cmdHistory = new ArrayList<>();
public Console() {
this("Console", -1, -1, null);
}
public Console(String title) {
this(title, -1, -1, null);
}
public Console(String title, String iconFile) {
this(title, -1, -1, iconFile);
}
public Console(String title, int requestWidth, int requestHeight) {
this(title, requestWidth, requestHeight, null);
}
public Console(String title, int requestWidth, int requestHeight, String iconFile) {
this.createWindow(title, requestWidth, requestHeight, iconFile);
// Redirecting input && output
this.setSystemInput();
this.setSystemOutput();
}
public void show() {
this.frame.setVisible(true);
}
public String getTitle(String title) {
return this.frame.getTitle();
}
public void setTitle(String title) {
this.frame.setTitle(title);
}
public void requestSize(int width, int height) {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int maxWidth = (int) screenSize.getWidth() - 10;
if (width > maxWidth) {
width = maxWidth;
} else if (width < 0) {
width = (int) (screenSize.getWidth() * 0.75);
}
int maxHeight = (int) screenSize.getWidth() - 10;
if (height > maxHeight) {
height = maxHeight;
} else if (height < 0) {
height = (int) (screenSize.getHeight() * 0.75);
}
this.frame.setSize(width, height);
}
public void setSize(int width, int height) {
this.frame.setSize(width, height);
}
public Dimension getSize() {
return this.frame.getSize();
}
public void loadIconFile(String file) {
ImageIcon imageIcon;
URL iconURL = Console.class.getResource(file);
if (iconURL == null) {
imageIcon = new ImageIcon(file);
} else {
imageIcon = new ImageIcon(iconURL);
}
this.frame.setIconImage(imageIcon.getImage());
}
public Image getIcon() {
return this.frame.getIconImage();
}
public void setIcon(Image image) {
this.frame.setIconImage(image);
}
public boolean isResizable() {
return this.frame.isResizable();
}
public void setResizable(Boolean resizable) {
this.frame.setResizable(resizable);
}
public void loadFontFile(String file) {
this.loadFontFile(file, Font.TRUETYPE_FONT, this.getFont().getSize());
}
public void loadFontFile(String file, int fontFormat) {
this.loadFontFile(file, fontFormat, this.getFont().getSize());
}
public void loadFontFile(String file, int fontFormat, int size) {
InputStream inputStream;
try {
try {
inputStream = new FileInputStream(file);
} catch (FileNotFoundException e) {
inputStream = Console.class.getResourceAsStream(file);
if (inputStream == null) {
throw new FileNotFoundException("Could not find the font file: " + file);
}
}
this.loadFontFile(inputStream, fontFormat, size);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public void loadFontFile(InputStream is) {
this.loadFontFile(is, Font.TRUETYPE_FONT, this.getFont().getSize());
}
public void loadFontFile(InputStream is, int fontFormat) {
this.loadFontFile(is, fontFormat, this.getFont().getSize());
}
public void loadFontFile(InputStream is, int fontFormat, int size) {
try {
Font font = Font.createFont(fontFormat, is).deriveFont(this.getFont().getStyle(), size);
// Register the font
GraphicsEnvironment.getLocalGraphicsEnvironment().registerFont(font);
this.setFont(font);
} catch (FontFormatException | IOException e) {
e.printStackTrace();
}
}
public Font getFont() {
return this.textArea.getFont();
}
public void setFont(Font font) {
this.textArea.setFont(font);
}
public void setFont(String string) {
Font font = this.getFont();
this.setFont(new Font(string, font.getStyle(), font.getSize()));
}
public void setFont(String string, int size) {
this.setFont(new Font(string, this.getFont().getStyle(), size));
}
public String getFontName() {
return this.getFont().getName();
}
public int getFontStyle() {
return this.getFont().getStyle();
}
public void setFontStyle(int style) {
this.textArea.setFont(this.getFont().deriveFont(style));
}
public int getFontSize(float size) {
return this.getFont().getSize();
}
public void setFontSize(float size) {
this.textArea.setFont(this.getFont().deriveFont(size));
}
public Color getFontColor() {
return this.textArea.getForeground();
}
public void setFontColor(Color color) {
this.textArea.setForeground(color);
}
public int getTabSize() {
return this.textArea.getTabSize();
}
public void setTabSize(int font) {
this.textArea.setTabSize(font);
}
public Color getBackgroundColor() {
return this.textArea.getBackground();
}
public void setBackgroundColor(Color color) {
this.textArea.setBackground(color);
}
public String[] getCmdHistory() {
return this.cmdHistory.toArray(new String[0]);
}
public void setCmdHistory(String[] cmdHistory) {
this.cmdHistory = new ArrayList<>(Arrays.asList(cmdHistory));
}
public void destroy() {
this.frame.dispatchEvent(new WindowEvent(this.frame, WindowEvent.WINDOW_CLOSING));
}
/* Events */
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON3) {
if (this.hasSelection()) {
this.textArea.copy();
this.setCaretPosition(this.curCaretPosition);
} else if (this.isInInput()) {
this.textArea.paste();
}
}
}
@Override
public void mouseReleased(MouseEvent e) {
if (this.isInInput()) {
this.curCaretPosition = this.getCaretPosition();
}
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
@Override
public void keyTyped(KeyEvent ke) {
}
@Override
public void keyReleased(KeyEvent ke) {
if (this.isInInput() && !(ke.getKeyCode() == KeyEvent.VK_A && this.isControlOrMetaDown(ke))) {
this.curCaretPosition = this.getCaretPosition();
}
}
@Override
public void keyPressed(KeyEvent ke) {
int keyCode = ke.getKeyCode();
for (int code : KEYS_WITHOUT_INTERESTS) {
if (code == keyCode) {
return;
}
}
if (keyCode == KeyEvent.VK_A && this.isControlOrMetaDown(ke)) {
int start = this.getInputStart();
int end = this.getInputEnd();
if (this.textArea.getSelectionStart() == start && this.textArea.getSelectionEnd() == end) {
start = 0;
}
this.textArea.setSelectionStart(start);
this.textArea.setSelectionEnd(end);
} else if (keyCode == KeyEvent.VK_HOME) {
this.setCaretPosition(this.getInputStart());
} else if (keyCode == KeyEvent.VK_END) {
this.setCaretPosition(this.getInputEnd());
} else if (keyCode == KeyEvent.VK_COPY || keyCode == KeyEvent.VK_CUT ||
((keyCode == KeyEvent.VK_C || keyCode == KeyEvent.VK_X) && this.isControlOrMetaDown(ke))) {
if (this.hasSelection()) {
this.textArea.copy();
}
this.setCaretPosition(this.curCaretPosition);
} else if (keyCode == KeyEvent.VK_PAGE_UP) {
this.replaceInput(this.getFirstCmdFromHistory());
} else if (keyCode == KeyEvent.VK_PAGE_DOWN) {
this.replaceInput(this.getLastCmdFromHistory());
} else if (keyCode == KeyEvent.VK_UP || keyCode == KeyEvent.VK_KP_UP) {
this.replaceInput(this.getPrevCmdFromHistory());
} else if (keyCode == KeyEvent.VK_DOWN || keyCode == KeyEvent.VK_KP_DOWN) {
this.replaceInput(this.getNextCmdFromHistory());
} else {
if (keyCode == KeyEvent.VK_ENTER) {
int start = this.getInputStart();
int end = this.getInputEnd();
this.setCaretPosition(end);
try {
String cmd = this.textArea.getText(start, end - start);
this.addCmdToHistory(cmd);
this.write(cmd);
this.resetInputPosition();
} catch (BadLocationException e) {
e.printStackTrace();
}
} else if (keyCode == KeyEvent.VK_LEFT || keyCode == KeyEvent.VK_KP_LEFT ||
keyCode == KeyEvent.VK_BACK_SPACE || (keyCode == KeyEvent.VK_H && this.isControlOrMetaDown(ke))) {
if (!this.isInInput()) {
this.setCaretPosition(this.curCaretPosition);
}
if (this.getCaretPosition() == this.getInputStart()) {
ke.consume();
}
} else if (!this.isInInput()) {
this.setCaretPosition(this.curCaretPosition);
}
return;
}
ke.consume();
}
private boolean isControlOrMetaDown(KeyEvent ke) {
return ke.isControlDown() || ke.isMetaDown();
}
/* Cmd history */
private void addCmdToHistory(String cmd) {
this.cmdHistory.add(cmd);
this.curCmdHistoryIndex = this.cmdHistory.size();
}
private String getPrevCmdFromHistory() {
this.curCmdHistoryIndex--;
if (this.curCmdHistoryIndex < 0) {
this.curCmdHistoryIndex = this.cmdHistory.size() - 1;
}
return this.getCmdFromHistory(this.curCmdHistoryIndex);
}
private String getNextCmdFromHistory() {
this.curCmdHistoryIndex++;
if (this.curCmdHistoryIndex >= this.cmdHistory.size()) {
this.curCmdHistoryIndex = 0;
}
return this.getCmdFromHistory(this.curCmdHistoryIndex);
}
private String getCmdFromHistory(int index) {
if (this.cmdHistory.size() > 0) {
return this.cmdHistory.get(index);
}
return "";
}
private String getLastCmdFromHistory() {
this.curCmdHistoryIndex = this.cmdHistory.size() - 1;
return this.getCmdFromHistory(this.curCmdHistoryIndex);
}
private String getFirstCmdFromHistory() {
this.curCmdHistoryIndex = 0;
return this.getCmdFromHistory(0);
}
/* Selection */
private boolean hasSelection() {
return this.textArea.getSelectedText() != null;
}
/* Caret */
private int getCaretPosition() {
return this.textArea.getCaretPosition();
}
private void setCaretPosition(int position) {
this.textArea.setCaretPosition(position);
}
/* Input */
private boolean isInInput() {
int selStart = this.textArea.getSelectionStart();
int selEnd = this.textArea.getSelectionEnd();
if (selStart < selEnd) {
return selStart >= this.getInputStart() && selEnd >= this.getInputEnd();
}
int caretPos = this.getCaretPosition();
return caretPos >= this.getInputStart() && caretPos <= this.getInputEnd();
}
private void replaceInput(String string) {
this.textArea.replaceRange(string, this.getInputStart(), this.getInputEnd());
}
private int getInputStart() {
return this.initialCaretPosition;
}
private int getInputEnd() {
return this.textArea.getDocument().getLength();
}
private void resetInputPosition() {
this.initialCaretPosition = this.curCaretPosition = this.textArea.getDocument().getLength();
}
private void write(String text) {
try {
this.outputToInputStream.write((text + "\n").getBytes());
this.outputToInputStream.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
private void setSystemInput() {
try {
System.setIn(new PipedInputStream(this.outputToInputStream));
} catch (IOException e) {
e.printStackTrace();
}
}
/* Output */
private void setSystemOutput() {
System.setOut(new PrintStream(new OutputStream() {
@Override
public void write(int b) {
byte[] bytes = new byte[1];
bytes[0] = (byte) (b & 0xff);
textArea.append(new String(bytes));
resetInputPosition();
}
}));
}
/* GUI */
private void createWindow(String title, int requestWidth, int requestHeight, String iconFile) {
// Create text area
this.textArea = this.createConsoleArea();
this.textArea.addKeyListener(this);
this.textArea.addMouseListener(this);
this.frame = new JFrame(title);
this.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.requestSize(requestWidth, requestHeight);
if (iconFile != null) {
this.loadIconFile(iconFile);
}
this.frame.add(this.createScrolls());
}
private JScrollPane createScrolls() {
JScrollPane jScrollPane = new JScrollPane(this.textArea);
jScrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
jScrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
return jScrollPane;
}
private JTextArea createConsoleArea() {
JTextArea jTextArea = new JTextArea();
jTextArea.setEditable(true);
jTextArea.setLineWrap(false);
jTextArea.setForeground(FONT_COLOR);
jTextArea.setBackground(BACKGROUND_COLOR);
// Automatically scroll
DefaultCaret caret = (DefaultCaret) jTextArea.getCaret();
caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
return jTextArea;
}
}