|
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 |
| package edu.rice.cs.drjava.model.repl; |
|
38 |
| |
|
39 |
| import edu.rice.cs.drjava.model.AbstractDJDocument; |
|
40 |
| import edu.rice.cs.drjava.model.definitions.indent.Indenter; |
|
41 |
| import edu.rice.cs.drjava.model.definitions.CompoundUndoManager; |
|
42 |
| import edu.rice.cs.drjava.model.GlobalEventNotifier; |
|
43 |
| |
|
44 |
| import edu.rice.cs.plt.tuple.Pair; |
|
45 |
| import edu.rice.cs.util.UnexpectedException; |
|
46 |
| import edu.rice.cs.util.text.EditDocumentException; |
|
47 |
| import edu.rice.cs.util.text.ConsoleDocumentInterface; |
|
48 |
| import edu.rice.cs.util.text.ConsoleDocument; |
|
49 |
| import edu.rice.cs.util.swing.Utilities; |
|
50 |
| |
|
51 |
| import java.awt.*; |
|
52 |
| import java.util.List; |
|
53 |
| import java.util.LinkedList; |
|
54 |
| import javax.swing.text.AbstractDocument; |
|
55 |
| import javax.swing.undo.*; |
|
56 |
| |
|
57 |
| import static edu.rice.cs.drjava.model.definitions.ColoringView.*; |
|
58 |
| |
|
59 |
| |
|
60 |
| |
|
61 |
| |
|
62 |
| public class InteractionsDJDocument extends AbstractDJDocument implements ConsoleDocumentInterface { |
|
63 |
| |
|
64 |
| |
|
65 |
| private volatile boolean _hasPrompt; |
|
66 |
| |
|
67 |
| |
|
68 |
| |
|
69 |
| |
|
70 |
| |
|
71 |
| private volatile boolean _toClear = false; |
|
72 |
| |
|
73 |
| |
|
74 |
| private static final int UNDO_LIMIT = 100; |
|
75 |
| private volatile CompoundUndoManager _undoManager; |
|
76 |
| private volatile boolean _isModifiedSinceSave = false; |
|
77 |
| private volatile GlobalEventNotifier _notifier; |
|
78 |
| |
|
79 |
| |
|
80 |
443
| public InteractionsDJDocument() {
|
|
81 |
443
| super();
|
|
82 |
443
| _hasPrompt = false;
|
|
83 |
443
| _notifier = new GlobalEventNotifier();
|
|
84 |
| } |
|
85 |
| |
|
86 |
| |
|
87 |
316
| public InteractionsDJDocument(GlobalEventNotifier notifier){
|
|
88 |
316
| super();
|
|
89 |
316
| _hasPrompt = false;
|
|
90 |
316
| _notifier=notifier;
|
|
91 |
316
| resetUndoManager();
|
|
92 |
| } |
|
93 |
| |
|
94 |
116
| public boolean hasPrompt() { return _hasPrompt; }
|
|
95 |
| |
|
96 |
| |
|
97 |
| |
|
98 |
| |
|
99 |
1051
| public void setHasPrompt(boolean val) {
|
|
100 |
1051
| _hasPrompt = val;
|
|
101 |
| } |
|
102 |
| |
|
103 |
749
| protected void _styleChanged() { }
|
|
104 |
| |
|
105 |
| |
|
106 |
| |
|
107 |
| private static class CommandUndoableEdit extends AbstractUndoableEdit { |
|
108 |
| private final Runnable _undoCommand; |
|
109 |
| private final Runnable _redoCommand; |
|
110 |
| |
|
111 |
743
| public CommandUndoableEdit(final Runnable undoCommand, final Runnable redoCommand) {
|
|
112 |
743
| _undoCommand = undoCommand;
|
|
113 |
743
| _redoCommand = redoCommand;
|
|
114 |
| } |
|
115 |
| |
|
116 |
5
| public void undo() throws CannotUndoException {
|
|
117 |
5
| super.undo();
|
|
118 |
5
| _undoCommand.run();
|
|
119 |
| } |
|
120 |
| |
|
121 |
1
| public void redo() throws CannotRedoException {
|
|
122 |
1
| super.redo();
|
|
123 |
1
| _redoCommand.run();
|
|
124 |
| } |
|
125 |
| |
|
126 |
0
| public boolean isSignificant() { return false; }
|
|
127 |
| } |
|
128 |
| |
|
129 |
| |
|
130 |
| |
|
131 |
| |
|
132 |
362
| public CompoundUndoManager getUndoManager() { return _undoManager; }
|
|
133 |
| |
|
134 |
| |
|
135 |
519
| public void resetUndoManager() {
|
|
136 |
519
| _undoManager = new CompoundUndoManager(_notifier);
|
|
137 |
519
| _undoManager.setLimit(UNDO_LIMIT);
|
|
138 |
| } |
|
139 |
| |
|
140 |
| |
|
141 |
0
| public UndoableEdit getNextUndo() { return _undoManager.getNextUndo(); }
|
|
142 |
| |
|
143 |
| |
|
144 |
0
| public UndoableEdit getNextRedo() { return _undoManager.getNextRedo(); }
|
|
145 |
| |
|
146 |
| |
|
147 |
0
| public void documentSaved() { _undoManager.documentSaved(); }
|
|
148 |
| |
|
149 |
0
| protected int startCompoundEdit() { return _undoManager.startCompoundEdit(); }
|
|
150 |
| |
|
151 |
0
| protected void endCompoundEdit(int key) { _undoManager.endCompoundEdit(key); }
|
|
152 |
| |
|
153 |
| |
|
154 |
0
| protected void endLastCompoundEdit() { _undoManager.endLastCompoundEdit(); }
|
|
155 |
| |
|
156 |
743
| protected void addUndoRedo(AbstractDocument.DefaultDocumentEvent chng, Runnable undoCommand, Runnable doCommand) {
|
|
157 |
743
| chng.addEdit(new CommandUndoableEdit(undoCommand, doCommand));
|
|
158 |
| } |
|
159 |
| |
|
160 |
250
| public boolean undoManagerCanUndo() { return _undoManager.canUndo(); }
|
|
161 |
| |
|
162 |
250
| public boolean undoManagerCanRedo() { return _undoManager.canRedo(); }
|
|
163 |
| |
|
164 |
0
| public void updateModifiedSinceSave() {
|
|
165 |
0
| _isModifiedSinceSave = _undoManager.isModified();
|
|
166 |
| |
|
167 |
| } |
|
168 |
| |
|
169 |
| |
|
170 |
| |
|
171 |
| |
|
172 |
0
| private void _setModifiedSinceSave() {
|
|
173 |
0
| assert Utilities.TEST_MODE || EventQueue.isDispatchThread();
|
|
174 |
0
| if (! _isModifiedSinceSave) {
|
|
175 |
0
| _isModifiedSinceSave = true;
|
|
176 |
| |
|
177 |
| } |
|
178 |
| } |
|
179 |
| |
|
180 |
| |
|
181 |
0
| public void resetModification() {
|
|
182 |
0
| _isModifiedSinceSave = false;
|
|
183 |
0
| _undoManager.documentSaved();
|
|
184 |
| |
|
185 |
| } |
|
186 |
| |
|
187 |
| |
|
188 |
| |
|
189 |
| |
|
190 |
0
| public boolean isModifiedSinceSave() { return _isModifiedSinceSave; }
|
|
191 |
| |
|
192 |
| |
|
193 |
| |
|
194 |
| |
|
195 |
| |
|
196 |
0
| protected Indenter makeNewIndenter(int indentLevel) { return new Indenter(indentLevel); }
|
|
197 |
| |
|
198 |
| |
|
199 |
| |
|
200 |
| |
|
201 |
| |
|
202 |
| |
|
203 |
| |
|
204 |
| |
|
205 |
| |
|
206 |
| |
|
207 |
| |
|
208 |
| private List<Pair<Pair<Integer,Integer>,String>> _stylesList = new LinkedList<Pair<Pair<Integer,Integer>,String>>(); |
|
209 |
| |
|
210 |
| |
|
211 |
703
| public void addColoring(int start, int end, String style) {
|
|
212 |
| |
|
213 |
703
| if (_toClear) {
|
|
214 |
20
| _stylesList.clear();
|
|
215 |
20
| _toClear = false;
|
|
216 |
| } |
|
217 |
703
| if (style != null)
|
|
218 |
689
| _stylesList.add(0, new Pair<Pair<Integer,Integer>,String>
|
|
219 |
| (new Pair<Integer,Integer>(Integer.valueOf(start),Integer.valueOf(end)), style)); |
|
220 |
| |
|
221 |
| } |
|
222 |
| |
|
223 |
| |
|
224 |
13
| public Pair<Pair<Integer, Integer>, String>[] getStyles() {
|
|
225 |
13
| synchronized(_stylesList) {
|
|
226 |
| |
|
227 |
13
| @SuppressWarnings({"unchecked", "rawtypes"})
|
|
228 |
| Pair<Pair<Integer, Integer>, String>[] result = _stylesList.toArray(new Pair[0]); |
|
229 |
13
| return result;
|
|
230 |
| } |
|
231 |
| } |
|
232 |
| |
|
233 |
| |
|
234 |
| |
|
235 |
| |
|
236 |
0
| public boolean setColoring(int point, Graphics g) {
|
|
237 |
0
| synchronized(_stylesList) {
|
|
238 |
0
| for(Pair<Pair<Integer,Integer>,String> p : _stylesList) {
|
|
239 |
0
| Pair<Integer,Integer> loc = p.first();
|
|
240 |
0
| if (loc.first() <= point && loc.second() >= point) {
|
|
241 |
0
| if (p.second().equals(InteractionsDocument.ERROR_STYLE)) {
|
|
242 |
| |
|
243 |
0
| g.setColor(ERROR_COLOR);
|
|
244 |
0
| g.setFont(g.getFont().deriveFont(Font.BOLD));
|
|
245 |
| } |
|
246 |
0
| else if (p.second().equals(InteractionsDocument.DEBUGGER_STYLE)) {
|
|
247 |
| |
|
248 |
0
| g.setColor(DEBUGGER_COLOR);
|
|
249 |
0
| g.setFont(g.getFont().deriveFont(Font.BOLD));
|
|
250 |
| } |
|
251 |
0
| else if (p.second().equals(ConsoleDocument.SYSTEM_OUT_STYLE)) {
|
|
252 |
| |
|
253 |
0
| g.setColor(INTERACTIONS_SYSTEM_OUT_COLOR);
|
|
254 |
0
| g.setFont(MAIN_FONT);
|
|
255 |
| } |
|
256 |
0
| else if (p.second().equals(ConsoleDocument.SYSTEM_IN_STYLE)) {
|
|
257 |
| |
|
258 |
0
| g.setColor(INTERACTIONS_SYSTEM_IN_COLOR);
|
|
259 |
0
| g.setFont(MAIN_FONT);
|
|
260 |
| } |
|
261 |
0
| else if (p.second().equals(ConsoleDocument.SYSTEM_ERR_STYLE)) {
|
|
262 |
| |
|
263 |
0
| g.setColor(INTERACTIONS_SYSTEM_ERR_COLOR);
|
|
264 |
0
| g.setFont(MAIN_FONT);
|
|
265 |
| } |
|
266 |
0
| else if (p.second().equals(InteractionsDocument.OBJECT_RETURN_STYLE)) {
|
|
267 |
0
| g.setColor(NORMAL_COLOR);
|
|
268 |
0
| g.setFont(MAIN_FONT);
|
|
269 |
| } |
|
270 |
0
| else if (p.second().equals(InteractionsDocument.STRING_RETURN_STYLE)) {
|
|
271 |
0
| g.setColor(DOUBLE_QUOTED_COLOR);
|
|
272 |
0
| g.setFont(MAIN_FONT);
|
|
273 |
| } |
|
274 |
0
| else if (p.second().equals(InteractionsDocument.NUMBER_RETURN_STYLE)) {
|
|
275 |
0
| g.setColor(NUMBER_COLOR);
|
|
276 |
0
| g.setFont(MAIN_FONT);
|
|
277 |
| } |
|
278 |
0
| else if (p.second().equals(InteractionsDocument.CHARACTER_RETURN_STYLE)) {
|
|
279 |
0
| g.setColor(SINGLE_QUOTED_COLOR);
|
|
280 |
0
| g.setFont(MAIN_FONT);
|
|
281 |
| } |
|
282 |
0
| else return false;
|
|
283 |
| |
|
284 |
0
| return true;
|
|
285 |
| } |
|
286 |
| } |
|
287 |
0
| return false;
|
|
288 |
| } |
|
289 |
| } |
|
290 |
| |
|
291 |
| |
|
292 |
| |
|
293 |
| |
|
294 |
0
| public void setBoldFonts(int point, Graphics g) {
|
|
295 |
0
| synchronized(_stylesList) {
|
|
296 |
0
| for(Pair<Pair<Integer,Integer>,String> p : _stylesList) {
|
|
297 |
0
| Pair<Integer,Integer> loc = p.first();
|
|
298 |
0
| if (loc.first() <= point && loc.second() >= point) {
|
|
299 |
0
| if (p.second().equals(InteractionsDocument.ERROR_STYLE))
|
|
300 |
0
| g.setFont(g.getFont().deriveFont(Font.BOLD));
|
|
301 |
0
| else if (p.second().equals(InteractionsDocument.DEBUGGER_STYLE))
|
|
302 |
0
| g.setFont(g.getFont().deriveFont(Font.BOLD));
|
|
303 |
0
| else g.setFont(MAIN_FONT);
|
|
304 |
0
| return;
|
|
305 |
| } |
|
306 |
| } |
|
307 |
| } |
|
308 |
| } |
|
309 |
| |
|
310 |
| |
|
311 |
21
| public void clearColoring() { synchronized(_stylesList) { _toClear = true; } }
|
|
312 |
| |
|
313 |
| |
|
314 |
| |
|
315 |
0
| public boolean _inBlockComment() {
|
|
316 |
0
| boolean toReturn = _inBlockComment(getLength());
|
|
317 |
0
| return toReturn;
|
|
318 |
| } |
|
319 |
| |
|
320 |
| |
|
321 |
| |
|
322 |
| |
|
323 |
| |
|
324 |
0
| public void appendExceptionResult(String message, String styleName) {
|
|
325 |
| |
|
326 |
0
| try { insertText(getLength(), message + "\n", styleName); }
|
|
327 |
0
| catch (EditDocumentException ble) { throw new UnexpectedException(ble); }
|
|
328 |
| } |
|
329 |
| } |