【问题标题】:Filtering Project explorer context menu筛选项目资源管理器上下文菜单
【发布时间】:2015-05-10 04:03:12
【问题描述】:

我在 Eclipse 项目资源管理器中有不同类型的项目,比如普通的 Java 项目和自定义项目。我有自定义项目的菜单贡献。右键单击自定义项目,我只想显示项目相关菜单,现在它显示所有菜单选项。我尝试通过附加 IMenuListener 来删除菜单,但我认为这不是正确的方法。使用活动似乎很好,但我不知道如何使用它来过滤上下文基础上的菜单: 这就是我删除不需要的菜单的方式,我已将此侦听器附加到 IMenumanager 中。但是在第一次右键单击时,所有菜单都出现了

  private final class MenuListener implements IMenuListener2
  {
    @Override
    public void menuAboutToShow(IMenuManager manager)
    {
      ISelection selection = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService().getSelection(IPageLayout.ID_PROJECT_EXPLORER);
      if (selection != null && !selection.isEmpty())
      {
        if (selection instanceof IStructuredSelection)
        {
          Object obj = ((IStructuredSelection) selection).getFirstElement();

          if (applContainerPorjectNatureChecker.apply(obj))
          {
            IContributionItem[] items = manager.getItems();
            for (IContributionItem item : items)
            {
              if (ids.contains(item.getId()))
              {
                item.setVisible(false);
              }
            }
          }
        }
      }
    }

    @Override
    public void menuAboutToHide(IMenuManager manager)
    {
      // TODO:Do Nothing
    }
  }

【问题讨论】:

  • 添加了代码sn-p
  • 我认为这真的很难(如果可以做到的话)。
  • 菜单仅在第二次右键单击后才消失,但在第一次右键单击时不会消失,我不知道为什么:(

标签: eclipse eclipse-plugin eclipse-rcp


【解决方案1】:

在方法 menuAboutToShow 中

public void menuAboutToShow(IMenuManager manager) {
    // Get the selection object
    // if selectedObj instanceof CustumProject {
         // get the list of customProject actions which you want to add
         // Add it like below:
         for (Action action : actionList)
             menuManager.add(action(action.getId()));
     }
}

【讨论】:

  • 感谢您的意见。是的,我也在做同样的事情,但是所有菜单都在第一次右键单击时启用,换句话说,第一次右键单击时不会发生任何变化:(
猜你喜欢
  • 2014-06-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-12
相关资源
最近更新 更多