带有Eclipse Look的Eclipse RCP工具栏按钮

时间:2021-11-12 18:59:55

In Eclipse, its easy to specify buttons for your toolbar using the ActionSets extension point. However, when I need to specify some items programmatically, I can't get the same look. I don't believe that the framework is using native buttons for these, but so far, I can't find the right recipe to match the Eclipse look. I wanted to see if anyone has found the right snippet to duplicate this functionality in code.

在Eclipse中,使用ActionSets扩展点可以轻松指定工具栏的按钮。但是,当我需要以编程方式指定一些项目时,我无法获得相同的外观。我不相信框架使用本机按钮,但到目前为止,我找不到与Eclipse外观相匹配的正确配方。我想看看是否有人找到了在代码中复制此功能的正确代码段。

2 个解决方案

#1


5  

It's difficult to tell from your question, but it sounds like you may be attempting to add a ControlContribution to the toolbar and returning a Button. This would make the button on the toolbar appear like a native button like you seem to be describing. This would look something like this:

从您的问题中很难说出来,但听起来您可能正在尝试将ControlContribution添加到工具栏并返回Button。这将使工具栏上的按钮看起来像您似乎在描述的本机按钮。这看起来像这样:

IToolBarManager toolBarManager = actionBars.getToolBarManager();
toolBarManager.add(new ControlContribution("Toggle Chart") {
    @Override
    protected Control createControl(Composite parent)
    {
        Button button = new Button(parent, SWT.PUSH);
        button.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent e) {
                // Perform action
            }
        });
     }
});

Instead you should add an Action to the toolbar. This will create a button on the toolbar that matches the standard eclipse toolbar buttons. This would look something like this:

相反,您应该向工具栏添加一个Action。这将在工具栏上创建一个与标准eclipse工具栏按钮匹配的按钮。这看起来像这样:

Action myAction = new Action("", imageDesc) {
    @Override
    public void run() {
        // Perform action
    }
};

IToolBarManager toolBarManager = actionBars.getToolBarManager();
toolBarManager.add(myAction);

#2


2  

Could you perhaps put in an extract of the code you have for adding actions programmatically to the toolbar? I assume you do this in an ApplicationActionBarAdvisor class? Their should be no difference in the look of buttons you add declaratively vs those you add programatically.

您是否可以将用于以编程方式添加操作的代码提取到工具栏中?我假设您在ApplicationActionBarAdvisor类中执行此操作?它们与你以编程方式添加的按钮的外观相比应该没有区别。

#1


5  

It's difficult to tell from your question, but it sounds like you may be attempting to add a ControlContribution to the toolbar and returning a Button. This would make the button on the toolbar appear like a native button like you seem to be describing. This would look something like this:

从您的问题中很难说出来,但听起来您可能正在尝试将ControlContribution添加到工具栏并返回Button。这将使工具栏上的按钮看起来像您似乎在描述的本机按钮。这看起来像这样:

IToolBarManager toolBarManager = actionBars.getToolBarManager();
toolBarManager.add(new ControlContribution("Toggle Chart") {
    @Override
    protected Control createControl(Composite parent)
    {
        Button button = new Button(parent, SWT.PUSH);
        button.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent e) {
                // Perform action
            }
        });
     }
});

Instead you should add an Action to the toolbar. This will create a button on the toolbar that matches the standard eclipse toolbar buttons. This would look something like this:

相反,您应该向工具栏添加一个Action。这将在工具栏上创建一个与标准eclipse工具栏按钮匹配的按钮。这看起来像这样:

Action myAction = new Action("", imageDesc) {
    @Override
    public void run() {
        // Perform action
    }
};

IToolBarManager toolBarManager = actionBars.getToolBarManager();
toolBarManager.add(myAction);

#2


2  

Could you perhaps put in an extract of the code you have for adding actions programmatically to the toolbar? I assume you do this in an ApplicationActionBarAdvisor class? Their should be no difference in the look of buttons you add declaratively vs those you add programatically.

您是否可以将用于以编程方式添加操作的代码提取到工具栏中?我假设您在ApplicationActionBarAdvisor类中执行此操作?它们与你以编程方式添加的按钮的外观相比应该没有区别。