1 33 34 package edu.rice.cs.drjava.model.repl; 35 36 import java.io.*; 37 import java.awt.print.*; 38 39 import edu.rice.cs.drjava.model.print.DrJavaBook; 40 41 import edu.rice.cs.drjava.model.FileSaveSelector; 42 import edu.rice.cs.util.UnexpectedException; 43 import edu.rice.cs.util.text.EditDocumentInterface; 44 import edu.rice.cs.util.text.EditDocumentException; 45 import edu.rice.cs.util.text.ConsoleDocument; 46 import edu.rice.cs.drjava.config.OptionListener; 47 48 51 public class InteractionsDocument extends ConsoleDocument { 52 53 54 public static final String DEFAULT_PROMPT = "> "; 55 56 57 public static final String ERROR_STYLE = "error"; 58 59 60 public static final String DEBUGGER_STYLE = "debugger"; 61 62 public static final String OBJECT_RETURN_STYLE = "object.return.style"; 63 64 public static final String STRING_RETURN_STYLE = "string.return.style"; 65 66 public static final String CHARACTER_RETURN_STYLE = "character.return.style"; 67 68 public static final String NUMBER_RETURN_STYLE = "number.return.style"; 69 70 71 private final History _history; 72 73 74 75 78 public InteractionsDocument(EditDocumentInterface document, String banner) { this(document, new History(), banner); } 79 80 85 public InteractionsDocument(EditDocumentInterface document, int maxHistorySize, String banner) { 86 this(document, new History(maxHistorySize), banner); 87 } 88 89 93 public InteractionsDocument(EditDocumentInterface document, History history, String banner) { 94 super(document); 95 _history = history; 96 _hasPrompt = true; 97 _prompt = DEFAULT_PROMPT; 98 reset(banner); 99 } 100 101 104 public void setInProgress(boolean inProgress) { 105 acquireWriteLock(); 106 _hasPrompt = ! inProgress; 107 releaseWriteLock(); 108 } 109 110 111 public boolean inProgress() { return ! _hasPrompt; } 112 113 114 public void reset(String banner) { 115 acquireWriteLock(); 116 try { 117 forceRemoveText(0, _document.getLength()); 118 forceInsertText(0, banner, OBJECT_RETURN_STYLE); 119 insertPrompt(); 120 _history.moveEnd(); 121 setInProgress(false); 122 } 123 catch (EditDocumentException e) { throw new UnexpectedException(e); } 124 finally { releaseWriteLock(); } 125 } 126 127 128 private void _replaceCurrentLineFromHistory() { 129 try { 130 _clearCurrentInputText(); 131 append(_history.getCurrent(), DEFAULT_STYLE); 132 } 133 catch (EditDocumentException ble) { throw new UnexpectedException(ble); } 134 } 135 136 137 public OptionListener<Integer > getHistoryOptionListener() { return _history.getHistoryOptionListener(); } 138 139 140 public void addToHistory(String text) { 141 acquireWriteLock(); 142 try { _history.add(text); } 143 finally { releaseWriteLock(); } 144 } 145 146 149 public void saveHistory(FileSaveSelector selector) throws IOException { 150 acquireReadLock(); try { _history.writeToFile(selector); } 152 finally { releaseReadLock(); } 153 } 154 155 162 public void saveHistory(FileSaveSelector selector, String editedVersion) throws IOException { 163 acquireReadLock(); try { _history.writeToFile(selector, editedVersion); } 165 finally { releaseReadLock(); } 166 } 167 168 171 public String getHistoryAsStringWithSemicolons() { 172 acquireReadLock(); 173 try { return _history.getHistoryAsStringWithSemicolons(); } 174 finally { releaseReadLock(); } 175 } 176 177 178 public String getHistoryAsString() { 179 acquireReadLock(); 180 try { return _history.getHistoryAsString(); } 181 finally { releaseReadLock(); } 182 } 183 184 185 public void clearHistory() { 186 acquireWriteLock(); 187 try { _history.clear(); } 188 finally { releaseWriteLock(); } 189 } 190 191 public String lastEntry() { 192 acquireReadLock(); 193 try { return _history.lastEntry(); } finally { releaseReadLock(); } 195 } 196 199 public void moveHistoryPrevious(String entry) { 200 acquireWriteLock(); 201 try { 202 _history.movePrevious(entry); 203 _replaceCurrentLineFromHistory(); 204 } 205 finally { releaseWriteLock(); } 206 } 207 208 211 public void moveHistoryNext(String entry) { 212 acquireWriteLock(); 213 try { 214 _history.moveNext(entry); 215 _replaceCurrentLineFromHistory(); 216 } 217 finally { releaseWriteLock(); } 218 } 219 220 221 public boolean hasHistoryPrevious() { 222 acquireReadLock(); 223 try { return _history.hasPrevious(); } 224 finally { releaseReadLock(); } 225 } 226 227 228 public boolean hasHistoryNext() { 229 acquireReadLock(); 230 try { return _history.hasNext(); } 231 finally { releaseReadLock(); } 232 } 233 234 237 public void reverseHistorySearch(String searchString) { 238 acquireWriteLock(); 239 try { 240 _history.reverseSearch(searchString); 241 _replaceCurrentLineFromHistory(); 242 } 243 finally { releaseWriteLock(); } 244 } 245 246 249 public void forwardHistorySearch(String searchString) { 250 acquireWriteLock(); 251 try { 252 _history.forwardSearch(searchString); 253 _replaceCurrentLineFromHistory(); 254 } 255 finally { releaseWriteLock(); } 256 } 257 258 261 public boolean recallPreviousInteractionInHistory() { 262 acquireWriteLock(); 263 try { 264 if (hasHistoryPrevious()) { 265 moveHistoryPrevious(getCurrentInteraction()); 266 return true; 267 } 268 _beep.run(); 269 return false; 270 } 271 finally { releaseWriteLock(); } 272 } 273 274 277 public boolean recallNextInteractionInHistory() { 278 acquireWriteLock(); 279 try { 280 if (hasHistoryNext()) { 281 moveHistoryNext(getCurrentInteraction()); 282 return true; 283 } 284 _beep.run(); 285 return false; 286 } 287 finally { releaseWriteLock(); } 288 } 289 290 291 292 public void reverseSearchInteractionsInHistory() { 293 acquireWriteLock(); 294 try { 295 if (hasHistoryPrevious()) reverseHistorySearch(getCurrentInteraction()); 296 else _beep.run(); 297 } 298 finally { releaseWriteLock(); } 299 } 300 301 302 public void forwardSearchInteractionsInHistory() { 303 acquireWriteLock(); 304 try { 305 if (hasHistoryNext()) forwardHistorySearch(getCurrentInteraction()); 306 else _beep.run(); 307 } 308 finally { releaseWriteLock(); } 309 } 310 311 317 public void appendExceptionResult(String exceptionClass, String message, String stackTrace, String styleName) { 318 if ((message!=null) && (message.equals("Connection refused to host: 127.0.0.1; nested exception is: \n" + 321 "\tjava.net.ConnectException: Connection refused: connect"))) return; 322 323 if (null == message || "null".equals(message)) message = ""; 324 325 if ("koala.dynamicjava.interpreter.error.ExecutionError".equals(exceptionClass) || 327 "edu.rice.cs.drjava.model.repl.InteractionsException".equals(exceptionClass)) { 328 exceptionClass = "Error"; 329 } 330 331 335 String c = exceptionClass; 336 if (c.indexOf('.') != -1) c = c.substring(c.lastIndexOf('.') + 1, c.length()); 337 338 acquireWriteLock(); 339 try { 340 append(c + ": " + message + "\n", styleName); 341 342 if (! stackTrace.trim().equals("")) { 348 BufferedReader reader = new BufferedReader(new StringReader(stackTrace)); 349 350 String line; 351 while ((line = reader.readLine()) != null) { 354 String fileName; 355 int lineNumber; 356 357 int openLoc = line.indexOf('('); 359 if (openLoc != -1) { 360 int closeLoc = line.indexOf(')', openLoc + 1); 361 362 if (closeLoc != -1) { 363 int colonLoc = line.indexOf(':', openLoc + 1); 364 if ((colonLoc > openLoc) && (colonLoc < closeLoc)) { 365 String lineNumStr = line.substring(colonLoc + 1, closeLoc); 367 try { 368 lineNumber = Integer.parseInt(lineNumStr); 369 fileName = line.substring(openLoc + 1, colonLoc); 370 } 371 catch (NumberFormatException nfe) { 372 } 374 } 375 } 376 } 377 378 append(line, styleName); 379 380 append("\n", styleName); 382 383 } } 385 } 386 catch (IOException ioe) { throw new UnexpectedException(ioe); } 387 catch (EditDocumentException ble) { throw new UnexpectedException(ble); } 388 finally { releaseWriteLock(); } 389 } 390 391 public void appendSyntaxErrorResult(String message, String interaction, int startRow, int startCol, 392 int endRow, int endCol, String styleName) { 393 try { 394 if (null == message || "null".equals(message)) message = ""; 395 396 if (message.indexOf("Lexical error") != -1) { 397 int i = message.lastIndexOf(':'); 398 if (i != -1) message = "Syntax Error:" + message.substring(i+2, message.length()); 399 } 400 401 if (message.indexOf("Error") == -1) message = "Error: " + message; 402 403 append(message + "\n" , styleName); 404 } 405 catch (EditDocumentException ble) { throw new UnexpectedException(ble); } 406 } 407 408 409 public void clearCurrentInteraction() { 410 acquireWriteLock(); 411 try { 412 super.clearCurrentInput(); 413 _history.moveEnd(); 414 } 415 finally { releaseWriteLock(); } 416 } 417 418 419 public String getCurrentInteraction() { return getCurrentInput(); } 420 421 public String getDefaultStyle() { return InteractionsDocument.DEFAULT_STYLE; } 422 423 424 public void preparePrintJob() { 425 _book = new DrJavaBook(getDocText(0, getLength()), "Interactions", new PageFormat()); 426 } 427 428 429 protected History getHistory() { return _history; } 430 } 431 | Popular Tags |