Eclipse RCP应用程序定制工具栏 [英] Eclipse RCP application custom toolbar

查看:94
本文介绍了Eclipse RCP应用程序定制工具栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为RCP应用程序创建自定义工具栏。

I am creating a custom toolbar for my RCP application.


  1. 如图所示,我想创建一个带有其他三个文本的下拉框盒子。这些基本上是输入框,并且是相互依赖的。现在,这些盒子中的每一个都位于单独的类中。我想将它们放在一个类中,以便更轻松地彼此创建侦听器。

  1. As shown in figure I want to have a drop down box with three other text boxes. These are basically the input box and are interdependent. Right now each of these boxes are in separate classes. I want to bring them together in one class so it is easier to create listeners for each other.

protected void fillCoolBar(ICoolBarManager coolBar) {

IToolBarManager toolbar = new ToolBarManager(coolBar.getStyle());
coolBar.add(toolbar);      

Toolbar extraToolBar = new Toolbar("Toolbar");
toolbar.add(extraToolBar);
toolbar.add(new Separator());

toolbar.add(new MyCombo("Demo Combo box"));
toolbar.add(new Separator());

toolbar.add(new IPaddress("Ip"));
toolbar.add(new Separator());

toolbar.add(new Mask("Mask"));
toolbar.add(new Separator());

toolbar.add(new Count("Count"));

}

public class IPaddress extends ControlContribution {

 Text textBox;


 public IPaddress(String id) {
     super(id);
    // TODO Auto-generated constructor stub
 }

