Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: b07d283b6121fcce4fd2a133a8fde26773b9a9f1 (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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/*******************************************************************************
 * Copyright (c) 2000, 2017 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
 *     Remy Chi Jian Suen <remy.suen@gmail.com> - Bug 214424 IOConsole(String, String, ImageDescriptor, String, boolean) constructor is missing api javadoc
 *******************************************************************************/

package org.eclipse.ui.console;

import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.ui.WorkbenchEncoding;
import org.eclipse.ui.internal.console.IOConsolePage;
import org.eclipse.ui.internal.console.IOConsolePartitioner;
import org.eclipse.ui.part.IPageBookViewPage;

/**
 * A console that displays text from I/O streams. An I/O console can have multiple
 * output streams connected to it and provides one input stream connected to the
 * keyboard.
 * <p>
 * Clients may instantiate and subclass this class.
 * </p>
 * @since 3.1
 */
public class IOConsole extends TextConsole {
	/**
	 * The document partitioner
	 */
	private IOConsolePartitioner partitioner;

	/**
	 * The stream from which user input may be read
	 */
	private InputStream inputStream;

	/**
	 * A collection of open streams connected to this console.
	 */
	private List<Closeable> openStreams = Collections.synchronizedList(new ArrayList<Closeable>());

	/**
	 * The encoding used to for displaying console output.
	 */
	private Charset charset;


	/**
	 * Constructs a console with the given name, type, image, and lifecycle, with the
	 * workbench's default encoding.
	 *
	 * @param name name to display for this console
	 * @param consoleType console type identifier or <code>null</code>
	 * @param imageDescriptor image to display for this console or <code>null</code>
	 * @param autoLifecycle whether lifecycle methods should be called automatically
	 *  when this console is added/removed from the console manager
	 */
	public IOConsole(String name, String consoleType, ImageDescriptor imageDescriptor, boolean autoLifecycle) {
		this(name, consoleType, imageDescriptor, (String)null, autoLifecycle);
	}

	/**
	 * Constructs a console with the given name, type, image, encoding and lifecycle.
	 *
	 * @param name name to display for this console
	 * @param consoleType console type identifier or <code>null</code>
	 * @param imageDescriptor image to display for this console or <code>null</code>
	 * @param encoding the encoding that should be used to render the text, or <code>null</code>
	 * 	if the system default encoding should be used
	 * @param autoLifecycle whether lifecycle methods should be called automatically
	 *  when this console is added/removed from the console manager
	 */
	public IOConsole(String name, String consoleType, ImageDescriptor imageDescriptor, String encoding, boolean autoLifecycle) {
		this(name, consoleType, imageDescriptor,
				encoding == null
				? Charset.forName(WorkbenchEncoding.getWorkbenchDefaultEncoding())
						: Charset.forName(encoding),
						autoLifecycle);
	}

	/**
	 * Constructs a console with the given name, type, image, encoding and
	 * lifecycle.
	 *
	 * @param name name to display for this console
	 * @param consoleType console type identifier or <code>null</code>
	 * @param imageDescriptor image to display for this console or
	 *            <code>null</code>
	 * @param charset the encoding that should be used to render the text, must
	 *            not be <code>null</code>
	 * @param autoLifecycle whether lifecycle methods should be called
	 *            automatically when this console is added/removed from the
	 *            console manager
	 * @since 3.7
	 */
	public IOConsole(String name, String consoleType, ImageDescriptor imageDescriptor, Charset charset, boolean autoLifecycle) {
		super(name, consoleType, imageDescriptor, autoLifecycle);
		this.charset = charset;
		synchronized (openStreams) {
			inputStream = new IOConsoleInputStream(this);
			openStreams.add(inputStream);
		}

		if (inputStream instanceof IOConsoleInputStream) {
			partitioner = new IOConsolePartitioner((IOConsoleInputStream) inputStream, this);
			partitioner.connect(getDocument());
		}
	}

	/**
	 * Constructs a console with the given name, type, and image with the workbench's
	 * default encoding. Lifecycle methods will be called when this console is
	 * added/removed from the console manager.
	 *
	 * @param name name to display for this console
	 * @param consoleType console type identifier or <code>null</code>
	 * @param imageDescriptor image to display for this console or <code>null</code>
	 */
	public IOConsole(String name, String consoleType, ImageDescriptor imageDescriptor) {
		this(name, consoleType, imageDescriptor, true);
	}

	/**
	 * Constructs a console with the given name and image. Lifecycle methods
	 * will be called when this console is added/removed from the console manager.
	 * This console will have an unspecified (<code>null</code>) type.
	 *
	 * @param name name to display for this console
	 * @param imageDescriptor image to display for this console or <code>null</code>
	 */
	public IOConsole(String name, ImageDescriptor imageDescriptor) {
		this(name, null, imageDescriptor);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ui.console.IConsole#createPage(org.eclipse.ui.console.IConsoleView)
	 */
	@Override
	public IPageBookViewPage createPage(IConsoleView view) {
		return new IOConsolePage(this, view);
	}

	/**
	 * Creates and returns a new output stream which may be used to write to this console.
	 * A console may be connected to more than one output stream at once. Clients are
	 * responsible for closing any output streams created on this console.
	 * <p>
	 * Clients should avoid writing large amounts of output to this stream in the UI
	 * thread. The console needs to process the output in the UI thread and if the client
	 * hogs the UI thread writing output to the console, the console will not be able
	 * to process the output.
	 * </p>
	 * @return a new output stream connected to this console
	 */
	public IOConsoleOutputStream newOutputStream() {
		IOConsoleOutputStream outputStream = new IOConsoleOutputStream(this, this.charset);
		synchronized(openStreams) {
			openStreams.add(outputStream);
		}
		return outputStream;
	}

	/**
	 * Returns the input stream connected to the keyboard.
	 *
	 * @return the input stream connected to the keyboard.
	 */
	public IOConsoleInputStream getInputStream() {
		if (inputStream instanceof IOConsoleInputStream) {
			return (IOConsoleInputStream) inputStream;
		}
		return null;
	}

	/**
	 * Sets the new input stream.
	 *
	 * @param inputStream the input stream
	 * @since 3.6
	 */
	public void setInputStream(InputStream inputStream) {
		this.inputStream = inputStream;
	}

	/**
	 * Returns this console's document partitioner.
	 *
	 * @return this console's document partitioner
	 */
	@Override
	protected IConsoleDocumentPartitioner getPartitioner() {
		return partitioner;
	}

	/**
	 * Returns the maximum number of characters that the console will display at
	 * once. This is analogous to the size of the text buffer this console
	 * maintains.
	 *
	 * @return the maximum number of characters that the console will display
	 */
	public int getHighWaterMark() {
		return partitioner.getHighWaterMark();
	}

	/**
	 * Returns the number of characters that will remain in this console
	 * when its high water mark is exceeded.
	 *
	 * @return the number of characters that will remain in this console
	 *  when its high water mark is exceeded
	 */
	public int getLowWaterMark() {
		return partitioner.getLowWaterMark();
	}

	/**
	 * Sets the text buffer size for this console. The high water mark indicates
	 * the maximum number of characters stored in the buffer. The low water mark
	 * indicates the number of characters remaining in the buffer when the high
	 * water mark is exceeded.
	 *
	 * @param low the number of characters remaining in the buffer when the high
	 *  water mark is exceeded (if -1 the console does not limit output)
	 * @param high the maximum number of characters this console will cache in
	 *  its text buffer (if -1 the console does not limit output)
	 * @exception IllegalArgumentException if low >= high & low != -1
	 */
	public void setWaterMarks(int low, int high) {
		if (low >= 0) {
			if (low >= high) {
				throw new IllegalArgumentException("High water mark must be greater than low water mark"); //$NON-NLS-1$
			}
		}
		partitioner.setWaterMarks(low, high);
	}

	/**
	 * Check if all streams connected to this console are closed. If so,
	 * notify the partitioner that this console is finished.
	 */
	private void checkFinished() {
		if (openStreams.isEmpty()) {
			partitioner.streamsClosed();
		}
	}

	/**
	 * Notification that an output stream connected to this console has been closed.
	 *
	 * @param stream stream that closed
	 */
	void streamClosed(IOConsoleOutputStream stream) {
		synchronized (openStreams) {
			openStreams.remove(stream);
			checkFinished();
		}
	}

	/**
	 * Notification that the input stream connected to this console has been closed.
	 *
	 * @param stream stream that closed
	 */
	void streamClosed(IOConsoleInputStream stream) {
		synchronized (openStreams) {
			openStreams.remove(stream);
			checkFinished();
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ui.console.TextConsole#clearConsole()
	 */
	@Override
	public void clearConsole() {
		if (partitioner != null) {
			partitioner.clearBuffer();
		}
	}

	/**
	 * Disposes this console.
	 */
	@Override
	protected void dispose() {
		super.dispose();
		partitioner.disconnect();
		//make a copy of the open streams and close them all
		//a copy is needed as close the streams results in a callback that
		//removes the streams from the openStreams collection (bug 152794)
		List<Closeable> list = new ArrayList<Closeable>(openStreams);
		for (Closeable closable : list) {
			try {
				closable.close();
			} catch (IOException e) {
				// e.printStackTrace();
			}
		}
		inputStream = null;
	}

	/**
	 * Returns the encoding for this console, or <code>null</code> to indicate
	 * default encoding.
	 *
	 * @return the encoding set for this console, or <code>null</code> to indicate
	 * 	default encoding
	 * @since 3.3
	 */
	public String getEncoding() {
		return this.charset.name();
	}

	/**
	 * Returns the Charset for this console, or <code>null</code> to indicate
	 * default encoding.
	 *
	 * @return the Charset for this console, or <code>null</code> to indicate
	 *         default encoding
	 * @since 3.7
	 */
	public Charset getCharset() {
		return this.charset;
	}

}

Back to the top