01: /*
02: * <copyright>
03: *
04: * Copyright 1997-2004 BBNT Solutions, LLC
05: * under sponsorship of the Defense Advanced Research Projects
06: * Agency (DARPA).
07: *
08: * You can redistribute this software and/or modify it under the
09: * terms of the Cougaar Open Source License as published on the
10: * Cougaar Open Source Website (www.cougaar.org).
11: *
12: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
13: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
14: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
15: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
16: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
17: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
18: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
22: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23: *
24: * </copyright>
25: */
26:
27: package org.cougaar.tools.server.examples;
28:
29: import java.io.OutputStream;
30: import java.awt.Color;
31: import javax.swing.text.*;
32:
33: /**
34: * Adapter for a swing Document to act like an OutputStream.
35: * <p>
36: * All "write" calls simply append to the document.
37: * <p>
38: * Additionally there is simple color and attribute support,
39: * such as "append in red".
40: * <p>
41: * All uses of this adapter <b>must</b> be done within the
42: * swing thread (or use swing's "invokeLater(..)").
43: * <p>
44: * I'm surprised that something like this isn't included in
45: * the JDK. Feel free to copy this class into other
46: * non-example packages...
47: */
48: public class DocumentOutputStream extends OutputStream {
49:
50: private final Document doc;
51: private final AttributeSet atts;
52:
53: public DocumentOutputStream(Document doc) {
54: this (doc, (Color) null);
55: }
56:
57: public DocumentOutputStream(Document doc, Color color) {
58: this (doc, (new SimpleAttributeSet()));
59: if (color != null) {
60: StyleConstants.setForeground((SimpleAttributeSet) atts,
61: color);
62: }
63: }
64:
65: public DocumentOutputStream(Document doc, AttributeSet atts) {
66: this .doc = doc;
67: this .atts = atts;
68: }
69:
70: public void write(int b) {
71: append(Integer.toString(b));
72: }
73:
74: public void write(byte[] b) {
75: append(new String(b));
76: }
77:
78: public void write(byte[] b, int off, int len) {
79: append(new String(b, off, len));
80: }
81:
82: public void flush() {
83: }
84:
85: public void close() {
86: }
87:
88: private void append(String s) {
89: try {
90: doc.insertString(doc.getLength(), s, atts);
91: } catch (BadLocationException ble) {
92: // shouldn't happen -- maybe concurrent mod?
93: throw new RuntimeException(ble.getMessage());
94: }
95: }
96: }
|