001: // $Id: ConsoleDocumentModel.java,v 1.1.1.1 2003/07/02 15:30:52 apopovic Exp $
002: // =====================================================================
003: //
004: // (history at end)
005: //
006:
007: package ch.ethz.prose.tools;
008:
009: // used packages
010: import java.io.*;
011: import javax.swing.text.*;
012:
013: /**
014: * Class ConsoleDocumentModel XXX
015: *
016: * @version $Revision: 1.1.1.1 $
017: * @author Andrei Popovici
018: */
019: public class ConsoleDocumentModel extends DefaultStyledDocument {
020:
021: private static final long serialVersionUID = 3904682656649130801L;
022: private Process currentProcess;
023: private int maxColumn = 0;
024: ReaderThread outputReader;
025: ReaderThread errorReader;
026: int rows;
027: int columns = 80;
028:
029: public int getRows() {
030: return rows;
031: }
032:
033: public int getColumns() {
034: return columns;
035: }
036:
037: protected void adjustToCurrentWidth(int crt) {
038: if (crt > columns)
039: columns = crt;
040: }
041:
042: public void useProcessOutput(Process p) {
043: if (outputReader != null) {
044: outputReader.stopReading();
045: // outputReader.stop(); // deprecated and not necessary
046: }
047: if (errorReader != null) {
048: errorReader.stopReading();
049: // errorReader.stop(); // deprecated and not necessary
050: }
051:
052: outputReader = new ReaderThread(this , p.getInputStream(), false);
053: errorReader = new ReaderThread(this , p.getErrorStream(), true);
054: outputReader.start();
055: errorReader.start();
056:
057: }
058:
059: public void append(String s) {
060: try {
061: insertString(getLength(), s, null);
062: } catch (BadLocationException e1) {
063: }
064: }
065:
066: static class ReaderThread extends Thread {
067: BufferedReader toRead;
068: // InputStream toRead;
069: Process proc;
070: boolean isActive;
071: ConsoleDocumentModel displayArea;
072: SimpleAttributeSet attrSet;
073:
074: ReaderThread(ConsoleDocumentModel owner, InputStream x,
075: boolean isErrorStream) {
076: this .displayArea = owner;
077: isActive = true;
078: toRead = new BufferedReader(new InputStreamReader(x));
079: attrSet = new SimpleAttributeSet();
080: if (isErrorStream)
081: StyleConstants.setForeground(attrSet,
082: java.awt.Color.red);
083: else
084: StyleConstants.setForeground(attrSet,
085: java.awt.Color.blue);
086: StyleConstants.setFontSize(attrSet, 10);
087: }
088:
089: public void stopReading() {
090: isActive = false;
091: }
092:
093: public void run() {
094: try {
095: try {
096: while (isActive) {
097: if (toRead.ready()) {
098: String crtLine = toRead.readLine();
099: displayArea.insertString(displayArea
100: .getLength(), crtLine, attrSet);
101: displayArea.insertString(displayArea
102: .getLength(), "\n", attrSet);
103: displayArea.rows++;
104: displayArea.adjustToCurrentWidth(crtLine
105: .length());
106: } else {
107: try {
108: sleep(5);
109: } catch (InterruptedException e) {
110: }
111: ;
112: }
113: }
114: } catch (IOException e) {
115: e.printStackTrace();
116: displayArea.insertString(displayArea.getLength(),
117: "\n\n", attrSet);
118: }
119: } catch (BadLocationException e1) {
120: e1.printStackTrace();
121: }
122: }
123:
124: };
125:
126: }
127:
128: //======================================================================
129: //
130: // $Log: ConsoleDocumentModel.java,v $
131: // Revision 1.1.1.1 2003/07/02 15:30:52 apopovic
132: // Imported from ETH Zurich
133: //
134: // Revision 1.1 2003/05/25 13:25:19 popovici
135: // Refactoring
136: // inf.iks.tools is now prose.tools
137: // Stupid 'MyTableModel' renamed to 'WorksheetClientMode'
138: // - other renamings from bad names to reasonable names
139: //
140: // Revision 1.1 2003/05/25 12:57:05 popovici
141: // Initial revision
142: //
|