Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: a5aa1b0f567abc97cd3d640691a7d4c5b0b95e06 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*******************************************************************************
 * Copyright (c) 2000, 2005 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.ui.console;

import java.io.IOException;
import java.io.OutputStream;

import org.eclipse.swt.graphics.Color;
import org.eclipse.ui.WorkbenchEncoding;
import org.eclipse.ui.internal.console.IOConsolePartitioner;

/**
 * OutputStream used to write to an IOConsole.
 * <p>
 * Clients are not intended to instantiate this class directly, instead
 * use <code>IOConsole.newOutputStream()</code>.
 * </p>
 * @since 3.1
 */
public class IOConsoleOutputStream extends OutputStream {
    /**
     * Flag indicating whether this stream has been closed.
     */
    private boolean closed = false;

    /**
     * The console's document partitioner.
     */
    private IOConsolePartitioner partitioner;
    
    /**
     * The console this stream is attached to.
     */
    private IOConsole console;
    
    /**
     * Flag indicating that the console should be activated when data
     * is written to this stream.
     */
    private boolean activateOnWrite = false;
    
    /**
     * The color used to decorate data written to this stream.
     */
    private Color color;
    
    /**
     * The font style used to decorate data written to this stream.
     */
    private int fontStyle;

    private String fEncoding;
    private String fDefaultEncoding = WorkbenchEncoding.getWorkbenchDefaultEncoding();
    
    /**
     * Constructs a new output stream on the given console.
     * 
     * @param console I/O console
     */
    IOConsoleOutputStream(IOConsole console) {
        this.console = console;
        this.partitioner = (IOConsolePartitioner) console.getPartitioner();
    }

    /**
     * Returns the font style used to decorate data written to this stream.
     * 
     * @return the font style used to decorate data written to this stream
     */
    public int getFontStyle() {
        return fontStyle;
    }
    
    /**
     * Sets the font style to be used to decorate data written to this stream.
     * 
     * @param newFontStyle the font style to be used to decorate data written to this stream
     */
    public void setFontStyle(int newFontStyle) {
        if (newFontStyle != fontStyle) {
            int old = fontStyle;
            fontStyle = newFontStyle;
            console.firePropertyChange(this, IConsoleConstants.P_FONT_STYLE, new Integer(old), new Integer(fontStyle));
        }
    }
    
    /**
     * Returns whether the console this stream is writing to will be activated when this stream
     * is written to.
     * 
     * @return whether the console this stream is writing to will be activated when this stream
     * is written to.
     */
    public boolean isActivateOnWrite() {
        return activateOnWrite;
    }

    /**
     * Sets whether to activate the console this stream is writing to when this stream
     * is written to.
     * 
     * @param activateOnWrite whether the console this stream is writing to will be activated when this stream
     * is written to.
     */
    public void setActivateOnWrite(boolean activateOnWrite) {
        this.activateOnWrite = activateOnWrite;
    }
    
	/**
	 * Sets the color of this stream. Use <code>null</code> to indicate
     * the default color.
	 * 
	 * @param color color of this stream, or <code>null</code>
	 */
	public void setColor(Color newColor) {
		Color old = color;
		if (old == null || !old.equals(newColor)) {
		    color = newColor;
		    console.firePropertyChange(this, IConsoleConstants.P_STREAM_COLOR, old, newColor);
		}
	}
	
	/**
	 * Returns the color of this stream, or <code>null</code>
	 * if default.
	 * 
	 * @return the color of this stream, or <code>null</code>
	 */
	public Color getColor() {
	    return color;
	}
	
	/*
	 *  (non-Javadoc)
	 * @see java.io.OutputStream#close()
	 */
    public synchronized void close() throws IOException {
        if(closed) {
            throw new IOException("Output Stream is closed"); //$NON-NLS-1$
        }
        console.streamClosed(this);
        closed = true;
        partitioner = null;
    }

    /*
     *  (non-Javadoc)
     * @see java.io.OutputStream#flush()
     */
    public void flush() throws IOException {
        if(closed) {
            throw new IOException("Output Stream is closed"); //$NON-NLS-1$
        }
    }
    
    /*
     *  (non-Javadoc)
     * @see java.io.OutputStream#write(byte[], int, int)
     */
    public void write(byte[] b, int off, int len) throws IOException {
        if (fEncoding!=null && !fEncoding.equals(fDefaultEncoding)) {
            encodedWrite(new String(b, off, len, fEncoding));
        } else {
            encodedWrite(new String(b, off, len));
        }
    }
    /*
     *  (non-Javadoc)
     * @see java.io.OutputStream#write(byte[])
     */
    public void write(byte[] b) throws IOException {
        write(b, 0, b.length);
    }
    /*
     *  (non-Javadoc)
     * @see java.io.OutputStream#write(int)
     */
    public void write(int b) throws IOException {
        write(new byte[] {(byte)b}, 0, 1);
    }    
    
    /**
     * Writes a string to the attached console.
     * 
     * @param str the string to write to the attached console.
     * @throws IOException if the stream is closed.
     */
    public synchronized void write(String str) throws IOException {
        if (fEncoding!=null && !fEncoding.equals(fDefaultEncoding)) {
	        byte[] defaultBytes = str.getBytes();
	        str = new String(defaultBytes, fEncoding);
        }
        encodedWrite(str);
    }
    
    private void encodedWrite(String encodedString) throws IOException {
        if(closed) {
            throw new IOException("Output Stream is closed"); //$NON-NLS-1$
        }
        partitioner.streamAppended(this, encodedString);
        if (activateOnWrite) {
        	console.activate();
        } else {
        	ConsolePlugin.getDefault().getConsoleManager().warnOfContentChange(console);
        }
    }

    /**
     * Sets the character encoding used to interpret characters written to this steam. 
     * 
     * @param encoding endcoding identifier
     */
    public void setEncoding(String encoding) {
        fEncoding = encoding;
    }
}

Back to the top