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

Last change on this file since 1081 was 1081, checked in by pharms, 11 years ago
  • prevented findbugs warnings
  • Property svn:mime-type set to text/plain
File size: 3.9 KB
RevLine 
[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]15package de.ugoe.cs.autoquest.ui.swt;
[526]16
[845]17import java.io.ByteArrayOutputStream;
18import java.io.PrintStream;
19import java.io.UnsupportedEncodingException;
[633]20import java.util.logging.Level;
21
[526]22import org.eclipse.swt.SWT;
23import org.eclipse.swt.custom.StyleRange;
24import org.eclipse.swt.custom.StyledText;
[845]25import org.eclipse.swt.widgets.MessageBox;
[526]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;
[845]31import de.ugoe.cs.util.console.listener.IExceptionListener;
[526]32import de.ugoe.cs.util.console.listener.IOutputListener;
33import de.ugoe.cs.util.console.listener.ITraceListener;
34
[570]35public 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");
92        messageBox.setMessage(e.getMessage());
93        messageBox.open();
94       
[1081]95        String stackTrace = null;
[845]96        ByteArrayOutputStream baos = new ByteArrayOutputStream();
97        try {
[1081]98            PrintStream ps = new PrintStream(baos, false, "UTF-8");
99            e.printStackTrace(ps);       
[845]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    }
[526]111}
Note: See TracBrowser for help on using the repository browser.