Author: koen.aers(a)jboss.com
Date: 2011-02-08 13:43:09 -0500 (Tue, 08 Feb 2011)
New Revision: 29069
Modified:
trunk/forge/plugins/org.jboss.tools.seam.forge/src/org/jboss/tools/seam/forge/console/Console.java
trunk/forge/plugins/org.jboss.tools.seam.forge/src/org/jboss/tools/seam/forge/console/ConsoleInputStream.java
trunk/forge/plugins/org.jboss.tools.seam.forge/src/org/jboss/tools/seam/forge/console/ConsoleOutputStream.java
trunk/forge/plugins/org.jboss.tools.seam.forge/src/org/jboss/tools/seam/forge/console/ConsolePartitioner.java
trunk/forge/plugins/org.jboss.tools.seam.forge/src/org/jboss/tools/seam/forge/console/StreamListener.java
Log:
cleanup and refactorings
Modified:
trunk/forge/plugins/org.jboss.tools.seam.forge/src/org/jboss/tools/seam/forge/console/Console.java
===================================================================
---
trunk/forge/plugins/org.jboss.tools.seam.forge/src/org/jboss/tools/seam/forge/console/Console.java 2011-02-08
17:27:53 UTC (rev 29068)
+++
trunk/forge/plugins/org.jboss.tools.seam.forge/src/org/jboss/tools/seam/forge/console/Console.java 2011-02-08
18:43:09 UTC (rev 29069)
@@ -1,8 +1,6 @@
package org.jboss.tools.seam.forge.console;
import java.io.IOException;
-import java.util.ArrayList;
-import java.util.List;
import org.eclipse.debug.core.DebugEvent;
import org.eclipse.debug.core.DebugPlugin;
@@ -10,12 +8,6 @@
import org.eclipse.debug.core.model.IProcess;
import org.eclipse.debug.core.model.IStreamMonitor;
import org.eclipse.debug.core.model.IStreamsProxy;
-import org.eclipse.debug.ui.IDebugUIConstants;
-import org.eclipse.jface.resource.JFaceResources;
-import org.eclipse.swt.graphics.Color;
-import org.eclipse.swt.graphics.RGB;
-import org.eclipse.swt.widgets.Display;
-import org.eclipse.ui.WorkbenchEncoding;
import org.eclipse.ui.console.IConsoleDocumentPartitioner;
import org.eclipse.ui.console.IConsoleView;
import org.eclipse.ui.console.TextConsole;
@@ -23,84 +15,61 @@
public class Console extends TextConsole implements IDebugEventSetListener {
- private static final RGB INPUT_STREAM_RGB = new RGB(25, 225, 25);
- private static final RGB ERROR_STREAM_RGB = new RGB(225, 25, 25);
- private static final RGB OUTPUT_STREAM_RGB = new RGB(0, 0, 0);
- private static final RGB CONSOLE_BACKGROUND_RGB = new RGB(255, 255, 255);
-
private ConsolePartitioner partitioner;
private ConsoleInputStream inputStream;
- private List<Object> openStreams;
- private String encoding = WorkbenchEncoding.getWorkbenchDefaultEncoding();
+ private ConsoleOutputStream outputStream;
private IProcess process = null;
- private List<StreamListener> streamListeners = new
ArrayList<StreamListener>();
+ private StreamListener outputStreamListener;
public Console(IProcess process) {
super("Forge Console", null, null, true);
this.process = process;
- openStreams = new ArrayList<Object>();
- inputStream = new ConsoleInputStream(this);
- synchronized (openStreams) {
- openStreams.add(inputStream);
- }
- partitioner = new ConsolePartitioner(inputStream, this);
- partitioner.connect(getDocument());
-// inputStream.setColor(ColorManager.getInstance().getColor(INPUT_STREAM_RGB));
- connect();
+ initInputStream();
+ initPartitioner();
+ initOutputStream();
+ initInputReadJob();
}
-
- public void connect() {
+
+ private void initInputStream() {
+ inputStream = new ConsoleInputStream();
+ }
+
+ private void initOutputStream() {
+ outputStream = new ConsoleOutputStream(partitioner);
+ IStreamMonitor streamMonitor = getOutputStreamMonitor();
+ if (streamMonitor != null) {
+ synchronized(streamMonitor) {
+ outputStreamListener = new StreamListener(outputStream);
+ streamMonitor.addListener(outputStreamListener);
+ }
+ }
+ }
+
+ private IStreamMonitor getOutputStreamMonitor() {
+ IStreamMonitor streamMonitor = null;
IStreamsProxy streamsProxy = process.getStreamsProxy();
- IStreamMonitor streamMonitor = streamsProxy.getErrorStreamMonitor();
- if (streamMonitor != null) {
- connect(streamMonitor, IDebugUIConstants.ID_STANDARD_ERROR_STREAM);
- }
- streamMonitor = streamsProxy.getOutputStreamMonitor();
- if (streamMonitor != null) {
- connect(streamMonitor, IDebugUIConstants.ID_STANDARD_OUTPUT_STREAM);
- }
- InputReadJob inputReadJob = new InputReadJob(streamsProxy, inputStream);
- inputReadJob.setSystem(true);
- inputReadJob.schedule();
+ if (streamsProxy != null) {
+ streamMonitor = streamsProxy.getOutputStreamMonitor();
+ }
+ return streamMonitor;
}
- private void connect(IStreamMonitor streamMonitor, String streamIdentifier) {
- ConsoleOutputStream stream = newOutputStream();
- Color color = getColor(streamIdentifier);
- stream.setColor(color);
- synchronized (streamMonitor) {
- StreamListener listener = new StreamListener(streamIdentifier, streamMonitor,
stream, getEncoding());
- streamListeners.add(listener);
- }
+ private void initInputReadJob() {
+ InputReadJob inputReadJob = new InputReadJob(process.getStreamsProxy(),
inputStream);
+ inputReadJob.setSystem(true);
+ inputReadJob.schedule();
}
+
+ private void initPartitioner() {
+ partitioner = new ConsolePartitioner(inputStream, this);
+ partitioner.connect(getDocument());
+ }
- private Color getColor(String streamIdentifer) {
- if (IDebugUIConstants.ID_STANDARD_OUTPUT_STREAM.equals(streamIdentifer)) {
- return ColorManager.getInstance().getColor(OUTPUT_STREAM_RGB);
- }
- if (IDebugUIConstants.ID_STANDARD_ERROR_STREAM.equals(streamIdentifer)) {
- return ColorManager.getInstance().getColor(ERROR_STREAM_RGB);
- }
- if (IDebugUIConstants.ID_STANDARD_INPUT_STREAM.equals(streamIdentifer)) {
- return ColorManager.getInstance().getColor(INPUT_STREAM_RGB);
- }
- return null;
- }
-
public IPageBookViewPage createPage(IConsoleView view) {
throw new UnsupportedOperationException();
}
- public ConsoleOutputStream newOutputStream() {
- ConsoleOutputStream outputStream = new ConsoleOutputStream(this);
- outputStream.setEncoding(encoding);
- synchronized(openStreams) {
- openStreams.add(outputStream);
- }
- return outputStream;
- }
-
public ConsoleInputStream getInputStream() {
return inputStream;
}
@@ -109,26 +78,6 @@
return partitioner;
}
- private void checkFinished() {
- if (openStreams.isEmpty()) {
- partitioner.streamsClosed();
- }
- }
-
- void streamClosed(ConsoleOutputStream stream) {
- synchronized (openStreams) {
- openStreams.remove(stream);
- checkFinished();
- }
- }
-
- void streamClosed(ConsoleInputStream stream) {
- synchronized (openStreams) {
- openStreams.remove(stream);
- checkFinished();
- }
- }
-
public void dispose() {
super.dispose();
partitioner.disconnect();
@@ -138,51 +87,27 @@
}
private synchronized void closeStreams() {
- Object[] allStreams= openStreams.toArray();
- for (int i = 0; i < allStreams.length; i++) {
- Object stream = allStreams[i];
- if (stream instanceof ConsoleInputStream) {
- ConsoleInputStream is = (ConsoleInputStream) stream;
- try {
- is.close();
- } catch (IOException e) {
- }
- } else if (stream instanceof ConsoleOutputStream) {
- ConsoleOutputStream os = (ConsoleOutputStream) stream;
- try {
- os.close();
- } catch (IOException e) {
- }
- }
- }
-// inputStream = null;
+ try {
+ inputStream.close();
+ outputStream.close();
+ partitioner.streamsClosed();
+ } catch (IOException e) {}
}
private synchronized void disposeStreams() {
- for (StreamListener listener : streamListeners) {
- listener.dispose();
- }
+ IStreamMonitor streamMonitor = getOutputStreamMonitor();
+ if (streamMonitor != null) {
+ synchronized(streamMonitor) {
+ if (outputStreamListener != null) {
+ streamMonitor.removeListener(outputStreamListener);
+ }
+ }
+ }
+ outputStreamListener = null;
+ outputStream = null;
inputStream = null;
}
-// private synchronized void closeStreams() {
-// if (streamsClosed) {
-// return;
-// }
-// for (StreamListener listener : streamListeners) {
-// listener.closeStream();
-// }
-// try {
-// input.close();
-// } catch (IOException e) {
-// }
-// streamsClosed = true;
-// }
-
- public String getEncoding() {
- return encoding;
- }
-
public void handleDebugEvents(DebugEvent[] events) {
for (int i = 0; i < events.length; i++) {
DebugEvent event = events[i];
@@ -202,21 +127,6 @@
} else {
DebugPlugin.getDefault().addDebugEventListener(this);
}
- Display.getDefault().asyncExec(new Runnable() {
- public void run() {
- setFont(JFaceResources.getFont(IDebugUIConstants.PREF_CONSOLE_FONT));
-
setBackground(ColorManager.getInstance().getColor(CONSOLE_BACKGROUND_RGB));
- }
- });
}
- public ConsoleOutputStream getStream(String streamIdentifier) {
- for (StreamListener listener : streamListeners) {
- if (listener.getStreamId().equals(streamIdentifier)) {
- return listener.getStream();
- }
- }
- return null;
- }
-
}
Modified:
trunk/forge/plugins/org.jboss.tools.seam.forge/src/org/jboss/tools/seam/forge/console/ConsoleInputStream.java
===================================================================
---
trunk/forge/plugins/org.jboss.tools.seam.forge/src/org/jboss/tools/seam/forge/console/ConsoleInputStream.java 2011-02-08
17:27:53 UTC (rev 29068)
+++
trunk/forge/plugins/org.jboss.tools.seam.forge/src/org/jboss/tools/seam/forge/console/ConsoleInputStream.java 2011-02-08
18:43:09 UTC (rev 29069)
@@ -10,10 +10,8 @@
private int size = 0;
private boolean eofSent = false;
private boolean closed = false;
- private Console console;
- ConsoleInputStream(Console console) {
- this.console = console;
+ ConsoleInputStream() {
}
public synchronized int read() throws IOException {
@@ -66,7 +64,6 @@
}
closed = true;
notifyAll();
- console.streamClosed(this);
}
}
Modified:
trunk/forge/plugins/org.jboss.tools.seam.forge/src/org/jboss/tools/seam/forge/console/ConsoleOutputStream.java
===================================================================
---
trunk/forge/plugins/org.jboss.tools.seam.forge/src/org/jboss/tools/seam/forge/console/ConsoleOutputStream.java 2011-02-08
17:27:53 UTC (rev 29068)
+++
trunk/forge/plugins/org.jboss.tools.seam.forge/src/org/jboss/tools/seam/forge/console/ConsoleOutputStream.java 2011-02-08
18:43:09 UTC (rev 29069)
@@ -3,243 +3,58 @@
import java.io.IOException;
import java.io.OutputStream;
-import org.eclipse.swt.graphics.Color;
-import org.eclipse.ui.WorkbenchEncoding;
-import org.eclipse.ui.console.ConsolePlugin;
-import org.eclipse.ui.console.IConsoleConstants;
-
-/**
- * OutputStream used to write to an IOConsole.
- * <p>
- * Clients are not intended to instantiate this class directly, instead
- * use <code>IOConsole.newOutputStream()</code>.
- * </p>
- * <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>
- * @since 3.1
- * @noinstantiate This class is not intended to be instantiated by clients.
- * @noextend This class is not intended to be subclassed by clients.
- */
public class ConsoleOutputStream extends OutputStream {
- /**
- * Flag indicating whether this stream has been closed.
- */
- private boolean closed = false;
- /**
- * The console's document partitioner.
- */
+ private boolean closed = false;
private ConsolePartitioner partitioner;
- /**
- * The console this stream is attached to.
- */
- private Console 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();
-
- private boolean fNeedsEncoding = false;
-
- private boolean prependCR;
-
- /**
- * Constructs a new output stream on the given console.
- *
- * @param console I/O console
- */
- ConsoleOutputStream(Console console) {
- this.console = console;
- this.partitioner = (ConsolePartitioner) console.getPartitioner();
+ ConsoleOutputStream(ConsolePartitioner partitioner) {
+ this.partitioner = partitioner;
}
- /**
- * 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 newColor 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;
- }
-
- /**
- * Returns true if the stream has been closed
- * @return true is the stream has been closed, false otherwise.
- */
public synchronized boolean isClosed() {
return closed;
}
- /*
- * (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$
+ throw new IOException("Output Stream is closed");
}
- if (prependCR) { // force writing of last /r
- prependCR = false;
- notifyParitioner("\r"); //$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$
+ throw new IOException("Output Stream is closed");
}
}
- /*
- * (non-Javadoc)
- * @see java.io.OutputStream#write(byte[], int, int)
- */
public void write(byte[] b, int off, int len) throws IOException {
- if (fNeedsEncoding) {
- encodedWrite(new String(b, off, len, fEncoding));
- } else {
- encodedWrite(new String(b, off, len));
+ if(closed) {
+ throw new IOException("Output Stream is closed");
}
+ notifyParitioner(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 (fNeedsEncoding) {
- 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$
+ throw new IOException("Output Stream is closed");
}
- if (prependCR){
- encodedString="\r"+encodedString; //$NON-NLS-1$
- prependCR=false;
- }
- if (encodedString.endsWith("\r")) { //$NON-NLS-1$
- prependCR = true;
- encodedString = new String(encodedString.substring(0,
encodedString.length()-1));
- }
- notifyParitioner(encodedString);
+ notifyParitioner(str);
}
-
- private void notifyParitioner(String encodedString) throws IOException {
+
+ private void notifyParitioner(String str) throws IOException {
try {
- partitioner.streamAppended(this, encodedString);
-
- if (activateOnWrite) {
- console.activate();
- } else {
-
ConsolePlugin.getDefault().getConsoleManager().warnOfContentChange(console);
- }
+ partitioner.streamAppended(this, str);
} catch (IOException e) {
if (!closed) {
close();
@@ -248,13 +63,4 @@
}
}
- /**
- * Sets the character encoding used to interpret characters written to this steam.
- *
- * @param encoding encoding identifier
- */
- public void setEncoding(String encoding) {
- fEncoding = encoding;
- fNeedsEncoding = (fEncoding!=null) &&
(!fEncoding.equals(fDefaultEncoding));
- }
}
Modified:
trunk/forge/plugins/org.jboss.tools.seam.forge/src/org/jboss/tools/seam/forge/console/ConsolePartitioner.java
===================================================================
---
trunk/forge/plugins/org.jboss.tools.seam.forge/src/org/jboss/tools/seam/forge/console/ConsolePartitioner.java 2011-02-08
17:27:53 UTC (rev 29068)
+++
trunk/forge/plugins/org.jboss.tools.seam.forge/src/org/jboss/tools/seam/forge/console/ConsolePartitioner.java 2011-02-08
18:43:09 UTC (rev 29069)
@@ -31,6 +31,7 @@
*
*/
public class ConsolePartitioner implements IConsoleDocumentPartitioner,
IDocumentPartitionerExtension {
+
private PendingPartition consoleClosedPartition;
private IDocument document;
private ArrayList partitions;
Modified:
trunk/forge/plugins/org.jboss.tools.seam.forge/src/org/jboss/tools/seam/forge/console/StreamListener.java
===================================================================
---
trunk/forge/plugins/org.jboss.tools.seam.forge/src/org/jboss/tools/seam/forge/console/StreamListener.java 2011-02-08
17:27:53 UTC (rev 29068)
+++
trunk/forge/plugins/org.jboss.tools.seam.forge/src/org/jboss/tools/seam/forge/console/StreamListener.java 2011-02-08
18:43:09 UTC (rev 29069)
@@ -3,98 +3,24 @@
import java.io.IOException;
import org.eclipse.debug.core.IStreamListener;
-import org.eclipse.debug.core.model.IFlushableStreamMonitor;
import org.eclipse.debug.core.model.IStreamMonitor;
import org.jboss.tools.seam.forge.Activator;
class StreamListener implements IStreamListener {
private ConsoleOutputStream stream;
- private IStreamMonitor streamMonitor;
- private String streamId;
- private boolean flushed = false;
- private boolean listenerRemoved = false;
- private String encoding;
- StreamListener(String streamIdentifier, IStreamMonitor monitor, ConsoleOutputStream
stream, String encoding) {
- this.streamId = streamIdentifier;
- this.streamMonitor = monitor;
+ StreamListener(ConsoleOutputStream stream) {
this.stream = stream;
- this.encoding = encoding;
- streamMonitor.addListener(this);
- streamAppended(null, monitor);
}
- String getStreamId() {
- return streamId;
- }
-
- ConsoleOutputStream getStream() {
- return stream;
- }
-
- public void streamAppended(String text, IStreamMonitor monitor) {
- if (flushed) {
- try {
- if (stream != null) {
- if (encoding == null)
- stream.write(text);
- else
- stream.write(text.getBytes(encoding));
- }
- } catch (IOException e) {
- Activator.log(e);
- }
- } else {
- String contents = null;
- synchronized (streamMonitor) {
- flushed = true;
- contents = streamMonitor.getContents();
- if (streamMonitor instanceof IFlushableStreamMonitor) {
- IFlushableStreamMonitor m = (IFlushableStreamMonitor) streamMonitor;
- m.flushContents();
- m.setBuffered(false);
- }
- }
- try {
- if (contents != null && contents.length() > 0) {
- if (stream != null) {
- stream.write(contents);
- }
- }
- } catch (IOException e) {
- Activator.log(e);
- }
+ public void streamAppended(String text, IStreamMonitor streamMonitor) {
+ try {
+ stream.write(text);
+ } catch (IOException e) {
+ Activator.log(e);
}
}
- void closeStream() {
- if (streamMonitor == null) {
- return;
- }
- synchronized (streamMonitor) {
- streamMonitor.removeListener(this);
- if (!flushed) {
- String contents = streamMonitor.getContents();
- streamAppended(contents, streamMonitor);
- }
- listenerRemoved = true;
- try {
- if (stream != null) {
- stream.close();
- }
- } catch (IOException e) {
- }
- }
- }
-
- void dispose() {
- if (!listenerRemoved) {
- closeStream();
- }
- stream = null;
- streamMonitor = null;
- streamId = null;
- }
}