java - Eclipse 插件 : Populating dynamically a menu with commands and corresponding handlers

标签 java dynamic eclipse-plugin

我正在尝试在 Eclipse 中创建一个插件,当右键单击包资源管理器中的文件或文件夹时,它会添加一个菜单项。该菜单项包含某些必须动态加载的命令(因此我不能在 plugin.xml 中对它们进行硬编码)。这些命令实际上位于另一个扩展中,它通过我提供的扩展点将自身附加到我的插件。

我现在正在做的是创建菜单项,用附加到我的扩展点的所有命令填充它,并向所有这些命令添加一个 ButtonListener。一切都是这样,但有一个问题。我需要获取有关用户在包资源管理器中右键单击了哪个项目的信息(它是 IFile 还是 IFolder 项目?),根据我的研究,我需要在处理程序中执行此操作,如下所示:

public Object execute(ExecutionEvent event) throws ExecutionException {

    ISelection currentSelection = HandlerUtil.getCurrentSelection(event);
    TreeSelection treeSelection = (TreeSelection) currentSelection;
    Object firstElement = treeSelection.getFirstElement();

    ActionMenu.setNode(firstElement);

    return null;
}

其中 Action menu 是表示菜单项的实际类,负责填充它。不幸的是,我的插件中现在从未调用过该代码。

我的问题是:

我如何为每个命令动态创建一个处理程序以便调用 execute() 函数?或者在我的 ActionMenu 类中是否有更好的方法来获取用户右键单击的选择(以了解它是 IFolder 还是 IFile)?

这是我的 ActionMenu 类中的相关代码(将每个命令添加为子菜单项的 fill() 函数和 ButtonListener 嵌套类)。 ICommand 是命令必须在其他扩展中实现的接口(interface):

...

public final void fill(final Menu menu, final int index) {
    // Here you could get selection and decide what to do
    // You can also simply return if you do not want to show a menu

    List<ICommand> commands = new ArrayList<ICommand>();
    IConfigurationElement[] contributions = Platform.getExtensionRegistry().getConfigurationElementsFor(Extension_Point_ID);

    try {
        for (IConfigurationElement e : contributions) {
            final Object o =
                e.createExecutableExtension("class");
            commands.add((ICommand) o);
        }
    } catch (CoreException ex) {
        System.out.println(ex.getMessage());
    }

    for (ICommand command : commands)
    {
        MenuItem menuItem = new MenuItem(menu, SWT.CHECK, index);
        menuItem.setText(command.getName());
        menuItem.addSelectionListener(new ButtonListener(command));
    }
}

public class ButtonListener extends SelectionAdapter {

    ICommand command_;

    public ButtonListener(ICommand command) {
        command_ = command;
    }

    @Override
    public final void widgetSelected(final SelectionEvent e) {

        if (node_ instanceof org.eclipse.core.resources.IFile) {
            command_.executeFile(node_);
        }

        else if (node_ instanceof org.eclipse.core.resources.IFolder) {
            command_.executeFolder(node_);
        }
    }
}

这是我的 plugin.xml 文件的内容:

<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.4"?>
<plugin>
   <extension-point id="ca.polymtl.log8430.Command" name="Command" schema="schema/ca.polymtl.log8430.Command.exsd"/>

<extension point="org.eclipse.ui.menus">
    <menuContribution locationURI="popup:org.eclipse.ui.navigator.ProjectExplorer#PopupMenu?after=additions">
    <menu
        id="apply_command"
        label="ApplyCommand">
    </menu>
    </menuContribution>

    <menuContribution locationURI="popup:apply_command?after=additions">
        <dynamic
                  class="ca.polymtl.log8430.popup.ActionMenu"
                  id="ca.polymtl.log8430.popup.actionMenu">   
        </dynamic>
    </menuContribution>
</extension>

 <extension
   point="org.eclipse.ui.handlers">
    <handler
      class="ca.polymtl.log8430.Handler"
      commandId="ca.polymtl.log8430.Command1">
    </handler>
 </extension>
 <extension
   point="org.eclipse.ui.commands">
    <command
      id="ca.polymtl.log8430.Command1"
      name="Command_1">
    </command>
 </extension>

</plugin>

感谢您的帮助,如果您需要更多详细信息,请随时询问。

最佳答案

鉴于您设置菜单的方式,您不需要使用处理程序。您可以简单地在监听器的 widgetSelected 方法中获取当前选择:

IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();

IStructuredSelection selection = (IStructuredSelection)page.getSelection();

注意:实际选择可能不会直接实现IFileIFolder,所以你应该使用平台适配管理器看它们是否适配到资源:

Object selObject = selection.getFirstElement();

IResource resource = (IResource)Platform.getAdapterManager().getAdapter(selObject, IResource.class);

然后测试resource看它是IFile还是IFolder

关于java - Eclipse 插件 : Populating dynamically a menu with commands and corresponding handlers,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22256387/

相关文章:

java - JSprit算法软硬时间窗

javascript - jQuery 动态内容如何工作?

ruby-on-rails - Rails - 'send' 和 'try' 的混合

java - 在 Eclipse 插件运行时询问用户 "object"

java - Java 中的 PartInitException?

java - 在 OSGI Bundle 中查找文件和文件夹

java - 如何计算最近的餐厅,但我的代码显示了附近的所有餐厅

java - Java 中 NaN 的数学规则

java - 在代码中编写此公式是否有更清晰/更简单的方法?

c - C 中的动态内存与结构体