Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
defaultsamson committed Jul 2, 2016
1 parent db0dcd5 commit cd0f83c
Show file tree
Hide file tree
Showing 6 changed files with 321 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="output" path="bin"/>
</classpath>
17 changes: 17 additions & 0 deletions .project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>Console</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
12 changes: 12 additions & 0 deletions .settings/org.eclipse.jdt.core.prefs
Original file line number Diff line number Diff line change
@@ -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
193 changes: 193 additions & 0 deletions src/samson/stream/Console.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
31 changes: 31 additions & 0 deletions src/samson/stream/ConsoleOutputStream.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
62 changes: 62 additions & 0 deletions src/samson/stream/MultiOutputStream.java
Original file line number Diff line number Diff line change
@@ -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<OutputStream> streams;

public MultiOutputStream(OutputStream... streams) {
this.streams = new ArrayList<OutputStream>();

for (OutputStream out : streams) {
if (out != null)
this.streams.add(out);
}
}

public void addStream(OutputStream stream) {
streams.add(stream);
}

public List<OutputStream> 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();
}
}
}

0 comments on commit cd0f83c

Please sign in to comment.