001: /*
002: * $Id: JGraphpadConsole.java,v 1.1.1.1 2005/08/04 11:21:58 gaudenz Exp $
003: * Copyright (c) 2001-2005, Gaudenz Alder
004: *
005: * All rights reserved.
006: *
007: * See LICENSE file for license details. If you are unable to locate
008: * this file please contact info (at) jgraph (dot) com.
009: */
010: package com.jgraph.pad.factory;
011:
012: import java.awt.Component;
013: import java.io.ByteArrayOutputStream;
014: import java.io.PrintStream;
015:
016: import javax.swing.JTextArea;
017:
018: import org.w3c.dom.Node;
019:
020: import com.jgraph.editor.JGraphEditorResources;
021: import com.jgraph.editor.factory.JGraphEditorFactoryMethod;
022:
023: /**
024: * Console to display a print stream.
025: */
026: public class JGraphpadConsole extends JTextArea {
027:
028: /**
029: * Holds the print stream that acts as a data source.
030: */
031: protected PrintStream stream;
032:
033: /**
034: * Constructs a console for the specified stream. If stream is one of
035: * System.out or System.err then the respective stream will be redirected to
036: * the constructed console.
037: *
038: * @param stream
039: * The stream that is to be displayed.
040: */
041: public JGraphpadConsole(PrintStream stream) {
042: this .stream = stream;
043: PrintStream textStream = new JTextAreaOutputStream(this ,
044: stream, true);
045: try {
046: if (stream == System.out)
047: System.setOut(textStream);
048: else if (stream == System.err)
049: System.setErr(textStream);
050: } catch (SecurityException e) {
051: textStream.println(JGraphEditorResources
052: .getString("SecurityRestrictions"));
053: }
054: }
055:
056: /**
057: * @return Returns the stream.
058: */
059: public PrintStream getStream() {
060: return stream;
061: }
062:
063: /**
064: * @param stream
065: * The stream to set.
066: */
067: public void setStream(PrintStream stream) {
068: this .stream = stream;
069: }
070:
071: /**
072: * A PrintStream for the text area output.
073: */
074: class JTextAreaOutputStream extends PrintStream {
075:
076: /**
077: * The target for this printstream.
078: */
079: private JTextArea target = null;
080:
081: /**
082: * The original PrintStream to forward this stream to the original
083: * stream.
084: */
085: private PrintStream orig = null;
086:
087: /**
088: * Flag is true if the stream should forward the output to the original
089: * stream.
090: */
091: private boolean showOrig = false;
092:
093: /**
094: * Constructs a new output stream for the specified stream and text
095: * area.
096: */
097: public JTextAreaOutputStream(JTextArea t, PrintStream orig,
098: boolean showOrig) {
099: super (new ByteArrayOutputStream());
100: target = t;
101:
102: this .showOrig = showOrig;
103: this .orig = orig;
104: }
105:
106: /**
107: * Writes the value to the target
108: */
109: public void print(boolean b) {
110: if (showOrig)
111: orig.print(b);
112: if (b)
113: target.append("true");
114: else
115: target.append("false");
116: target.setCaretPosition(target.getText().length());
117: }
118:
119: /**
120: * Writes the value to the target
121: */
122: public void println(boolean b) {
123: if (showOrig)
124: orig.println(b);
125:
126: if (b)
127: target.append("true\n");
128: else
129: target.append("false\n");
130: target.setCaretPosition(target.getText().length());
131: }
132:
133: /**
134: * Writes the value to the target
135: */
136: public void print(char c) {
137: if (showOrig)
138: orig.print(c);
139:
140: char[] tmp = new char[1];
141: tmp[0] = c;
142: target.append(new String(tmp));
143: target.setCaretPosition(target.getText().length());
144: }
145:
146: /**
147: * Writes the value to the target
148: */
149: public void println(char c) {
150: if (showOrig)
151: orig.println(c);
152:
153: char[] tmp = new char[2];
154: tmp[0] = c;
155: tmp[1] = '\n';
156: target.append(new String(tmp));
157: target.setCaretPosition(target.getText().length());
158: }
159:
160: /**
161: * Writes the value to the target
162: */
163: public void print(char[] s) {
164: if (showOrig)
165: orig.print(s);
166:
167: target.append(new String(s));
168: target.setCaretPosition(target.getText().length());
169: }
170:
171: /**
172: * Writes the value to the target
173: */
174: public void println(char[] s) {
175: if (showOrig)
176: orig.println(s);
177:
178: target.append(new String(s) + "\n");
179: target.setCaretPosition(target.getText().length());
180: }
181:
182: /**
183: * Writes the value to the target
184: */
185: public void print(double d) {
186: if (showOrig)
187: orig.print(d);
188:
189: target.append(Double.toString(d));
190: target.setCaretPosition(target.getText().length());
191: }
192:
193: /**
194: * Writes the value to the target
195: */
196: public void println(double d) {
197: if (showOrig)
198: orig.println(d);
199:
200: target.append(Double.toString(d) + "\n");
201: target.setCaretPosition(target.getText().length());
202: }
203:
204: /**
205: * Writes the value to the target
206: */
207: public void print(float f) {
208: if (showOrig)
209: orig.print(f);
210:
211: target.append(Float.toString(f));
212: target.setCaretPosition(target.getText().length());
213: }
214:
215: /**
216: * Writes the value to the target
217: */
218: public void println(float f) {
219: if (showOrig)
220: orig.println(f);
221:
222: target.append(Float.toString(f) + "\n");
223: target.setCaretPosition(target.getText().length());
224: }
225:
226: /**
227: * Writes the value to the target
228: */
229: public void print(int i) {
230: if (showOrig)
231: orig.print(i);
232:
233: target.append(Integer.toString(i));
234: target.setCaretPosition(target.getText().length());
235: }
236:
237: /**
238: * Writes the value to the target
239: */
240: public void println(int i) {
241: if (showOrig)
242: orig.println(i);
243:
244: target.append(Integer.toString(i) + "\n");
245: target.setCaretPosition(target.getText().length());
246: }
247:
248: /**
249: * Writes the value to the target
250: */
251: public void print(long l) {
252: if (showOrig)
253: orig.print(l);
254:
255: target.append(Long.toString(l));
256: target.setCaretPosition(target.getText().length());
257: }
258:
259: /**
260: * Writes the value to the target
261: */
262: public void println(long l) {
263: if (showOrig)
264: orig.println(l);
265:
266: target.append(Long.toString(l) + "\n");
267: target.setCaretPosition(target.getText().length());
268: }
269:
270: /**
271: * Writes the value to the target
272: */
273: public void print(Object o) {
274: if (showOrig)
275: orig.print(o);
276:
277: target.append(o.toString());
278: target.setCaretPosition(target.getText().length());
279: }
280:
281: /**
282: * Writes the value to the target
283: */
284: public void println(Object o) {
285: if (showOrig)
286: orig.println(o);
287:
288: target.append(o.toString() + "\n");
289: target.setCaretPosition(target.getText().length());
290: }
291:
292: /**
293: * Writes the value to the target
294: */
295: public void print(String s) {
296: if (showOrig)
297: orig.print(s);
298:
299: target.append(s);
300: target.setCaretPosition(target.getText().length());
301: }
302:
303: /**
304: * Writes the value to the target
305: */
306: public void println(String s) {
307: if (showOrig)
308: orig.println(s);
309:
310: target.append(s + "\n");
311: target.setCaretPosition(target.getText().length());
312: }
313:
314: /**
315: * Writes the value to the target
316: */
317: public void println() {
318: if (showOrig)
319: orig.println();
320:
321: target.append(new String("\n"));
322: target.setCaretPosition(target.getText().length());
323: }
324: }
325:
326: /**
327: * Provides a factory method to construct a console for System.err.
328: */
329: public static class FactoryMethod extends JGraphEditorFactoryMethod {
330:
331: /**
332: * Defines the default name for factory methods of this kind.
333: */
334: public static String NAME = "createConsole";
335:
336: /**
337: * Constructs a new console factory method using {@link #NAME}.
338: */
339: public FactoryMethod() {
340: super (NAME);
341: }
342:
343: /*
344: * (non-Javadoc)
345: */
346: public Component createInstance(Node configuration) {
347: JGraphpadConsole console = new JGraphpadConsole(System.err);
348: console.setEditable(false);
349: return console;
350: }
351:
352: }
353:
354: }
|