gpt4 book ai didi

java - 如何为 Eclipse 插件制作动态上下文菜单?

转载 作者:行者123 更新时间:2023-11-30 09:26:59 25 4
gpt4 key购买 nike

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

有一点原理图: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 类的父类(super class)更改为 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;
}

}

最佳答案

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

    @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.
}

关于java - 如何为 Eclipse 插件制作动态上下文菜单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14783424/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com