-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy pathEditorDocument.java
More file actions
217 lines (191 loc) · 7.89 KB
/
EditorDocument.java
File metadata and controls
217 lines (191 loc) · 7.89 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
package nodebox.client;
import nodebox.graphics.CanvasContext;
import nodebox.graphics.Text;
import nodebox.graphics.Geometry;
import nodebox.graphics.Path;
import org.python.util.PythonInterpreter;
import javax.swing.*;
import javax.swing.text.BadLocationException;
import javax.swing.undo.CannotRedoException;
import javax.swing.undo.CannotUndoException;
import javax.swing.undo.UndoManager;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.io.ByteArrayOutputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
public class EditorDocument extends JFrame {
private final static String WINDOW_MODIFIED = "windowModified";
private static Logger logger = Logger.getLogger("nodebox.client.EditorDocument");
private CanvasContext context;
private PythonInterpreter interpreter;
private EditorViewer viewer;
private CodeArea codeArea;
private FeedbackArea feedbackArea;
private UndoManager undo = new UndoManager();
private UndoAction undoAction = new UndoAction();
private RedoAction redoAction = new RedoAction();
public EditorDocument() {
context = new CanvasContext();
interpreter = new PythonInterpreter();
JPanel rootPanel = new JPanel(new BorderLayout());
rootPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
viewer = new EditorViewer();
codeArea = new CodeArea();
codeArea.setBorder(BorderFactory.createEtchedBorder());
JScrollPane codeScroll = new JScrollPane(codeArea);
codeScroll.setBorder(BorderFactory.createEmptyBorder());
feedbackArea = new FeedbackArea();
feedbackArea.setFont(Theme.EDITOR_FONT);
feedbackArea.setBorder(BorderFactory.createEtchedBorder());
JScrollPane feedbackScroll = new JScrollPane(feedbackArea);
feedbackScroll.setBorder(BorderFactory.createEmptyBorder());
JSplitPane split1 = new JSplitPane(JSplitPane.VERTICAL_SPLIT, codeScroll, feedbackScroll);
split1.setBorder(BorderFactory.createEmptyBorder());
split1.setDividerLocation(0.5);
split1.setResizeWeight(0.5);
JSplitPane split2 = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, viewer, split1);
split2.setBorder(BorderFactory.createEmptyBorder());
split2.setDividerLocation(0.5);
split2.setResizeWeight(0.5);
rootPanel.add(split2, BorderLayout.CENTER);
setContentPane(rootPanel);
initMenu();
setSize(1100, 800);
}
private void initMenu() {
JMenuBar menuBar = new JMenuBar();
// Edit
JMenu editMenu = new JMenu("Edit");
editMenu.add(undoAction);
editMenu.add(redoAction);
editMenu.addSeparator();
menuBar.add(editMenu);
// Python
JMenu pythonMenu = new JMenu("Python");
pythonMenu.add(new RunAction());
menuBar.add(pythonMenu);
setJMenuBar(menuBar);
}
private class EditorViewer extends JComponent {
@Override
protected void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
//g2.translate(getWidth() / 2, getHeight() / 2);
context.getCanvas().draw(g2);
}
}
private void addString(String s) {
try {
feedbackArea.getDocument().insertString(feedbackArea.getDocument().getLength(), s, null);
} catch (BadLocationException e) {
logger.log(Level.WARNING, "addString: bad location (" + s + ")", e);
}
}
private class RunAction extends AbstractAction {
private RunAction() {
putValue(Action.NAME, "Run");
putValue(ACCELERATOR_KEY, PlatformUtils.getKeyStroke(KeyEvent.VK_R));
}
public void actionPerformed(ActionEvent actionEvent) {
// Clear out feedback area
context.resetContext();
context.getCanvas().clear();
feedbackArea.setText("");
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ByteArrayOutputStream errorStream = new ByteArrayOutputStream();
interpreter.set("g", context);
interpreter.set("Path", nodebox.graphics.Path.class);
interpreter.set("Canvas", nodebox.graphics.Canvas.class);
interpreter.set("Color", nodebox.graphics.Color.class);
interpreter.set("GraphicsContext", CanvasContext.class);
interpreter.set("Grob", nodebox.graphics.Grob.class);
interpreter.set("Path", Path.class);
interpreter.set("Geometry", Geometry.class);
interpreter.set("Image", nodebox.graphics.Image.class);
interpreter.set("NodeBoxError", nodebox.graphics.NodeBoxError.class);
interpreter.set("PathElement", nodebox.graphics.PathElement.class);
interpreter.set("Point", nodebox.graphics.Point.class);
interpreter.set("Rect", nodebox.graphics.Rect.class);
interpreter.set("Text", nodebox.graphics.Text.class);
interpreter.set("Transform", nodebox.graphics.Transform.class);
interpreter.set("LEFT", Text.Align.LEFT);
interpreter.set("RIGHT", Text.Align.RIGHT);
interpreter.set("CENTER", Text.Align.CENTER);
interpreter.set("JUSTIFY", Text.Align.JUSTIFY);
interpreter.setOut(outputStream);
interpreter.setErr(errorStream);
Exception pythonException = null;
String pythonCode = codeArea.getText();
pythonCode = "_g = globals()\nfor n in dir(g): _g[n] = getattr(g, n)\n\n" + pythonCode;
try {
interpreter.exec(pythonCode);
} catch (Exception e) {
pythonException = e;
logger.log(Level.INFO, "Error on exec", e);
}
String os = outputStream.toString();
if (os.length() > 0)
addString(os);
if (!os.endsWith("\n"))
addString("\n");
if (pythonException != null)
addString(pythonException.toString() + "\n");
viewer.repaint();
}
}
public class UndoAction extends AbstractAction {
public UndoAction() {
putValue(NAME, "Undo");
putValue(ACCELERATOR_KEY, PlatformUtils.getKeyStroke(KeyEvent.VK_Z));
setEnabled(false);
}
public void actionPerformed(ActionEvent e) {
try {
undo.undo();
} catch (CannotUndoException ex) {
logger.log(Level.WARNING, "Unable to undo.", ex);
}
update();
redoAction.update();
}
public void update() {
if (undo.canUndo()) {
setEnabled(true);
putValue(Action.NAME, undo.getUndoPresentationName());
} else {
setEnabled(false);
putValue(Action.NAME, "Undo");
}
}
}
public class RedoAction extends AbstractAction {
public RedoAction() {
putValue(NAME, "Redo");
putValue(ACCELERATOR_KEY, PlatformUtils.getKeyStroke(KeyEvent.VK_Z, Event.SHIFT_MASK));
setEnabled(false);
}
public void actionPerformed(ActionEvent e) {
try {
undo.redo();
} catch (CannotRedoException ex) {
logger.log(Level.WARNING, "Unable to redo.", ex);
}
update();
undoAction.update();
}
public void update() {
if (undo.canRedo()) {
setEnabled(true);
putValue(Action.NAME, undo.getRedoPresentationName());
} else {
setEnabled(false);
putValue(Action.NAME, "Redo");
}
}
}
private class FeedbackArea extends JTextArea {
}
}