blob: 14f32aac5f535d8b254ea6151474eed6f609cd8d [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2014 Bosch Software Innovations GmbH and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v1.0 which accompany this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v10.html
* The Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* Contributors:
* Bosch Software Innovations GmbH - Please refer to git log
*
*******************************************************************************/
package org.eclipse.vorto.api.console;
import java.io.IOException;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.text.IDocument;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.console.ConsolePlugin;
import org.eclipse.ui.console.IConsole;
import org.eclipse.ui.console.IConsoleConstants;
import org.eclipse.ui.console.IOConsoleOutputStream;
import org.eclipse.ui.console.MessageConsole;
/**
*
*
* Used to write to console. Usage: create instance,
* ConsoleDisplayMgr.getDefault().println("message",
* ConsoleDisplayMgr.MSG_ERROR)
*
*/
public class ConsoleDisplayMgr {
private static ConsoleDisplayMgr fDefault = null;
private String fTitle = null;
private static MessageConsole fMessageConsole = null;
private static Color warningColor;
private static Color infoColor;
private static Color errorColor;
private static Color successColor;
private static final String SEPERATOR = "------------------------------------------------------------------------";
public static enum MSG_KIND {
ERROR, WARNING, INFO, SUCCESS
}
private ConsoleDisplayMgr(String messageTitle) {
//fDefault = this;
fTitle = messageTitle;
Display display = Display.getDefault();
infoColor = display.getSystemColor(SWT.COLOR_BLACK);
errorColor = display.getSystemColor(SWT.COLOR_RED);
successColor = display.getSystemColor(SWT.COLOR_DARK_GREEN);
warningColor = display.getSystemColor(SWT.COLOR_BLUE);
}
public static ConsoleDisplayMgr getDefault() {
if (fDefault == null) {
fDefault = new ConsoleDisplayMgr(" M2M ");
}
ConsolePlugin.getDefault().getConsoleManager()
.showConsoleView(fMessageConsole);
return fDefault;
}
/**
* prints a message to the current console.
*
* @param msg
* message to be printed
* @param msgKind
* Defines the kind of message: MSG_INFORMATION(black) or
* MSG_ERROR(red)
*
*/
public void println(final String msg, final MSG_KIND msgKind) {
Display.getDefault().asyncExec(new Runnable() {
@Override
public void run() {
if (msg == null)
return;
if (!displayConsoleView()) {
MessageDialog.openError(PlatformUI.getWorkbench()
.getActiveWorkbenchWindow().getShell(), "Error",
msg);
return;
}
final String msgWithMarker;
switch (msgKind) {
case INFO:
msgWithMarker = "[INFO] " + msg;
break;
case ERROR:
msgWithMarker = "[ERROR] " + msg;
break;
case SUCCESS:
msgWithMarker = "[SUCCESS] " + msg;
break;
case WARNING:
msgWithMarker = "[WARNING] " + msg;
break;
default:
msgWithMarker = msg;
}
try {
getNewMessageConsoleStream(msgKind).write(msgWithMarker);
getNewMessageConsoleStream(msgKind).write('\n');
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
public void printError(final String msg) {
this.println(msg, MSG_KIND.ERROR);
}
public void printSuccess(final String msg) {
this.println(msg, MSG_KIND.SUCCESS);
}
public void printWarning(final String msg) {
this.println(msg, MSG_KIND.WARNING);
}
public void printInfo(final String msg) {
this.println(msg, MSG_KIND.INFO);
}
public void clear() {
IDocument document = getMessageConsole().getDocument();
if (document != null) {
document.set("");
}
}
public void printSeperator() {
println(SEPERATOR, MSG_KIND.INFO);
}
private boolean displayConsoleView() {
try {
IWorkbenchWindow activeWorkbenchWindow = PlatformUI.getWorkbench()
.getActiveWorkbenchWindow();
if (activeWorkbenchWindow != null) {
IWorkbenchPage activePage = activeWorkbenchWindow
.getActivePage();
if (activePage != null)
activePage.showView(IConsoleConstants.ID_CONSOLE_VIEW,
null, IWorkbenchPage.VIEW_VISIBLE);
}
} catch (PartInitException partEx) {
return false;
}
return true;
}
public IOConsoleOutputStream getNewMessageConsoleStream(MSG_KIND msgKind) {
Color color = null;
switch (msgKind) {
case INFO:
color = infoColor;
break;
case ERROR:
color = errorColor;
break;
case SUCCESS:
color = successColor;
break;
case WARNING:
color = warningColor;
break;
default:
color = infoColor;
}
IOConsoleOutputStream msgConsoleStream = getMessageConsole()
.newOutputStream();
msgConsoleStream.setColor(color);
return msgConsoleStream;
}
private MessageConsole getMessageConsole() {
if (fMessageConsole == null)
createMessageConsoleStream(fTitle);
return fMessageConsole;
}
private void createMessageConsoleStream(String title) {
initalize(title);
ConsolePlugin.getDefault().getConsoleManager()
.addConsoles(new IConsole[] { fMessageConsole });
}
private static void initalize(String title) {
fMessageConsole = new MessageConsole(title, null);
}
}