我正在尝试制作一个 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;
}
}