1 7 package ch.ethz.prose.tools; 8 9 import java.util.*; 11 import java.io.*; 12 import javax.swing.*; 13 import javax.swing.text.*; 14 import java.awt.*; 15 16 22 public 23 class ConsoleDocumentModel extends DefaultStyledDocument { 24 25 26 private Process currentProcess; 27 private int maxColumn = 0; 28 ReaderThread outputReader; 29 ReaderThread errorReader; 30 int rows; 31 int columns = 80; 32 33 public int getRows() 34 { 35 return rows; 36 } 37 38 public int getColumns() 39 { 40 return columns; 41 } 42 43 protected void adjustToCurrentWidth(int crt) 44 { 45 if (crt > columns) 46 columns = crt; 47 } 48 49 public void useProcessOutput(Process p) 50 { 51 if (outputReader != null) 52 { 53 outputReader.stopReading(); 54 outputReader.stop(); 55 } 56 if (errorReader != null) 57 { 58 errorReader.stopReading(); 59 errorReader.stop(); 60 } 61 62 outputReader = new ReaderThread(this,p.getInputStream(),false); 63 errorReader = new ReaderThread(this,p.getErrorStream(),true); 64 outputReader.start(); 65 errorReader.start(); 66 67 } 68 69 public void append(String s) 70 { 71 try { insertString(getLength(),s,null); } 72 catch (BadLocationException e1) {} 73 } 74 75 static class ReaderThread extends Thread 76 { 77 BufferedReader toRead; 78 Process proc; 80 boolean isActive; 81 ConsoleDocumentModel displayArea; 82 SimpleAttributeSet attrSet; 83 ReaderThread(ConsoleDocumentModel owner, InputStream x, boolean isErrorStream) 84 { 85 this.displayArea = owner; 86 isActive=true; 87 toRead = new BufferedReader(new InputStreamReader(x)); 88 attrSet = new SimpleAttributeSet(); 89 if(isErrorStream) 90 StyleConstants.setForeground(attrSet,java.awt.Color.red); 91 else 92 StyleConstants.setForeground(attrSet,java.awt.Color.blue); 93 StyleConstants.setFontSize(attrSet,10); 94 } 95 96 97 public void stopReading() 98 { isActive = false; } 99 100 public void run() 101 { 102 try 103 { 104 try 105 { 106 while(isActive) 107 { 108 if (toRead.ready()) 109 { 110 String crtLine = toRead.readLine(); 111 displayArea.insertString(displayArea.getLength(),crtLine,attrSet); 112 displayArea.insertString(displayArea.getLength(),"\n",attrSet); 113 displayArea.rows++; 114 displayArea.adjustToCurrentWidth(crtLine.length()); 115 } 116 else 117 {try{sleep(5);} catch (InterruptedException e){};} 118 } 119 } 120 catch (IOException e) 121 { 122 e.printStackTrace(); 123 displayArea.insertString(displayArea.getLength(),"\n\n",attrSet); 124 } 125 } 126 catch (BadLocationException e1) 127 { 128 e1.printStackTrace(); 129 } 130 } 131 132 }; 133 134 } 135 136 137 | Popular Tags |