Skip to main content



      Home
Home » Eclipse Projects » Eclipse Platform » How to declare an Action to an existing Command?
How to declare an Action to an existing Command? [message #318295] Wed, 25 July 2007 01:09 Go to next message
Eclipse UserFriend
Hi,

I would like to know if there is a way for me to assign my own action to one
of the Commands provided by Eclipse.

I already have created my very own commands and actions, but on this way I'm
not using the same commands that Eclipse is using (I also had to give them a
different name).
However it sounds lame to have a command named "paste (my style)" being
assigned to my context.
It would look a lot better if I could just tell what Action should be
executed when the user "executes" the command (in this case paste).

However I have not yet found a description on how to connect to the Eclipse
provided commands (not new ones created by me), and I absolutely have not
found any data about the Eclipse provided commands (commandID and such).

Thanks,

Kristof
Re: How to declare an Action to an existing Command? [message #318312 is a reply to message #318295] Wed, 25 July 2007 10:13 Go to previous messageGo to next message
Eclipse UserFriend
Take a look at the ActionFactory class [1]. The Paste action is defined
there, and can be created in your ActionBarAdvisor class and added to
the application menu or toolbar. Paste is also a retargetable action,
meaning that you would need to provide it's behavior somewhere.

Eg. in a ViewPart:

// Define behavior for the PROPERTIES retargetable action
IAction retargetAction = new Action("RetargetAction") //$NON-NLS-1$
{
public void run()
{
//Part specific paste behavior goes here
}
};

// Link PASTE retargetable action with our behavior
getViewSite().getActionBars().setGlobalActionHandler(ActionF actory.PASTE.getId(),
retargetAction);

[1]
http://help.eclipse.org/help33/index.jsp?topic=/org.eclipse. platform.doc.isv/reference/api/org/eclipse/ui/actions/Action Factory.html

Kristof Szabados wrote:
> Hi,
>
> I would like to know if there is a way for me to assign my own action to one
> of the Commands provided by Eclipse.
>
> I already have created my very own commands and actions, but on this way I'm
> not using the same commands that Eclipse is using (I also had to give them a
> different name).
> However it sounds lame to have a command named "paste (my style)" being
> assigned to my context.
> It would look a lot better if I could just tell what Action should be
> executed when the user "executes" the command (in this case paste).
>
> However I have not yet found a description on how to connect to the Eclipse
> provided commands (not new ones created by me), and I absolutely have not
> found any data about the Eclipse provided commands (commandID and such).
>
> Thanks,
>
> Kristof
>
>
Re: How to declare an Action to an existing Command? [message #318319 is a reply to message #318295] Wed, 25 July 2007 14:10 Go to previous messageGo to next message
Eclipse UserFriend
Kristof Szabados wrote:
>
> However I have not yet found a description on how to connect to the Eclipse
> provided commands (not new ones created by me), and I absolutely have not
> found any data about the Eclipse provided commands (commandID and such).


The general case is straight-forward. Declaratively you can use
org.eclipse.ui.handlers to attach your own handler to one of the
platform global commands. How you set up your activeWhen clause will
determine when your handler is active for that command. When using the
IHandlerService to programmatically activate a handler, the Expression
will determine when your handler is active.

There are 2 issues.

1) how do I find the commands to override? Some of the platform
commands are published as API (string constants in an interface
somewhere). But they're not easy to find, and it's not all of them. I
use the Plug-in Registry View to look at the commands contributed by
org.eclipse.ui and org.eclipse.ui.ide. Platform Text also contributes
many commands and also has some of the Platform UI command IDs:
IWorkbenchActionDefinitionIds.

2) there are a few special cases. cut/copy/paste/delete/select all come
to mind :-) Even if you use ActionFactory to contribute these to the
main menu, you can still (sometimes) use handlers to define their
behaviour. Sometimes you really do have to treat them as actions.

Later,
PW
Re: How to declare an Action to an existing Command? [message #318470 is a reply to message #318319] Mon, 30 July 2007 12:29 Go to previous messageGo to next message
Eclipse UserFriend
Right -- but for the "Open" command, the id is neither in the
ActionFactory nor in org.eclipse.ui -- so where is it?

(I do see an "Open Resource" in org.eclipse.ui.ide ==
"org.eclipse.ui.navigate.openResource" ==>> but I am not writing for the
ide, I am writing an RCP.)

Perhaps there is no "open" command for RCP? If not, how to *plug in* a
double-click action to open something?

thanks,
Paul
Re: How to declare an Action to an existing Command? [message #318482 is a reply to message #318470] Mon, 30 July 2007 14:21 Go to previous messageGo to next message
Eclipse UserFriend
Paul Th. Keyser wrote:
> Right -- but for the "Open" command, the id is neither in the
> ActionFactory nor in org.eclipse.ui -- so where is it?
>
> (I do see an "Open Resource" in org.eclipse.ui.ide ==
> "org.eclipse.ui.navigate.openResource" ==>> but I am not writing for the
> ide, I am writing an RCP.)

Right, org.eclipse.ui.ide contains all of the workbench commands that
have to do with resources (since org.eclipse.ui is resource/workspace
free :-)

> Perhaps there is no "open" command for RCP? If not, how to *plug in* a
> double-click action to open something?

It really depends on what your RCP app depends on. org.eclipse.ui.ide
provides the link between the workbench and org.eclipse.core.resoures.
If you don't depend on org.eclipse.ui.ide, you would probably have to
re-implement some of its functionality.

Later,
PW
Re: How to declare an Action to an existing Command? [message #318568 is a reply to message #318482] Tue, 31 July 2007 19:51 Go to previous messageGo to next message
Eclipse UserFriend
Well, what I wound up doing was:

1) define my own command-id (say, "com.mun.ist.open")

2) set "com.mun.ist.open" as the "definitionId" of my plugged-in
context-menu's action

3) then in the part (as it happens, View), I wrote:
theStructuredViewer.addDoubleClickListener(new IDoubleClickListener() {
public void doubleClick(DoubleClickEvent event) {
IHandlerService hs = (IHandlerService)
PlatformUI.getWorkbench().getAdapter(IHandlerService.class);
try {
// although javadocs for this method claim one
// can pass any event, using the
// DoubleClickEvent will not compile:
hs.executeCommand("com.mun.ist.open", null);

} catch (Exception e) {
// TODO keyser: handle somehow: rare
// handler-exceptions of various kinds
}
}});

4) =>, and, like magic, it works!

thanks,
Paul
Re: How to declare an Action to an existing Command? [message #318610 is a reply to message #318568] Wed, 01 August 2007 08:35 Go to previous message
Eclipse UserFriend
And that looks exactly like it is meant to be used :-)

Paul Th. Keyser wrote:
> IHandlerService hs = (IHandlerService)
> PlatformUI.getWorkbench().getAdapter(IHandlerService.class);

The only suggestion that I would make is use a closer IServiceLocator
than the workbench:
IHandlerService hs
= (IHandlerService) getSite().getService(IHandlerService.class);

since you are creating an anonymous inner class to your view.

Later,
PW
Previous Topic:Custom action contribution on F5
Next Topic:Re: Problem in using setFocus() method from Viewpart class
Goto Forum:
  


Current Time: Tue Jul 22 15:52:47 EDT 2025

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

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

Back to the top