如何为Eclipse插件创建动态上下文菜单?

时间:2013-02-09 00:14:25

标签: java eclipse dynamic eclipse-plugin contextmenu

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

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

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

我已经用Google搜索了,但我无法继续工作。

例如:

package 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");
        }
    });
}
}

with:

<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;
    }

}

2 个答案:

答案 0 :(得分: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.
    }

答案 1 :(得分:1)

可以使用enabledWhen/visibleWhen元素显示/隐藏,启用/禁用菜单,并实现您自己的property testerssource providers。另请参阅following tutorial例如。

在这种情况下,您可以创建所有菜单项并在要显示这些项时测试所需条件。像这样:

<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;
    }
}