Danny Lo
Danny Lo

Reputation: 1583

Eclipse plugin developement. Keyboard binding to an Action object

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);
}

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.

Upvotes: 0

Views: 111

Answers (1)

Danny Lo
Danny Lo

Reputation: 1583

Phew... it has cost me a lot of time to figure out what I had to do to make it work.

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();
    }
}

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...
}

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>

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;
            }
        }
    });
}

Upvotes: 0

Related Questions