-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBufferView.java
137 lines (121 loc) · 3.25 KB
/
BufferView.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
package TextEditor;
import java.io.IOException;
import com.googlecode.lanterna.TerminalFacade;
import com.googlecode.lanterna.input.Key;
import com.googlecode.lanterna.screen.Screen;
import com.googlecode.lanterna.screen.ScreenWriter;
import com.googlecode.lanterna.terminal.swing.SwingTerminal;
public class BufferView {
private final int WIDTH = 80, HEIGHT = 30;
public BufferView(FileBuffer filebuf) throws IOException {
FileBuffer fb = filebuf;
fb.getCursor();
Screen screen = TerminalFacade.createScreen(new SwingTerminal(WIDTH, HEIGHT));
ScreenWriter writer = new ScreenWriter(screen);
screen.startScreen();
showMenu(writer, screen);
screen.completeRefresh();
while (true) {
Key k = screen.readInput();
screen.refresh();
keyEvent(k, screen, writer, fb);
}
}
private void screenCursorUpdate(Screen screen, FileBuffer fb) {
screen.setCursorPosition(fb.getCursor().getY(), fb.getCursor().getX());
}
private void reDraw(ScreenWriter writer, FileBuffer fb) {
for (int i = 0; i < fb.getSize(); i++) {
String line = fb.getNthLine(i).toString();
writer.drawString(0, i, line);
}
}
private void updateText(FileBuffer fb, Screen screen, ScreenWriter writer) {
for (int i = 0; i < fb.getSize(); i++) {
if (fb.getCursor().getY() == WIDTH) {
fb.foldLine();
fb.moveToBetweenLine();
screen.clear();
}
String line = fb.getNthLine(i).toString();
writer.drawString(0, i, line);
}
}
public void showMenu(ScreenWriter writer, Screen screen) {
writer.drawString((WIDTH / 3) - 1, HEIGHT / 3, "PRESS <ENTER> TO START");
screen.completeRefresh();
writer.drawString((WIDTH / 3) - 1, (HEIGHT / 3) + 1, "PRESS <ESCAPE> TO EXIT");
screen.completeRefresh();
writer.drawString((WIDTH / 3) - 1, (HEIGHT / 3) + 2, "PRESS <PAGE DOWN> TO SAVE THE DOCUMENT");
screen.completeRefresh();
}
public void keyEvent(Key k, Screen screen, ScreenWriter writer, FileBuffer fb) throws IOException {
if (k != null) {
switch (k.getKind()) {
case ArrowDown: {
fb.moveNextLine();
screenCursorUpdate(screen, fb);
screen.completeRefresh();
break;
}
case ArrowUp: {
fb.movePreviousLine();
screenCursorUpdate(screen, fb);
screen.completeRefresh();
break;
}
case ArrowLeft: {
fb.moveToPreviousCursor();
screenCursorUpdate(screen, fb);
screen.completeRefresh();
break;
}
case ArrowRight: {
fb.moveToNextCursor();
screenCursorUpdate(screen, fb);
screen.completeRefresh();
break;
}
case Enter: {
fb.insertLine();
fb.moveToBetweenLine();
screenCursorUpdate(screen, fb);
screen.clear();
reDraw(writer, fb);
screen.completeRefresh();
break;
}
case Backspace: {
fb.deleteChar();
screen.clear();
reDraw(writer, fb);
screenCursorUpdate(screen, fb);
screen.completeRefresh();
break;
}
case PageDown: {
fb.save();
break;
}
case Home: {
fb.moveToStartLine();
screenCursorUpdate(screen, fb);
break;
}
case End: {
fb.moveToEndLine();
screenCursorUpdate(screen, fb);
break;
}
case Escape:
screen.stopScreen();
System.exit(1);
break;
default:
fb.insert(k.getCharacter());
updateText(fb, screen, writer);
screenCursorUpdate(screen, fb);
}
}
}
}