[927] | 1 | // Copyright 2012 Georg-August-Universität Göttingen, Germany
|
---|
| 2 | //
|
---|
| 3 | // Licensed under the Apache License, Version 2.0 (the "License");
|
---|
| 4 | // you may not use this file except in compliance with the License.
|
---|
| 5 | // You may obtain a copy of the License at
|
---|
| 6 | //
|
---|
| 7 | // http://www.apache.org/licenses/LICENSE-2.0
|
---|
| 8 | //
|
---|
| 9 | // Unless required by applicable law or agreed to in writing, software
|
---|
| 10 | // distributed under the License is distributed on an "AS IS" BASIS,
|
---|
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
| 12 | // See the License for the specific language governing permissions and
|
---|
| 13 | // limitations under the License.
|
---|
| 14 |
|
---|
[922] | 15 | package de.ugoe.cs.autoquest.ui.swt;
|
---|
[526] | 16 |
|
---|
[845] | 17 | import java.io.ByteArrayOutputStream;
|
---|
| 18 | import java.io.PrintStream;
|
---|
| 19 | import java.io.UnsupportedEncodingException;
|
---|
[633] | 20 | import java.util.logging.Level;
|
---|
| 21 |
|
---|
[526] | 22 | import org.eclipse.swt.SWT;
|
---|
| 23 | import org.eclipse.swt.custom.StyleRange;
|
---|
| 24 | import org.eclipse.swt.custom.StyledText;
|
---|
[845] | 25 | import org.eclipse.swt.widgets.MessageBox;
|
---|
[526] | 26 |
|
---|
| 27 | import de.ugoe.cs.util.StringTools;
|
---|
| 28 | import de.ugoe.cs.util.console.Console;
|
---|
| 29 | import de.ugoe.cs.util.console.listener.ICommandListener;
|
---|
| 30 | import de.ugoe.cs.util.console.listener.IErrorListener;
|
---|
[845] | 31 | import de.ugoe.cs.util.console.listener.IExceptionListener;
|
---|
[526] | 32 | import de.ugoe.cs.util.console.listener.IOutputListener;
|
---|
| 33 | import de.ugoe.cs.util.console.listener.ITraceListener;
|
---|
| 34 |
|
---|
[570] | 35 | public class SWTConsole implements IOutputListener, IErrorListener, ITraceListener,
|
---|
[845] | 36 | ICommandListener, IExceptionListener
|
---|
[570] | 37 | {
|
---|
[526] | 38 |
|
---|
[674] | 39 | private StyledText output;
|
---|
| 40 |
|
---|
| 41 | private Level traceLevel;
|
---|
[526] | 42 |
|
---|
[674] | 43 | public SWTConsole(StyledText styledText, Level traceLevel) {
|
---|
[570] | 44 | Console.getInstance().registerOutputListener(this);
|
---|
| 45 | Console.getInstance().registerErrorListener(this);
|
---|
| 46 | Console.getInstance().registerTraceListener(this);
|
---|
| 47 | Console.getInstance().registerCommandListener(this);
|
---|
[845] | 48 | Console.getInstance().registerExceptionListener(this);
|
---|
[570] | 49 | this.output = styledText;
|
---|
[674] | 50 | this.traceLevel = traceLevel;
|
---|
[570] | 51 | }
|
---|
[526] | 52 |
|
---|
[570] | 53 | @Override
|
---|
| 54 | public void outputMsg(String newMessage) {
|
---|
| 55 | output.append(newMessage);
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | @Override
|
---|
| 59 | public void errorMsg(String errMessage) {
|
---|
[828] | 60 | appendColored("[ERROR] " + errMessage, SWT.COLOR_RED);
|
---|
[570] | 61 | }
|
---|
| 62 |
|
---|
| 63 | @Override
|
---|
[633] | 64 | public void traceMsg(String traceMessage, Level level) {
|
---|
[674] | 65 | if( level.intValue()>=traceLevel.intValue()) {
|
---|
[828] | 66 | int color = SWT.COLOR_BLUE;
|
---|
| 67 | if( level==Level.SEVERE ) {
|
---|
| 68 | color = SWT.COLOR_RED;
|
---|
| 69 | }
|
---|
| 70 | appendColored("[" + level.toString() + "] " + traceMessage, color);
|
---|
[674] | 71 | }
|
---|
[570] | 72 | }
|
---|
| 73 |
|
---|
| 74 | @Override
|
---|
| 75 | public void commandNotification(String command) {
|
---|
| 76 | output.append("> " + command + StringTools.ENDLINE);
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | private void appendColored(String str, int id) {
|
---|
| 80 | StyleRange styleRange = new StyleRange();
|
---|
| 81 | styleRange.start = output.getText().length();
|
---|
| 82 | styleRange.length = str.length();
|
---|
| 83 | styleRange.foreground = output.getDisplay().getSystemColor(id);
|
---|
| 84 | output.append(str);
|
---|
| 85 | output.setStyleRange(styleRange);
|
---|
| 86 | }
|
---|
[845] | 87 |
|
---|
| 88 | @Override
|
---|
| 89 | public void logException(Exception e) {
|
---|
| 90 | MessageBox messageBox = new MessageBox(output.getShell(), SWT.ERROR);
|
---|
| 91 | messageBox.setText("Error");
|
---|
[1431] | 92 |
|
---|
| 93 | if (e.getMessage() != null) {
|
---|
| 94 | messageBox.setMessage(e.getMessage());
|
---|
| 95 | }
|
---|
| 96 | else {
|
---|
| 97 | messageBox.setMessage("Unexpected exception occurred: " + e);
|
---|
| 98 | }
|
---|
[845] | 99 | messageBox.open();
|
---|
| 100 |
|
---|
[1081] | 101 | String stackTrace = null;
|
---|
[845] | 102 | ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
---|
| 103 | try {
|
---|
[1081] | 104 | PrintStream ps = new PrintStream(baos, false, "UTF-8");
|
---|
| 105 | e.printStackTrace(ps);
|
---|
[845] | 106 | stackTrace = baos.toString("UTF-8");
|
---|
| 107 | }
|
---|
| 108 | catch (UnsupportedEncodingException e1) {
|
---|
| 109 | }
|
---|
| 110 | if( stackTrace!=null ) {
|
---|
| 111 | appendColored(stackTrace, SWT.COLOR_RED);
|
---|
| 112 | } else {
|
---|
| 113 | appendColored(e.getMessage(), SWT.COLOR_RED);
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | }
|
---|
[526] | 117 | }
|
---|