Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse Platform » How to override a standard command handler?(Need a custom behavior for a standard command in some cases)
How to override a standard command handler? [message #498009] Fri, 13 November 2009 16:52 Go to next message
Yves Monier is currently offline Yves MonierFriend
Messages: 9
Registered: November 2009
Junior Member
I develop on top of the WTP plugins, especially the XML editor. In some cases I'd like the "Add Block Comment" (Remove Block Comment as well) command to behave differently.
I think this is not a WTP-specific question but more a command-handler framework question.

What I did:

1) searched in WTP sources how the Add Block Comment command was implemented: I found the plugin.xml that declares various menuContributions and the <extension
point="org.eclipse.ui.handlers"> that defines this handler:
<handler
class=" org.eclipse.wst.xml.ui.internal.handlers.AddBlockCommentHand ler "
commandId="org.eclipse.wst.sse.ui.add.block.comment">
<activeWhen>
<reference definitionId="org.eclipse.wst.xml.ui.comments"/>
</activeWhen>
<enabledWhen>
<reference definitionId="org.eclipse.wst.xml.ui.comments"/>
</enabledWhen>
</handler>
2) in my plugin.xml, I added a similar definition, just replacing the class and hoping this would be enough to override the standard WTP's AddBlockCommentHandler. Of course ... this didn't work...

3) I used the following debug.options:
org.eclipse.ui/debug=true
org.eclipse.ui/trace/keyBindings=true
org.eclipse.ui/trace/keyBindings.verbose=true
org.eclipse.ui/trace/sources=true
org.eclipse.ui/trace/handlers=true
org.eclipse.ui/trace/handlers.verbose=true
#org.eclipse.ui/trace/handlers.verbose.commandId=org.eclipse .ui.edit.copy
org.eclipse.ui/trace/handlers.verbose.commandId=org.eclipse. wst.sse.ui.add.block.comment
org.eclipse.ui/trace/contexts=true
org.eclipse.ui/trace/contexts.verbose=true

this told me that I had an unresolved conflict between the two handlers. Seems that in such case the command is disabled, right?

4) in case of conflict Eclipse keeps the handler with the highest priority, priorities depending on activeWhen conditions. Therefore I added an extra condition to my handler:

<activeWhen>
<and>
<reference definitionId="org.eclipse.wst.xml.ui.comments"/>
<with variable="activeEditorId">
<iterate operator="or">
<equals value="com.temis.scs.editors.SCStructuredTextEditor.scp"/>
<equals value="com.temis.scs.editors.SCStructuredTextEditor.sct"/>
<equals value="com.temis.scs.editors.SCStructuredTextEditor.tsl"/>
</iterate>
</with>
</and>
</activeWhen>

I added breakpoints in HandlerAuthority, HandlerActivation and some ExpressionXXXX classes, and noticed that my handler actually had a higher priority than the standard one: fine! Still, the default handler was called later at runtime

5) it happens that, at startup, both handled are not active, probably due to their common condition (<reference definitionId="org.eclipse.wst.xml.ui.comments"/>). In such case (only inactive handlers) it seems that HandlerAuthority.resolveConflicts keeps the last one, without considering the priority. In my case the last one is the WTP handler...

boolean conflict = false;
while (activationItr.hasNext()) {
currentActivation = (IHandlerActivation) activationItr.next();
if (!evaluate(currentActivation)) {
continue; // only consider potentially active handlers
}
[......]
if (bestActivation == null) {
bestActivation = currentActivation;
conflict = false;
continue;
}
[......]
}

I may post this question to the WTP forum too, with more focus on my specific need (the AddBlockComment of the XML editor)...
But, more generally, my question is: how to override a standard/existing Eclipse handler, using state-of-the-art techniques?

Hope my problem description is clear enough!

Thanks in advance for any help...
Best regards,


