Eclipse插件开发.键盘绑定到动作对象 [英] Eclipse plugin developement. Keyboard binding to an Action object
问题描述
我开发了一个多页XML编辑器,并希望将调用 parseDocument()
和 updateTabs()
的特定动作绑定到一个键上.该动作在我的编辑器贡献者中定义如下:
I develop a multipage XML-Editor and would like bind a specific action which calls parseDocument()
and updateTabs()
to a key. The action is defined in my editor contributor as follows:
private void createActions() {
updateTabsAction = new Action() {
@Override
public void run() {
ARTEditor artEditor = ((ARTEditor)((MultiPageEditorSite)activeEditorPart.getEditorSite()).getMultiPageEditor());
artEditor.parseDocument();
artEditor.updateTabs();
}
@Override
public String getId()
{
return "com.portal.agenda.editors.updatetabs";
}
};
updateTabsAction.setText("Update tabs");
updateTabsAction.setToolTipText("Parses document and updates tabs to reflect textual changes");
updateTabsAction.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages().
getImageDescriptor(IDE.SharedImages.IMG_OBJS_TASK_TSK));
}
@Override
public void contributeToToolBar(IToolBarManager manager) {
manager.add(new Separator());
manager.add(updateTabsAction);
}
是否有可能以某种方式执行此操作?还是我必须在plugin.xml中定义命令扩展名并为其创建默认处理程序(如
Is there any possibility to do this some way? Or do I obligatory have to define command extension in the plugin.xml and to create a default handler for it (as described there for example)? In this case it would be kind of redundant code and I'd like to avoid it.
推荐答案
Ph ...这使我花了很多时间弄清楚使它工作所需要做的事情.
Phew... it has cost me a lot of time to figure out what I had to do to make it work.
第一步:出于我的目的, getId()
是错误的方法, setActionDefinitionId()
是正确的方法.我创建了一个嵌套类,如下所示:
Step one: getId()
was the wrong method for my purpose, setActionDefinitionId()
is the correct one. I created a nested class as follows:
public final class UpdateTabsAction extends Action
{
public UpdateTabsAction()
{
setText("Update tabs");
setToolTipText("Parses document and updates tabs to reflect textual changes");
setImageDescriptor(PlatformUI.getWorkbench()
.getSharedImages()
.getImageDescriptor(IDE.SharedImages.IMG_OBJS_TASK_TSK));
setActionDefinitionId("com.portal.agenda.editors.updatetabs");
}
@Override
public void run()
{
ARTEditor artEditor = (ARTEditor)activeEditorPart.getSite().getPage().getActiveEditor();
artEditor.parseDocument();
artEditor.updateTabs();
}
}
第二步:该操作必须在处理程序服务中注册.我决定重写 MultiPageEditorActionBarContributor
的方法 setActivePage
,因为它传递了有效的 EditorPart
实例,如果选择了该实例,它将引用我的文本编辑器:
Step two The action must be registered with the handler service. I decided to override MultiPageEditorActionBarContributor
's method setActivePage
since it get passed a valid EditorPart
instance, which is reference to my text editor if it's selected:
// Required to avoid multiple registering of the action
private IHandlerActivation iHandlerActivation;
@Override
public void setActivePage(IEditorPart part)
{
if (part != null && iHandlerActivation == null)
{
IHandlerService hService = ((IHandlerService)part.getSite().getService(IHandlerService.class));
iHandlerActivation = hService.activateHandler(updateTabsAction.getActionDefinitionId(),
new ActionHandler(updateTabsAction));
}
if (activeEditorPart == part)
return;
activeEditorPart = part;
// ...skipped...
}
第三步:我将此操作映射到plugin.xml中的命令扩展点.除了它,我还创建了上下文和绑定:
Step three: I mapped this action to a command extension point in the plugin.xml. In addition to it I created a context and binding:
<extension
point="org.eclipse.ui.commands">
<category
id="com.portal.agenda.editors.category"
name="ARTEditor">
</category>
<command
categoryId="com.portal.agenda.editors.category"
description="Parse document and update tabs to reflect textual changes"
id="com.portal.agenda.editors.updatetabs"
name="Update tabs">
</command>
</extension>
<extension
point="org.eclipse.ui.contexts">
<context
id="com.portal.agenda.editors.context"
name="%context.name"
parentId="org.eclipse.ui.textEditorScope">
</context>
</extension>
<extension
point="org.eclipse.ui.bindings">
<key
commandId="com.portal.agenda.editors.updatetabs"
contextId="com.portal.agenda.editors.context"
schemeId="org.eclipse.ui.defaultAcceleratorConfiguration"
sequence="F5">
</key>
</extension>
第四步:我在 StructuredTextEditor
中添加了 FocusListener
,因此仅在激活编辑器的情况下才激活上下文:
Step four: I added a FocusListener
to my StructuredTextEditor
so the context is only activated if the editor is active:
private void initKeyBindingContext()
{
final IContextService cService = (IContextService)getSite().getService(IContextService.class);
textEditor.getTextViewer().getTextWidget().addFocusListener(new FocusListener()
{
IContextActivation currentContext = null;
public void focusGained(FocusEvent e)
{
if (currentContext == null)
currentContext = cService.activateContext("com.portal.agenda.editors.context");
}
public void focusLost(FocusEvent e)
{
if (currentContext != null)
{
cService.deactivateContext(currentContext);
currentContext = null;
}
}
});
}
这篇关于Eclipse插件开发.键盘绑定到动作对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!