Eclipse插件开发菜单篇

  • 介绍
在Eclipse中,菜单是非常重要的一个角色,下面就介绍如何自定义菜单。 菜单包括有主菜单、视图菜单、上下文菜单。 Eclipse提供了两种方式扩展菜单,分别是通过org.eclipse.ui.commands和org.eclipse.ui.actionSets。
  • 扩展点org.eclipse.ui.actionSets
[codesyntax lang="xml"]
<extension point="org.eclipse.ui.actionSets">
<actionSet label="EasyShell Action Set"
    visible="true"
    id="org.suren.easyshell.actionSet">
 <menu label="Explorer &amp;Menu"
       id="suren.menu">
    <separator
	  name="surenGroup">
    </separator>
 </menu>
 <action label="&amp;Explorer Action"
       icon="icons/fldr_obj.gif"
       class="org.suren.easyshell.actions.ExplorerAction"
       tooltip="Explorer Resources"
       menubarPath="suren.menu/surenGroup"
       toolbarPath="surenGroup"
       id="org.suren.easyshell.actions.ExplorerAction">
 </action>
</actionSet>
</extension>
[/codesyntax]
  • org.eclipse.ui.actionSets实现类
可以实现接口org.eclipse.ui.IWorkbenchWindowActionDelegate 这种方式并没有把界面和实现分离,而下面的commands的方式确做到了分离的效果。
  • 扩展点org.eclipse.ui.commands
[codesyntax lang="xml"]
<extension point="org.eclipse.ui.commands">
	<command categoryId="org.suren.easyshell.command.category"
	    id="org.suren.easyshell.command.shellExplore"
	    name="Easy Shell Explore" />
</extension>

<extension point="org.eclipse.ui.handlers">
	<handler class="org.suren.easyshell.command.EasyShellCommand"
	    commandId="org.suren.easyshell.command.command.shellExplore">
	</handler>
</extension>

<extension point="org.eclipse.ui.menus">
	<command
	    categoryId="org.suren.easyshell.command.command.category1"
	    id="org.suren.easyshell.command.command.shellExplore"
	    name="Easy Shell Explore">
	</command>

	<menuContribution locationURI="popup:org.eclipse.ui.popup.any?before=additions">
	 <menu id="com.tetrade.eclipse.plugins.easyshell.menu"
	       label="Easy Shell">
	    <command
		  commandId="org.suren.easyshell.command.command.shellExplore"
		  icon="icons/fldr_obj.gif"
		  label="Explore..."
		  style="push">
	    </command>
	 </menu>
	</menuContribution>

	<menuContribution
	    locationURI="toolbar:org.eclipse.ui.main.toolbar?after=additions">
	 <toolbar id="org.suren.easyshell.toolbars.shellExplore">
	    <command commandId="org.suren.easyshell.command.command.shellExplore"
		  icon="icons/fldr_obj.gif"
		  tooltip="Explore..."
		  id="org.suren.easyshell.toolbars.shellExplore.cmd">
	    </command>
	 </toolbar>
	</menuContribution>
</extension>
[/codesyntax] menuContribution的语法:locationURI="[scheme]:[id]?[argument-list]" scheme表明了菜单应该出现的区域——menu为视图的下拉菜单,toolbar为视图的工具栏 id为菜单区域的id argument-list为该菜单项出现在菜单的位置,这里的参数常见的如:after=additions。如果你想要把你的菜单项放到一个确切的菜单项后面的话,把additions修改为那个菜单项的id即可。
  • org.eclipse.ui.commands实现类
步骤如下:
  1. 通过org.eclipse.ui.commands来扩展命令(类别category)
  2. 通过org.eclipse.ui.menus来指定命令在哪里出现
  3. 通过org.eclipse.ui.handlers指定命令的实现类
[codesyntax lang="java"]
package org.suren.easyshell.actions;

import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkbenchWindowActionDelegate;
import org.eclipse.jface.dialogs.MessageDialog;

public class ExplorerAction implements IWorkbenchWindowActionDelegate {
	private IWorkbenchWindow window;
	public ExplorerAction() {
	}

	public void run(IAction action) {
		MessageDialog.openInformation(
			window.getShell(),
			"SEasyshell",
			"Hello, Eclipse world");
	}

	public void selectionChanged(IAction action, ISelection selection) {
	}

	public void dispose() {
	}

	public void init(IWorkbenchWindow window) {
		this.window = window;
	}
}
[/codesyntax] 要实现上下文的菜单扩展的话,要使用org.eclipse.ui.popupMenus来实现。
  • 参考
http://www.ibm.com/developerworks/cn/opensource/os-cn-ecl-menuext/ http://www.fengfly.com/plus/view-179398-1.html

转载于:https://my.oschina.net/surenpi/blog/816965

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值