Yves Monier
Re: How to override a standard command handler? [message #498339 is a reply to message #498009] Mon, 16 November 2009 14:15 Go to previous messageGo to next message
Paul Webster is currently offline Paul WebsterFriend
Messages: 6859
Registered: July 2009
Location: Ottawa
Senior Member

It matters what <reference definitionId="org.eclipse.wst.xml.ui.comments"/> this reference contains. i.e. what variables does it access?

But you're on the right track, the HandlerAuthority selects the highest priority handler, based on its activeWhen clause. o.e.ui.ISources can give you the relative priority of the platform variables.

For your handler to be selected, it must be active and calculate to the highest priority.

PW


Re: How to override a standard command handler? [message #499646 is a reply to message #498339] Mon, 23 November 2009 15:34 Go to previous messageGo to next message
Yves Monier is currently offline Yves MonierFriend
Messages: 9
Registered: November 2009
Junior Member
Thanks Paul for your answer,

Well... as usual, writing the problem description finally helps to find the solution Smile
My handler was not active due to a bad understanding/usage of <iterate>. I replaced the <iterate> part with the simpler:
<with variable="activeEditor">
<instanceof value="com.temis.scs.editors.SCStructuredTextEditor"></instanceof >
</with>

My handler is now active and thanks to the activeEditor sourceId, its priority is greater than the default handler's one.

Best,



Yves Monier
Re: How to override a standard command handler? [message #532083 is a reply to message #498009] Fri, 07 May 2010 06:24 Go to previous messageGo to next message
eliza sahoos is currently offline eliza sahoosFriend
Messages: 3
Registered: February 2010
Junior Member
The solution is to override the default handler with a custom one. But the problem with that is you can not declare a custom handler for existing command directly. It will raise a conflict.

So what we will do is we will activate the custom handler by activating a context.

Now, declare the custom handler like shown below:

<handler

class="<custom handler>"

commandId="<standard / existing command id>">

<activeWhen>

<with

variable="activeContexts">

<iterate

ifEmpty="false"

operator="or">

<equals

value="<custom context name>">

</equals>

</iterate>

</with>

</activeWhen>

</handler>

The above declaration will activate the custom handler when the declared context will be activated.

Now at the start up of the application you can activate the context by using following code.

IContextService contextService = (IContextService) PlatformUI.getWorkbench().getService(IContextService.class);

contextService.activateContext( <custom context name> );

So now the above declared command will use the custom handler.

For more information refer
Re: How to override a standard command handler? [message #656262 is a reply to message #532083] Thu, 24 February 2011 19:12 Go to previous messageGo to next message
AJ  is currently offline AJ Friend
Messages: 77
Registered: July 2009
Member
The above doesn't work for the standard top menu items (e.g. IWorkbenchCommandConstants.FILE_PRINT)
Re: How to override a standard command handler? [message #656284 is a reply to message #656262] Thu, 24 February 2011 21:51 Go to previous message
Paul Webster is currently offline Paul WebsterFriend
Messages: 6859
Registered: July 2009
Location: Ottawa
Senior Member

On 02/24/2011 02:13 PM, AJ wrote:
> The above doesn't work for the standard top menu items (e.g.
> IWorkbenchCommandConstants.FILE_PRINT)

it works for the command part, but correct it won't override an action
turned directly into a MenuItem. That being said, in my 3.7 it will
override File>Print.

Check out your org.eclipse.ui.internal.ide.WorkbenchActionBuilder to
figure out which menu items can have their handlers swapped (for plugins
into the IDE)

PW



--
Paul Webster
http://wiki.eclipse.org/Platform_Command_Framework
http://wiki.eclipse.org/Command_Core_Expressions
http://wiki.eclipse.org/Platform_Expression_Framework
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:Scripting - Getting Project name and path
Next Topic:Layout issues after using IWorkBenchPage.hideView
Goto Forum:
  


Current Time: Fri Mar 29 15:27:57 GMT 2024

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

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

Back to the top