[526] | 1 | package de.ugoe.cs.quest.ui.swt;
|
---|
| 2 |
|
---|
[633] | 3 | import java.util.logging.Level;
|
---|
| 4 |
|
---|
[526] | 5 | import org.eclipse.swt.SWT;
|
---|
| 6 | import org.eclipse.swt.custom.StyleRange;
|
---|
| 7 | import org.eclipse.swt.custom.StyledText;
|
---|
| 8 |
|
---|
| 9 | import de.ugoe.cs.util.StringTools;
|
---|
| 10 | import de.ugoe.cs.util.console.Console;
|
---|
| 11 | import de.ugoe.cs.util.console.listener.ICommandListener;
|
---|
| 12 | import de.ugoe.cs.util.console.listener.IErrorListener;
|
---|
| 13 | import de.ugoe.cs.util.console.listener.IOutputListener;
|
---|
| 14 | import de.ugoe.cs.util.console.listener.ITraceListener;
|
---|
| 15 |
|
---|
[570] | 16 | public class SWTConsole implements IOutputListener, IErrorListener, ITraceListener,
|
---|
| 17 | ICommandListener
|
---|
| 18 | {
|
---|
[526] | 19 |
|
---|
[674] | 20 | private StyledText output;
|
---|
| 21 |
|
---|
| 22 | private Level traceLevel;
|
---|
[526] | 23 |
|
---|
[674] | 24 | public SWTConsole(StyledText styledText, Level traceLevel) {
|
---|
[570] | 25 | Console.getInstance().registerOutputListener(this);
|
---|
| 26 | Console.getInstance().registerErrorListener(this);
|
---|
| 27 | Console.getInstance().registerTraceListener(this);
|
---|
| 28 | Console.getInstance().registerCommandListener(this);
|
---|
| 29 | this.output = styledText;
|
---|
[674] | 30 | this.traceLevel = traceLevel;
|
---|
[570] | 31 | }
|
---|
[526] | 32 |
|
---|
[570] | 33 | @Override
|
---|
| 34 | public void outputMsg(String newMessage) {
|
---|
| 35 | output.append(newMessage);
|
---|
| 36 | }
|
---|
| 37 |
|
---|
| 38 | @Override
|
---|
| 39 | public void errorMsg(String errMessage) {
|
---|
| 40 | appendColored(errMessage, SWT.COLOR_RED);
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 | @Override
|
---|
[633] | 44 | public void traceMsg(String traceMessage, Level level) {
|
---|
[674] | 45 | if( level.intValue()>=traceLevel.intValue()) {
|
---|
| 46 | appendColored("[" + level.toString() + "] " + traceMessage, SWT.COLOR_BLUE);
|
---|
| 47 | }
|
---|
[570] | 48 | }
|
---|
| 49 |
|
---|
| 50 | @Override
|
---|
| 51 | public void commandNotification(String command) {
|
---|
| 52 | output.append("> " + command + StringTools.ENDLINE);
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | private void appendColored(String str, int id) {
|
---|
| 56 | StyleRange styleRange = new StyleRange();
|
---|
| 57 | styleRange.start = output.getText().length();
|
---|
| 58 | styleRange.length = str.length();
|
---|
| 59 | styleRange.foreground = output.getDisplay().getSystemColor(id);
|
---|
| 60 | output.append(str);
|
---|
| 61 | output.setStyleRange(styleRange);
|
---|
| 62 | }
|
---|
[526] | 63 | }
|
---|