|
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 java.io.*; |
|
40 |
| import java.net.ServerSocket; |
|
41 |
| import java.util.List; |
|
42 |
| import java.util.ArrayList; |
|
43 |
| import java.util.HashSet; |
|
44 |
| |
|
45 |
| import javax.swing.text.BadLocationException; |
|
46 |
| import java.awt.EventQueue; |
|
47 |
| |
|
48 |
| import edu.rice.cs.drjava.ui.DrJavaErrorHandler; |
|
49 |
| import edu.rice.cs.drjava.ui.InteractionsPane; |
|
50 |
| import edu.rice.cs.util.FileOpenSelector; |
|
51 |
| import edu.rice.cs.util.OperationCanceledException; |
|
52 |
| import edu.rice.cs.util.StringOps; |
|
53 |
| import edu.rice.cs.util.UnexpectedException; |
|
54 |
| import edu.rice.cs.util.*; |
|
55 |
| import edu.rice.cs.util.swing.Utilities; |
|
56 |
| import edu.rice.cs.util.text.ConsoleDocumentInterface; |
|
57 |
| import edu.rice.cs.util.text.ConsoleDocument; |
|
58 |
| import edu.rice.cs.util.text.EditDocumentException; |
|
59 |
| import edu.rice.cs.plt.tuple.Pair; |
|
60 |
| import edu.rice.cs.drjava.DrJava; |
|
61 |
| import edu.rice.cs.drjava.config.OptionConstants; |
|
62 |
| |
|
63 |
| import static edu.rice.cs.plt.debug.DebugUtil.debug; |
|
64 |
| |
|
65 |
| |
|
66 |
| |
|
67 |
| |
|
68 |
| |
|
69 |
| |
|
70 |
| public abstract class InteractionsModel implements InteractionsModelCallback { |
|
71 |
| |
|
72 |
| |
|
73 |
| public static final String BANNER_PREFIX = "Welcome to DrJava."; |
|
74 |
| |
|
75 |
| |
|
76 |
| |
|
77 |
| public static final int WRITE_DELAY = 50; |
|
78 |
| |
|
79 |
| |
|
80 |
| |
|
81 |
| |
|
82 |
| protected final InteractionsEventNotifier _notifier = new InteractionsEventNotifier(); |
|
83 |
| |
|
84 |
| |
|
85 |
| |
|
86 |
| |
|
87 |
| |
|
88 |
| protected volatile InteractionsDocument _document; |
|
89 |
| |
|
90 |
| |
|
91 |
| protected volatile boolean _waitingForFirstInterpreter; |
|
92 |
| |
|
93 |
| |
|
94 |
| protected volatile File _workingDirectory; |
|
95 |
| |
|
96 |
| |
|
97 |
| |
|
98 |
| public final Object _writerLock; |
|
99 |
| |
|
100 |
| |
|
101 |
| private final int _writeDelay; |
|
102 |
| |
|
103 |
| |
|
104 |
| private volatile int _debugPort; |
|
105 |
| |
|
106 |
| |
|
107 |
| private volatile boolean _debugPortSet; |
|
108 |
| |
|
109 |
| |
|
110 |
| private volatile String _toAddToHistory = ""; |
|
111 |
| |
|
112 |
| |
|
113 |
| protected volatile InputListener _inputListener; |
|
114 |
| |
|
115 |
| |
|
116 |
| protected final ConsoleDocumentInterface _cDoc; |
|
117 |
| |
|
118 |
| |
|
119 |
| |
|
120 |
| |
|
121 |
| |
|
122 |
| |
|
123 |
| |
|
124 |
| |
|
125 |
| |
|
126 |
| |
|
127 |
| public volatile InteractionsPane _pane; |
|
128 |
| |
|
129 |
| |
|
130 |
| private volatile String _banner; |
|
131 |
| |
|
132 |
| |
|
133 |
| protected volatile String _lastError = null; |
|
134 |
| protected volatile String _secondToLastError = null; |
|
135 |
| |
|
136 |
| |
|
137 |
| protected final HashSet<String> _autoImportSet = new HashSet<String>(); |
|
138 |
| |
|
139 |
| |
|
140 |
| |
|
141 |
| |
|
142 |
| |
|
143 |
| |
|
144 |
| |
|
145 |
| |
|
146 |
| |
|
147 |
188
| public InteractionsModel(ConsoleDocumentInterface cDoc, final File wd, int historySize, int writeDelay) {
|
|
148 |
188
| _document = new InteractionsDocument(cDoc, historySize);
|
|
149 |
188
| _cDoc = cDoc;
|
|
150 |
188
| _writeDelay = writeDelay;
|
|
151 |
188
| _waitingForFirstInterpreter = true;
|
|
152 |
188
| _workingDirectory = wd;
|
|
153 |
188
| _writerLock = new Object();
|
|
154 |
188
| _debugPort = -1;
|
|
155 |
188
| _debugPortSet = false;
|
|
156 |
188
| _inputListener = NoInputListener.ONLY;
|
|
157 |
188
| Utilities.invokeLater(new Runnable() {
|
|
158 |
188
| public void run() { _document.setBanner(generateBanner(wd));}
|
|
159 |
| }); |
|
160 |
| } |
|
161 |
| |
|
162 |
| |
|
163 |
165
| public void setUpPane(InteractionsPane pane) {
|
|
164 |
165
| _pane = pane;
|
|
165 |
165
| _pane.setCaretPosition(_document.getLength());
|
|
166 |
| |
|
167 |
| } |
|
168 |
| |
|
169 |
| |
|
170 |
| |
|
171 |
| |
|
172 |
481
| public void addListener(InteractionsListener listener) { _notifier.addListener(listener); }
|
|
173 |
| |
|
174 |
| |
|
175 |
| |
|
176 |
| |
|
177 |
| |
|
178 |
2
| public void removeListener(InteractionsListener listener) { _notifier.removeListener(listener); }
|
|
179 |
| |
|
180 |
| |
|
181 |
112
| public void removeAllInteractionListeners() { _notifier.removeAllListeners(); }
|
|
182 |
| |
|
183 |
| |
|
184 |
233
| public InteractionsDocument getDocument() { return _document; }
|
|
185 |
| |
|
186 |
1
| public void interactionContinues() {
|
|
187 |
1
| _document.setInProgress(false);
|
|
188 |
1
| _notifyInteractionEnded();
|
|
189 |
1
| _notifyInteractionIncomplete();
|
|
190 |
| } |
|
191 |
| |
|
192 |
| |
|
193 |
| |
|
194 |
| |
|
195 |
2
| public void setWaitingForFirstInterpreter(boolean waiting) { _waitingForFirstInterpreter = waiting; }
|
|
196 |
| |
|
197 |
| |
|
198 |
47
| public void interpretCurrentInteraction() {
|
|
199 |
| |
|
200 |
47
| Utilities.invokeLater(new Runnable() {
|
|
201 |
47
| public void run() {
|
|
202 |
| |
|
203 |
0
| if (_document.inProgress()) return;
|
|
204 |
| |
|
205 |
47
| String text = _document.getCurrentInteraction();
|
|
206 |
47
| String toEval = text.trim();
|
|
207 |
47
| _prepareToInterpret(toEval);
|
|
208 |
| |
|
209 |
| |
|
210 |
47
| toEval = transformCommands(toEval);
|
|
211 |
47
| if (DrJava.getConfig().getSetting(OptionConstants.DEBUG_AUTO_IMPORT).booleanValue() &&
|
|
212 |
| toEval.startsWith("import ")) { |
|
213 |
| |
|
214 |
| |
|
215 |
| |
|
216 |
| |
|
217 |
| |
|
218 |
1
| String line = toEval;
|
|
219 |
1
| do {
|
|
220 |
1
| line = line.substring("import ".length());
|
|
221 |
1
| String substr = line;
|
|
222 |
1
| int endPos = 0;
|
|
223 |
1
| while((endPos<substr.length()) &&
|
|
224 |
| ((Character.isJavaIdentifierPart(substr.charAt(endPos))) || |
|
225 |
| (substr.charAt(endPos) == '.') || |
|
226 |
9
| (substr.charAt(endPos) == '*'))) ++endPos;
|
|
227 |
1
| substr = substr.substring(0,endPos);
|
|
228 |
1
| _autoImportSet.add(substr);
|
|
229 |
| |
|
230 |
| |
|
231 |
1
| line = line.substring(substr.length()).trim();
|
|
232 |
0
| if (!line.startsWith(";")) break;
|
|
233 |
1
| line = line.substring(1).trim();
|
|
234 |
1
| } while(line.startsWith("import "));
|
|
235 |
| } |
|
236 |
| |
|
237 |
47
| final String evalText = toEval;
|
|
238 |
| |
|
239 |
47
| new Thread(new Runnable() {
|
|
240 |
47
| public void run() {
|
|
241 |
47
| try { interpret(evalText); }
|
|
242 |
0
| catch(Throwable t) { DrJavaErrorHandler.record(t); }
|
|
243 |
| } |
|
244 |
| }).start(); |
|
245 |
| } |
|
246 |
| }); |
|
247 |
| } |
|
248 |
| |
|
249 |
| |
|
250 |
0
| public void autoImport() {
|
|
251 |
0
| java.util.Vector<String> classes = DrJava.getConfig().getSetting(OptionConstants.INTERACTIONS_AUTO_IMPORT_CLASSES);
|
|
252 |
0
| final StringBuilder sb = new StringBuilder();
|
|
253 |
| |
|
254 |
0
| for(String s: classes) {
|
|
255 |
0
| String name = s.trim();
|
|
256 |
0
| if (s.length() > 0) {
|
|
257 |
0
| sb.append("import ");
|
|
258 |
0
| sb.append(s.trim());
|
|
259 |
0
| sb.append("; ");
|
|
260 |
| } |
|
261 |
| } |
|
262 |
0
| if (DrJava.getConfig().getSetting(OptionConstants.DEBUG_AUTO_IMPORT).booleanValue()) {
|
|
263 |
0
| for(String s: _autoImportSet) {
|
|
264 |
0
| sb.append("import ");
|
|
265 |
0
| sb.append(s);
|
|
266 |
0
| sb.append("; ");
|
|
267 |
| } |
|
268 |
| } |
|
269 |
| |
|
270 |
0
| if (sb.length() > 0) {
|
|
271 |
0
| interpret(sb.toString());
|
|
272 |
0
| _document.insertBeforeLastPrompt("Auto-import: " + sb.toString() + "\n", InteractionsDocument.DEBUGGER_STYLE);
|
|
273 |
| } |
|
274 |
| } |
|
275 |
| |
|
276 |
| |
|
277 |
| |
|
278 |
47
| private void _prepareToInterpret(String text) {
|
|
279 |
47
| _addNewline();
|
|
280 |
47
| _notifyInteractionStarted();
|
|
281 |
47
| _document.setInProgress(true);
|
|
282 |
47
| _toAddToHistory = text;
|
|
283 |
| |
|
284 |
| } |
|
285 |
| |
|
286 |
| |
|
287 |
47
| public void _addNewline() { append(StringOps.NEWLINE, InteractionsDocument.DEFAULT_STYLE); }
|
|
288 |
| |
|
289 |
| |
|
290 |
| |
|
291 |
47
| public final void interpret(String toEval) { _interpret(toEval); }
|
|
292 |
| |
|
293 |
| |
|
294 |
| |
|
295 |
| |
|
296 |
| protected abstract void _interpret(String toEval); |
|
297 |
| |
|
298 |
| |
|
299 |
| protected abstract void _notifyInteractionIncomplete(); |
|
300 |
| |
|
301 |
| |
|
302 |
| public abstract void _notifyInteractionStarted(); |
|
303 |
| |
|
304 |
| |
|
305 |
| |
|
306 |
| |
|
307 |
| |
|
308 |
| public abstract Pair<String,String> getVariableToString(String var); |
|
309 |
| |
|
310 |
| |
|
311 |
25
| public final void resetInterpreter(File wd, boolean force) {
|
|
312 |
25
| _workingDirectory = wd;
|
|
313 |
25
| _autoImportSet.clear();
|
|
314 |
25
| _resetInterpreter(wd, force);
|
|
315 |
| } |
|
316 |
| |
|
317 |
| |
|
318 |
| protected abstract void _resetInterpreter(File wd, boolean force); |
|
319 |
| |
|
320 |
| |
|
321 |
137
| public File getWorkingDirectory() { return _workingDirectory; }
|
|
322 |
| |
|
323 |
| |
|
324 |
| |
|
325 |
| |
|
326 |
| public abstract void addProjectClassPath(File f); |
|
327 |
| |
|
328 |
| |
|
329 |
| |
|
330 |
| |
|
331 |
| public abstract void addBuildDirectoryClassPath(File f); |
|
332 |
| |
|
333 |
| |
|
334 |
| |
|
335 |
| |
|
336 |
| public abstract void addProjectFilesClassPath(File f); |
|
337 |
| |
|
338 |
| |
|
339 |
| |
|
340 |
| |
|
341 |
| public abstract void addExternalFilesClassPath(File f); |
|
342 |
| |
|
343 |
| |
|
344 |
| |
|
345 |
| |
|
346 |
| public abstract void addExtraClassPath(File f); |
|
347 |
| |
|
348 |
| |
|
349 |
| |
|
350 |
| |
|
351 |
| |
|
352 |
| protected abstract void _notifySyntaxErrorOccurred(int offset, int length); |
|
353 |
| |
|
354 |
| |
|
355 |
| |
|
356 |
| |
|
357 |
| |
|
358 |
| |
|
359 |
3
| public void loadHistory(final FileOpenSelector selector) throws IOException {
|
|
360 |
3
| ArrayList<String> histories;
|
|
361 |
3
| try { histories = _getHistoryText(selector); }
|
|
362 |
0
| catch (OperationCanceledException oce) { return; }
|
|
363 |
3
| final ArrayList<String> _histories = histories;
|
|
364 |
| |
|
365 |
3
| _document.clearCurrentInteraction();
|
|
366 |
| |
|
367 |
| |
|
368 |
3
| final StringBuilder buf = new StringBuilder();
|
|
369 |
3
| for (String hist: _histories) {
|
|
370 |
3
| ArrayList<String> interactions = _removeSeparators(hist);
|
|
371 |
3
| for (String curr: interactions) {
|
|
372 |
5
| int len = curr.length();
|
|
373 |
5
| buf.append(curr);
|
|
374 |
1
| if (len > 0 && curr.charAt(len - 1) != ';') buf.append(';');
|
|
375 |
5
| buf.append(StringOps.EOL);
|
|
376 |
| } |
|
377 |
| } |
|
378 |
3
| String text = buf.toString().trim();
|
|
379 |
| |
|
380 |
3
| append(text, InteractionsDocument.DEFAULT_STYLE);
|
|
381 |
3
| interpretCurrentInteraction();
|
|
382 |
| |
|
383 |
| |
|
384 |
| } |
|
385 |
| |
|
386 |
| |
|
387 |
| |
|
388 |
| |
|
389 |
| |
|
390 |
| |
|
391 |
4
| protected static ArrayList<String> _getHistoryText(FileOpenSelector selector)
|
|
392 |
| throws IOException, OperationCanceledException { |
|
393 |
4
| File[] files = selector.getFiles();
|
|
394 |
0
| if (files == null) throw new IOException("No Files returned from FileSelector");
|
|
395 |
| |
|
396 |
4
| ArrayList<String> histories = new ArrayList<String>();
|
|
397 |
4
| ArrayList<String> strings = new ArrayList<String>();
|
|
398 |
| |
|
399 |
4
| for (File f: files) {
|
|
400 |
0
| if (f == null) throw new IOException("File name returned from FileSelector is null");
|
|
401 |
4
| try {
|
|
402 |
4
| FileInputStream fis = new FileInputStream(f);
|
|
403 |
4
| InputStreamReader isr = new InputStreamReader(fis);
|
|
404 |
4
| BufferedReader br = new BufferedReader(isr);
|
|
405 |
4
| while (true) {
|
|
406 |
16
| String line = br.readLine();
|
|
407 |
4
| if (line == null) break;
|
|
408 |
12
| strings.add(line);
|
|
409 |
| } |
|
410 |
4
| br.close();
|
|
411 |
| } |
|
412 |
0
| catch (IOException ioe) { throw new IOException("File name returned from FileSelector is null"); }
|
|
413 |
| |
|
414 |
| |
|
415 |
4
| final StringBuilder text = new StringBuilder();
|
|
416 |
4
| boolean firstLine = true;
|
|
417 |
4
| int formatVersion = 1;
|
|
418 |
4
| for (String s: strings) {
|
|
419 |
12
| int sl = s.length();
|
|
420 |
12
| if (sl > 0) {
|
|
421 |
| |
|
422 |
| |
|
423 |
2
| if (firstLine && (s.trim().equals(History.HISTORY_FORMAT_VERSION_2.trim()))) formatVersion = 2;
|
|
424 |
| |
|
425 |
12
| switch (formatVersion) {
|
|
426 |
4
| case (1):
|
|
427 |
| |
|
428 |
| |
|
429 |
4
| text.append(s);
|
|
430 |
2
| if (s.charAt(sl - 1) != ';') text.append(';');
|
|
431 |
4
| text.append(StringOps.EOL);
|
|
432 |
4
| break;
|
|
433 |
8
| case (2):
|
|
434 |
6
| if (!firstLine) text.append(s).append(StringOps.EOL);
|
|
435 |
8
| break;
|
|
436 |
| } |
|
437 |
12
| firstLine = false;
|
|
438 |
| } |
|
439 |
| } |
|
440 |
| |
|
441 |
| |
|
442 |
4
| histories.add(text.toString());
|
|
443 |
| } |
|
444 |
4
| return histories;
|
|
445 |
| } |
|
446 |
| |
|
447 |
| |
|
448 |
| |
|
449 |
1
| public InteractionsScriptModel loadHistoryAsScript(FileOpenSelector selector)
|
|
450 |
| throws IOException, OperationCanceledException { |
|
451 |
1
| ArrayList<String> histories = _getHistoryText(selector);
|
|
452 |
1
| ArrayList<String> interactions = new ArrayList<String>();
|
|
453 |
1
| for (String hist: histories) interactions.addAll(_removeSeparators(hist));
|
|
454 |
1
| return new InteractionsScriptModel(this, interactions);
|
|
455 |
| } |
|
456 |
| |
|
457 |
| |
|
458 |
| |
|
459 |
| |
|
460 |
| |
|
461 |
| |
|
462 |
| |
|
463 |
4
| protected static ArrayList<String> _removeSeparators(String text) {
|
|
464 |
4
| String sep = History.INTERACTION_SEPARATOR;
|
|
465 |
4
| int len = sep.length();
|
|
466 |
4
| ArrayList<String> interactions = new ArrayList<String>();
|
|
467 |
| |
|
468 |
| |
|
469 |
| |
|
470 |
4
| int index = text.indexOf(sep);
|
|
471 |
4
| int lastIndex = 0;
|
|
472 |
4
| while (index != -1) {
|
|
473 |
5
| interactions.add(text.substring(lastIndex, index).trim());
|
|
474 |
5
| lastIndex = index + len;
|
|
475 |
5
| index = text.indexOf(sep, lastIndex);
|
|
476 |
| } |
|
477 |
| |
|
478 |
| |
|
479 |
4
| String last = text.substring(lastIndex, text.length()).trim();
|
|
480 |
2
| if (!"".equals(last)) interactions.add(last);
|
|
481 |
4
| return interactions;
|
|
482 |
| } |
|
483 |
| |
|
484 |
| |
|
485 |
| |
|
486 |
| |
|
487 |
| |
|
488 |
182
| public int getDebugPort() throws IOException {
|
|
489 |
159
| if (!_debugPortSet) _createNewDebugPort();
|
|
490 |
182
| return _debugPort;
|
|
491 |
| } |
|
492 |
| |
|
493 |
| |
|
494 |
| |
|
495 |
| |
|
496 |
181
| protected void _createNewDebugPort() throws IOException {
|
|
497 |
| |
|
498 |
181
| try {
|
|
499 |
181
| ServerSocket socket = new ServerSocket(0);
|
|
500 |
181
| _debugPort = socket.getLocalPort();
|
|
501 |
181
| socket.close();
|
|
502 |
| } |
|
503 |
| catch (java.net.SocketException se) { |
|
504 |
| |
|
505 |
0
| _debugPort = -1;
|
|
506 |
| } |
|
507 |
181
| _debugPortSet = true;
|
|
508 |
181
| System.setProperty("drjava.debug.port", String.valueOf(_debugPort));
|
|
509 |
| } |
|
510 |
| |
|
511 |
| |
|
512 |
| |
|
513 |
| |
|
514 |
2
| public void setDebugPort(int port) {
|
|
515 |
2
| _debugPort = port;
|
|
516 |
2
| _debugPortSet = true;
|
|
517 |
| } |
|
518 |
| |
|
519 |
| private static final int DELAY_INTERVAL = 10; |
|
520 |
| private volatile int delayCount = DELAY_INTERVAL; |
|
521 |
| |
|
522 |
| |
|
523 |
| |
|
524 |
| |
|
525 |
| |
|
526 |
12
| public void replSystemOutPrint(final String s) {
|
|
527 |
12
| Utilities.invokeLater(new Runnable() {
|
|
528 |
12
| public void run() { _document.insertBeforeLastPrompt(s, InteractionsDocument.SYSTEM_OUT_STYLE); }
|
|
529 |
| }); |
|
530 |
12
| if (delayCount == 0) {
|
|
531 |
0
| scrollToCaret();
|
|
532 |
| |
|
533 |
0
| _writerDelay();
|
|
534 |
0
| delayCount = DELAY_INTERVAL;
|
|
535 |
| } |
|
536 |
12
| else delayCount--;
|
|
537 |
| } |
|
538 |
| |
|
539 |
| |
|
540 |
| |
|
541 |
| |
|
542 |
| |
|
543 |
2
| public void replSystemErrPrint(final String s) {
|
|
544 |
2
| Utilities.invokeLater(new Runnable() {
|
|
545 |
2
| public void run() { _document.insertBeforeLastPrompt(s, InteractionsDocument.SYSTEM_ERR_STYLE); }
|
|
546 |
| }); |
|
547 |
2
| if (delayCount == 0) {
|
|
548 |
0
| scrollToCaret();
|
|
549 |
| |
|
550 |
0
| _writerDelay();
|
|
551 |
0
| delayCount = DELAY_INTERVAL;
|
|
552 |
| } |
|
553 |
2
| else delayCount--;
|
|
554 |
| } |
|
555 |
| |
|
556 |
| |
|
557 |
4
| public String getConsoleInput() { return _inputListener.getConsoleInput(); }
|
|
558 |
| |
|
559 |
| |
|
560 |
| |
|
561 |
| |
|
562 |
| |
|
563 |
| |
|
564 |
| |
|
565 |
167
| public void setInputListener(InputListener listener) {
|
|
566 |
167
| if (_inputListener == NoInputListener.ONLY || Utilities.TEST_MODE) { _inputListener = listener; }
|
|
567 |
0
| else throw new IllegalStateException("Cannot change the input listener until it is released.");
|
|
568 |
| } |
|
569 |
| |
|
570 |
| |
|
571 |
| |
|
572 |
| |
|
573 |
| |
|
574 |
| |
|
575 |
| |
|
576 |
| |
|
577 |
1
| public void changeInputListener(InputListener oldListener, InputListener newListener) {
|
|
578 |
1
| if (_inputListener == oldListener) _inputListener = newListener;
|
|
579 |
| else |
|
580 |
0
| throw new IllegalArgumentException("The given old listener is not installed!");
|
|
581 |
| } |
|
582 |
| |
|
583 |
| |
|
584 |
| |
|
585 |
| |
|
586 |
44
| public void _interactionIsOver() {
|
|
587 |
44
| Utilities.invokeLater(new Runnable() {
|
|
588 |
44
| public void run() {
|
|
589 |
44
| _document.addToHistory(_toAddToHistory);
|
|
590 |
44
| _document.setInProgress(false);
|
|
591 |
44
| _document.insertPrompt();
|
|
592 |
44
| _notifyInteractionEnded();
|
|
593 |
| } |
|
594 |
| }); |
|
595 |
44
| scrollToCaret();
|
|
596 |
| } |
|
597 |
| |
|
598 |
| |
|
599 |
| protected abstract void _notifyInteractionEnded(); |
|
600 |
| |
|
601 |
| |
|
602 |
| |
|
603 |
| |
|
604 |
| |
|
605 |
73
| public void append(final String s, final String styleName) {
|
|
606 |
73
| Utilities.invokeLater(new Runnable() { public void run() { _document.append(s, styleName); } });
|
|
607 |
73
| scrollToCaret();
|
|
608 |
| } |
|
609 |
| |
|
610 |
| |
|
611 |
0
| public void _writerDelay() {
|
|
612 |
0
| synchronized(_writerLock) {
|
|
613 |
0
| try {
|
|
614 |
| |
|
615 |
0
| _writerLock.wait(_writeDelay);
|
|
616 |
| } |
|
617 |
0
| catch (EditDocumentException e) { throw new UnexpectedException(e); }
|
|
618 |
| catch (InterruptedException e) { } |
|
619 |
| } |
|
620 |
| } |
|
621 |
| |
|
622 |
| |
|
623 |
14
| public void replReturnedVoid() {
|
|
624 |
14
| _secondToLastError = _lastError;
|
|
625 |
14
| _lastError = null;
|
|
626 |
14
| _interactionIsOver();
|
|
627 |
| } |
|
628 |
| |
|
629 |
| |
|
630 |
| |
|
631 |
| |
|
632 |
| |
|
633 |
| |
|
634 |
23
| public void replReturnedResult(String result, String style) {
|
|
635 |
| |
|
636 |
23
| _secondToLastError = _lastError;
|
|
637 |
23
| _lastError = null;
|
|
638 |
23
| append(result + "\n", style);
|
|
639 |
23
| _interactionIsOver();
|
|
640 |
| } |
|
641 |
| |
|
642 |
| |
|
643 |
| |
|
644 |
| |
|
645 |
| |
|
646 |
| |
|
647 |
| |
|
648 |
| |
|
649 |
| |
|
650 |
| |
|
651 |
0
| public StackTraceElement[] replaceLLException(StackTraceElement[] sT) {
|
|
652 |
0
| return sT;
|
|
653 |
| |
|
654 |
| } |
|
655 |
| |
|
656 |
| |
|
657 |
0
| public void replThrewException(String message, StackTraceElement[] stackTrace) {
|
|
658 |
0
| stackTrace = replaceLLException(stackTrace);
|
|
659 |
0
| StringBuilder sb = new StringBuilder(message);
|
|
660 |
0
| for(StackTraceElement ste: stackTrace) {
|
|
661 |
0
| sb.append("\n\tat ");
|
|
662 |
0
| sb.append(ste);
|
|
663 |
| } |
|
664 |
0
| replThrewException(sb.toString().trim());
|
|
665 |
| } |
|
666 |
| |
|
667 |
| |
|
668 |
| |
|
669 |
1
| public void replThrewException(final String message) {
|
|
670 |
1
| if (message.endsWith("<EOF>\"")) {
|
|
671 |
0
| interactionContinues();
|
|
672 |
| } |
|
673 |
| else { |
|
674 |
1
| Utilities.invokeLater(new Runnable() {
|
|
675 |
1
| public void run() { _document.appendExceptionResult(message, InteractionsDocument.ERROR_STYLE); }
|
|
676 |
| }); |
|
677 |
1
| _secondToLastError = _lastError;
|
|
678 |
1
| _lastError = message;
|
|
679 |
1
| _interactionIsOver();
|
|
680 |
| } |
|
681 |
| } |
|
682 |
| |
|
683 |
| |
|
684 |
| |
|
685 |
| |
|
686 |
| |
|
687 |
| |
|
688 |
| |
|
689 |
| |
|
690 |
| |
|
691 |
1
| public void replReturnedSyntaxError(String errorMessage, String interaction, int startRow, int startCol,
|
|
692 |
| int endRow, int endCol ) { |
|
693 |
| |
|
694 |
| |
|
695 |
1
| _secondToLastError = _lastError;
|
|
696 |
1
| _lastError = errorMessage;
|
|
697 |
1
| if (errorMessage != null) {
|
|
698 |
1
| if (errorMessage.endsWith("<EOF>\"")) {
|
|
699 |
1
| interactionContinues();
|
|
700 |
1
| return;
|
|
701 |
| } |
|
702 |
| } |
|
703 |
| |
|
704 |
0
| Pair<Integer,Integer> oAndL =
|
|
705 |
| StringOps.getOffsetAndLength(interaction, startRow, startCol, endRow, endCol); |
|
706 |
| |
|
707 |
0
| _notifySyntaxErrorOccurred(_document.getPromptPos() + oAndL.first().intValue(),oAndL.second().intValue());
|
|
708 |
| |
|
709 |
0
| _document.appendSyntaxErrorResult(errorMessage, interaction, startRow, startCol, endRow, endCol,
|
|
710 |
| InteractionsDocument.ERROR_STYLE); |
|
711 |
| |
|
712 |
0
| _interactionIsOver();
|
|
713 |
| } |
|
714 |
| |
|
715 |
| |
|
716 |
| |
|
717 |
| |
|
718 |
1
| public void replCalledSystemExit(int status) {
|
|
719 |
| |
|
720 |
1
| _notifyInterpreterExited(status);
|
|
721 |
| } |
|
722 |
| |
|
723 |
| |
|
724 |
| |
|
725 |
| |
|
726 |
| protected abstract void _notifyInterpreterExited(int status); |
|
727 |
| |
|
728 |
| |
|
729 |
22
| public void interpreterResetting() {
|
|
730 |
22
| if (! _waitingForFirstInterpreter) {
|
|
731 |
22
| Utilities.invokeLater(new Runnable() {
|
|
732 |
22
| public void run() {
|
|
733 |
22
| _document.insertBeforeLastPrompt(" Resetting Interactions ...\n", InteractionsDocument.ERROR_STYLE);
|
|
734 |
22
| _document.setInProgress(true);
|
|
735 |
| } |
|
736 |
| }); |
|
737 |
| |
|
738 |
| |
|
739 |
22
| try { _createNewDebugPort(); }
|
|
740 |
| catch (IOException ioe) { |
|
741 |
| |
|
742 |
| } |
|
743 |
22
| _notifyInterpreterResetting();
|
|
744 |
| |
|
745 |
| } |
|
746 |
| } |
|
747 |
| |
|
748 |
| |
|
749 |
| protected abstract void _notifyInterpreterResetting(); |
|
750 |
| |
|
751 |
| |
|
752 |
| |
|
753 |
| |
|
754 |
0
| public void interpreterResetFailed(Throwable t) {
|
|
755 |
0
| _interpreterResetFailed(t);
|
|
756 |
0
| _document.setInProgress(false);
|
|
757 |
0
| _notifyInterpreterResetFailed(t);
|
|
758 |
| } |
|
759 |
| |
|
760 |
0
| public void interpreterWontStart(Exception e) {
|
|
761 |
0
| _interpreterWontStart(e);
|
|
762 |
0
| _document.setInProgress(true);
|
|
763 |
| } |
|
764 |
| |
|
765 |
| |
|
766 |
| |
|
767 |
| |
|
768 |
| protected abstract void _interpreterResetFailed(Throwable t); |
|
769 |
| |
|
770 |
| |
|
771 |
| |
|
772 |
| |
|
773 |
| protected abstract void _notifyInterpreterResetFailed(Throwable t); |
|
774 |
| |
|
775 |
| |
|
776 |
| protected abstract void _interpreterWontStart(Exception e); |
|
777 |
| |
|
778 |
0
| public String getBanner() { return _banner; }
|
|
779 |
| |
|
780 |
1
| public String getStartUpBanner() { return getBanner(_workingDirectory); }
|
|
781 |
| |
|
782 |
214
| public static String getBanner(File wd) { return BANNER_PREFIX + " Working directory is " + wd + '\n'; }
|
|
783 |
| |
|
784 |
213
| private String generateBanner(File wd) {
|
|
785 |
213
| _banner = getBanner(wd);
|
|
786 |
213
| return _banner;
|
|
787 |
| } |
|
788 |
| |
|
789 |
| |
|
790 |
| |
|
791 |
| |
|
792 |
| |
|
793 |
| |
|
794 |
| |
|
795 |
| |
|
796 |
| |
|
797 |
| |
|
798 |
| |
|
799 |
| |
|
800 |
| |
|
801 |
| |
|
802 |
| |
|
803 |
| |
|
804 |
| |
|
805 |
| |
|
806 |
118
| protected void scrollToCaret() {
|
|
807 |
118
| Utilities.invokeLater(new Runnable() {
|
|
808 |
118
| public void run() {
|
|
809 |
118
| final InteractionsPane pane = _pane;
|
|
810 |
23
| if (pane == null) return;
|
|
811 |
95
| int pos = pane.getCaretPosition();
|
|
812 |
95
| try { pane.scrollRectToVisible(pane.modelToView(pos)); }
|
|
813 |
0
| catch(BadLocationException e) { throw new UnexpectedException(e); }
|
|
814 |
| } |
|
815 |
| }); |
|
816 |
| } |
|
817 |
| |
|
818 |
| |
|
819 |
183
| public void interpreterReady(final File wd) {
|
|
820 |
183
| debug.logStart();
|
|
821 |
| |
|
822 |
| |
|
823 |
183
| if (! _waitingForFirstInterpreter) {
|
|
824 |
25
| Utilities.invokeLater(new Runnable() {
|
|
825 |
25
| public void run() {
|
|
826 |
25
| _document.reset(generateBanner(wd));
|
|
827 |
25
| _document.setInProgress(false);
|
|
828 |
25
| if (_pane != null) _pane.setCaretPosition(_document.getLength());
|
|
829 |
| |
|
830 |
25
| performDefaultImports();
|
|
831 |
| |
|
832 |
25
| _notifyInterpreterReady(wd);
|
|
833 |
| } |
|
834 |
| }); |
|
835 |
| } |
|
836 |
183
| _waitingForFirstInterpreter = false;
|
|
837 |
183
| debug.logEnd();
|
|
838 |
| } |
|
839 |
| |
|
840 |
| |
|
841 |
25
| public void performDefaultImports() {
|
|
842 |
25
| java.util.Vector<String> classes = DrJava.getConfig().getSetting(OptionConstants.INTERACTIONS_AUTO_IMPORT_CLASSES);
|
|
843 |
25
| final StringBuilder sb = new StringBuilder();
|
|
844 |
| |
|
845 |
25
| for(String s: classes) {
|
|
846 |
0
| String name = s.trim();
|
|
847 |
0
| if (s.length() > 0) {
|
|
848 |
0
| sb.append("import ");
|
|
849 |
0
| sb.append(s.trim());
|
|
850 |
0
| sb.append("; ");
|
|
851 |
| } |
|
852 |
| } |
|
853 |
25
| if (sb.length() > 0) {
|
|
854 |
0
| interpret(sb.toString());
|
|
855 |
0
| _document.insertBeforeLastPrompt("Default imports: " + sb.toString() + "\n", InteractionsDocument.DEBUGGER_STYLE);
|
|
856 |
| } |
|
857 |
| } |
|
858 |
| |
|
859 |
| |
|
860 |
| public abstract void _notifyInterpreterReady(File wd); |
|
861 |
| |
|
862 |
| |
|
863 |
| private static class NoInputListener implements InputListener { |
|
864 |
| public static final NoInputListener ONLY = new NoInputListener(); |
|
865 |
23
| private NoInputListener() { }
|
|
866 |
| |
|
867 |
1
| public String getConsoleInput() { throw new IllegalStateException("No input listener installed!"); }
|
|
868 |
| } |
|
869 |
| |
|
870 |
| |
|
871 |
| public abstract ConsoleDocument getConsoleDocument(); |
|
872 |
| |
|
873 |
| |
|
874 |
10
| public String getLastError() {
|
|
875 |
10
| return _lastError;
|
|
876 |
| } |
|
877 |
| |
|
878 |
| |
|
879 |
0
| public String getSecondToLastError() {
|
|
880 |
0
| return _secondToLastError;
|
|
881 |
| } |
|
882 |
| |
|
883 |
| |
|
884 |
0
| public void resetLastErrors() {
|
|
885 |
0
| _lastError = _secondToLastError = null;
|
|
886 |
| } |
|
887 |
| |
|
888 |
| |
|
889 |
0
| public String removeLastFromHistory() {
|
|
890 |
0
| return _document.removeLastFromHistory();
|
|
891 |
| } |
|
892 |
| } |