1 33 34 package edu.rice.cs.util.text; 35 36 import java.awt.print.*; 37 38 import edu.rice.cs.drjava.model.print.DrJavaBook; 39 40 import edu.rice.cs.util.UnexpectedException; 41 import edu.rice.cs.util.text.EditDocumentInterface; 42 import edu.rice.cs.util.text.DocumentEditCondition; 43 import edu.rice.cs.util.text.EditDocumentException; 44 45 46 public class ConsoleDocument implements EditDocumentInterface { 47 48 49 public static final String DEFAULT_STYLE = "default"; 50 51 52 public static final String SYSTEM_OUT_STYLE = "System.out"; 53 54 55 public static final String SYSTEM_ERR_STYLE = "System.err"; 56 57 58 public static final String SYSTEM_IN_STYLE = "System.in"; 59 60 61 public static final String DEFAULT_CONSOLE_PROMPT = ""; 62 63 64 protected final EditDocumentInterface _document; 65 66 67 protected volatile Runnable _beep; 68 69 70 protected volatile int _promptPos; 71 72 73 protected volatile String _prompt; 74 75 76 protected volatile boolean _hasPrompt; 77 78 79 protected DrJavaBook _book; 80 81 84 public ConsoleDocument(EditDocumentInterface adapter) { 85 _document = adapter; 86 87 _beep = new Runnable () { public void run() { } }; 88 _promptPos = 0; 89 _prompt = DEFAULT_CONSOLE_PROMPT; 90 _hasPrompt = false; 91 92 _document.setEditCondition(new ConsoleEditCondition()); 94 } 95 96 97 public boolean hasPrompt() { return _hasPrompt; } 98 99 100 public String getPrompt() { return _prompt; } 101 102 105 public void setPrompt(String prompt) { 106 acquireWriteLock(); 107 _prompt = prompt; 108 releaseWriteLock(); 109 } 110 111 114 public DocumentEditCondition getEditCondition() { return _document.getEditCondition(); } 115 116 120 public void setEditCondition(DocumentEditCondition condition) { _document.setEditCondition(condition); } 121 122 123 public int getPromptPos() { return _promptPos; } 124 125 128 public void setPromptPos(int newPos) { 129 acquireReadLock(); 130 _promptPos = newPos; 131 releaseReadLock(); 132 } 133 134 137 public void setBeep(Runnable beep) { 138 acquireReadLock(); 139 _beep = beep; 140 releaseReadLock(); 141 } 142 143 144 public void reset(String banner) { 145 acquireWriteLock(); 146 try { 147 forceRemoveText(0, _document.getLength()); 148 forceInsertText(0, banner, DEFAULT_STYLE); 149 _promptPos = 0; 150 } 151 catch (EditDocumentException e) { throw new UnexpectedException(e); } 152 finally { releaseWriteLock(); } 153 } 154 155 156 public void insertPrompt() { 157 acquireWriteLock(); 158 try { 159 forceInsertText(_document.getLength(), _prompt, DEFAULT_STYLE); 163 _promptPos = _document.getLength(); 164 _hasPrompt = true; 165 166 } 167 catch (EditDocumentException e) { throw new UnexpectedException(e); } 168 finally { releaseWriteLock(); } 169 } 170 171 172 public void disablePrompt() { 173 acquireWriteLock(); 174 try { 175 _hasPrompt = false; 176 _promptPos = _document.getLength(); 177 } 178 finally { releaseWriteLock(); } 179 } 180 181 184 public void insertNewLine(int pos) { 185 acquireWriteLock(); 187 try { 188 int len = _document.getLength(); 189 if (pos > len) pos = len; 190 else if (pos < 0) pos = 0; 191 192 String newLine = System.getProperty("line.separator"); 193 insertText(pos, newLine, DEFAULT_STYLE); 194 } 195 catch (EditDocumentException e) { throw new UnexpectedException(e); } 196 finally { releaseWriteLock(); } 197 } 198 199 200 public int getPositionBeforePrompt() { 201 acquireReadLock(); 202 int len = _document.getLength(); 203 try { 204 if (_hasPrompt) { 205 int promptStart = _promptPos - _prompt.length(); 206 return (promptStart < len && promptStart >= 0) ? promptStart : len; } 208 return len; 209 } 210 finally { releaseReadLock(); } 211 } 212 213 217 public void insertBeforeLastPrompt(String text, String style) { 218 acquireWriteLock(); 219 try { 220 int pos = getPositionBeforePrompt(); 221 _promptPos += text.length(); 222 _addToStyleLists(pos, text, style); 223 _document.forceInsertText(pos, text, style); 224 } 225 catch (EditDocumentException ble) { throw new UnexpectedException(ble); } 226 finally { releaseWriteLock(); } 227 } 228 229 235 public void insertText(int offs, String str, String style) throws EditDocumentException { 236 acquireWriteLock(); 237 try { 238 if (offs < _promptPos) _beep.run(); 239 else { 240 _addToStyleLists(offs, str, style); 241 _document.insertText(offs, str, style); 242 } 243 } 244 finally { releaseWriteLock(); } 245 } 246 247 252 public void append(String str, String style) throws EditDocumentException { 253 acquireWriteLock(); 254 try { 255 int offs = _document.getLength(); 256 _addToStyleLists(offs, str, style); 257 _document.insertText(offs, str, style); 258 } 259 finally { releaseWriteLock(); } 260 } 261 262 269 public void forceInsertText(int offs, String str, String style) throws EditDocumentException { 270 acquireWriteLock(); 271 try { 272 _addToStyleLists(offs, str, style); 273 _document.forceInsertText(offs, str, style); 274 } 275 finally { releaseWriteLock(); } 276 } 277 278 private void _addToStyleLists(int offs, String str, String style) { 279 if (_document instanceof SwingDocument) 280 ((SwingDocument)_document).addColoring(offs, offs + str.length(), style); 281 } 282 287 public void removeText(int offs, int len) throws EditDocumentException { 288 acquireWriteLock(); 289 try { 290 if (offs < _promptPos) _beep.run(); 291 else _document.removeText(offs, len); 292 } 293 finally { releaseWriteLock(); } 294 } 295 296 301 public void forceRemoveText(int offs, int len) throws EditDocumentException { 302 _document.forceRemoveText(offs, len); 303 } 304 305 306 public int getLength() { return _document.getLength(); } 307 308 313 public String getDocText(int offs, int len) throws EditDocumentException { 314 return _document.getDocText(offs, len); 315 } 316 317 320 public String getText() { 321 acquireWriteLock(); 322 try { 323 return _document.getDocText(0, getLength()); 324 } 325 finally { releaseWriteLock(); } 326 } 327 328 331 public String getCurrentInput() { 332 acquireReadLock(); 333 try { 334 try { return getDocText(_promptPos, _document.getLength() - _promptPos); } 335 catch (EditDocumentException e) { throw new UnexpectedException(e); } 336 } 337 finally { releaseReadLock(); } 338 } 339 340 341 public void clearCurrentInput() { _clearCurrentInputText(); } 342 343 344 protected void _clearCurrentInputText() { 345 acquireWriteLock(); 346 try { 347 removeText(_promptPos, _document.getLength() - _promptPos); 349 } 350 catch (EditDocumentException ble) { throw new UnexpectedException(ble); } 351 finally { releaseWriteLock(); } 352 } 353 354 355 public String getDefaultStyle() { return ConsoleDocument.DEFAULT_STYLE; } 356 357 360 public Pageable getPageable() throws IllegalStateException { return _book; } 361 362 363 public void preparePrintJob() { 364 _book = new DrJavaBook(getDocText(0, getLength()), "Console", new PageFormat()); 365 } 366 367 368 public void print() throws PrinterException { 369 preparePrintJob(); 370 PrinterJob printJob = PrinterJob.getPrinterJob(); 371 printJob.setPageable(_book); 372 if (printJob.printDialog()) printJob.print(); 373 cleanUpPrintJob(); 374 } 375 376 377 public void cleanUpPrintJob() { _book = null; } 378 379 380 class ConsoleEditCondition extends DocumentEditCondition { 381 public boolean canInsertText(int offs) { return canRemoveText(offs); } 382 383 public boolean canRemoveText(int offs) { 384 if (offs < _promptPos) { 385 _beep.run(); 386 return false; 387 } 388 return true; 389 } 390 } 391 392 393 394 395 public void acquireReadLock() { _document.acquireReadLock(); } 396 397 398 public void releaseReadLock() { _document.releaseReadLock(); } 399 400 401 public void acquireWriteLock() { _document.acquireWriteLock(); } 402 403 404 public void releaseWriteLock() { _document.releaseWriteLock(); } 405 406 } 408 | Popular Tags |