ConsoleView.java 5.31 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
/*******************************************************************************
 * Copyright (c) 2006 Elphel, Inc and Excelsior, LLC.
 * This file is a part of Eclipse/VDT plug-in.
 * Eclipse/VDT plug-in is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your option)
 * any later version.
 *
 * Eclipse/VDT plug-in is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with Eclipse VDT plug-in; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *******************************************************************************/
package com.elphel.vdt.ui.views;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.text.IDocument;
import org.eclipse.swt.SWT;
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.MessageConsole;
import org.eclipse.ui.console.MessageConsoleStream;

/**
 * Create an instance of this class in any of your plugin classes.
 * 
 * Use it as follows ...
 * 
 * ConsoleView.getDefault().println("Some error msg", ConsoleDisplayMgr.MSG_INFORMATION);
 * ...
 * ...
 * ConsoleView.getDefault().clear();
 * ...
 * 
 *   This code was taken from an article available at
 *      http://www.eclipsezone.com/eclipse/forums/t52910.html
 */
public class ConsoleView {
	
    private static ConsoleView fDefault = new ConsoleView("My console view");
    private String fTitle = null;
    private MessageConsole fMessageConsole = null;
    
    public static final int MSG_INFORMATION = 1;
    public static final int MSG_ERROR = 2;
    public static final int MSG_WARNING = 3;
    
    public ConsoleView(String messageTitle) {     
        fDefault = this;
        fTitle = messageTitle;
    }
    
    public static ConsoleView getDefault() {
        return fDefault;
    }  
    
    public void println(String msg, int msgKind)
    {     
        if(msg == null)
            return;
        
        // if console-view in Java-perspective is not active, then show it and
        // then display the message in the console attached to it      
        if(!displayConsoleView()) {
            // If an exception occurs while displaying in the console, then just diplay atleast the same in a message-box
            MessageDialog.openError(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), "Error", msg);
            return;
        }
        
        // display message on console
        getNewMessageConsoleStream(msgKind).println(msg);           
    }
    
    public void clear()
    {     
        IDocument document = getMessageConsole().getDocument();
        if (document != null) {
            document.set("");
        }        
    }  
    
    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;
    }
    
    private MessageConsoleStream getNewMessageConsoleStream(int msgKind)
    {     
        int swtColorId = SWT.COLOR_DARK_GREEN;
        
        switch (msgKind) {
            case MSG_INFORMATION:
                swtColorId = SWT.COLOR_BLACK;
                break;
                
            case MSG_ERROR:
                swtColorId = SWT.COLOR_RED;
                break;
                
            case MSG_WARNING:
                swtColorId = SWT.COLOR_DARK_BLUE;
                break;
                
            default:          
                swtColorId = SWT.COLOR_DARK_MAGENTA;
        }  
        
        MessageConsoleStream msgConsoleStream = getMessageConsole().newMessageStream();     
        msgConsoleStream.setColor(Display.getCurrent().getSystemColor(swtColorId));
        return msgConsoleStream;
    }
    
    private MessageConsole getMessageConsole()
    {
        if( fMessageConsole == null )
            createMessageConsoleStream(fTitle); 
        
        return fMessageConsole;
    }
    
    private void createMessageConsoleStream(String title)
    {
        fMessageConsole = new MessageConsole(title, null); 
        ConsolePlugin.getDefault().getConsoleManager().addConsoles(new IConsole[]{ fMessageConsole });
    }  
}