??xml version="1.0" encoding="utf-8" standalone="yes"?>
]]>
Eclipse里每U编辑器都有它自q视图Q有时候我们需要在上面d自己的功能,扩充视图弹出菜单则是必不可少的工作之一?
在一个视囄Popup Menu里增加一个菜单项Q含?U方式:(x)
1、viewerContributionQ也是通过视图的id。这样只有视图id和给定的id一致的时候,它的弹出菜单才会(x)增加我们定义的菜单项?
2、objectContributionQ通过对象cd。当在视N选定的对象类型和l定的一致时Q在视图的弹?gu)单增加我们定义的菜单V?
菜单的Action需要承org.eclipse.ui.actions.ActionDelegateQ然后实现特D的View的ActionDelegate接口。比如要扩展Java~辑器:(x)
public class FinderActionDelegate extends ActionDelegate implements
IEditorActionDelegate {
来看看插件的Plugin.xml的组成部分:(x)
viewerContribution形式Q?
<extension
point="org.eclipse.ui.popupMenus">
<viewerContribution
targetID="#TextEditorContext"
id="com.cnfree.finder.editor">
<action
label="查找最大ID"
icon="icon/search.gif"
helpContextId="com.cnfree.finder.finderaction"
class="com.cnfree.finder.FinderActionDelegate"
menubarPath="additions"
enablesFor="+"
id="com.cnfree.finder">
</action>
</viewerContribution>
<viewerContribution
targetID="#CompilationUnitEditorContext"
id="com.cnfree.finder.editor">
<action
label="查找最大ID"
icon="icon/search.gif"
helpContextId="com.cnfree.finder.finderaction"
class="com.cnfree.finder.FinderActionDelegate"
menubarPath="additions"
enablesFor="*"
id="com.cnfree.finder">
</action>
</viewerContribution>
</extension> < br /> <extension
point="org.eclipse.ui.editorActions">
</extension>
q里我们需要知道扩展的视图它的菜单在Eclipse插g注册表里定义的idQ稍后我?x)附上Eclipse插g菜单ID注册表)(j)。比如Java~辑器对应的ID为:(x)
#TextEditorContext ?nbsp;#CompilationUnitEditorContextQ我们定义好 targetIDQ就把我们的插gid指向视图菜单的id?jin)。这样Java~辑器就?x)添加我们的菜单?jin)?
<!ATTLIST action
id CDATA #REQUIRED
label CDATA #REQUIRED
menubarPath CDATA #IMPLIED
toolbarPath CDATA #IMPLIED
icon CDATA #IMPLIED
disabledIcon CDATA #IMPLIED
hoverIcon CDATA #IMPLIED
tooltip CDATA #IMPLIED
helpContextId CDATA #IMPLIED
style (push|radio|toggle) "push"
state (true | false)
class CDATA #REQUIRED
enablesFor CDATA #IMPLIED>
< br />此元素定义用户可在用L(fng)面中调用的操作?
objectContribution形式Q?
<objectContribution objectClass="com.cnfree.action.IOpenable" >
<action
id="com.cnfree.open"
menubarPath="new.ext"
definitionID="com.cnfree.open.selection"
label="&Open@Ctrl+O"
tooltip="Open the selected item(s)"
helpContextId="com.cnfree.open.selection"
icon="icon/open.gif"
class="com.cnfree.action.Op enSelectedActionDelegate"
enablesFor="+">
</action>
</objectContribution>
action Open实现?jin)IOpenableQ这样就意味着Q所有包含了(jin)IOpenable对象的视图,当我们选中其中的IOpenable对象Ӟ它的弹出菜单都会(x)增加我们自定义的菜单V?
附带的例子:(x)
cnfree editor plugin
使用说明Q徏立一个Java文gQ在它的注释里添?nbsp;xxx.yyy QxxxZ个类名,yyyZ个三位数IDQ比如cnfee.100,cnfree.101,cnfree.102Q在Java透视N打开q个Java文gQ右键菜单会(x)有一个选项Q查找最大IDQ打开后输入cnfreeQ它?yu)׃?x)自动扑ֈcnfree.102Qƈ该字符串高?sh)显C?
Eclipse插g菜单ID注册表参考:(x)http://www.jdg2e.com/ch21.actions.table/doc/
参考资料:(x)
http://www.eclipsesource.com/EN_ARCHIVE/etips_07072004.html
Eclipse帮助Q?a target="_blank">org.eclipse.platform.doc.isv/guide/workbench_basicext_popupMenus.htm
上一文?/font>我们知道?jin)Eclipse弹出菜单的基本用法。其实Eclipse的弹?gu)单可以用来做很多文章Q简单一点的Ҏ(gu)文gcdQ我们可以进行不同的文g操作Q比如Ant的build.xml我们可以用来buildQJava文g我们可以用Java Editor打开Q这些基于文件类型的操作我们都可以很Ҏ(gu)的实现。但是还有一U情况,如果文gcd一P我们惌行不同的操作Q该怎么实现呢?实际上这L(fng)应用很多Q比如同hJava文gQ含有mainҎ(gu)的Java文g有Run和Debug的选项Q其它的都没有。还有现在的框架都是ZXML文gq行配置的,如果一个项目用了(jin)多个框架Q我们怎么Ҏ(gu)不同的XML文gq行框架的区分呢Q答案就是enablement的test?/font>
<!ELEMENT test EMPTY>
<!ATTLIST test
property CDATA #REQUIRED
args CDATA #IMPLIED
value CDATA #IMPLIED>
This element is used to evaluate the property state of the object in focus. The set of testable properties can be extended using the propery tester extension point. The test expression returns EvaluationResult.NOT_LOADED if teh property tester doing the actual testing isn't loaded yet.
比如我们要让含有mainҎ(gu)的Java文g它的右键弹出菜单包含一个额外的选项“This is main class”Q需要编写如下的Plugin.xmlQ?/font>
我们需要检在当前情况下是否需要显C个菜单项Q用扩展点 org.eclipse.core.expressions.propertyTesters Q?br />
<!ELEMENT propertyTester EMPTY>
<!ATTLIST propertyTester
id CDATA #REQUIRED
type CDATA #REQUIRED
namespace CDATA #REQUIRED
properties CDATA #REQUIRED
class CDATA #REQUIRED>
id - unique identifier for the property tester
type - the type to be extended by this property tester
namespace - a unique id determining the name space the properties are added to
properties - a comma separated list of properties provided by this property tester
class - the name of the class that implements the testing methods. The class must be public and extend org.eclipse.core.expressions.PropertyTester with a public 0-argument constructor.
q里只须注意 propertyTester的namespace和properties正好对应test的property?br />
至于(g)的逻辑我们在advancedpopupmenus.popup.actions.VisablePropertyTester中实玎ͼq个cdȝ承自org.eclipse.core.expressions.PropertyTester?/font>
我们只要判断接受的Java文g中是否含有mainҎ(gu)Q如果有Q则q回TrueQ没有则q回False?br />
如果我们是要接受一个Web开发的配置文gQ我们可以这样写Q?/font>
注意和上一个例子不同的地方QobjectClassQnameFileter和type(在上一个例子中Q我们也可以使用objectClass="org.eclipse.core.resources.IFile" nameFilter ="*.java" )Q相应的我们的VisablePropertyTestercM要做一些改动:(x)
q样Ҏ(gu)不同的xml SystemIDQ我们就能够知道到底q是哪一U框架的配置文g?jin)?/font>