001: // THIS SOFTWARE IS PROVIDED BY SOFTARIS PTY.LTD. AND OTHER METABOSS
002: // CONTRIBUTORS ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
003: // BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
004: // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SOFTARIS PTY.LTD.
005: // OR OTHER METABOSS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
006: // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
007: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
008: // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
009: // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
010: // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
011: // EVEN IF SOFTARIS PTY.LTD. OR OTHER METABOSS CONTRIBUTORS ARE ADVISED OF THE
012: // POSSIBILITY OF SUCH DAMAGE.
013: //
014: // Copyright 2000-2005 © Softaris Pty.Ltd. All Rights Reserved.
015: package com.metaboss.applications.designstudio.systemlog;
016:
017: import java.io.ByteArrayOutputStream;
018: import java.io.PrintStream;
019:
020: import javax.swing.JEditorPane;
021:
022: /** Custom output stream. Used in conjunction with the Log panel */
023: public class LogOutputStream extends PrintStream {
024: private static final int maxLength = 8000;
025: private String mContent = "";
026: private JEditorPane mTargetViewer = null;
027: private static final String sLineSeparator = System
028: .getProperty("line.separator");
029: private static final int sLineSeparatorLength = sLineSeparator
030: .length();
031: private PrintStream mReflectorStream = null; // The stream we might need to reflect output to
032:
033: /** Default constructor with no reflector stream */
034: public LogOutputStream() {
035: super (new ByteArrayOutputStream());
036: }
037:
038: /** Constructor with reflector stream. All output to this stream will be mirrored to the reflector stream */
039: public LogOutputStream(PrintStream pReflectorStream) {
040: super (new ByteArrayOutputStream());
041: mReflectorStream = pReflectorStream;
042: }
043:
044: public void setTargetViewer(JEditorPane pViewer) {
045: mTargetViewer = pViewer;
046: mTargetViewer.setText(mContent);
047: }
048:
049: public void clearTargetViewer() {
050: mTargetViewer = null;
051: }
052:
053: public void clear() {
054: mContent = "";
055: if (mTargetViewer != null) {
056: mTargetViewer.setText(mContent);
057: mTargetViewer.paintImmediately(0, 0, 10000, 10000);
058: }
059: }
060:
061: public void write(byte[] buf, int off, int len) {
062: print(new String(buf, off, len));
063: }
064:
065: public void write(byte[] buf) {
066: print(new String(buf));
067: }
068:
069: public void print(String pString) {
070: // Output to the reflector stream if necessary
071: if (mReflectorStream != null)
072: mReflectorStream.print(pString);
073: // Mind our own business
074: mContent += pString;
075: if (mContent.length() > maxLength)
076: mContent = mContent.substring(mContent
077: .indexOf(sLineSeparator)
078: + sLineSeparatorLength);
079: if (mTargetViewer != null) {
080: mTargetViewer.setText(mContent);
081: mTargetViewer.paintImmediately(0, 0, 10000, 10000);
082: }
083: }
084:
085: public void print(Object pObject) {
086: // Output to the reflector stream if necessary
087: if (mReflectorStream != null)
088: mReflectorStream.print(pObject);
089: // Mind our own business
090: mContent += pObject.toString();
091: if (mContent.length() > maxLength)
092: mContent = mContent.substring(mContent
093: .indexOf(sLineSeparator)
094: + sLineSeparatorLength);
095: if (mTargetViewer != null) {
096: mTargetViewer.setText(mContent);
097: mTargetViewer.paintImmediately(0, 0, 10000, 10000);
098: }
099: }
100:
101: public void println() {
102: // Output to the reflector stream if necessary
103: if (mReflectorStream != null)
104: mReflectorStream.println();
105: // Mind our own business
106: mContent += sLineSeparator;
107: if (mContent.length() > maxLength)
108: mContent = mContent.substring(mContent
109: .indexOf(sLineSeparator)
110: + sLineSeparatorLength);
111: if (mTargetViewer != null) {
112: mTargetViewer.setText(mContent);
113: mTargetViewer.paintImmediately(0, 0, 10000, 10000);
114: }
115: }
116:
117: public void println(String pString) {
118: // Output to the reflector stream if necessary
119: if (mReflectorStream != null)
120: mReflectorStream.println(pString);
121: // Mind our own business
122: mContent += pString + sLineSeparator;
123: if (mContent.length() > maxLength)
124: mContent = mContent.substring(mContent
125: .indexOf(sLineSeparator)
126: + sLineSeparatorLength);
127: if (mTargetViewer != null) {
128: mTargetViewer.setText(mContent);
129: mTargetViewer.paintImmediately(0, 0, 10000, 10000);
130: }
131: }
132:
133: public void println(Object pObject) {
134: // Output to the reflector stream if necessary
135: if (mReflectorStream != null)
136: mReflectorStream.println(pObject);
137: // Mind our own business
138: mContent += pObject.toString() + sLineSeparator;
139: if (mContent.length() > maxLength)
140: mContent = mContent.substring(mContent
141: .indexOf(sLineSeparator)
142: + sLineSeparatorLength);
143: if (mTargetViewer != null) {
144: mTargetViewer.setText(mContent);
145: mTargetViewer.paintImmediately(0, 0, 10000, 10000);
146: }
147: }
148: }
|