Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse Platform » How to participate in cut / copy / paste?
How to participate in cut / copy / paste? [message #331100] Mon, 25 August 2008 12:28 Go to next message
Prasanna K is currently offline Prasanna KFriend
Messages: 78
Registered: July 2009
Member
Hi,

I want the cut / copy / paste buttons on the toolbar (and the Edit menu
options) to be enabled when user selects a view of my plugin. And I want
to perform some actions when the respective button is pressed. How can I
achieve this?

Thanks in advance.
Re: How to participate in cut / copy / paste? [message #331236 is a reply to message #331100] Thu, 28 August 2008 14:45 Go to previous messageGo to next message
Paul Webster is currently offline Paul WebsterFriend
Messages: 6859
Registered: July 2009
Location: Ottawa
Senior Member

kaprasanna wrote:
> Hi,
>
> I want the cut / copy / paste buttons on the toolbar (and the Edit menu
> options) to be enabled when user selects a view of my plugin. And I want
> to perform some actions when the respective button is pressed. How can I
> achieve this?

Check out the help listed in my sig and
http://www.eclipse.org/articles/Article-action-contribution/ Contributing%20Actions%20to%20the%20Eclipse%20Workbench.html

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/ganymede/index.jsp?topic=/org.eclips e.platform.doc.isv/guide/workbench.htm


Re: How to participate in cut / copy / paste? [message #331257 is a reply to message #331236] Thu, 28 August 2008 15:30 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: mario.winterer.gmx.net

Hi!

I think since eclipse 3.4 it should also be possible to use the command
framework and register a command handler for the required retargetable
action.
But I'm wondering what happens if both, an action delegate and a command
handler are registered for a given command/action id. What happens if
the command is executed? Will the handler be used or the action delegate?

Mario

Paul Webster schrieb:
> kaprasanna wrote:
>> Hi,
>>
>> I want the cut / copy / paste buttons on the toolbar (and the Edit
>> menu options) to be enabled when user selects a view of my plugin. And
>> I want to perform some actions when the respective button is pressed.
>> How can I achieve this?
>
> Check out the help listed in my sig and
> http://www.eclipse.org/articles/Article-action-contribution/ Contributing%20Actions%20to%20the%20Eclipse%20Workbench.html
>
>
> PW
>
>
Re: How to participate in cut / copy / paste? [message #331276 is a reply to message #331257] Thu, 28 August 2008 17:56 Go to previous messageGo to next message
Paul Webster is currently offline Paul WebsterFriend
Messages: 6859
Registered: July 2009
Location: Ottawa
Senior Member

Mario Winterer wrote:
> Hi!
>
> I think since eclipse 3.4 it should also be possible to use the command
> framework and register a command handler for the required retargetable
> action.
> But I'm wondering what happens if both, an action delegate and a command
> handler are registered for a given command/action id. What happens if
> the command is executed? Will the handler be used or the action delegate?

In 3.4 you can do it entirely using commands and handlers. For example,
when your view is created simply get the IHandlerService and register
handlers:

IHandlerService hs
= (IHandlerService) getSite().getService(IHandlerService.class);
hs.activeHandler("org.eclipse.ui.edit.copy", new MyCopyHandler());
// and so forth for cut and paste

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/ganymede/index.jsp?topic=/org.eclips e.platform.doc.isv/guide/workbench.htm


Re: How to participate in cut / copy / paste? [message #331300 is a reply to message #331276] Fri, 29 August 2008 10:29 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: mario.winterer.gmx.net

Ah. Nice. I thought this is only possible using the extension point.
But in my case, a global action handler is already registered for the
copy action. Now I want to additionally register a handler for the same
copy action.
But unfortunately, the global action handler is chosen always, my
handler never gets executed.

Paul Webster schrieb:
> Mario Winterer wrote:
>> Hi!
>>
>> I think since eclipse 3.4 it should also be possible to use the
>> command framework and register a command handler for the required
>> retargetable action.
>> But I'm wondering what happens if both, an action delegate and a
>> command handler are registered for a given command/action id. What
>> happens if the command is executed? Will the handler be used or the
>> action delegate?
>
> In 3.4 you can do it entirely using commands and handlers. For example,
> when your view is created simply get the IHandlerService and register
> handlers:
>
> IHandlerService hs
> = (IHandlerService) getSite().getService(IHandlerService.class);
> hs.activeHandler("org.eclipse.ui.edit.copy", new MyCopyHandler());
> // and so forth for cut and paste
>
> PW
>
Re: How to participate in cut / copy / paste? [message #331364 is a reply to message #331300] Wed, 03 September 2008 16:44 Go to previous messageGo to next message
Paul Webster is currently offline Paul WebsterFriend
Messages: 6859
Registered: July 2009
Location: Ottawa
Senior Member

Mario Winterer wrote:
> Ah. Nice. I thought this is only possible using the extension point.
> But in my case, a global action handler is already registered for the
> copy action. Now I want to additionally register a handler for the same
> copy action.
> But unfortunately, the global action handler is chosen always, my
> handler never gets executed.

The active handler gets determined by a (totally bogus) priority. The
default handler has a pri==0. The rest of the priorities are calculated
based on the activeWhen expression and the variables that they use (so
no activeWhen expression means pri==0).

The code example I showed you gets an ActivePartExpression on its way up
the chain, and so it has a higher priority than the default handler (and
works correctly as long as your part is active).

How are you registering your handler?

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/ganymede/index.jsp?topic=/org.eclips e.platform.doc.isv/guide/workbench.htm


Re: How to participate in cut / copy / paste? [message #331412 is a reply to message #331236] Fri, 05 September 2008 07:01 Go to previous messageGo to next message
Prasanna K is currently offline Prasanna KFriend
Messages: 78
Registered: July 2009
Member
Hi Paul,

Looks like this is not working for me fully.

This is what I have done :
I am using Eclipse 3.3.2 (Europa).
I have used the extension point : org.eclipse.ui.handlers as :

<extension
point="org.eclipse.ui.handlers">
<handler
class="com.example.MyHandler"
commandId="com.example.handler">
<activeWhen>
<instanceof
value="com.example.helloworld.MyView">
</instanceof>
</activeWhen>
</handler>
</extension>

I have created a class MyHandler which extends
org.eclipse.core.commands.AbstractHandler.

This is the code in my view's createPartControl method :
IHandlerService hs =
(IHandlerService)getSite().getService(IHandlerService.class) ;
hs.activateHandler("org.eclipse.ui.edit.copy", new MyHandler());

I have a println statement in execute method of MyHandler as :
System.out.println("execute called...");

I run the project and click on my view.
The copy option on in Edit menu is not enabled.
But if I press ctrl+c. The above println comes up on the console.

Question : How do I enable the Edit -> copy / cut options when user clicks
on my view.

Thanks.
Re: How to participate in cut / copy / paste? [message #331423 is a reply to message #331412] Fri, 05 September 2008 12:15 Go to previous messageGo to next message
Paul Webster is currently offline Paul WebsterFriend
Messages: 6859
Registered: July 2009
Location: Ottawa
Senior Member

kaprasi wrote:
> Hi Paul,
>
> Looks like this is not working for me fully.
>
> This is what I have done :
> I am using Eclipse 3.3.2 (Europa).
> I have used the extension point : org.eclipse.ui.handlers as :
>
> <extension
> point="org.eclipse.ui.handlers">
> <handler
> class="com.example.MyHandler"
> commandId="com.example.handler">
> <activeWhen>
> <instanceof
> value="com.example.helloworld.MyView">
> </instanceof>
> </activeWhen>
> </handler>
> </extension>

^^^ This definition is interesting, but has nothing to do with copy. On
top of that, the activeWhen clause will always be false (since the
default variable is a Collection).

> I have created a class MyHandler which extends
> org.eclipse.core.commands.AbstractHandler.
>
> This is the code in my view's createPartControl method :
> IHandlerService hs =
> (IHandlerService)getSite().getService(IHandlerService.class) ;
> hs.activateHandler("org.eclipse.ui.edit.copy", new MyHandler());

This is fine, and what makes CTRL+C work.

> I run the project and click on my view.
> The copy option on in Edit menu is not enabled.
> But if I press ctrl+c. The above println comes up on the console.
>
> Question : How do I enable the Edit -> copy / cut options when user
> clicks on my view.

In 3.3 Edit>Copy is a RetargetAction. It will only enable if you also
have a
getViewSite().getActionBars().setGlobalActionHandler(ActionF actory.COPY.getId(),
new MyCopyAction());

You can make the handler and the action work off of the same code base
(so they're in sync).

In 3.4, the RetargetAction has been replaced with a
CommandContributionItem (which tracks the same state as CTRL+C)

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/ganymede/index.jsp?topic=/org.eclips e.platform.doc.isv/guide/workbench.htm


Re: How to participate in cut / copy / paste? [message #331427 is a reply to message #331423] Fri, 05 September 2008 13:36 Go to previous messageGo to next message
Prasanna K is currently offline Prasanna KFriend
Messages: 78
Registered: July 2009
Member
Hi Paul,
Thanks for the reply.
I got it working. Now both ctrl+x / c / v as well as Edit->options work
fine.

But I still have a slight problem.
Because this code is being called from createPartControl() of the view, I
can't change these behaviors according to the selection.
To elaborate more :
The code in createPartControl() of the view looks like this :


Action copyAction = new Action() {
@Override
public void run() {
System.out.println("copied!!");
super.run();
}
};

IActionBars actionBars = getViewSite().getActionBars();
actionBars.setGlobalActionHandler(ActionFactory.COPY.getId() ,
copyAction);
// similar code for cut and paste

This enables cut / copy / paste options in Edit menu all-time;
irrespective of what is selected.

I want a control over when the edit menu options get enabled.

I have a method which gets called when a particular part of the view gets
selected. Placing the above code in that method doesn't work.

How can I achieve this?
Thanks.
Re: How to participate in cut / copy / paste? [message #331450 is a reply to message #331427] Mon, 08 September 2008 12:09 Go to previous messageGo to next message
Paul Webster is currently offline Paul WebsterFriend
Messages: 6859
Registered: July 2009
Location: Ottawa
Senior Member

your code must call setEnabled(*) on your copyAction.

When your view is active, then Edit>Copy will be enabled depending on
your copyAction. When your view is not active, Edit>Copy is controlled
by some other part.

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/ganymede/index.jsp?topic=/org.eclips e.platform.doc.isv/guide/workbench.htm


Re: How to participate in cut / copy / paste? [message #331471 is a reply to message #331450] Tue, 09 September 2008 07:03 Go to previous message
Prasanna K is currently offline Prasanna KFriend
Messages: 78
Registered: July 2009
Member
> your code must call setEnabled(*) on your copyAction.

Paul,

It works fabulous.
Thanks so much.
Previous Topic:clipboard paste fails in eclipse 3.4.0
Next Topic:Get workbench's toolbar
Goto Forum:
  


Current Time: Tue Dec 03 17:25:22 GMT 2024

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

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

Back to the top