blog220123.Eclipse ui之command, handler, menu, 和binding的extPoints
- Commands, extP="org.eclipse.ui.commands"
- Handlers, extP="org.eclipse.ui.handlers",
- Bindings, extP="org.eclipse.ui.bindings"
- Menus, extP="org.eclipse.ui.menus"
本文以eclipse 4.20为参考.
使用extension points是实现软件menu, toolbar, 和key binding相关功能的主要办法. 相对于代码编程而言, 有易配置, 逻辑清晰易维护的优点.
Commands, extP=“org.eclipse.ui.commands”
用于添加category, command.
复杂的command还可以使用commandParameter, 需要定义commandParameterType.
commandParameterType/converter需要继承org.eclipse.core.commands.AbstractParameterValueConverter.
command在没有绑定handler时是disabled.
一个例子,
<extension
point="org.eclipse.ui.commands">
<category
name="..."
description="..."
id="com.example.commands.category">
</category>
<command
categoryId="com.example.commands.category"
defaultHandler="com.example.handlers.DefaultHandler"
id="com.example.commands.cmd1"
name="...">
</command>
<command
categoryId="com.example.commands.category"
id="com.example.commands.cmd2"
name="...">
</command>
<command
categoryId="com.example.commands.category"
id="com.example.commands.cmd3"
name="...">
<commandParameter
id="param1"
name="..."
optional="false"
typeId="com.example.commandParameterType.IntegerType">
</commandParameter>
</command>
<commandParameterType
converter="com.example.converter.IntegerTypeConverter"
id="come.example.commandParameterType.IntegerType"
type="java.lang.Integer">
</commandParameterType>
</extension>
Handlers, extP=“org.eclipse.ui.handlers”,
handler定义的是command被激发时要执行的动作. 用于绑定command和handler, 前者指定command id, 后者指定handler class.
handlerClass要实现org.eclipse.core.commands.IHandler, 通常继承org.eclipse.core.commands.AbstractHandler.
除此, 考虑动态情形, 可以指定activeWhen和enableWhen.
一个例子
<extension point="org.eclipse.ui.handlers">
<handler
class="com.example.handlers.Handler1"
commandId="com.example.commands.cmd1">
</handler>
<handler
class="com.example.handlers.Handler2"
commandId="com.example.commands.cmd2">
<enabledWhen>
<with variable="selection">
<count
value="2">
</count>
</with>
</enabledWhen>
</handler>
</extension>
Bindings, extP=“org.eclipse.ui.bindings”
设定key bindings, 包括keyScheme和keyBinding.
对于keyScheme, 可以在Window > Preference > General > Keys > scheme中选择使用.
需要注意, keyScheme可以有parent scheme.
一个例子,
<extension point="org.eclipse.ui.bindings">
<scheme
id="com.example.bindings.scheme"
name="...">
</scheme>
<key
commandId="com.example.commands.cmd1"
contextId="org.eclipse.ui.contexts.window"
sequence="M1+O"
schemeId="org.eclipse.ui.examples.contributions.scheme">
</key>
<key
commandId="com.example.commands.cmd3"
contextId="com.example.view.context"
schemeId="com.example.bindings.scheme"
sequence="CTRL+SHIFT+P">
<parameter
id="param1"
value="10">
</parameter>
</key>
</extension>
Menus, extP=“org.eclipse.ui.menus”
用于设定main menu/toolbar, view menu/view, view dropdown menu, conext menu和trim.
menuContribution/locationURI用于指定出现的位置.
允许实现动态的menu list, 其class需继承ContributionItem.
一个例子
<extension point="org.eclipse.ui.menus">
<menuContribution
locationURI="menu:org.eclipse.ui.main.menu">
<menu
id="file"
label="%menu.file.label"
mnemonic="%menu.file.mnemonic">
<command
commandId="org.eclipse.ui.file.refresh"
mnemonic="..."
style="push">
</command>
<separator
name="sep1"
visible="true">
</separator>
<command
commandId="org.eclipse.ui.file.exit"
mnemonic="..."
style="push">
</command>
</menu>
<separator
name="additions"
visible="false">
</separator>
<menu
id="window"
label="..."
mnemonic="...">
<command
commandId="org.eclipse.ui.window.newWindow"
mnemonic="..."
style="push">
</command>
<separator
name="sep1"
visible="true">
</separator>
<dynamic
class="com.example.menus.DynamicMenuList"
id="...">
</dynamic>
</menu>
<menu
id="help"
label="..."
mnemonic="...">
<command
commandId="org.eclipse.ui.help.aboutAction"
mnemonic="..."
style="push">
</command>
</menu>
</menuContribution>
<menuContribution
locationURI="toolbar:org.eclipse.ui.main.toolbar">
<toolbar
id="com.example.toolbar1">
<command
commandId="org.eclipse.ui.file.save"
style="push">
</command>
<command
commandId="org.eclipse.ui.file.saveAll"
style="push">
</command>
</toolbar>
<separator
name="additions"
visible="false">
</separator>
</menuContribution>
</extension>
驽马一架 一花一世界 2022/1/23