Eclipse RCP菜单和动作:配置还是编码? [英] Eclipse RCP menus & actions: Configure or code?
问题描述
这是一个普遍的问题,但是我目前的问题主要围绕菜单处理.
This is a general question but my current problem revolves around menu handling.
在具有贡献菜单操作的普通插件中,您可以在plugin.xml配置中配置ActionSets等.这显然是明智的.
In a normal plugin with contributes menu actions you would configure ActionSets etc in the plugin.xml configuration. This is obviously sensible.
我正在开发一个RCP应用程序(实际上是RAP),我想知道是否值得通过plugin.xml配置所有内容.我的插件不必与其他未知插件交互,因此,从理论上讲,我可以控制.我可以通过编程方式添加菜单和操作.
I am working on a RCP application (actually RAP) and I'm wondering if it's worth the effort to configure everything via plugin.xml. My plugin does not have to interact with an other unknown plugins so, theoretically, I have control. I can add menus and actions programmatically.
我一直在尝试配置一个包含子菜单的菜单.我尝试定义ActionSet并将其中一个链接到另一个内部,但是没有成功.根据用户角色,需要禁用某些项目.
I have been trying to configure a menu which contains a submenu. I have tried defining ActionSets and linking one inside the other but without success. Some items need to be disabled depending on the user role.
我认为我可以在几分钟内完成全部编码,但是我不确定这是否适合日食'ethos'.
I figure I could have coded the whole lot in a few minutes but I'm not sure if that fits with the eclipse 'ethos'.
那里有什么意见?该应用程序将变得相当大,因此我想从一开始就正确地采用该方法.也许有人可以给我指出一个配置嵌套菜单的示例:-)
What opinions are out there? The application will get fairly big so I'd like to get the approach right from the start. Perhaps someone can point me at an example for configuring a nested menu :-)
推荐答案
我的观点是,plugin.xml实现是必经之路.
My opinion is that the plugin.xml implementation is the way to go.
我使用此方法的两个主要原因:
My main two reasons for using this method:
- 无需编写Java代码即可很容易地重新配置和重新组织菜单和按钮.
- 菜单树的层次结构非常清晰.
这是一个实现菜单和子菜单的代码段.在此示例中,它们被添加到主菜单中.
Here is a code snippet that implements menus and submenus. In this example, they are added to the main menu.
您可以将其粘贴到您的plugin.xml中:
You can paste this into your plugin.xml:
<extension
name="Main Menu Contributions"
point="org.eclipse.ui.menus">
<menuContribution
allPopups="false"
locationURI="menu:org.eclipse.ui.main.menu">
<menu
id="fileMenu"
label="File">
<command
commandId="org.eclipse.ui.file.exit"
label="Exit"
style="push">
</command>
</menu>
<menu
label="Edit">
<command
commandId="org.eclipse.ui.edit.selectAll"
label="Select All"
style="push">
</command>
<menu
label="Submenu">
<command
commandId="org.eclipse.ui.edit.selectAll"
label="Select All Submenu"
style="push">
</command>
<command
commandId="org.eclipse.ui.edit.delete"
label="Delete submenu"
style="push">
</command>
</menu>
</menu>
</menuContribution>
</extension>
要激活/禁用菜单,必须使用Core Expressions启用/禁用命令处理程序.如果命令没有附加任何活动的处理程序,它将被禁用.因此,调用该命令的菜单项也将被禁用.
For activating/deactivating a menu, you have to use Core Expressions to enable/disable command handlers. If a command doesn't have any active handlers attached, it will be disabled. So, the menu item that calls that command will also be disabled.
以下代码段显示了如何在视图的工具栏上创建一个按钮,以及如何根据变量的值来启用/禁用该按钮.切记,您必须在此代码中进行一些更改才能使其正常工作.大部分更改是为了引用名称和类实现.
The following code snippets show how to create a button on the toolbar of a view and have it be enabled/disabled depending of a variable's value. Bare in mind that you will have to change some things in this code to make it work. Most of the changes are for reference names and class implementation.
在工具栏(plugin.xml)中创建按钮:
Create the button in the toolbar (plugin.xml):
<extension
name="View Toolbar Contributions"
point="org.eclipse.ui.menus">
<menuContribution
allPopups="false"
locationURI="toolbar:myapp.views.MyView">
<command
commandId="myapp.commands.PauseSound"
icon=""
label="Pause Playback Sound"
style="push"
tooltip="Pause">
</command>
</menuContribution>
</extension>
创建命令(plugin.xml):
Create the command (plugin.xml):
<extension
id="myapp.commands.PauseSound"
name="Pause sound command"
point="org.eclipse.ui.commands">
<command
id="myapp.commands.PauseSound"
name="Pause Sound">
</command>
</extension>
创建命令处理程序(plugin.xml):
Create the command handler (plugin.xml):
<extension
point="org.eclipse.ui.handlers">
<handler
commandId="myapp.commands.PauseSound">
<activeWhen>
<with
variable="myapp.commands.sourceprovider.active">
<or>
<equals
value="PLAYING">
</equals>
<equals
value="PAUSED">
</equals>
</or>
</with>
</activeWhen>
<class
class="myapp.rcp.commands.toolbar.PausePlayback">
</class>
</handler>
</extension>
为命令(plugin.xml)创建状态变量:
Create the state variable for the command (plugin.xml):
<extension
point="org.eclipse.ui.services">
<sourceProvider
provider="myapp.commands.sourceprovider.CommandState">
<variable
name="myapp.commands.sourceprovider.active"
priorityLevel="workbench">
</variable>
</sourceProvider>
</extension>
实现可更改变量状态的类:
Implement the class that changes the variable's state:
public class CommandState extends AbstractSourceProvider {
public final static String STATE = "myapp.commands.sourceprovider.active";
public final static String STOPPED = "STOPPED";
public final static String PLAYING = "PLAYING";
public final static String PAUSED = "PAUSED";
public final static String NOT_LOADED = "NOT_LOADED";
enum State {
NOT_LOADED, PLAYING, PAUSED, STOPPED
};
private State curState = State.NOT_LOADED;
@Override
public void dispose() {
}
@Override
public String[] getProvidedSourceNames() {
return new String[] { STATE };
}
// You cannot return NULL
@SuppressWarnings("unchecked")
@Override
public Map getCurrentState() {
Map map = new HashMap(1);
if (curState == State.PLAYING)
map.put(STATE, PLAYING);
else if (curState == State.STOPPED)
map.put(STATE, STOPPED);
else if (curState == State.PAUSED)
map.put(STATE, PAUSED);
return map;
}
public void setPlaying() {
fireSourceChanged(ISources.WORKBENCH, STATE, PLAYING);
}
public void setPaused() {
fireSourceChanged(ISources.WORKBENCH, STATE, PAUSED);
}
public void setStopped() {
fireSourceChanged(ISources.WORKBENCH, STATE, STOPPED);
}
public void setNotLoaded() {
fireSourceChanged(ISources.WORKBENCH, STATE, NOT_LOADED);
}
}
有关如何实现这些功能的更多详细信息,请参见以下位置:
More details on how to implement these features can be found at these locations:
- Eclipse Commands Tutorial
- Limiting Visibility of Commands
这篇关于Eclipse RCP菜单和动作:配置还是编码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!