eclipse-pluginconsoleeclipse-rcp

Add custom console to Eclipse console list


Based on this tutorial, I'm creating an Eclipse plugin which provides a new console, The console is added to the view and I can print messages there, but for some reason it is not added to the consoles list (the dropdown list in the view's corner, see image below).

This is how I'm creating the console:

public void createConsole(String name) {
    ConsolePlugin plugin = ConsolePlugin.getDefault();
    IConsoleManager consoleManager = plugin.getConsoleManager();

    console = new MessageConsole(name, null);
    consoleManager.addConsoles(new IConsole[]{console});
}

And then I can print messages using this method:

public void print(String msg) {
    MessageConsoleStream out = console.newMessageStream();
    out.println(msg);
}

Also I'm using this method to bring the console view to the front:

public void bringToFront() {
    try{
        IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
        String id = IConsoleConstants.ID_CONSOLE_VIEW;
        IConsoleView view = (IConsoleView) page.showView(id);
        view.display(console);
    } catch(PartInitException e) {
        e.printStackTrace();
    }
}

enter image description here

Any suggestions?


Solution

  • To add a new type of console to the console view, you need to provide a consoleFactories extension:

    <extension
          point="org.eclipse.ui.console.consoleFactories">
       <consoleFactory
             class="com.example.MyConsoleFactory"
             icon="icons/etool16/my-console.png"
             label="My Console">
       </consoleFactory>
    </extension>
    

    The factory class needs to provide an implementation for openConsole in which your console is created and shown, just like you did in your existing code:

    class ConsoleFactory implements IConsoleFactory {
       @Override
       public void openConsole() {
          IConsoleManager consoleManager =
             ConsolePlugin.getDefault().getConsoleManager();
          MyConsole console = new MyConsole();
          consoleManager.addConsoles( new IConsole[] { console } );
          consoleManager.showConsoleView( console );
       }
    }