2

我正在尝试制作一个 Eclipse 插件,该插件能够使用第一个插件提供的扩展点根据其他插件动态更改导航视图的上下文菜单。

带一点示意图:http ://hpics.li/bddf781

目的是插件 2、3 ... 提供的所有命令都可以在导航上下文菜单中访问,这要归功于插件 1

我已经用谷歌搜索了,但我什么也做不了。

例如 :

包 com.sigasi;

 import java.util.Date;

 import org.eclipse.jface.action.ContributionItem;
 import org.eclipse.swt.SWT;
 import org.eclipse.swt.events.SelectionAdapter;
 import org.eclipse.swt.events.SelectionEvent;
 import org.eclipse.swt.widgets.Menu;
 import org.eclipse.swt.widgets.MenuItem;


 public class MyDynamicMenu extends ContributionItem {

    public MyDynamicMenu() {
    }

    public MyDynamicMenu(String id) {
    super(id);
    }

@Override
public void fill(Menu menu, 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

    //create the menu item
    MenuItem menuItem = new MenuItem(menu, SWT.CHECK, index);
    menuItem.setText("My menu item (" + new Date() + ")");
    menuItem.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent e) {
            //what to do when menu is subsequently selected.
            System.err.println("Dynamic menu selected");
        }
    });
}
}

和 :

<extension point="org.eclipse.ui.menus">
<menuContribution locationURI="popup:org.eclipse.ui.navigator.ProjectExplorer#PopupMenu?after=additions">
    <dynamic
              class="com.sigasi.MydynamicMenu"
              id="com.sigasi.myDynamicMenu">
    </dynamic>
  </menuContribution>
</extension>

来源: http ://www.sigasi.com/content/dynamic-menu-items-eclipse

如果它不会只向菜单中添加一项,那可能会很棒。我试图让它适应我的需要,但没有成功。

编辑:我试图将 java 类的超类更改为 CompoundContributionItem;它允许向菜单添加多个项目,但似乎根据索引替换现有项目,而不是简单地添加它。此外,我仍然无法插入菜单。这是我所做的:

package fr.cardinjoseph.extensionpoint.definition;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.jface.action.ContributionItem;
import org.eclipse.jface.action.IContributionItem;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.actions.CompoundContributionItem;
import org.eclipse.ui.menus.CommandContributionItem;
import org.eclipse.ui.menus.CommandContributionItemParameter;

public class MyDynamicMenu extends CompoundContributionItem {

    ExtensionsFinder ef;

    public MyDynamicMenu() {
        ef = new ExtensionsFinder();
        ef.evaluateExtensions();
    }

    public MyDynamicMenu(String id) {
        super(id);
    }

    @Override
    public void fill(Menu menu, 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

        // create the menu item

          Menu commandMenu = new Menu(menu);


         MenuItem test = new MenuItem(menu, index); test.setText("Test");
         test.addSelectionListener(new SelectionAdapter() { public void
         widgetSelected(SelectionEvent e) {  System.err.println("Dynamic menu selected"); }
         });


    }

    @Override
    protected IContributionItem[] getContributionItems() {
        // TODO Auto-generated method stub
        int size = ef.getExtensions().size();
        IContributionItem[] list = new IContributionItem[size];

        IWorkbench wb = PlatformUI.getWorkbench();
        IWorkbenchWindow win = wb.getActiveWorkbenchWindow();

        for(int i = 0 ; i< size ; i++){
            list[i] = new CommandContributionItem(new CommandContributionItemParameter(win, "MenuCommandeId_"+i, "CommandeId_"+i, SWT.None));
        }

        System.out.println("test");

        return list;
    }

    @Override
    public boolean isDynamic(){
        return true;
    }

}
4

2 回答 2

1

通过使用enabledWhen/visibleWhen元素并实现您自己的属性测试器或源提供程序,可以显示/隐藏、启用/禁用菜单。例如,另请参阅以下教程

在这种情况下,您将创建所有菜单项并在您想要显示它们时测试所需的条件。像这样的东西:

<extension point="org.eclipse.ui.menus">
<menuContribution allPopups="false" locationURI="menu:org.eclipse.ui.main.menu">
           <menu id="id" label="%label">
               <visibleWhen
                    checkEnabled="false">
                    <with
                        variable="SomeProvider.var1">
                    </with>
              </visibleWhen>
              <command commandId="command1" id="com1" label="%test" style="push">
              </command>
           </menu>
        </menuContribution>
 </extension>

 <extension
         point="org.eclipse.ui.services">
      <sourceProvider
            provider="com.your_package.SomeProvider">
         <variable
               name="SomeProvider.var1"
               priorityLevel="workbench">
         </variable>
      </sourceProvider>
</extension>
public class SomeProvider extends AbstractSourceProvider {
   private static final String VAR1 = "SomeProvider.var1";

    @Override
    public Map getCurrentState() {
        Map<String, String> stateMap = new HashMap<String, String>();
                stateMap.put(VAR1, testIfMenuForVar1ShouldBeShown());
        return stateMap;
    }
}
于 2014-02-17T09:23:02.870 回答
1

您可以像这样添加多个菜单项:

    @Override
    public void fill(Menu menu, int index) {
        MenuItem menuItem = new MenuItem(menu, SWT.CHECK, index);
        menuItem.setText("1. menu item (" + new Date() + ")");

        menuItem = new MenuItem(menu, SWT.CHECK, index);
        menuItem.setText("2. menu item (" + new Date() + ")");

        menuItem = new MenuItem(menu, SWT.CHECK, index);
        menuItem.setText("3. menu item (" + new Date() + ")");
    }

或添加这样的级联菜单:

    @Override
    public void fill(Menu menu, int index) {
        Menu cascMenu = new Menu(menu);
        MenuItem cascade = new MenuItem(cascMenu, SWT.CASCADE, index);
        cascade.setText("Cascade...");
        cascade.setMenu(cascMenu);

        MenuItem menuItem = new MenuItem(cascade, SWT.CHECK, index);
        menuItem.setText("1. menu item (" + new Date() + ")");
        // etc.
    }
于 2014-02-14T16:15:11.100 回答