【问题标题】:Dynamic population of dropdown list added to Eclipse toolbar within Eclipse plugin在 Eclipse 插件中添加到 Eclipse 工具栏的下拉列表的动态填充
【发布时间】:2021-05-26 18:09:09
【问题描述】:

主题似乎在 Eclipse 插件开发的其他领域很容易,例如在对话框中自己的工具栏。在这种情况下,我通过 plugin.xml 向我的视角中的主工具栏添加了一个下拉菜单,如下所示

<menuContribution
        locationURI="toolbar:org.eclipse.ui.main.toolbar">
    <toolbar
            id="com.company.ui.toolbar2">
            // other items removed for clarity

        // following is a drop-down
        <command
                commandId="com.company.ui.mydropdown"
                label="%config.label.default"
                icon="icons/myIcon.png"
                style="pulldown">
            <visibleWhen checkEnabled="false">
                <reference definitionId="com.company.ui.visibilityTester"/>
            </visibleWhen>
        </command>
    </toolbar>
</menuContribution>

我已经在别处定义了命令,并且在 plugin.xml 文件中定义了一个处理程序,该处理程序当前的工作方式与按钮一样。

但是,下拉列表未填充。我需要一种动态填充的方法,原因如下:

  1. 下拉列表内容来自 API
  2. 其中一个列表项(或像单击按钮一样单击列表)会启动一个可以编辑列表的对话框
  3. 关闭对话框意味着需要刷新(重建)列表并重新评估选定/活动项目

我还需要能够挂钩选择不同列表项并调用我的 API 的用户。

我注意到网上的其他示例尝试以不同的方式创建下拉列表,但我对这些连接方式存在知识空白。例如,在使用 Contribution 的地方,它如何链接回 plugin.xml 文件中的特定定义并不总是很清楚。我还没有看到一个清楚地表明这一点的例子。

我需要支持 Eclipse 4.4.2 及更高版本,因此当前的 Eclipse 4 示例可能不合适。

任何帮助表示赞赏。

【问题讨论】:

    标签: eclipse plugins swt


    【解决方案1】:

    答案是在菜单贡献的工具栏部分添加一个控件,例如:

    <menuContribution locationURI="toolbar:org.eclipse.ui.main.toolbar">
        <toolbar id="com.company.ui.customToolbar">
            <control
                class="com.company.ui.MyDropDownContribution"
                icon="icons/yourIconHere.png"
                label="%config.label.default">
                id="theDropdown">
                <visibleWhen checkEnabled="false">
                    <reference definitionId="com.company.ui.isMyTestCaseSatisfied"/>
                </visibleWhen>
            </control>
        </toolbar>
    </menuContribution>
    

    然后实现一个扩展WorkbenchWindowControlContribution的贡献类。我的简化代码,包括从列表项之一启动 EXE 的方法(并且可以重新用于做其他事情),基本上是:

    public class MyDropDownContribution extends
            WorkbenchWindowControlContribution {
    
        private Combo dropdownlist;
    
        private boolean listenerAdded = false;
        private String currentlySelected = "Item 1";
        private String[] listOfItems = new String[]{ "Item 1", "Item 2", "Item 3", "Launch my EXE" };
    
        private final SelectionAdapter selectionListener = new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                if(e.widget instanceof Combo) {
                    // if the selection is currentlySelected, do nothing
                    if(dropdownlist.getText().equals(currentlySelected)) {
                        return;
                    }
    
                    //if the selection is to launch 'something', do that
                    if(dropdownlist.getText().equals("Launch my EXE")) {
                        launchExecutable();
                    } else {
                        // otherwise, a new item has been selected, do what you need to
                    }
                }
            }
        };
    
    
        private void launchExecutable() {
            // place your code to launch your EXE here - you could do some other special case, not just launch EXE
        }
    
    
        @Override
        protected Control createControl(Composite parent) {
            Composite container = new Composite(parent, SWT.NONE);
            GridLayout glContainer = new GridLayout(1, false);
            glContainer.marginTop = 0;
            glContainer.marginHeight = 0;
            glContainer.marginWidth = 0;
            container.setLayout(glContainer);
            GridData glReader = new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1);
            glReader.widthHint = 150;
            dropdownlist = new Combo(container, SWT.BORDER | SWT.READ_ONLY
                    | SWT.DROP_DOWN);
            dropdownlist.setLayoutData(glReader);
            populateDropdown();
            toggleComboListener();
            return container;
        }
    
        private void toggleComboListener() {
            if(listenerAdded) {
                dropdownlist.removeSelectionListener(selectionListener);
            } else {
                dropdownlist.addSelectionListener(selectionListener);
            }
            listenerAdded = !listenerAdded;
        }
    
        public void populateDropdown() {
            PlatformUI.getWorkbench().getDisplay().syncExec(new Runnable() {
                @Override
                public void run() {
                    dropdownlist.setItems(new String[]{});
                    for(String itemName : listOfItems) {
                        dropdownlist.add(itemName);
                    }
                    dropdownlist.setText(currentlySelected);
                }
            });
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-17
      相关资源
      最近更新 更多