From cd0f83cb5159ead0060d6a1dcbeb7dafe08d9f2b Mon Sep 17 00:00:00 2001 From: Samson Close Date: Sat, 2 Jul 2016 13:06:38 -0400 Subject: [PATCH] Initial Commit --- .classpath | 6 + .project | 17 ++ .settings/org.eclipse.jdt.core.prefs | 12 ++ src/samson/stream/Console.java | 193 +++++++++++++++++++++ src/samson/stream/ConsoleOutputStream.java | 31 ++++ src/samson/stream/MultiOutputStream.java | 62 +++++++ 6 files changed, 321 insertions(+) create mode 100644 .classpath create mode 100644 .project create mode 100644 .settings/org.eclipse.jdt.core.prefs create mode 100644 src/samson/stream/Console.java create mode 100644 src/samson/stream/ConsoleOutputStream.java create mode 100644 src/samson/stream/MultiOutputStream.java diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..fceb480 --- /dev/null +++ b/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/.project b/.project new file mode 100644 index 0000000..cf42444 --- /dev/null +++ b/.project @@ -0,0 +1,17 @@ + + + Console + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..eb1cb4c --- /dev/null +++ b/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,12 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.methodParameters=do not generate +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.5 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=1.5 diff --git a/src/samson/stream/Console.java b/src/samson/stream/Console.java new file mode 100644 index 0000000..91911be --- /dev/null +++ b/src/samson/stream/Console.java @@ -0,0 +1,193 @@ +package samson.stream; + +import java.awt.BorderLayout; +import java.awt.Color; +import java.awt.Component; +import java.awt.Font; +import java.awt.Insets; +import java.io.PrintStream; + +import javax.swing.JFrame; +import javax.swing.JPanel; +import javax.swing.JScrollPane; +import javax.swing.JTextPane; +import javax.swing.border.EmptyBorder; +import javax.swing.text.MutableAttributeSet; +import javax.swing.text.SimpleAttributeSet; +import javax.swing.text.StyleConstants; + +public class Console { + public static Console console = null; + private static MultiOutputStream out = null; + private static MultiOutputStream err = null; + + /** + * Initialised the out and err streams. + */ + public static void initialize() { + initialize(800, 500, null); + } + + /** + * Initialised the out and err streams. + * + * @param width + * the width of the window + * @param width + * the height of the window + */ + public static void initialize(int width, int height) { + initialize(width, height, null); + } + + /** + * Initialised the out and err streams. + * + * @param relativeTo + * the component to set the location of this window to + */ + public static void initialize(Component relativeTo) { + initialize(800, 500, relativeTo); + } + + /** + * Initialised the out and err streams. + * + * @param width + * the width of the window + * @param width + * the height of the window + * @param relativeTo + * the component to set the location of this window to + */ + public static void initialize(int width, int height, Component relativeTo) { + if (console == null) { + console = new Console(width, height, relativeTo); + } + + if (out == null) { + ConsoleOutputStream o = new ConsoleOutputStream(Color.BLACK); + out = new MultiOutputStream(o, System.out); + System.setOut(new PrintStream(out)); + } + + if (err == null) { + ConsoleOutputStream e = new ConsoleOutputStream(Color.RED); + err = new MultiOutputStream(e, System.err); + System.setErr(new PrintStream(err)); + } + } + + /** + * @return the MultiOutputStream being used to output to multiple streams at + * once. + */ + public static MultiOutputStream getOutputStream() { + initialize(); + return out; + } + + /** + * @return the MultiOutputStream being used to output to multiple streams at + * once. + */ + public static MultiOutputStream getErrorStream() { + initialize(); + return err; + } + + private JFrame frame; + private JTextPane text; + private JPanel panel; + + public static Console instance() { + initialize(); + return console; + } + + public JFrame getJFrame() { + return frame; + } + + public JTextPane getJTextArea() { + return text; + } + + public JPanel getJPanel() { + return panel; + } + + private Console(int width, int height, Component comp) { + frame = new JFrame("CONSOLE"); + frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); + frame.setSize(width, height); + frame.setLocationRelativeTo(comp); + + text = new JTextPane(); + text.setBorder(new EmptyBorder(new Insets(0, 4, 2, 2))); + text.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 14)); + + JScrollPane scrollPane = new JScrollPane(text, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, + JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); + + panel = new JPanel(); + + panel.setLayout(new BorderLayout()); + panel.add(scrollPane); + + frame.getContentPane().add(panel); + } + + /** + * Puts a message in the console. + * + * @param msg + * the message + */ + public void appendToPane(String msg) { + appendToPane(msg, Color.BLACK); + } + + /** + * Puts a message in the console. + * + * @param msg + * the message + * @param c + * the colour of the message + */ + public void appendToPane(String msg, Color c) { + MutableAttributeSet set = new SimpleAttributeSet(); + StyleConstants.setLineSpacing(set, -0.16F); + StyleConstants.setForeground(set, c); + + int len = text.getDocument().getLength(); + text.setCaretPosition(len); + text.setParagraphAttributes(set, false); + text.replaceSelection(msg); + } + + /** + * Sets the visibility of the console. + * + * @param show + * whether or not to display the console + */ + public static void setVisible(boolean show) { + instance().getJFrame().setVisible(show); + } + + /** + * Displays the console. + */ + public static void show() { + setVisible(true); + } + + /** + * Hides the console. + */ + public static void hide() { + setVisible(false); + } +} diff --git a/src/samson/stream/ConsoleOutputStream.java b/src/samson/stream/ConsoleOutputStream.java new file mode 100644 index 0000000..e312c09 --- /dev/null +++ b/src/samson/stream/ConsoleOutputStream.java @@ -0,0 +1,31 @@ +package samson.stream; + +import java.awt.Color; +import java.io.IOException; +import java.io.OutputStream; + +public class ConsoleOutputStream extends OutputStream { + private final StringBuilder sb = new StringBuilder(); + + private Color colour; + + public ConsoleOutputStream(Color colour) { + this.colour = colour; + } + + @Override + public void write(int b) throws IOException { + + if (b == '\r') + return; + + if (b == '\n') { + final String text = sb.toString() + "\n"; + Console.instance().appendToPane(text, colour); + sb.setLength(0); + return; + } + + sb.append((char) b); + } +} diff --git a/src/samson/stream/MultiOutputStream.java b/src/samson/stream/MultiOutputStream.java new file mode 100644 index 0000000..8fbfae3 --- /dev/null +++ b/src/samson/stream/MultiOutputStream.java @@ -0,0 +1,62 @@ +package samson.stream; + +import java.io.IOException; +import java.io.OutputStream; +import java.util.ArrayList; +import java.util.List; + +public class MultiOutputStream extends OutputStream { + private List streams; + + public MultiOutputStream(OutputStream... streams) { + this.streams = new ArrayList(); + + for (OutputStream out : streams) { + if (out != null) + this.streams.add(out); + } + } + + public void addStream(OutputStream stream) { + streams.add(stream); + } + + public List getStreams() { + return streams; + } + + @Override + public void write(int b) throws IOException { + for (OutputStream stream : streams) { + stream.write(b); + } + } + + @Override + public void write(byte[] b) throws IOException { + for (OutputStream stream : streams) { + stream.write(b); + } + } + + @Override + public void write(byte[] b, int off, int len) throws IOException { + for (OutputStream stream : streams) { + stream.write(b, off, len); + } + } + + @Override + public void flush() throws IOException { + for (OutputStream stream : streams) { + stream.flush(); + } + } + + @Override + public void close() throws IOException { + for (OutputStream stream : streams) { + stream.close(); + } + } +}