在我的eclipse插件的控制台中终止按钮

时间:2013-04-23 13:44:34

标签: eclipse-plugin

我在xtext中开发了一个eclipse插件,我需要在控制台中编写一些消息。 为此,我看到了这个网站http://wiki.eclipse.org/FAQ_How_do_I_write_to_the_console_from_a_plug-in%3F,然后我实现了这个代码:

private static MessageConsole findConsole(String name) {

    if (ConsolePlugin.getDefault() == null)
        return null;
    ConsolePlugin plugin = ConsolePlugin.getDefault();
    IConsoleManager conMan = plugin.getConsoleManager();
    IConsole[] existing = conMan.getConsoles();
    for (int i = 0; i < existing.length; i++)
        if (name.equals(existing[i].getName())) {
            conMan.showConsoleView(existing[i]);
            return (MessageConsole) existing[i];
        }
    // no console found, so create a new one
    MessageConsole myConsole = new MessageConsole(name, null);
    conMan.addConsoles(new IConsole[] { myConsole });
    return myConsole;
}

public MessageConsoleStream getMessageStream() {
    MessageConsole myConsole = findConsole("console");
    if (myConsole != null) {

        IWorkbench wb = PlatformUI.getWorkbench();
        IWorkbenchWindow win = wb.getActiveWorkbenchWindow();
        IWorkbenchPage page = win.getActivePage();
        String id = IConsoleConstants.ID_CONSOLE_VIEW;
        IConsoleView view;
        try {

            view = (IConsoleView) page.showView(id);
            view.display(myConsole);

            return myConsole.newMessageStream();
        } catch (PartInitException e) {
            e.printStackTrace();
        }
    }
    return null;
}

我已将org.eclipse.ui.console添加到plugin.xml&gt;依赖关系&gt;需要的插件。

当我想打印一些消息时: MessageConsoleStream out = getMessageStream(); 通过out.println(...);

它正在发挥作用。但我需要在我的控制台中使用“终止按钮”,似乎这段代码还不够。 我怎样才能做到这一点? 感谢。

1 个答案:

答案 0 :(得分:1)

这完全与控制台无关。您想要创建一个viewContribution,只需在现有视图的工具栏区域添加一个按钮即可。还有一个example on stackoverflow。或者您可以查阅Eclipse help on that topic