source: trunk/autoquest-ui-swt/src/main/java/de/ugoe/cs/autoquest/ui/swt/SWTConsole.java @ 927

Last change on this file since 927 was 927, checked in by sherbold, 12 years ago
  • added copyright under the Apache License, Version 2.0
  • Property svn:mime-type set to text/plain
File size: 3.8 KB
Line 
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
15package de.ugoe.cs.autoquest.ui.swt;
16
17import java.io.ByteArrayOutputStream;
18import java.io.PrintStream;
19import java.io.UnsupportedEncodingException;
20import java.util.logging.Level;
21
22import org.eclipse.swt.SWT;
23import org.eclipse.swt.custom.StyleRange;
24import org.eclipse.swt.custom.StyledText;
25import org.eclipse.swt.widgets.MessageBox;
26
27import de.ugoe.cs.util.StringTools;
28import de.ugoe.cs.util.console.Console;
29import de.ugoe.cs.util.console.listener.ICommandListener;
30import de.ugoe.cs.util.console.listener.IErrorListener;
31import de.ugoe.cs.util.console.listener.IExceptionListener;
32import de.ugoe.cs.util.console.listener.IOutputListener;
33import de.ugoe.cs.util.console.listener.ITraceListener;
34
35public 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        messageBox.setMessage(e.getMessage());
93        messageBox.open();
94       
95        ByteArrayOutputStream baos = new ByteArrayOutputStream();
96        PrintStream ps = new PrintStream(baos);
97        e.printStackTrace(ps);       
98        String stackTrace = null;
99        try {
100            stackTrace = baos.toString("UTF-8");
101        }
102        catch (UnsupportedEncodingException e1) {
103        }
104        if( stackTrace!=null ) {
105            appendColored(stackTrace, SWT.COLOR_RED);
106        } else {
107            appendColored(e.getMessage(), SWT.COLOR_RED);
108        }
109       
110    }
111}
Note: See TracBrowser for help on using the repository browser.