-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy pathConsole.java
More file actions
239 lines (202 loc) · 7.99 KB
/
Console.java
File metadata and controls
239 lines (202 loc) · 7.99 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
package nodebox.client;
import nodebox.base.Preconditions;
import nodebox.node.Parameter;
import org.python.util.PythonInterpreter;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.text.*;
import java.awt.*;
import java.awt.event.*;
import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Console extends JFrame implements WindowListener, FocusListener {
private static final Color PROMPT_COLOR = new Color(72, 134, 242);
private static final Color PROMPT_BORDER_TOP_COLOR = new Color(240, 240, 240);
private static final Color ERROR_COLOR = new Color(255, 0, 0);
private static final SimpleAttributeSet ATTRIBUTES_REGULAR = new SimpleAttributeSet();
private static final SimpleAttributeSet ATTRIBUTES_ERROR = new SimpleAttributeSet();
private static final SimpleAttributeSet ATTRIBUTES_COMMAND = new SimpleAttributeSet();
static {
ATTRIBUTES_COMMAND.addAttribute(StyleConstants.ColorConstants.Foreground, PROMPT_COLOR);
ATTRIBUTES_ERROR.addAttribute(StyleConstants.ColorConstants.Foreground, ERROR_COLOR);
}
private static Logger logger = Logger.getLogger("nodebox.client.Console");
private PythonInterpreter interpreter;
private ArrayList<String> history = new ArrayList<String>();
private int historyOffset = 0;
private String temporarySavedCommand = null;
private JTextPane consoleMessages;
private JTextField consolePrompt;
private Document messagesDocument;
private JScrollPane messagesScroll;
public Console() {
super("Console");
consoleMessages = new JTextPane();
consoleMessages.setMargin(new Insets(2, 20, 2, 5));
consoleMessages.setFont(Theme.EDITOR_FONT);
consoleMessages.setEditable(false);
consoleMessages.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent mouseEvent) {
consolePrompt.requestFocus();
}
});
messagesDocument = consoleMessages.getDocument();
messagesScroll = new JScrollPane(consoleMessages, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
messagesScroll.setBorder(BorderFactory.createEmptyBorder());
consolePrompt = new JTextField();
consolePrompt.setFont(Theme.EDITOR_FONT);
Keymap defaultKeymap = JTextComponent.getKeymap(JTextComponent.DEFAULT_KEYMAP);
Keymap keymap = JTextComponent.addKeymap(null, defaultKeymap);
keymap.addActionForKeyStroke(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), new EnterAction());
keymap.addActionForKeyStroke(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0), new HistoryUpAction());
keymap.addActionForKeyStroke(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0), new HistoryDownAction());
consolePrompt.setKeymap(keymap);
consolePrompt.setBorder(new PromptBorder());
setLayout(new BorderLayout());
add(messagesScroll, BorderLayout.CENTER);
add(consolePrompt, BorderLayout.SOUTH);
interpreter = new PythonInterpreter();
consolePrompt.requestFocus();
addFocusListener(this);
addWindowListener(this);
}
private void addMessage(String s, AttributeSet attributes) {
try {
messagesDocument.insertString(messagesDocument.getLength(), s, attributes);
} catch (BadLocationException e) {
logger.log(Level.WARNING, "addMessage: bad location (" + s + ")", e);
}
}
private void addMessage(String s) {
addMessage(s, ATTRIBUTES_REGULAR);
}
private void addCommandMessage(String s) {
addMessage(s, ATTRIBUTES_COMMAND);
}
private void addErrorMessage(String s) {
addMessage(s, ATTRIBUTES_ERROR);
}
public void doEnter() {
String command = getCommand();
addCommandToHistory(command);
setCommand("");
addCommandMessage(command + "\n");
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ByteArrayOutputStream errorStream = new ByteArrayOutputStream();
interpreter.setOut(outputStream);
interpreter.setErr(errorStream);
// HACK Indirect way to access the current document.
NodeBoxDocument document = Application.getInstance().getCurrentDocument();
interpreter.set("document", document);
interpreter.set("root", document.getNodeLibrary().getRootNode());
interpreter.set("parent", document.getActiveNetwork());
interpreter.set("node", document.getActiveNode());
interpreter.exec("from nodebox.node import *");
for (Parameter.Type type : Parameter.Type.values()) {
interpreter.set(type.name(), type);
}
for (Parameter.Type t : Parameter.Type.values())
interpreter.set(t.name(), t);
Exception pythonException = null;
try {
Object result = interpreter.eval(command);
if (result != null) {
addMessage(result.toString() + "\n");
}
} catch (Exception e) {
pythonException = e;
}
String os = outputStream.toString();
if (os.length() > 0) {
addMessage(os);
if (!os.endsWith("\n"))
addMessage("\n");
}
if (pythonException != null)
addErrorMessage(pythonException.toString());
}
public String getCommand() {
return consolePrompt.getText();
}
public void setCommand(String command) {
consolePrompt.setText(command);
}
public void moveBackInHistory() {
if (historyOffset == history.size())
return;
if (historyOffset == 0) {
temporarySavedCommand = getCommand();
}
historyOffset++;
setCommand(history.get(history.size() - historyOffset));
}
public void moveForwardInHistory() {
if (historyOffset == 0)
return;
historyOffset--;
if (historyOffset == 0) {
Preconditions.checkNotNull(temporarySavedCommand, "temporarySavedCommand is null.");
setCommand(temporarySavedCommand);
temporarySavedCommand = null;
} else {
setCommand(history.get(history.size() - historyOffset));
}
}
public void addCommandToHistory(String command) {
history.add(command);
historyOffset = 0;
}
public void focusGained(FocusEvent focusEvent) {
consolePrompt.requestFocus();
}
public void focusLost(FocusEvent focusEvent) {
}
private class EnterAction extends AbstractAction {
public void actionPerformed(ActionEvent e) {
doEnter();
}
}
private class HistoryUpAction extends AbstractAction {
public void actionPerformed(ActionEvent e) {
moveBackInHistory();
}
}
private class HistoryDownAction extends AbstractAction {
public void actionPerformed(ActionEvent e) {
moveForwardInHistory();
}
}
//// Window events ////
public void windowOpened(WindowEvent e) {
}
public void windowClosing(WindowEvent e) {
Application.getInstance().onHideConsole();
}
public void windowClosed(WindowEvent e) {
}
public void windowIconified(WindowEvent e) {
}
public void windowDeiconified(WindowEvent e) {
}
public void windowActivated(WindowEvent e) {
}
public void windowDeactivated(WindowEvent e) {
}
private class PromptBorder implements Border {
public void paintBorder(Component component, Graphics g, int x, int y, int width, int height) {
g.setColor(PROMPT_BORDER_TOP_COLOR);
g.drawLine(0, 0, width, 0);
g.setColor(PROMPT_COLOR);
g.drawString(">", 5, 14);
}
public Insets getBorderInsets(Component component) {
return new Insets(3, 20, 3, 0);
}
public boolean isBorderOpaque() {
return true;
}
}
}