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 |
|
---|
15 | package de.ugoe.cs.autoquest.ui.swt;
|
---|
16 |
|
---|
17 | import java.io.ByteArrayOutputStream;
|
---|
18 | import java.io.PrintStream;
|
---|
19 | import java.io.UnsupportedEncodingException;
|
---|
20 | import java.util.logging.Level;
|
---|
21 |
|
---|
22 | import org.eclipse.swt.SWT;
|
---|
23 | import org.eclipse.swt.custom.StyleRange;
|
---|
24 | import org.eclipse.swt.custom.StyledText;
|
---|
25 | import org.eclipse.swt.widgets.MessageBox;
|
---|
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;
|
---|
31 | import de.ugoe.cs.util.console.listener.IExceptionListener;
|
---|
32 | import de.ugoe.cs.util.console.listener.IOutputListener;
|
---|
33 | import de.ugoe.cs.util.console.listener.ITraceListener;
|
---|
34 |
|
---|
35 | public class SWTConsole implements IOutputListener, IErrorListener, ITraceListener,
|
---|
36 | ICommandListener, IExceptionListener
|
---|
37 | {
|
---|
38 |
|
---|
39 | private StyledText output;
|
---|
40 |
|
---|
41 | private Level traceLevel;
|
---|
42 |
|
---|
43 | public SWTConsole(StyledText styledText, Level traceLevel) {
|
---|
44 | Console.getInstance().registerOutputListener(this);
|
---|
45 | Console.getInstance().registerErrorListener(this);
|
---|
46 | Console.getInstance().registerTraceListener(this);
|
---|
47 | Console.getInstance().registerCommandListener(this);
|
---|
48 | Console.getInstance().registerExceptionListener(this);
|
---|
49 | this.output = styledText;
|
---|
50 | this.traceLevel = traceLevel;
|
---|
51 | }
|
---|
52 |
|
---|
53 | @Override
|
---|
54 | public void outputMsg(String newMessage) {
|
---|
55 | output.append(newMessage);
|
---|
56 | }
|
---|
57 |
|
---|
58 | @Override
|
---|
59 | public void errorMsg(String errMessage) {
|
---|
60 | appendColored("[ERROR] " + errMessage, SWT.COLOR_RED);
|
---|
61 | }
|
---|
62 |
|
---|
63 | @Override
|
---|
64 | public void traceMsg(String traceMessage, Level level) {
|
---|
65 | if( level.intValue()>=traceLevel.intValue()) {
|
---|
66 | int color = SWT.COLOR_BLUE;
|
---|
67 | if( level==Level.SEVERE ) {
|
---|
68 | color = SWT.COLOR_RED;
|
---|
69 | }
|
---|
70 | appendColored("[" + level.toString() + "] " + traceMessage, color);
|
---|
71 | }
|
---|
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 | }
|
---|
87 |
|
---|
88 | @Override
|
---|
89 | public void logException(Exception e) {
|
---|
90 | MessageBox messageBox = new MessageBox(output.getShell(), SWT.ERROR);
|
---|
91 | messageBox.setText("Error");
|
---|
92 |
|
---|
93 | if (e.getMessage() != null) {
|
---|
94 | messageBox.setMessage(e.getMessage());
|
---|
95 | }
|
---|
96 | else {
|
---|
97 | messageBox.setMessage("Unexpected exception occurred: " + e);
|
---|
98 | }
|
---|
99 | messageBox.open();
|
---|
100 |
|
---|
101 | String stackTrace = null;
|
---|
102 | ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
---|
103 | try {
|
---|
104 | PrintStream ps = new PrintStream(baos, false, "UTF-8");
|
---|
105 | e.printStackTrace(ps);
|
---|
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 | }
|
---|
117 | }
|
---|