/
Console.java
166 lines (150 loc) · 5.56 KB
/
Console.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
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.text.html.*;
import java.util.function.*;
import javax.script.ScriptException;
class Console extends JTextPane implements MouseListener, MouseMotionListener {
private Style errorStyle;
private Style errorStyleRed;
private Style errorStyleMono;
private Cursor handCursor = new Cursor(Cursor.HAND_CURSOR);
private Cursor defaultCursor = new Cursor(Cursor.DEFAULT_CURSOR);
private JScrollPane parent;
private StyledDocument doc;
private Network net;
private JFrame parentFrame;
private boolean autoscroll = true;
public Console(JFrame parentFrame, Network net) {
this.parentFrame = parentFrame;
this.net = net;
setEditable(false);
setContentType("text/html");
doc = getStyledDocument();
addStyledText("Initialized", null);
setPreferredSize(new Dimension(150, 0));
addMouseMotionListener(this);
addMouseListener(this);
initializeStyles();
}
private void initializeStyles() {
errorStyle = doc.addStyle("errorStyle",
StyleContext.
getDefaultStyleContext().
getStyle(StyleContext.DEFAULT_STYLE));
errorStyleRed = doc.addStyle("errorStyleRed",
errorStyle);
StyleConstants.setForeground(errorStyleRed, Color.RED);
errorStyleMono = doc.addStyle("errorStyleMono",
errorStyle);
StyleConstants.setFontFamily(errorStyleMono, "Monospaced");
}
public BiConsumer<String, String> getPrintCallback() {
return ((ip, str) -> {
if(str.charAt(str.length()-1) == '\n') {
str = str.substring(0, str.length()-1);
}
displayMessageFromIP(ip, str);
});
}
public Consumer<ScriptExceptionContainer> getErrorCallback() {
return ((error) -> {
displayScriptError(error);
});
}
public void setScrollPane(JScrollPane parent) {
this.parent = parent;
JScrollBar jsb = parent.getVerticalScrollBar();
jsb.addAdjustmentListener(new AdjustmentListener() {
@Override
public void adjustmentValueChanged(AdjustmentEvent ae) {
if(!ae.getValueIsAdjusting() && autoscroll) {
jsb.setValue(jsb.getMaximum());
}
}
});
}
public void displayMessageFromIP(String ip, String text) {
addHTML("<br><b>" + ip + "</b>: " + text);
}
private void addHTML(String text) {
HTMLDocument hdoc = (HTMLDocument)doc;
try {
hdoc.insertAfterEnd(hdoc.getCharacterElement(hdoc.getLength()), text);
} catch(BadLocationException|IOException ex) {
ex.printStackTrace();
}
}
public void displayScriptError(ScriptExceptionContainer se) {
errorStyle.addAttribute("ip", se.ip);
errorStyle.addAttribute("exception", se.exception);
errorStyleRed.addAttribute("ip", se.ip);
errorStyleRed.addAttribute("exception", se.exception);
errorStyleMono.addAttribute("ip", se.ip);
errorStyleMono.addAttribute("exception", se.exception);
addStyledText("\n", null);
addStyledText("Error", errorStyleRed);
addStyledText(" at ", errorStyle);
addStyledText(se.ip, errorStyleMono);
}
private void addStyledText(String text, Style style) {
try {
doc.insertString(doc.getLength(), text, style);
} catch(BadLocationException ble) {
ble.printStackTrace();
}
}
public void setAutoscroll(boolean autoscroll) {
this.autoscroll = autoscroll;
}
public boolean getAutoscroll() {
return autoscroll;
}
@Override
public void mouseClicked(MouseEvent me) {
Element el = doc.getCharacterElement(viewToModel(me.getPoint()));
if(el == null) return;
AttributeSet as = el.getAttributes();
if(as.isDefined("ip")) {
String ip = (String)as.getAttribute("ip");
ScriptException se = (ScriptException)as.getAttribute("exception");
Node node = net.getAtIP(ip);
if(node == null) {
Utility.displayError("Error", "Computer does not exist");
return;
}
String errorString = "--ERROR--\n" +
"Error at line number " + se.getLineNumber() +
" and column number " + se.getColumnNumber() +
"\n" + se.getMessage();
new ScriptDialog(parentFrame, str -> {
if(str != null) {
node.setScript(str);
}
}, node.getScript(), errorString).setVisible(true);
}
}
@Override
public void mouseMoved(MouseEvent me) {
Element el = doc.getCharacterElement(viewToModel(me.getPoint()));
if(el == null) return;
AttributeSet as = el.getAttributes();
if(as.isDefined("ip")) {
setCursor(handCursor);
} else {
setCursor(defaultCursor);
}
}
@Override
public void mouseExited(MouseEvent me) {}
@Override
public void mouseEntered(MouseEvent me) {}
@Override
public void mouseReleased(MouseEvent me) {}
@Override
public void mousePressed(MouseEvent me) {}
@Override
public void mouseDragged(MouseEvent me) {}
}