001 /*
002 GRANITE DATA SERVICES
003 Copyright (C) 2011 GRANITE DATA SERVICES S.A.S.
004
005 This file is part of Granite Data Services.
006
007 Granite Data Services is free software; you can redistribute it and/or modify
008 it under the terms of the GNU Library General Public License as published by
009 the Free Software Foundation; either version 2 of the License, or (at your
010 option) any later version.
011
012 Granite Data Services is distributed in the hope that it will be useful, but
013 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
014 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
015 for more details.
016
017 You should have received a copy of the GNU Library General Public License
018 along with this library; if not, see <http://www.gnu.org/licenses/>.
019 */
020
021 package org.granite.builder;
022
023 import org.eclipse.swt.SWT;
024 import org.eclipse.swt.widgets.Display;
025 import org.eclipse.ui.console.ConsolePlugin;
026 import org.eclipse.ui.console.IConsole;
027 import org.eclipse.ui.console.MessageConsole;
028 import org.eclipse.ui.console.MessageConsoleStream;
029
030 /**
031 * @author Franck WOLFF
032 */
033 public class BuilderConsole {
034
035 public enum MessageType {
036 TITLE,
037 DEBUG,
038 INFO,
039 WARNING,
040 ERROR
041 }
042
043 private static final String INDENT = " ";
044
045 private static MessageConsole console = null;
046 private static boolean debugEnabled = false;
047
048 public static boolean isDebugEnabled() {
049 return debugEnabled;
050 }
051
052 public static void setDebugEnabled(boolean debugEnabled) {
053 BuilderConsole.debugEnabled = debugEnabled;
054 }
055
056 private static synchronized MessageConsole getConsole() {
057 if (console == null) {
058 console = new MessageConsole("Granite", null);
059 ConsolePlugin.getDefault().getConsoleManager().addConsoles(new IConsole[]{console});
060 }
061 return console;
062 }
063
064 private static MessageConsoleStream getConsoleStream(MessageType type) {
065 int color = SWT.COLOR_BLACK;
066 int font = SWT.NORMAL;
067
068 switch (type) {
069 case DEBUG:
070 color = SWT.COLOR_DARK_GREEN;
071 font = SWT.ITALIC;
072 break;
073 case ERROR:
074 color = SWT.COLOR_RED;
075 break;
076 case WARNING:
077 color = SWT.COLOR_DARK_MAGENTA;
078 break;
079 case TITLE:
080 color = SWT.COLOR_DARK_GRAY;
081 font = SWT.BOLD | SWT.ITALIC;
082 break;
083 default:
084 break;
085 }
086
087 MessageConsoleStream msgConsoleStream = getConsole().newMessageStream();
088 msgConsoleStream.setFontStyle(font);
089 msgConsoleStream.setColor(Display.getDefault().getSystemColor(color));
090 return msgConsoleStream;
091 }
092
093 public static void println(String msg) {
094 println(msg, MessageType.INFO);
095 }
096
097 public static void activate() {
098 Display.getDefault().syncExec(new Runnable() {
099 public void run() {
100 getConsole().activate(); // show console view...
101 }
102 });
103 }
104
105 public static void println(final String msg, final MessageType type) {
106 if (msg == null || (!debugEnabled && type == MessageType.DEBUG))
107 return;
108
109 Display.getDefault().syncExec(new Runnable() {
110 public void run() {
111 if (type == MessageType.ERROR)
112 getConsole().activate(); // show console view...
113 String qMsg = (type == MessageType.TITLE ? msg : INDENT + "[" + type + "] " + msg);
114 getConsoleStream(type).println(qMsg);
115 }
116 });
117 }
118 }