Skip to main content



      Home
Home » Eclipse Projects » Eclipse Platform » Contribute both a command Id plus an action implementation to another plugin.
Contribute both a command Id plus an action implementation to another plugin. [message #324039] Wed, 16 January 2008 18:53 Go to next message
Eclipse UserFriend
My plugin wants to contribute a key bindable action/handler to, for example,
the Navigator view. There doesn't seem to be a way to do this. I am able to
contribute menu items and toolbar buttons to editors and views from other
plugins using the actionSets extension. But I can't find any way to simply
make a contributed action available for binding to a key.

My plugin defines various new command Ids in this way,

<extension
point="org.eclipse.ui.commands">
<category
name="%Category.Navigate.name"
description="%Category.Navigate.description"
id="com.ui.category.navigate">
</category>

<command
id="com.ui.menu.navigate.visit"
name="%Button.visit.name"
description="%Button.visit.tooltip"
categoryId="com.ui.category.navigate">
</command>
</extension>

These allow the user to go into Window>Preferences, the Editor>Key page, and
to assign one of these command Ids to a key. If the user marks the key as
being used "In Windows" then Eclipse will attempt to locate an action handler
for the Id every time that key is pressed in any window, including for example
in the Navigator view.

And using actionSets I can define actions for my own editors and views like this,

<action
id="com.ui.menu.navigate.visit"
definitionId="com.ui.menu.navigate.visit"
label="%Button.visit.label"
tooltip="%Button.visit.tooltip"
class="com.action.delegate.navigate.Button_visit"
menubarPath="navigate/open.ext2"
style="push">
</action>

I am then able add a handler for these Ids in editors and views in my plugin
by adding code to each of the editors and views individually. The code
activates the handlers when the editor or view becomes active.

However I can't find any way to add a command Id handler for an editor or view
where I don't control the source code.

I know that the command Ids are being defined and properly bound, and I know
that Eclipse is looking for a handler when the keys are pressed, I have a
CommandManager.ExecutionListener set up in my plugin that is notified whenever
Eclipse went looking for a handler but didn't find one or didn't find one that
was enabled. In this case it isn't finding one (handler field in the
ParameterizedCommand is null). What I don't know is how to tell Eclipse what
action (or class or whatever) to use when the active workspace part doesn't
itself have a hard coded handler for a particular command Id.

I suppose I could put a hack into my ExecutionListener but that seems an
awkward way to do something that must have come up before.

