1 package org.mythsim.swing;
2
3
4 import java.io.*;
5 import java.awt.*;
6 import java.awt.event.*;
7 import javax.swing.*;
8
9 /***
10 *
11 * A simple Java Console for your application (Swing version)
12 * Requires Java 1.1.5 or higher
13 * Disclaimer the use of this source is at your own risk.
14 * Permision to use and distribute into your own applications
15 * RJHM van den Bergh , rvdb@comweb.nl
16 * http://alpha.comweb.nl/java/Console/Console.html
17 * Modified to work with JTextPanel
18 * @author RJHM van den Bergh
19 * @author Jason Vroustouris
20 */
21 public class Console
22 extends WindowAdapter
23 implements WindowListener, ActionListener, Runnable {
24 private MainJFrame frame;
25 private JTextPane textArea;
26 private Thread reader;
27 private Thread reader2;
28 private boolean quit;
29
30 private final PipedInputStream pin = new PipedInputStream();
31 private final PipedInputStream pin2 = new PipedInputStream();
32
33 Thread errorThrower; // just for testing (Throws an Exception at this Console
34
35 public Console(MainJFrame f) {
36 // create all components and add them
37 frame = f;
38 //Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
39 //Dimension frameSize = new Dimension( (int) (screenSize.width / 2),
40 // (int) (screenSize.height / 2));
41 //int x = (int) (frameSize.width / 2);
42 //int y = (int) (frameSize.height / 2);
43 //frame.setBounds(x, y, frameSize.width, frameSize.height);
44
45 // Size Frame
46 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
47 Dimension frameSize = new Dimension( (int) (530), (int) (220));
48 int x = (int) (screenSize.width / 2 - frameSize.width / 2);
49 int y = (int) (screenSize.height / 2 - frameSize.height / 2);
50 frame.setBounds(x, y, frameSize.width, frameSize.height);
51
52 textArea = frame.getJTextPane();
53 textArea.setEditable(false);
54 JButton button = new JButton("clear");
55
56 //frame.getContentPane().setLayout(new BorderLayout());
57 //frame.getContentPane().add(new JScrollPane(textArea),BorderLayout.CENTER);
58 //frame.getContentPane().add(button,BorderLayout.SOUTH);
59 frame.setVisible(true);
60
61 frame.addWindowListener(this);
62 button.addActionListener(this);
63
64 try {
65 PipedOutputStream pout = new PipedOutputStream(this.pin);
66 System.setOut(new PrintStream(pout, true));
67 }
68 catch (java.io.IOException io) {
69 frame.appendToLog("Couldn't redirect STDOUT to this console\n" +
70 io.getMessage());
71 }
72 catch (SecurityException se) {
73 frame.appendToLog("Couldn't redirect STDOUT to this console\n" +
74 se.getMessage());
75 }
76
77 try {
78 PipedOutputStream pout2 = new PipedOutputStream(this.pin2);
79 System.setErr(new PrintStream(pout2, true));
80 }
81 catch (java.io.IOException io) {
82 frame.appendToLog("Couldn't redirect STDERR to this console\n" +
83 io.getMessage());
84 }
85 catch (SecurityException se) {
86 frame.appendToLog("Couldn't redirect STDERR to this console\n" +
87 se.getMessage());
88 }
89
90 quit = false; // signals the Threads that they should exit
91
92 // Starting two seperate threads to read from the PipedInputStreams
93 //
94 reader = new Thread(this);
95 reader.setDaemon(true);
96 reader.start();
97 //
98 reader2 = new Thread(this);
99 reader2.setDaemon(true);
100 reader2.start();
101 }
102
103 public synchronized void windowClosed(WindowEvent evt) {
104 quit = true;
105 this.notifyAll(); // stop all threads
106 try {
107 reader.join(1000);
108 pin.close();
109 }
110 catch (Exception e) {}
111 try {
112 reader2.join(1000);
113 pin2.close();
114 }
115 catch (Exception e) {}
116 System.exit(0);
117 }
118
119 public synchronized void windowClosing(WindowEvent evt) {
120 frame.setVisible(false); // default behaviour of JFrame
121 frame.dispose();
122 }
123
124 public synchronized void actionPerformed(ActionEvent evt) {
125 textArea.setText("");
126 }
127
128 public synchronized void run() {
129 try {
130 while (Thread.currentThread() == reader) {
131 try {
132 this.wait(100);
133 }
134 catch (InterruptedException ie) {}
135 if (pin.available() != 0) {
136 String input = this.readLine(pin);
137 frame.appendToLog(input);
138 }
139 if (quit) {
140 return;
141 }
142 }
143
144 while (Thread.currentThread() == reader2) {
145 try {
146 this.wait(100);
147 }
148 catch (InterruptedException ie) {}
149 if (pin2.available() != 0) {
150 String input = this.readLine(pin2);
151 frame.appendToLog(input);
152 }
153 if (quit) {
154 return;
155 }
156 }
157 }
158 catch (Exception e) {
159 frame.appendToLog("\nConsole reports an Internal error.");
160 frame.appendToLog("The error is: " + e);
161 }
162
163 // just for testing (Throw a Nullpointer after 1 second)
164 if (Thread.currentThread() == errorThrower) {
165 try {
166 this.wait(1000);
167 }
168 catch (InterruptedException ie) {}
169 throw new NullPointerException("Application test: throwing an NullPointerException It should arrive at the console");
170 }
171
172 }
173
174 public synchronized String readLine(PipedInputStream in) throws IOException {
175 String input = "";
176 do {
177 int available = in.available();
178 if (available == 0) {
179 break;
180 }
181 byte b[] = new byte[available];
182 in.read(b);
183 input = input + new String(b, 0, b.length);
184 }
185 while (!input.endsWith("\n") && !input.endsWith("\r\n") && !quit);
186 return input;
187 }
188
189 public static void main(String[] arg) {
190 }
191 }
This page was automatically generated by Maven