imakerplugin/com.nokia.s60tools.imaker/src/com/nokia/s60tools/imaker/internal/console/IMakerConsole.java
changeset 0 61163b28edca
equal deleted inserted replaced
-1:000000000000 0:61163b28edca
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 *
       
    16 */
       
    17 
       
    18 package com.nokia.s60tools.imaker.internal.console;
       
    19 
       
    20 import org.eclipse.jface.dialogs.MessageDialog;
       
    21 import org.eclipse.jface.resource.ImageDescriptor;
       
    22 import org.eclipse.jface.text.IDocument;
       
    23 import org.eclipse.swt.SWT;
       
    24 import org.eclipse.swt.widgets.Display;
       
    25 import org.eclipse.ui.IWorkbenchPage;
       
    26 import org.eclipse.ui.IWorkbenchWindow;
       
    27 import org.eclipse.ui.PartInitException;
       
    28 import org.eclipse.ui.PlatformUI;
       
    29 import org.eclipse.ui.console.ConsolePlugin;
       
    30 import org.eclipse.ui.console.IConsole;
       
    31 import org.eclipse.ui.console.IConsoleConstants;
       
    32 import org.eclipse.ui.console.MessageConsole;
       
    33 import org.eclipse.ui.console.MessageConsoleStream;
       
    34 
       
    35 import com.nokia.s60tools.imaker.IMakerPlugin;
       
    36 
       
    37 public class IMakerConsole {
       
    38 	
       
    39 	private static final String IMAKER_CONSOLE_NAME = "iMaker";
       
    40 	private static IMakerConsole fDefault = null;
       
    41 	private MessageConsole fMessageConsole = null;
       
    42 	
       
    43 	public static final int MSG_INFORMATION = 1;
       
    44 	public static final int MSG_ERROR = 2;
       
    45 	public static final int MSG_WARNING = 3;
       
    46 		
       
    47 	private IMakerConsole() {}
       
    48 	
       
    49 	public static IMakerConsole getDefault() {
       
    50 		if(fDefault==null) {
       
    51 			fDefault = new IMakerConsole();
       
    52 		}
       
    53 		return fDefault;
       
    54 	}
       
    55 		
       
    56 	public void println(String msg, int msgKind) {
       
    57 		if( msg == null ) return;
       
    58 		
       
    59 		/* if console-view in Java-perspective is not active, then show it and
       
    60 		 * then display the message in the console attached to it */		
       
    61 		if( !displayConsoleView() )
       
    62 		{
       
    63 			/*If an exception occurs while displaying in the console, then just diplay atleast the same in a message-box */
       
    64 			MessageDialog.openError(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), "Error", msg);
       
    65 			return;
       
    66 		}
       
    67 		
       
    68 		/* display message on console */	
       
    69 		getNewMessageConsoleStream(msgKind).println(msg);				
       
    70 	}
       
    71 	
       
    72 	public void clear() {		
       
    73 		final IDocument document = getMessageConsole().getDocument();
       
    74 		if (document != null) {
       
    75 			PlatformUI.getWorkbench().getDisplay().syncExec(new Runnable() {
       
    76 				
       
    77 //				@Override
       
    78 				public void run() {
       
    79 					document.set("");
       
    80 				}
       
    81 			});
       
    82 		}			
       
    83 	}	
       
    84 		
       
    85 	private boolean displayConsoleView() {
       
    86 		try
       
    87 		{
       
    88 			IWorkbenchWindow activeWorkbenchWindow = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
       
    89 			if( activeWorkbenchWindow != null )
       
    90 			{
       
    91 				IWorkbenchPage activePage = activeWorkbenchWindow.getActivePage();
       
    92 				if( activePage != null )
       
    93 					activePage.showView(IConsoleConstants.ID_CONSOLE_VIEW, null, IWorkbenchPage.VIEW_VISIBLE);
       
    94 			}
       
    95 			
       
    96 		} catch (PartInitException partEx) {			
       
    97 			return false;
       
    98 		}
       
    99 		
       
   100 		return true;
       
   101 	}
       
   102 	
       
   103 	public MessageConsoleStream getNewMessageConsoleStream(final int kind) {		
       
   104 		int swtColorId = SWT.COLOR_DARK_GREEN;
       
   105 		
       
   106 		switch (kind) {
       
   107 			case MSG_INFORMATION:
       
   108 				swtColorId = SWT.COLOR_DARK_GREEN;				
       
   109 				break;
       
   110 			case MSG_ERROR:
       
   111 				swtColorId = SWT.COLOR_DARK_MAGENTA;
       
   112 				break;
       
   113 			case MSG_WARNING:
       
   114 				swtColorId = SWT.COLOR_DARK_BLUE;
       
   115 				break;
       
   116 			default:				
       
   117 		}	
       
   118 		final int colorID = swtColorId;
       
   119 		final MessageConsoleStream msgConsoleStream = getMessageConsole().newMessageStream();
       
   120 		final Display display = PlatformUI.getWorkbench().getDisplay();
       
   121 		display.syncExec(new Runnable() {
       
   122 			
       
   123 //			@Override
       
   124 			public void run() {
       
   125 				msgConsoleStream.setColor(display.getSystemColor(colorID));
       
   126 			}
       
   127 		});
       
   128 		return msgConsoleStream;
       
   129 	}
       
   130 	
       
   131 	public MessageConsole getMessageConsole() {
       
   132 		if( fMessageConsole == null )
       
   133 			createMessageConsoleStream();		
       
   134 		return fMessageConsole;
       
   135 	}
       
   136 		
       
   137 	private void createMessageConsoleStream() {
       
   138 		fMessageConsole = new MessageConsole(getProductConsoleName(), getProductConsoleImageDescriptor()); 
       
   139 		ConsolePlugin.getDefault().getConsoleManager().addConsoles(new IConsole[]{ fMessageConsole });
       
   140 	}	
       
   141 	
       
   142 	private ImageDescriptor getProductConsoleImageDescriptor() {
       
   143 		return IMakerPlugin.getImageDescriptor("icons/imakerplugin_icon.png");
       
   144 	}
       
   145 
       
   146 	private String getProductConsoleName() {
       
   147 		return IMAKER_CONSOLE_NAME;
       
   148 	}
       
   149 }