-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy pathEditorPane.java
More file actions
323 lines (282 loc) · 10.8 KB
/
EditorPane.java
File metadata and controls
323 lines (282 loc) · 10.8 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
package nodebox.client;
import nodebox.client.editor.SimpleEditor;
import nodebox.node.Node;
import nodebox.node.Parameter;
import nodebox.node.ProcessingContext;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.text.Element;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Map;
import java.util.WeakHashMap;
import static nodebox.base.Preconditions.checkNotNull;
public class EditorPane extends Pane implements CaretListener, ChangeListener, ActionListener {
private final Map<Parameter, String> inProgressCode = new WeakHashMap<Parameter, String>();
private final NodeBoxDocument document;
private Node activeNode;
private Parameter activeParameter;
private String codeType = "_code";
private PaneHeader paneHeader;
private SimpleEditor editor;
private EditorSplitPane splitter;
private JTextArea messages;
private NButton messagesCheck;
private NButton reloadButton;
private boolean ignoringChanges = true;
public EditorPane(NodeBoxDocument document) {
this.document = document;
setLayout(new BorderLayout(0, 0));
paneHeader = new PaneHeader(this);
reloadButton = new NButton("Reload", "res/code-reload.png");
setReloadButtonEnabled(false); // Only enable the button if the code has changed.
reloadButton.setActionMethod(this, "reload");
messagesCheck = new NButton(NButton.Mode.CHECK, "Messages");
messagesCheck.setActionMethod(this, "toggleMessages");
paneHeader.add(reloadButton);
paneHeader.add(new Divider());
paneHeader.add(messagesCheck);
paneHeader.add(new Divider());
PaneCodeMenu paneCodeMenu = new PaneCodeMenu();
paneCodeMenu.addActionListener(this);
paneHeader.add(paneCodeMenu);
editor = new SimpleEditor();
editor.addCaretListener(this);
editor.addChangeListener(this);
add(paneHeader, BorderLayout.NORTH);
messages = new JTextArea();
messages.setEditable(false);
messages.setBorder(new TopLineBorder());
messages.setFont(Theme.EDITOR_FONT);
messages.setMargin(new Insets(0, 5, 0, 5));
JScrollPane messagesScroll = new JScrollPane(messages, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
messagesScroll.setBorder(BorderFactory.createEmptyBorder());
splitter = new EditorSplitPane(JSplitPane.VERTICAL_SPLIT, editor, messagesScroll);
add(splitter, BorderLayout.CENTER);
}
public Pane duplicate() {
return new EditorPane(document);
}
public String getPaneName() {
return "Source";
}
public PaneHeader getPaneHeader() {
return paneHeader;
}
public PaneView getPaneView() {
return editor;
}
public void setActiveNode(Node node) {
activeNode = node;
if (activeNode != null) {
activeParameter = getCodeParameter(activeNode, codeType);
} else {
activeParameter = null;
}
updateSource();
}
public void setCodeType(String codeType) {
this.codeType = codeType;
activeParameter = getCodeParameter(activeNode, codeType);
updateSource();
paneHeader.repaint();
clearMessages();
}
private void updateSource() {
if (activeParameter != null) {
if (hasInProgressCode(activeParameter)) {
setSource(getInProgressCode(activeParameter));
setReloadButtonEnabled(true);
} else {
Parameter codeParameter = activeNode.getParameter(codeType);
setSource(codeParameter.asCode().getSource());
setReloadButtonEnabled(false);
}
} else {
setSource(null);
setReloadButtonEnabled(false);
}
}
public void reload() {
// This method can be called from the menu bar, so we need to check if the code has really changed.
if (!hasInProgressCode(activeParameter)) return;
document.setActiveNodeCode(activeParameter, getSource());
removeInProgressCode(activeParameter);
// The code has been set so there is nothing to reload.
setReloadButtonEnabled(false);
}
private void setReloadButtonEnabled(boolean v) {
reloadButton.setEnabled(v);
reloadButton.setWarning(v);
}
public String getSource() {
return editor.getSource();
}
public boolean isIgnoringChanges() {
return ignoringChanges;
}
public void setIgnoringChanges(boolean ignoringChanges) {
this.ignoringChanges = ignoringChanges;
}
public void setSource(String source) {
// Setting the source will trigger change events for every line of source code.
// Ignore changes temporarily, and re-enable when the source has been set.
setIgnoringChanges(true);
if (source == null) {
editor.setSource("");
editor.setEnabled(false);
messages.setEnabled(false);
messages.setBackground(Theme.MESSAGES_BACKGROUND_COLOR);
splitter.setShowMessages(false);
} else {
editor.setSource(source);
editor.setEnabled(true);
messages.setEnabled(true);
messages.setBackground(Color.white);
// The source has been set. All subsequent changes are triggered by the user.
setIgnoringChanges(false);
}
}
public void toggleMessages() {
setShowMessages(messagesCheck.isChecked());
}
private void setShowMessages(boolean v) {
splitter.setShowMessages(v);
messagesCheck.setChecked(v);
}
/**
* Set the messages to a given string.
* This is used for exceptions in the handle code.
*
* @param s The string to display.
*/
public void setMessages(String s) {
if (s != null && !s.isEmpty()) {
messages.setText(s);
setShowMessages(true);
// Ensure messages are visible.
splitter.setShowMessages(true);
} else {
messages.setText("");
}
}
/**
* Clear out the text in messages.
*/
public void clearMessages() {
setMessages("");
}
/**
* Update the messages after the node has done processing.
* <p/>
* This is only used for _code, not for _handles.
*
* @param node The node that was processed.
* @param context The node's processing context.
*/
public void updateMessages(Node node, ProcessingContext context) {
// If we're looking at the handle don't show messages for the node.
if (codeType.equals("_handle")) return;
StringBuilder sb = new StringBuilder();
if (node != null) {
// Add the error messages.
if (node.hasError()) {
StringWriter sw = new StringWriter();
Throwable t = node.getError();
t.printStackTrace(new PrintWriter(sw));
sb.append(t.toString());
//sb.append(t.getMessage()).append("\n");
Throwable cause = t.getCause();
while (cause != null) {
sb.append("Caused by: \n");
sb.append(cause.toString()).append("\n");
// Sometimes the cause is stored recursively.
// Break out of the loop instead of triggering infinite recursion.
if (cause.getCause() == cause) {
break;
}
cause = cause.getCause();
}
}
// Add the stdout messages.
if (context != null && context.getOutput().length() > 0) {
sb.append(context.getOutput());
}
}
if (sb.length() > 0) {
messages.setText(sb.toString());
setShowMessages(true);
// Ensure messages are visible
splitter.setShowMessages(true);
} else {
messages.setText("");
}
}
public void caretUpdate(CaretEvent e) {
JEditorPane editArea = (JEditorPane) e.getSource();
int caretPosition = editArea.getCaretPosition();
Element root = editArea.getDocument().getDefaultRootElement();
int line = root.getElementIndex(caretPosition) + 1;
// Subtract the offset of the start of the line from the caret position.
// Add one because line numbers are zero-based.
int column = 1 + caretPosition - root.getElement(line - 1).getStartOffset();
updatePosition(line, column);
}
private void updatePosition(int line, int column) {
splitter.setLocation(line, column);
}
/**
* This method gets called by the editor component if the source has changed.
*
* @param changeEvent The type of change.
*/
public void stateChanged(ChangeEvent changeEvent) {
// The editor also triggers state changes when using setSource(). We ignore those changes.
if (isIgnoringChanges()) return;
setInProgressCode(activeParameter, getSource());
setReloadButtonEnabled(true);
document.codeEdited(getSource());
}
//// In-progress code ////
private Parameter getCodeParameter(Node node, String codeType) {
checkNotNull(node, "Trying to get a code parameter for a null node.");
Parameter p = node.getParameter(codeType);
checkNotNull(p, "Parameter %s on node %s could not be found.", codeType, node);
return p;
}
private void setInProgressCode(Parameter p, String source) {
inProgressCode.put(p, source);
}
private String getInProgressCode(Parameter p) {
return inProgressCode.get(p);
}
private void removeInProgressCode(Parameter p) {
inProgressCode.remove(p);
}
private boolean hasInProgressCode(Parameter p) {
return inProgressCode.containsKey(p);
}
public void actionPerformed(ActionEvent e) {
// We know the event will always come from the pane code type menu.
setCodeType(e.getActionCommand());
}
private class TopLineBorder implements Border {
public void paintBorder(Component component, Graphics g, int x, int y, int width, int height) {
g.setColor(Theme.DEFAULT_SPLIT_COLOR);
g.drawLine(x, y, x + width, y);
}
public Insets getBorderInsets(Component component) {
return new Insets(1, 0, 0, 0);
}
public boolean isBorderOpaque() {
return true;
}
}
}