KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > console > IOConsoleOutputStream


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.console;
12
13 import java.io.IOException JavaDoc;
14 import java.io.OutputStream JavaDoc;
15
16 import org.eclipse.swt.graphics.Color;
17 import org.eclipse.ui.WorkbenchEncoding;
18 import org.eclipse.ui.internal.console.IOConsolePartitioner;
19
20 /**
21  * OutputStream used to write to an IOConsole.
22  * <p>
23  * Clients are not intended to instantiate this class directly, instead
24  * use <code>IOConsole.newOutputStream()</code>. Clients are not intended
25  * to subclass this class.
26  * </p>
27  * <p>
28  * Clients should avoid writing large amounts of output to this stream in the UI
29  * thread. The console needs to process the output in the UI thread and if the client
30  * hogs the UI thread writing output to the console, the console will not be able
31  * to process the output.
32  * </p>
33  * @since 3.1
34  */

35 public class IOConsoleOutputStream extends OutputStream JavaDoc {
36     /**
37      * Flag indicating whether this stream has been closed.
38      */

39     private boolean closed = false;
40
41     /**
42      * The console's document partitioner.
43      */

44     private IOConsolePartitioner partitioner;
45     
46     /**
47      * The console this stream is attached to.
48      */

49     private IOConsole console;
50     
51     /**
52      * Flag indicating that the console should be activated when data
53      * is written to this stream.
54      */

55     private boolean activateOnWrite = false;
56     
57     /**
58      * The color used to decorate data written to this stream.
59      */

60     private Color color;
61     
62     /**
63      * The font style used to decorate data written to this stream.
64      */

65     private int fontStyle;
66
67     private String JavaDoc fEncoding;
68     private String JavaDoc fDefaultEncoding = WorkbenchEncoding.getWorkbenchDefaultEncoding();
69
70     private boolean fNeedsEncoding = false;
71
72     private boolean prependCR;
73     
74     /**
75      * Constructs a new output stream on the given console.
76      *
77      * @param console I/O console
78      */

79     IOConsoleOutputStream(IOConsole console) {
80         this.console = console;
81         this.partitioner = (IOConsolePartitioner) console.getPartitioner();
82     }
83
84     /**
85      * Returns the font style used to decorate data written to this stream.
86      *
87      * @return the font style used to decorate data written to this stream
88      */

89     public int getFontStyle() {
90         return fontStyle;
91     }
92     
93     /**
94      * Sets the font style to be used to decorate data written to this stream.
95      *
96      * @param newFontStyle the font style to be used to decorate data written to this stream
97      */

98     public void setFontStyle(int newFontStyle) {
99         if (newFontStyle != fontStyle) {
100             int old = fontStyle;
101             fontStyle = newFontStyle;
102             console.firePropertyChange(this, IConsoleConstants.P_FONT_STYLE, new Integer JavaDoc(old), new Integer JavaDoc(fontStyle));
103         }
104     }
105     
106     /**
107      * Returns whether the console this stream is writing to will be activated when this stream
108      * is written to.
109      *
110      * @return whether the console this stream is writing to will be activated when this stream
111      * is written to.
112      */

113     public boolean isActivateOnWrite() {
114         return activateOnWrite;
115     }
116
117     /**
118      * Sets whether to activate the console this stream is writing to when this stream
119      * is written to.
120      *
121      * @param activateOnWrite whether the console this stream is writing to will be activated when this stream
122      * is written to.
123      */

124     public void setActivateOnWrite(boolean activateOnWrite) {
125         this.activateOnWrite = activateOnWrite;
126     }
127     
128     /**
129      * Sets the color of this stream. Use <code>null</code> to indicate
130      * the default color.
131      *
132      * @param newColor color of this stream, or <code>null</code>
133      */

134     public void setColor(Color newColor) {
135         Color old = color;
136         if (old == null || !old.equals(newColor)) {
137             color = newColor;
138             console.firePropertyChange(this, IConsoleConstants.P_STREAM_COLOR, old, newColor);
139         }
140     }
141     
142     /**
143      * Returns the color of this stream, or <code>null</code>
144      * if default.
145      *
146      * @return the color of this stream, or <code>null</code>
147      */

148     public Color getColor() {
149         return color;
150     }
151     
152     /**
153      * Returns true if the stream has been closed
154      * @return true is the stream has been closed, false otherwise.
155      */

156     public synchronized boolean isClosed() {
157         return closed;
158     }
159     
160     /*
161      * (non-Javadoc)
162      * @see java.io.OutputStream#close()
163      */

164     public synchronized void close() throws IOException JavaDoc {
165         if(closed) {
166             throw new IOException JavaDoc("Output Stream is closed"); //$NON-NLS-1$
167
}
168         if (prependCR) { // force writing of last /r
169
prependCR = false;
170             notifyParitioner("\r"); //$NON-NLS-1$
171
}
172         console.streamClosed(this);
173         closed = true;
174         partitioner = null;
175     }
176
177     /*
178      * (non-Javadoc)
179      * @see java.io.OutputStream#flush()
180      */

181     public void flush() throws IOException JavaDoc {
182         if(closed) {
183             throw new IOException JavaDoc("Output Stream is closed"); //$NON-NLS-1$
184
}
185     }
186     
187     /*
188      * (non-Javadoc)
189      * @see java.io.OutputStream#write(byte[], int, int)
190      */

191     public void write(byte[] b, int off, int len) throws IOException JavaDoc {
192         if (fNeedsEncoding) {
193             encodedWrite(new String JavaDoc(b, off, len, fEncoding));
194         } else {
195             encodedWrite(new String JavaDoc(b, off, len));
196         }
197     }
198     /*
199      * (non-Javadoc)
200      * @see java.io.OutputStream#write(byte[])
201      */

202     public void write(byte[] b) throws IOException JavaDoc {
203         write(b, 0, b.length);
204     }
205     /*
206      * (non-Javadoc)
207      * @see java.io.OutputStream#write(int)
208      */

209     public void write(int b) throws IOException JavaDoc {
210         write(new byte[] {(byte)b}, 0, 1);
211     }
212     
213     /**
214      * Writes a string to the attached console.
215      *
216      * @param str the string to write to the attached console.
217      * @throws IOException if the stream is closed.
218      */

219     public synchronized void write(String JavaDoc str) throws IOException JavaDoc {
220         if (fNeedsEncoding) {
221             byte[] defaultBytes = str.getBytes();
222             str = new String JavaDoc(defaultBytes, fEncoding);
223         }
224         encodedWrite(str);
225     }
226     
227     private void encodedWrite(String JavaDoc encodedString) throws IOException JavaDoc {
228         if(closed) {
229             throw new IOException JavaDoc("Output Stream is closed"); //$NON-NLS-1$
230
}
231         if (prependCR){
232             encodedString="\r"+encodedString; //$NON-NLS-1$
233
prependCR=false;
234         }
235         if (encodedString.endsWith("\r")) { //$NON-NLS-1$
236
prependCR = true;
237             encodedString = new String JavaDoc(encodedString.substring(0, encodedString.length()-1));
238         }
239         notifyParitioner(encodedString);
240     }
241
242     private void notifyParitioner(String JavaDoc encodedString) throws IOException JavaDoc {
243         try {
244             partitioner.streamAppended(this, encodedString);
245
246             if (activateOnWrite) {
247                 console.activate();
248             } else {
249                 ConsolePlugin.getDefault().getConsoleManager().warnOfContentChange(console);
250             }
251         } catch (IOException JavaDoc e) {
252             if (!closed) {
253                 close();
254             }
255             throw e;
256         }
257     }
258
259     /**
260      * Sets the character encoding used to interpret characters written to this steam.
261      *
262      * @param encoding encoding identifier
263      */

264     public void setEncoding(String JavaDoc encoding) {
265         fEncoding = encoding;
266         fNeedsEncoding = (fEncoding!=null) && (!fEncoding.equals(fDefaultEncoding));
267     }
268 }
269
Popular Tags