Gary
Re: Contribute both a command Id plus an action implementation to another plugin. [message #324096 is a reply to message #324039] Thu, 17 January 2008 10:30 Go to previous messageGo to next message
Eclipse UserFriend
Gary E. Barnes wrote:
> My plugin wants to contribute a key bindable action/handler to, for
> example, the Navigator view. There doesn't seem to be a way to do
> this. I am able to contribute menu items and toolbar buttons to editors
> and views from other plugins using the actionSets extension. But I
> can't find any way to simply make a contributed action available for
> binding to a key.
>
> My plugin defines various new command Ids in this way,
>
> <extension
> point="org.eclipse.ui.commands">
> <category
> name="%Category.Navigate.name"
> description="%Category.Navigate.description"
> id="com.ui.category.navigate">
> </category>
>
> <command
> id="com.ui.menu.navigate.visit"
> name="%Button.visit.name"
> description="%Button.visit.tooltip"
> categoryId="com.ui.category.navigate">
> </command>
> </extension>
>
> I am then able add a handler for these Ids in editors and views in my
> plugin by adding code to each of the editors and views individually.
> The code activates the handlers when the editor or view becomes active.

Once you get this far, you would use org.eclipse.ui.handlers to
declaratively add a handler for the Navigator view.

You would set the handler element activeWhen clause to something like:

<activeWhen>
<with variable="activePartId">
<equals value="org.eclipse.ui.views.ResourceNavigator"/>
</with>
</activeWhen>
[/xml]


When you are executed you'll be able to retrieve the navigator view in
your execute(*) method using:
IWorkbenchPart part = HandlerUtil.getActivePart(event);


You can also simply supply a default handler in your command definition
(but you can only supply one).

In 3.3, you can also use org.eclipse.ui.menus to contribute a toolitem
or menu item to the navigator view that simply references the command.

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/help33/index.jsp?topic=/org.eclipse. platform.doc.isv/guide/workbench.htm
Re: Contribute both a command Id plus an action implementation to another plugin. [message #324277 is a reply to message #324096] Mon, 21 January 2008 20:35 Go to previous messageGo to next message
Eclipse UserFriend
Paul Webster wrote:
> Once you get this far, you would use org.eclipse.ui.handlers to
> declaratively add a handler for the Navigator view.
>
> You would set the handler element activeWhen clause to something like:
>
> <activeWhen>
> <with variable="activePartId">
> <equals value="org.eclipse.ui.views.ResourceNavigator"/>
> </with>
> </activeWhen>
> [/xml]
>
>
> When you are executed you'll be able to retrieve the navigator view in
> your execute(*) method using:
> IWorkbenchPart part = HandlerUtil.getActivePart(event);

I'm using 3.2. There doesn't appear to be a HandlerUtil in 3.2. So I'm doing,

Object ac = event.getApplicationContext ();
if (! (ac instanceof IEvaluationContext)) {
return null;
}
IEvaluationContext context = (IEvaluationContext)ac;
... = context.getVariable ("activePart");

> You can also simply supply a default handler in your command definition
> (but you can only supply one).

Does that work in 3.2? It is documented in the 3.2 Help files but if I
specify a default handler it never gets invoked. Never even gets constructed
(I put a breakpoint in the constructor).

<command id="com.apex.ui.menu.compile.analyze"
defaultHandler="com.action.TestHandler"
name="%Button.analyze.name"
description="%Button.analyze.tooltip"
categoryId="com.ui.category.compile">
</command>

The org.eclipse.ui.handlers documention in 3.2 says "A handler that specifies
no conditions is a default handler." But that doesn't work either. If I do this,

<handler commandId="com.ui.menu.compile.analyze"
class="com.action.TestHandler">
<!--
<activeWhen>
<with variable="activePartId">
<equals value="org.eclipse.ui.views.ResourceNavigator"/>
</with>
</activeWhen>
-->
</handler>
With the activeWhen commented out the handler is never invoked. With the
activeWhen not commented out then the handler is invoked.

Is there perhaps a way to specify an activeWhen expression that is simply a
constant TRUE literal? Looking at the documentation there doesn't seem to be
a way.

Gary
Re: Contribute both a command Id plus an action implementation to another plugin. [message #324296 is a reply to message #324277] Tue, 22 January 2008 09:16 Go to previous messageGo to next message
Eclipse UserFriend
Gary E. Barnes wrote:
>
> I'm using 3.2. There doesn't appear to be a HandlerUtil in 3.2. So I'm
> doing,

OK, in 3.2 you're using the best code, although you can actually use
ISources.ACTIVE_PART_NAME instead of the string literal.

>> You can also simply supply a default handler in your command
>> definition (but you can only supply one).
>
> Does that work in 3.2? It is documented in the 3.2 Help files but if I
> specify a default handler it never gets invoked. Never even gets
> constructed (I put a breakpoint in the constructor).

It should work, but maybe that's a bug in 3.2 ... a number of bugs with
commands, handlers, and menus were fixed in 3.3, and even more in 3.4M4
(M5 will be coming out in 3 weeks or so).


> With the activeWhen commented out the handler is never invoked. With
> the activeWhen not commented out then the handler is invoked.
>
> Is there perhaps a way to specify an activeWhen expression that is
> simply a constant TRUE literal? Looking at the documentation there
> doesn't seem to be a way.

Your syntax looks correct, it must be a bug in 3.2. If you can write a
property tester in 3.2(depend on org.eclipse.core.expressions plugin)
that extends PropertyTester then you can just return TRUE all the time.

Later,
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/help33/index.jsp?topic=/org.eclipse. platform.doc.isv/guide/workbench.htm
Re: Contribute both a command Id plus an action implementation to another plugin. [message #324327 is a reply to message #324296] Tue, 22 January 2008 16:21 Go to previous messageGo to next message
Eclipse UserFriend
Paul Webster wrote:
> Gary E. Barnes wrote:
>>> You can also simply supply a default handler in your command
>>> definition (but you can only supply one).
>>
>> Does that work in 3.2? It is documented in the 3.2 Help files but if
>> I specify a default handler it never gets invoked. Never even gets
>> constructed (I put a breakpoint in the constructor).
>
> It should work, but maybe that's a bug in 3.2 ... a number of bugs with
> commands, handlers, and menus were fixed in 3.3, and even more in 3.4M4
> (M5 will be coming out in 3 weeks or so).

I decided to try 3.4M4. My constructor now gets called. So I'm one step
closer than before.

A 2nd handler instance is constructed when I Quit out of the workspace. That
also happened in 3.2. As Eclipse exits it creates a new instance of the handler.

Gary
Re: Contribute both a command Id plus an action implementation to another plugin. [message #324359 is a reply to message #324327] Wed, 23 January 2008 10:00 Go to previous message
Eclipse UserFriend
Gary E. Barnes wrote:
> I decided to try 3.4M4. My constructor now gets called. So I'm one
> step closer than before.
>
> A 2nd handler instance is constructed when I Quit out of the workspace.
> That also happened in 3.2. As Eclipse exits it creates a new instance
> of the handler.
>
> Gary

That might be an exit-ordering issue ... i.e. the handlers all get
disposed, and then as part of shutdown something asks the HandlerProxy
for isEnabled() (which would re-instantiate the handler).

If you put a Thread.dumpStack() (in a conditional breakpoint or to the
console) you could open a bug with the closing (2nd instance) stack
trace at:

https://bugs.eclipse.org/bugs/enter_bug.cgi?product=Platform &component=UI&short_desc=[Contributions]

--
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/help33/index.jsp?topic=/org.eclipse. platform.doc.isv/guide/workbench.htm
Previous Topic:Standard properties window
Next Topic:Can plugin.xml contribute context menu items for an editor?
Goto Forum:
  


Current Time: Mon Jul 14 21:35:32 EDT 2025

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

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

Back to the top