Dynamic menu items in Eclipse

本文介绍如何在Eclipse中创建动态菜单项。通过编辑plugin.xml文件并指定贡献位置,可以实现在项目资源管理器中添加自定义菜单项。文章还提供了一个示例Java类,该类扩展了ContributionItem,用于动态生成菜单项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

Dynamic menu items in Eclipse

First you have to add the menu contribution to your plugin.xml. I wanted to add an extra menu item to the project explorer so I used"popup:org.eclipse.ui.navigator.ProjectExplorer#PopupMenu?after=additions" as locationURI. Next you only have to specify a class that will create the menu item and specify an id.

 

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

 

Next you have to create the Java class that extends org.eclipse.jface.action.ContributionItem (You can use autocomplete for this).

The method that you have to override for a dynamic menu is fill(Menu menu, int index).
In this method you have to create the new (dynamic) MenuItem. You can also simply return if (e.g. based on the selection) you do not want to show a menu item. In the example code below I simply display the current date in the dynamic menu item.

Do not forget to add a addSelectionListener to the MenuItem, to start some action when the menu is selected.

That's it.

The complete java source:

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) {
		    MenuItem menuItem = new MenuItem(menu, SWT.CASCADE);
		menuItem.setText("HiBuild");
		Menu submenu = new Menu(menu.getShell(),SWT.DROP_DOWN);
		menuItem.setMenu(submenu);
		MenuItem menuItem1 = new MenuItem(submenu,SWT.NONE);
		menuItem1.setText("HiBuild 1");
		
		MenuItem menuItem2 = new MenuItem(submenu, SWT.NONE);
		menuItem2.setText("HiBuild 2");
		
		menuItem1.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				//what to do when menu is subsequently selected.
				System.err.println("Dynamic menu selected 1");
			}
		});
 
		//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");
			}
		});
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值