package essm.b2;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.custom.StyledText;
public class B2Console extends Composite {
java.io.PrintStream consolenStream=null;
java.io.PrintStream consolenErrorStream=null;
java.io.PrintStream sysStreamer=null;
java.io.PrintStream errStreamer=null;
Display display = null;
StyledText styledText = null;
/**
* Main-Methode.<br>
*/
public static void main(String[] args) {
Display display = Display.getDefault();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
shell.setSize(new Point(300, 200));
B2Console thisClass = new B2Console(shell, SWT.NONE);
shell.open();
thisClass.makeSysTestPrints(100);
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
public B2Console(Composite parent, int style) {
super(parent, style);
display=parent.getDisplay();
initialize();
myInitialize();
System.out.println("Console gestartet!");
}
private void initialize() {
styledText = new StyledText(this, SWT.V_SCROLL | SWT.H_SCROLL);
styledText.setEditable(false);
this.setLayout(new FillLayout());
setSize(new Point(300, 200));
this.addDisposeListener(new org.eclipse.swt.events.DisposeListener() {
public void widgetDisposed(org.eclipse.swt.events.DisposeEvent e) {
System.setOut(sysStreamer);
System.setErr(errStreamer);
}
});
}
private void myInitialize(){
// neuer Stream der Alle println aufrufe nach foo.onEvent umleitet
sysStreamer= new java.io.PrintStream(System.out);
consolenStream = new java.io.PrintStream(System.out) {
@Override
public void print(boolean b) {
onEvent(""+b);
}
@Override
public void print(char c) {
onEvent(""+c);
}
@Override
public void print(double d) {
onEvent(""+d);
}
@Override
public void print(float f) {
onEvent(""+f);
}
@Override
public void print(int i) {
onEvent(""+i);
}
@Override
public void print(long l) {
onEvent(""+l);
}
@Override
public void print(Object obj) {
onEvent(""+obj);
}
@Override
public void println(boolean x) {
onEventln(""+x);
}
@Override
public void println(char x) {
onEventln(""+x);
}
@Override
public void println(double x) {
onEventln(""+x);
}
@Override
public void println(float x) {
onEventln(""+x);
}
@Override
public void println(int x) {
onEventln(""+x);
}
@Override
public void println(long x) {
onEventln(""+x);
}
@Override
public void println(Object x) {
onEventln(""+x);
}
@Override
public void print(String s) {
onEvent(s);
}
@Override
public void println(String s) {
onEventln(s);
}
};
errStreamer= new java.io.PrintStream(System.err);
consolenErrorStream = new java.io.PrintStream(System.err) {
@Override
public void print(boolean b) {
onEventErr(""+b);
}
@Override
public void print(char c) {
onEventErr(""+c);
}
@Override
public void print(double d) {
onEventErr(""+d);
}
@Override
public void print(float f) {
onEventErr(""+f);
}
@Override
public void print(int i) {
onEventErr(""+i);
}
@Override
public void print(long l) {
onEventErr(""+l);
}
@Override
public void print(Object obj) {
onEventErr(""+obj);
}
@Override
public void println(boolean x) {
onEventlnErr(""+x);
}
@Override
public void println(char x) {
onEventlnErr(""+x);
}
@Override
public void println(double x) {
onEventlnErr(""+x);
}
@Override
public void println(float x) {
onEventlnErr(""+x);
}
@Override
public void println(int x) {
onEventlnErr(""+x);
}
@Override
public void println(long x) {
onEventlnErr(""+x);
}
@Override
public void println(Object x) {
onEventlnErr(""+x);
}
@Override
public void print(String s) {
onEventErr(s);
}
@Override
public void println(String s) {
onEventlnErr(s);
}
};
System.setOut(consolenStream);
System.setErr(consolenErrorStream);
}
public void makeSysTestPrints(int anzahl){
for (int i = 0; i < anzahl; i++) {
System.out.println(i+". "+Math.random());
try {
Thread.sleep(1);
} catch (Exception e) {
}
}
}
/**
* Fügt den String dem Textfeld hinzu
*
* @param str
*/
void onEvent(String str) {
final String strM=str;
Thread tr = new Thread(new Runnable() {
public void run() {
display.syncExec(new Runnable() {
public void run() {
styledText.append(strM);
}
});
}
},"printThread");
tr.start();
}
/**
* Fügt den String dem Textfeld hinzu
*
* @param str
*/
void onEventln(String str) {
final String strM=str;
Thread tr = new Thread(new Runnable() {
public void run() {
display.syncExec(new Runnable() {
public void run() {
styledText.append(strM+"\n");
}
});
}
},"printlnThread");
tr.start();
}
/**
* Fügt den String dem Textfeld hinzu
*
* @param str
*/
void onEventErr(String str) {
final String strM=str;
Thread tr = new Thread(new Runnable() {
public void run() {
display.syncExec(new Runnable() {
public void run() {
int indexFor=styledText.getText().length();
String st=strM;
styledText.append(strM);
int indexNach=st.length();
org.eclipse.swt.custom.StyleRange styleRange
= new org.eclipse.swt.custom.StyleRange();
styleRange.start = indexFor;
styleRange.length = indexNach;
styleRange.foreground = display.getSystemColor(SWT.COLOR_RED);
styledText.setStyleRange(styleRange);
}
});
}
},"printThread");
tr.start();
}
/**
* Fügt den String dem Textfeld hinzu
*
* @param str
*/
void onEventlnErr(String str) {
final String strM=str;
Thread tr = new Thread(new Runnable() {
public void run() {
display.syncExec(new Runnable() {
public void run() {
int indexFor=styledText.getText().length();
String st=strM+"\n";
styledText.append(strM+"\n");
int indexNach=st.length();
org.eclipse.swt.custom.StyleRange styleRange
= new org.eclipse.swt.custom.StyleRange();
styleRange.start = indexFor;
styleRange.length = indexNach;
styleRange.foreground = display.getSystemColor(SWT.COLOR_RED);
styledText.setStyleRange(styleRange);
}
});
}
},"printlnThread");
tr.start();
}
}