【问题标题】:Adding combo box on toolbar of eclipse using Eclipse plug-in development API使用 Eclipse 插件开发 API 在 Eclipse 工具栏上添加组合框
【发布时间】:2018-05-24 21:06:15
【问题描述】:

我想使用 Eclipse 插件开发 API(不是 RCP 应用程序)向 Eclipse 的工具栏(coolbar)添加一个组合框。此组合框项目应动态添加/删除。

我知道在 RCP 应用程序中可以通过以下链接进行操作:http://www.thedeveloperspoint.com/?p=140

但我正在查看 Eclipse 插件 API。

任何帮助将不胜感激。

谢谢
赛姆

【问题讨论】:

    标签: eclipse eclipse-plugin


    【解决方案1】:

    这可以通过使用 2 个步骤来完成。

    步骤1:通过使用扩展点机制创建/添加工具栏到全局工具栏(使用locationURI作为“工具栏:org.eclipse.ui.main.toolbar”)

      <extension
         point="org.eclipse.ui.menus">
        <menuContribution
            allPopups="false"
            locationURI="toolbar:org.eclipse.ui.main.toolbar">
         <toolbar
               id="com.company.module.toolbar"
               label="Sample">
            <control
                  class="com.company.module.ui.ComboToolbarContribution"
                  id="ratata">
            </control>
        </toolbar>
       </menuContribution>
     </extension>
    

    第 2 步:实现 ComboToolbarContribution,如下所示。

    package com.company.module.ui;
    
    import org.eclipse.swt.SWT;
    import org.eclipse.swt.layout.GridData;
    import org.eclipse.swt.layout.GridLayout;
    import org.eclipse.swt.widgets.Combo;
    import org.eclipse.swt.widgets.Composite;
    import org.eclipse.swt.widgets.Control;
    import org.eclipse.ui.menus.WorkbenchWindowControlContribution;
    
    public class ComboToolbarContribution extends
        WorkbenchWindowControlContribution {
    private Combo mReader;
    
    public ComboToolbarContribution() {
    
    }
    
    @Override
    protected Control createControl(Composite parent) {
        Composite container = new Composite(parent, SWT.NONE);
        GridLayout glContainer = new GridLayout(1, false);
        glContainer.marginTop = -1;
        glContainer.marginHeight = 0;
        glContainer.marginWidth = 0;
        container.setLayout(glContainer);
        GridData glReader = new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1);
        glReader.widthHint = 280;
        mReader = new Combo(container, SWT.BORDER | SWT.READ_ONLY
                | SWT.DROP_DOWN);
        mReader.setLayoutData(glReader);
    
        return container;
    }
    
    @Override
    protected int computeWidth(Control control) {
        return 300;
    } }
    

    通过以上两个步骤,一个组合框将被添加到全局工具栏中,用户需要提供对组合框的全局访问权限。

    【讨论】:

      【解决方案2】:

      为了避免头痛.. 如果有人面临 Eclipse 仅绘制控件高度 7px 的问题,我想指出一个解决方法:

      https://www.eclipse.org/forums/index.php/t/1076367/

      在同一个工具栏(即 com.company.module.toolbar)中添加另一个带有图标的贡献,以保留足够的空间。

      【讨论】:

        【解决方案3】:

        正如上面提到的@Jonny,可能需要保留一些空间。

        <menuContribution
                allPopups="false"
                locationURI="toolbar:org.eclipse.ui.main.toolbar">
          <toolbar id="new">
            <control
                  class="org.xy.composite.NewComposite"
                  id="org.xy.composite.newcomposite.id">
            </control>
            <command
                  commandId="newcomposite"
                  icon="resources/nothing.png"
                  label="nix"/>
          </toolbar>
        </menuContribution>
        

        使用command 这是可能的

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-05-26
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多