KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ch > ethz > prose > tools > ConsoleDocumentModel


1 // $Id: ConsoleDocumentModel.java,v 1.1.1.1 2003/07/02 15:30:52 apopovic Exp $
2
// =====================================================================
3
//
4
// (history at end)
5
//
6

7 package ch.ethz.prose.tools;
8
9 // used packages
10
import java.util.*;
11 import java.io.*;
12 import javax.swing.*;
13 import javax.swing.text.*;
14 import java.awt.*;
15
16 /**
17  * Class ConsoleDocumentModel XXX
18  *
19  * @version $Revision: 1.1.1.1 $
20  * @author Andrei Popovici
21  */

22 public
23 class ConsoleDocumentModel extends DefaultStyledDocument {
24
25
26   private Process JavaDoc 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 JavaDoc 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 JavaDoc s)
70     {
71       try { insertString(getLength(),s,null); }
72       catch (BadLocationException e1) {}
73     }
74
75   static class ReaderThread extends Thread JavaDoc
76   {
77     BufferedReader toRead;
78 // InputStream toRead;
79
Process JavaDoc 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 JavaDoc 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 JavaDoc 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 //======================================================================
138
//
139
// $Log: ConsoleDocumentModel.java,v $
140
// Revision 1.1.1.1 2003/07/02 15:30:52 apopovic
141
// Imported from ETH Zurich
142
//
143
// Revision 1.1 2003/05/25 13:25:19 popovici
144
// Refactoring
145
// inf.iks.tools is now prose.tools
146
// Stupid 'MyTableModel' renamed to 'WorksheetClientMode'
147
// - other renamings from bad names to reasonable names
148
//
149
// Revision 1.1 2003/05/25 12:57:05 popovici
150
// Initial revision
151
//
152
Popular Tags