 @Override
 protected Control createControl(Composite parent) {
textBox = new Text(parent, SWT.MULTI | SWT.BORDER | SWT.WRAP);
textBox.setLayoutData(new GridData(GridData.FILL_BOTH));
textBox.addModifyListener(new ModifyListener(){
    public void modifyText(ModifyEvent event) {
        Text text = (Text) event.widget;
        System.out.println(text.getText());
    } 
});
return textBox;
}

}


  • 因此,我想创建一个新的自定义工具栏将拥有我想要的所有功能,然后将其粘贴到原始功能栏上。

  • Thus I want to create a new custom Toolbar will all the functionalities that I want and then stick it to the original. But somehow it only shows an empty bar on the left.

    protected Control createControl(Composite parent) {
    toolBar = new ToolBar(parent, SWT.FLAT |SWT.BORDER);
    
    Device dev = toolBar.getDisplay();
    
    try {
        newi = new Image(dev, "C:\\Users\\RahmanAs\\ChipcoachWorkspace\\ChipCoach\\icons\\FileClose.png");
        opei = new Image(dev, "C:\\Users\\RahmanAs\\ChipcoachWorkspace\\ChipCoach\\icons\\FileOpen.png");
    
    
    } catch (Exception e) {
        System.out.println("Cannot load images");
        System.out.println(e.getMessage());
        System.exit(1);
    }
    
    
    ToolItem item0 = new ToolItem (toolBar, SWT.PUSH);
    item0.setImage(newi);
    item0.setText("Hello");
    
    ToolItem item1 = new ToolItem(toolBar, SWT.PUSH);
    item1.setText("Push");
    
    ToolItem item2 = new ToolItem(toolBar, SWT.PUSH);
    item2.setText("Pull");
    
    
    return toolBar;
    
    
    }
    


  • 我也有运行按钮,这是我使用Vogella的教程在插件中创建的。但是我不能用这种方式对他们的展示位置进行编程。 (例如,如果我想在一开始就使用它们。)是否可以通过编程方式创建它们?

  • I also have run buttons, which I created in the plugin using Vogella's tutorial. But I cannot program their placements in this way. (For example if I want them in the beginning.) Is there a way to create them programmatically?


    推荐答案

    我认为最左边的 ToolBar 为空的原因是布局问题。在下面的代码中,当我没有 custom ToolBar 问题。 > ToolBar ,但仍在 main ToolBar 中。添加 foo和 bar按钮可解决布局问题,但我无法弄清楚对 layout() pack的正确调用()进行修复。我认为这可能与此处的错误有关。

    I think the reason your leftmost ToolBar is empty is a layout issue. In my code below, I had a similar "empty" ToolBar problem when I did not have any buttons located outside the custom ToolBar but still in the main ToolBar. Adding in the "foo" and "bar" buttons fixed the layout issue, but I could not figure out the right calls to layout() or pack() to fix it. I think this may be related to the bug here.

    我在创建类似的 ToolBar 时花了很多时间,并围绕 RCP邮件模板插件项目进行构建,您可以从中创建新插件项目向导。

    I took a swing at creating a similar ToolBar and built around the "RCP Mail Template" plugin-project that you can create from the "New Plug-in Project" wizard.

    为解决您的前两个问题,我在示例RCP捆绑包中创建了3个软件包(我将项目称为 com.bar .foo):

    To address your first two concerns, I created 3 packages in the example RCP bundle (I called my project "com.bar.foo"):


    1. com.bar.foo.actions -包含扩展了<$ c $的类c> ContributionControl 并包装 Combo Text 小部件。这些与数据模型无关,只是担心创建小部件。

    2. com.bar.foo.model -包含数据模型。我只是在这里用IP,掩码,网关和一个或两个有用的方法组成了一个简单的模型。

    3. com.bar.foo.toolBar -这些类通过 org.eclipse.ui.menus 扩展点插入到主UI ToolBar 中。它们将数据模型链接到第一个包中的 ContributionControls 。这里最重要的类是 ToolBarContribution ,它可以有效地集中所有侦听器。这使您更容易将小部件的侦听器链接到同一模型。

    1. com.bar.foo.actions - Contains classes that extend ContributionControl and wrap Combo and Text widgets. These have nothing to do with the data model and just worry about creating widgets.
    2. com.bar.foo.model - Contains the data model. I just made up a simple model here with an IP, mask, gateway, and one or two helpful methods.
    3. com.bar.foo.toolBar - These classes are plugged up to the main UI ToolBar via the org.eclipse.ui.menus extension point. They link the data model to the ContributionControls in the first package. The most important class here is ToolBarContribution, which effectively centralizes all of your listeners. This makes it easier for you to link the listeners for the widgets to the same model.

    以下是<$ c $的来源c> ToolBarContribution (请注意,它解决了您的前两个问题,因为它将侦听器连接到模型,并且提供了自己的 ToolBar 到用户界面):

    Here's the source for the ToolBarContribution (note that it addresses your first two concerns because it hooks up the listeners to the model and provides its own ToolBar to the UI):

    package com.bar.foo.toolBar;
    
    import org.eclipse.jface.action.Action;
    import org.eclipse.jface.action.ToolBarManager;
    import org.eclipse.swt.SWT;
    import org.eclipse.swt.events.SelectionAdapter;
    import org.eclipse.swt.events.SelectionEvent;
    import org.eclipse.swt.events.SelectionListener;
    import org.eclipse.swt.widgets.Combo;
    import org.eclipse.swt.widgets.Composite;
    import org.eclipse.swt.widgets.Control;
    import org.eclipse.swt.widgets.ToolBar;
    import org.eclipse.ui.menus.WorkbenchWindowControlContribution;
    
    import com.bar.foo.actions.ComboContributionItem;
    import com.bar.foo.actions.TextContributionItem;
    import com.bar.foo.model.NetworkConfig;
    
    public class ToolBarContribution extends WorkbenchWindowControlContribution {
    
        // Our data model.
        private NetworkConfig configuration = new NetworkConfig();
    
        // Each of these corresponds to a widget in the ToolBar.
        private Action scanAction;
        private ComboContributionItem sourceCombo;
        private TextContributionItem ipText;
        private TextContributionItem maskText;
        private TextContributionItem gatewayText;
    
        @Override
        protected Control createControl(Composite parent) {
    
            setupContributionItems();
    
            // Let's not get our hands messy with SWT... add IActions or
            // IContributionItems to a ToolBarManager and let the ToolBarManager
            // create the SWT ToolBar.
            ToolBarManager manager = new ToolBarManager();
            manager.add(scanAction);
            manager.add(sourceCombo);
            manager.add(ipText);
            manager.add(maskText);
            manager.add(gatewayText);
    
            ToolBar toolBar = manager.createControl(parent);
    
            // Highlight the ToolBar in red.
            toolBar.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_RED));
    
            return toolBar;
        }
    
        private void setupContributionItems() {
            scanAction = new Action("Scan Host") {
                @Override
                public void run() {
                    System.out.println("Scanning...");
                    String host = sourceCombo.getComboControl().getText();
                    configuration.scanHost(host);
                    System.out.println("Scanned!");
                    refreshTexts();
                }
            };
            scanAction.setToolTipText("Scans the host for a configuration.");
    
            final SelectionListener comboListener = new SelectionAdapter() {
                @Override
                public void widgetSelected(SelectionEvent e) {
                    ipText.getTextControl().setText("");
                    maskText.getTextControl().setText("");
                    gatewayText.getTextControl().setText("");
                }
            };
            sourceCombo = new ComboContributionItem("sourceCombo") {
                @Override
                public Control createControl(Composite parent) {
                    // Let ComboContributionItem create the initial control.
                    Control control = super.createControl(parent);
                    // Now customize the Combo widget.
                    Combo combo = getComboControl();
                    combo.setItems(configuration.getAvailableHosts());
                    combo.addSelectionListener(comboListener);
                    // Return the default control.
                    return control;
                }
            };
    
            ipText = new TextContributionItem("ipText", SWT.BORDER | SWT.SINGLE
                    | SWT.READ_ONLY);
            maskText = new TextContributionItem("maskText");
            gatewayText = new TextContributionItem("gatewayText");
        }
    
        private void refreshTexts() {
            ipText.getTextControl().setText(configuration.getIP());
            maskText.getTextControl().setText(configuration.getMask());
            gatewayText.getTextControl().setText(configuration.getGateway());
        }
    }
    

    除了此 ToolBar ,我在主界面 工具栏中有两个单独的按钮,一个在自定义之前,一个在之后 工具栏。它们的来源在com.bar.foo.toolBar包中。这是第一个命令:

    In addition to this ToolBar, I have two separate buttons in the main UI ToolBar, one before, and one after the custom ToolBar. Their sources are in the package com.bar.foo.toolBar. Here is the first command:

    package com.bar.foo.toolBar;
    
    import org.eclipse.core.commands.AbstractHandler;
    import org.eclipse.core.commands.ExecutionEvent;
    import org.eclipse.core.commands.ExecutionException;
    
    public class FooHandler extends AbstractHandler {
        @Override
        public Object execute(ExecutionEvent event) throws ExecutionException {
            System.out.println("foo");
            return null;
        }
    }
    

    这是第二个:

    package com.bar.foo.toolBar;
    
    import org.eclipse.core.commands.AbstractHandler;
    import org.eclipse.core.commands.ExecutionEvent;
    import org.eclipse.core.commands.ExecutionException;
    
    public class BarHandler extends AbstractHandler {
        @Override
        public Object execute(ExecutionEvent event) throws ExecutionException {
            System.out.println("bar");
            return null;
        }
    }
    

    因为我对您的数据不太了解,我必须创建自己的模型。包com.bar.foo.model中的模型只是一个类:

    Since I didn't know too much about your data, I had to create my own model. The model in the package com.bar.foo.model is just one class:

    package com.bar.foo.model;
    
    public class NetworkConfig {
    
        private String ip = "";
        private String mask = "";
        private String gateway = "";
    
        public String[] getAvailableHosts() {
            return new String[] { "fooHost" };
        }
    
        public void scanHost(String host) { 
            if ("fooHost".equals(host)) {
                ip = "192.168.1.2";
                mask = "255.255.255.0";
                gateway = "192.168.1.1";    
            } else {
                ip = "";
                mask = "";
                gateway = "";
            }
        }
    
        public String getIP() {
            return ip;
        }
        public String getMask() {
            return mask;
        }   
        public String getGateway() {
            return gateway;
        }   
    }
    

    现在使用com.bar.foo.actions包其中包含自定义 ToolBar 中的 ControlContributions 。请注意,这两个类均与模型无关,并且它们可以在产品的其他位置重复使用。

    Now for the com.bar.foo.actions package that contains the ControlContributions that go in the custom ToolBar. Note that neither of these two classes have anything to do with the model, and they can be re-used elsewhere in your product.

    头等舱只包装了 Combo 小部件。可以通过覆盖 controlCreated(Combo)方法来初始自定义窗口小部件。我在 ToolBarContribution 类中使用它来添加 SelectionListener 并设置 Combo 的项目。这是课程:

    The first class just wraps a Combo widget. The widget can be initially customized by overriding the controlCreated(Combo) method. I use that in the ToolBarContribution class to add a SelectionListener and set the Combo's items. Here's the class:

    package com.bar.foo.actions;
    
    import org.eclipse.jface.action.ControlContribution;
    import org.eclipse.swt.SWT;
    import org.eclipse.swt.widgets.Combo;
    import org.eclipse.swt.widgets.Composite;
    import org.eclipse.swt.widgets.Control;
    
    public class ComboContributionItem extends ControlContribution {
    
        private Combo combo;
    
        public ComboContributionItem(String id) {
            super(id);
        }
    
        @Override
        protected Control createControl(Composite parent) {
            combo = new Combo(parent, SWT.READ_ONLY | SWT.V_SCROLL | SWT.H_SCROLL);
            return combo;
        }
    
        @Override
        public int computeWidth(Control control) {
            // The widget is now 100 pixels. You can new GC gc = new GC(control) and
            // use the gc.stringExtent(String) method to help compute a more dynamic
            // width.
            return 100;
        }
    
        public Combo getComboControl() {
            return combo;
        }
    }
    

    此程序包中的其他类包装了文本小部件:

    The other class in this package wraps a Text widget:

    package com.bar.foo.actions;
    
    import org.eclipse.jface.action.ControlContribution;
    import org.eclipse.swt.SWT;
    import org.eclipse.swt.widgets.Composite;
    import org.eclipse.swt.widgets.Control;
    import org.eclipse.swt.widgets.Text;
    
    public class TextContributionItem extends ControlContribution {
    
        private final int style;
        private Text text;
    
        public TextContributionItem(String id) {
            this(id, SWT.BORDER | SWT.SINGLE);
        }
    
        public TextContributionItem(String id, int style) {
            super(id);
            this.style = style;
        }
    
        @Override
        protected Control createControl(Composite parent) {
            text = new Text(parent, style);
            return text;
        }
    
        @Override
        public int computeWidth(Control control) {
            return 100;
        }
    
        public Text getTextControl() {
            return text;
        }
    }
    

    我没有这样做,但是如果您需要为了进一步为 ToolBar 自定义 Text 小部件,您可以覆盖 createControl(Composite) 方法就像初始化 ComboContributionItem 时一样。

    I didn't do this, but if you need to further customize the Text widget for your ToolBar, you can override the createControl(Composite) method just like I did when initializing the ComboContributionItem.

    现在最后一件事:我使用扩展程序自定义了 ToolBar 。但是, ToolBarContribution 使用的相同逻辑适用于您的 fillCoolBar(ICoolBarManager)方法或 createControl(Composite)方法,具体取决于您最终希望修改哪个 ToolBar

    Now one last thing: I used extensions to customize the ToolBar. However, the same logic used by ToolBarContribution applies to your fillCoolBar(ICoolBarManager) method or your createControl(Composite) method, depending on which ToolBar you ultimately wish to modify.

    就我而言,这是我在插件 plugin.xml 末尾添加的内容:

    In my case, here's what I added to the end of the plugin's plugin.xml:

    <extension
          point="org.eclipse.ui.menus">
       <menuContribution
             locationURI="toolbar:org.eclipse.ui.main.toolbar">
          <toolbar
                id="com.bar.foo.toolbar">
             <command
                   commandId="com.bar.foo.commands.foo"
                   label="Foo"
                   style="push">
             </command>
             <control
                   class="com.bar.foo.toolBar.ToolBarContribution">
             </control>
             <command
                   commandId="com.bar.foo.commands.bar"
                   label="Bar"
                   style="push">
             </command>
          </toolbar>
       </menuContribution>
    </extension>
    <extension
          point="org.eclipse.ui.commands">
       <command
             id="com.bar.foo.commands.foo"
             name="Foo">
       </command>
       <command
             id="com.bar.foo.commands.bar"
             name="Bar">
       </command>
    </extension>
    <extension
          point="org.eclipse.ui.handlers">
       <handler
             class="com.bar.foo.toolBar.FooHandler"
             commandId="com.bar.foo.commands.foo">
       </handler>
       <handler
             class="com.bar.foo.toolBar.BarHandler"
             commandId="com.bar.foo.commands.bar">
       </handler>
    </extension>
    

    连接了命令,以便有一个用于 FooHandler custom ToolBar 之前的c $ c>和 BarHandler 的按钮>自定义 工具栏。在xml中指定这些命令的顺序将反映在应用程序中。同样,将项目添加到 custom ToolBar 的顺序将反映在您的产品中。

    The commands are hooked up so that there's a button for FooHandler before the custom ToolBar and a button for BarHandler after the custom ToolBar. The order in which these commands are specified in the xml will be reflected in the application. Likewise, the order in which the items are added to the custom ToolBar will reflect in your product.

    关于放置的另一条注释:您可以通过在locationURI的查询中设置放置来使menuContributions出现在不同的位置,例如 toolbar:org.eclipse.ui.main.toolbar?after =添加。 之前是另一个放置关键字,例如之后。有关更多示例,请参见在此Eclipse帮助文档中

    Another note on placement: You can make the menuContributions appear in different places by setting a placement in the locationURI's query, e.g., toolbar:org.eclipse.ui.main.toolbar?after=additions. "before" is another placement keyword like "after". More examples of this can be found in this Eclipse help doc.

    这篇关于Eclipse RCP应用程序定制工具栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