Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Rich Client Platform (RCP) » How to define custom handler for standard command?
How to define custom handler for standard command? [message #547814] Mon, 19 July 2010 16:40 Go to next message
Jeffery Yuan is currently offline Jeffery YuanFriend
Messages: 27
Registered: July 2009
Junior Member
Hi, all:
For my editor com.my.editor.MyEditor,
I want to define a custom handler for the standard command org.eclipse.ui.edit.copy.

I added the following to plugin.xml, but it doesn't work, my handler CopyHandler is even not called at all.
      <handler            class="com.my.editor.myEditor.handler.CopyHandler"
            commandId="org.eclipse.ui.edit.copy">
         <activeWhen>
            <with
                  variable="activePartId">
               <equals
                     value="com.my.editor.MyEditor">
               </equals>
            </with>
         </activeWhen>            
      </handler>    

As suggested in http://www.eclipse.org/forums/index.php?t=msg&goto=53208 3& and http://www.mindfiresolutions.com/Using-custom-handlers-for-s tandard-commands-in-Eclipse-RCP-726.php,
I tried to define a context in plugin.xml:
    <extension
          point="org.eclipse.ui.contexts">
       <context
             id="com.my.ui.definition.activatedEditorContext"
             name="Editor Context"
             parentId="org.eclipse.ui.textEditorScope">
       </context>
    </extension>

And in editor, activate the context:
    IContextService contextService = (IContextService) PlatformUI
            .getWorkbench().getService(IContextService.class);
		contextService.activateContext("com.my.ui.definition.activatedEditorContext");

Define a handler in plugin.xml, and set it active when context is enabled.
      <handler             class="com.my.editor.myEditor.handler.CopyHandler"
            commandId="org.eclipse.ui.edit.copy">
         <activeWhen>
		    <with
		       variable="activeContexts">
		       <iterate
		          ifEmpty="false"
		          operator="or">
		       <equals
		           value="com.my.ui.definition.activatedEditorContext">
		       </equals>
		       </iterate>
		    </with>
		 </activeWhen>
      </handler>

But it neither works..

As a temporary solution, I restored back to use the old action, overwrite Editor's createActions methods:
	protected void createActions() {
		super.createActions();
		cliEditorCopyAction = new CliEditorCopyAction();
		setAction(ActionFactory.COPY.getId(), cliEditorCopyAction);
    }

This works, but I prefer to use the command and handler framework.

Is there a way to define custom handler for standard command? this should be a common problem, and should have simple solution, but I tried google search in the last few days, can't find any solution yet.
Or is there anything I missed?

Could anyone help on this? Thanks for any help or reply in advance : )
Re: How to define custom handler for standard command? [message #547823 is a reply to message #547814] Mon, 19 July 2010 17:07 Go to previous message
Paul Webster is currently offline Paul WebsterFriend
Messages: 6859
Registered: July 2009
Location: Ottawa
Senior Member

Your first XML seems to work fine for me:
<handler
class="z.ex.editor.editors.CopyHandler"
commandId="org.eclipse.ui.edit.copy">
<activeWhen>
<with
variable="activePartId">
<equals
value="z.ex.editor.editors.MyEditorId">
</equals>
</with>
</activeWhen>
</handler>

This probably won't work with a subclass of TextEditor, though, since
they do provide local copy action and that will trump your activePartId.

PW


--
Paul Webster
http://wiki.eclipse.org/Platform_Command_Framework
http://wiki.eclipse.org/Command_Core_Expressions
http://wiki.eclipse.org/Menu_Contributions
http://wiki.eclipse.org/Menus_Extension_Mapping
http://help.eclipse.org/galileo/index.jsp?topic=/org.eclipse .platform.doc.isv/guide/workbench.htm


Previous Topic:How to create Section with toolbar?
Next Topic:Adding classes to startup.jar to get around JNI problems
Goto Forum:
  


Current Time: Fri Apr 26 16:10:10 GMT 2024

Powered by FUDForum. Page generated in 0.03173 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top