Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » GEF » implementing cut/copy/paste
implementing cut/copy/paste [message #159391] Fri, 26 November 2004 14:00 Go to next message
Eclipse UserFriend
Originally posted by: jojo.virtutech.se

I'm trying to implement cut/copy/paste in my editor using actions created
by

ActionFactory.CUT.create(window)

etc.

I've managed to create the actions and added them to the context menu and
toolbar, but how do I implement the actual code which performs the actions?

Or should I implement my own action classes?

--
/Jesper
Re: implementing cut/copy/paste [message #159424 is a reply to message #159391] Sat, 27 November 2004 19:16 Go to previous messageGo to next message
Pratik Shah is currently offline Pratik ShahFriend
Messages: 1077
Registered: July 2009
Senior Member
Yes, you'll have to write your own actions (in addition to the retarget
actions that you've already created) and add them to your Editor's
actionRegistry. Make sure these actions have the same IDs as the retarget
actions.

"Jesper Eskilson" <jojo@virtutech.se> wrote in message
news:u67ekign210.fsf@morpheus.hq.vtech...
>
> I'm trying to implement cut/copy/paste in my editor using actions created
> by
>
> ActionFactory.CUT.create(window)
>
> etc.
>
> I've managed to create the actions and added them to the context menu and
> toolbar, but how do I implement the actual code which performs the
actions?
>
> Or should I implement my own action classes?
>
> --
> /Jesper
Re: implementing cut/copy/paste [message #159541 is a reply to message #159424] Mon, 29 November 2004 10:15 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: jojo.virtutech.se

"Pratik Shah" <ppshah@us.ibm.com> writes:

> Yes, you'll have to write your own actions (in addition to the retarget
> actions that you've already created) and add them to your Editor's
> actionRegistry. Make sure these actions have the same IDs as the
> retarget actions.

What purpose does it serve to create both retarget-actions and "regular"
actions?

--
/Jesper
Re: implementing cut/copy/paste [message #159557 is a reply to message #159424] Mon, 29 November 2004 12:27 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: jojo.virtutech.se

"Pratik Shah" <ppshah@us.ibm.com> writes:

> Yes, you'll have to write your own actions (in addition to the retarget
> actions that you've already created) and add them to your Editor's
> actionRegistry. Make sure these actions have the same IDs as the retarget
> actions.

The copy-action is now called properly, but I can't get a hold of the
selection. getSelectedObjects() returns the empty list.

My editor's createActions() looks like this:

protected void createActions()
{
super.createActions();
IActionBars bars = getEditorSite().getActionBars();
ActionRegistry registry = getActionRegistry();
IAction action;

action = new CopyAction(this);
registry.registerAction(action);
bars.setGlobalActionHandler(CopyAction.ID, action);

[...]

}

I've tried following the logic-example, but to no avail.

--
/Jesper
Re: implementing cut/copy/paste [message #159564 is a reply to message #159557] Mon, 29 November 2004 13:03 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: Lamont_Gilbert.rigidsoftware.com

getSelectedObjects should never return the empty list. If anything it
should return your main editpart (your drawing).

try

ActionRegistry registry = getActionRegistry();
IAction action;
action = new CopyAction(this);
registry.registerAction(action);
getSelectionActions().add(action.getID());




Jesper Eskilson wrote:
> "Pratik Shah" <ppshah@us.ibm.com> writes:
>
>
>>Yes, you'll have to write your own actions (in addition to the retarget
>>actions that you've already created) and add them to your Editor's
>>actionRegistry. Make sure these actions have the same IDs as the retarget
>>actions.
>
>
> The copy-action is now called properly, but I can't get a hold of the
> selection. getSelectedObjects() returns the empty list.
>
> My editor's createActions() looks like this:
>
> protected void createActions()
> {
> super.createActions();
> IActionBars bars = getEditorSite().getActionBars();
> ActionRegistry registry = getActionRegistry();
> IAction action;
>
> action = new CopyAction(this);
> registry.registerAction(action);
> bars.setGlobalActionHandler(CopyAction.ID, action);
>
> [...]
>
> }
>
> I've tried following the logic-example, but to no avail.
>


--
Respectfully,


CL Gilbert

"Verily, verily, I say unto you, He that entereth not by the door() into
the sheepfold{}, but climbeth up some other *way, the same is a thief
and a robber."

GnuPG Key Fingerprint:
82A6 8893 C2A1 F64E A9AD 19AE 55B2 4CD7 80D2 0A2D

For a free Java interface to Freechess.org see
http://www.rigidsoftware.com/Chess/chess.html
Re: implementing cut/copy/paste [message #159658 is a reply to message #159564] Tue, 30 November 2004 08:36 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: jojo.virtutech.se

"CL [dnoyeb] Gilbert" <Lamont_Gilbert@rigidsoftware.com> writes:

> getSelectedObjects should never return the empty list.

Well, it does.

After some investigation, it appears that CopyAction's update() method is
never called, and thus the selection field is never set. I wonder why.

I'll keep investigating.

--
/Jesper
Re: implementing cut/copy/paste [message #159675 is a reply to message #159658] Tue, 30 November 2004 09:26 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: jojo.virtutech.se

Jesper Eskilson wrote:
> "CL [dnoyeb] Gilbert" <Lamont_Gilbert@rigidsoftware.com> writes:
>
>
>>getSelectedObjects should never return the empty list.
>
>
> Well, it does.
>
> After some investigation, it appears that CopyAction's update() method is
> never called, and thus the selection field is never set. I wonder why.
>
> I'll keep investigating.
>

What seems to happen is this: when selectionChanged (in the
GraphicalEditor class) calls updateAction() on all selection-actions,
only the retarget-version of the action gets called -- they have the
same ID, so the action-registry can only hold one of them:

protected void updateActions(List actionIds)
{
ActionRegistry registry = getActionRegistry();
Iterator iter = actionIds.iterator();
while (iter.hasNext()) {
IAction action = registry.getAction(iter.next());
if (action instanceof UpdateAction)
((UpdateAction)action).update();
}
}

The parameter actionIds is here [ 'delete', 'copy' ] when called from
selectionChanged, and registry.getAction() returns a RetargetAction(),
not my CopyAction.

So, something is wrong in my action-registry. Pratik said that I should
create both retarget-actions and "normal" actions with the same ID, but
then they can't reside in the same action-registry. Or did I miss something?

/Jesper
Re: implementing cut/copy/paste [message #159700 is a reply to message #159675] Tue, 30 November 2004 16:19 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: Lamont_Gilbert.rigidsoftware.com

Jesper Eskilson wrote:
> Jesper Eskilson wrote:
>
>> "CL [dnoyeb] Gilbert" <Lamont_Gilbert@rigidsoftware.com> writes:
>>
>>
>>> getSelectedObjects should never return the empty list.
>>
>>
>>
>> Well, it does.
>>
>> After some investigation, it appears that CopyAction's update() method is
>> never called, and thus the selection field is never set. I wonder why.
>>
>> I'll keep investigating.
>>
>
> What seems to happen is this: when selectionChanged (in the
> GraphicalEditor class) calls updateAction() on all selection-actions,
> only the retarget-version of the action gets called -- they have the
> same ID, so the action-registry can only hold one of them:
>
> protected void updateActions(List actionIds)
> {
> ActionRegistry registry = getActionRegistry();
> Iterator iter = actionIds.iterator();
> while (iter.hasNext()) {
> IAction action = registry.getAction(iter.next());
> if (action instanceof UpdateAction)
> ((UpdateAction)action).update();
> }
> }
>
> The parameter actionIds is here [ 'delete', 'copy' ] when called from
> selectionChanged, and registry.getAction() returns a RetargetAction(),
> not my CopyAction.
>
> So, something is wrong in my action-registry. Pratik said that I should
> create both retarget-actions and "normal" actions with the same ID, but
> then they can't reside in the same action-registry. Or did I miss
> something?
>
> /Jesper

Relax my friend. Did you try what I indicated in my post?

--
Respectfully,


CL Gilbert

"Verily, verily, I say unto you, He that entereth not by the door() into
the sheepfold{}, but climbeth up some other *way, the same is a thief
and a robber."

GnuPG Key Fingerprint:
82A6 8893 C2A1 F64E A9AD 19AE 55B2 4CD7 80D2 0A2D

For a free Java interface to Freechess.org see
http://www.rigidsoftware.com/Chess/chess.html
Re: implementing cut/copy/paste [message #159866 is a reply to message #159675] Tue, 30 November 2004 20:58 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: none.us.ibm.com

The registry should hold onto actions maintained by the Editor instance.
RetargetActions should only be created by the ActionBarContributor, not each
editor instance.

The purpose of retarget actions is to map a single ActionBarContributor to
multiple editor instances within a given WorkbenchWindow. I think actionbar
contributors were designed as 1-per-window-per-editor-type to reduce flicker
and repainting when changing between multiple java CU editors. It also
reduces the number of native widgets created.

"Jesper Eskilson" <jojo@virtutech.se> wrote in message
news:cohec2$bf8$1@www.eclipse.org...
> Jesper Eskilson wrote:
> > "CL [dnoyeb] Gilbert" <Lamont_Gilbert@rigidsoftware.com> writes:
> >
> >
> >>getSelectedObjects should never return the empty list.
> >
> >
> > Well, it does.
> >
> > After some investigation, it appears that CopyAction's update() method
is
> > never called, and thus the selection field is never set. I wonder why.
> >
> > I'll keep investigating.
> >
>
> What seems to happen is this: when selectionChanged (in the
> GraphicalEditor class) calls updateAction() on all selection-actions,
> only the retarget-version of the action gets called -- they have the
> same ID, so the action-registry can only hold one of them:
>
> protected void updateActions(List actionIds)
> {
> ActionRegistry registry = getActionRegistry();
> Iterator iter = actionIds.iterator();
> while (iter.hasNext()) {
> IAction action = registry.getAction(iter.next());
> if (action instanceof UpdateAction)
> ((UpdateAction)action).update();
> }
> }
>
> The parameter actionIds is here [ 'delete', 'copy' ] when called from
> selectionChanged, and registry.getAction() returns a RetargetAction(),
> not my CopyAction.
>
> So, something is wrong in my action-registry. Pratik said that I should
> create both retarget-actions and "normal" actions with the same ID, but
> then they can't reside in the same action-registry. Or did I miss
something?
>
> /Jesper
Re: implementing cut/copy/paste [message #159879 is a reply to message #159866] Tue, 30 November 2004 21:59 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: Lamont_Gilbert.rigidsoftware.com

I believe he has just failed to do this

getSelectionActions().add(action.getID());

so his actions are registered, but not with the selection service or
whatever.


Randy Hudson wrote:
> The registry should hold onto actions maintained by the Editor instance.
> RetargetActions should only be created by the ActionBarContributor, not each
> editor instance.
>
> The purpose of retarget actions is to map a single ActionBarContributor to
> multiple editor instances within a given WorkbenchWindow. I think actionbar
> contributors were designed as 1-per-window-per-editor-type to reduce flicker
> and repainting when changing between multiple java CU editors. It also
> reduces the number of native widgets created.
>
> "Jesper Eskilson" <jojo@virtutech.se> wrote in message
> news:cohec2$bf8$1@www.eclipse.org...
>
Re: implementing cut/copy/paste [message #159984 is a reply to message #159700] Wed, 01 December 2004 13:27 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: jojo.virtutech.se

CL [dnoyeb] Gilbert wrote:

> Relax my friend. Did you try what I indicated in my post?

I did. The actions are indeed registered as selection actions.

/Jesper
Re: implementing cut/copy/paste [message #159991 is a reply to message #159866] Wed, 01 December 2004 13:52 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: jojo.virtutech.se

Randy Hudson wrote:
> The registry should hold onto actions maintained by the Editor instance.
> RetargetActions should only be created by the ActionBarContributor, not each
> editor instance.
>
> The purpose of retarget actions is to map a single ActionBarContributor to
> multiple editor instances within a given WorkbenchWindow. I think actionbar
> contributors were designed as 1-per-window-per-editor-type to reduce flicker
> and repainting when changing between multiple java CU editors. It also
> reduces the number of native widgets created.

Ok, thanks. The mist is clearing a little now.

The problem was (I think) that I added the retarget-actions to the
editors registry instead of using addRetargetAction().

So, now I've changed my actionbar-contributor to do the following:

IWorkbenchWindow window =
PlatformUI.getWorkbench().getActiveWorkbenchWindow();

addRetargetAction((RetargetAction) ActionFactory.CUT.create(window));
addRetargetAction((RetargetAction) ActionFactory.COPY.create(window));
addRetargetAction((RetargetAction) ActionFactory.PASTE.create(window));

but now I get a ClassCastException() in

org.eclipse.ui.actions.RetargetAction.propagateChange(Retarg etAction.java:195)

Apparently this mist didn't clear enough. :-)

/Jesper
Re: implementing cut/copy/paste [message #160071 is a reply to message #159991] Thu, 02 December 2004 02:28 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: Lamont_Gilbert.rigidsoftware.com

Well lets go over all the steps.

1. Create Cut/Copy/Paste commands based on org.eclipse.gef.commands.Command

2. Create Cut/Copy actions based on
org.eclipse.gef.ui.actions.SelectionAction
paste probably wont be a selection action since its not based on selection
be sure to properly override calculateEnabled()
be sure in the init method or some other appropriate place to
setID(ActionFactory.PASTE.getId());



3. Override createActions of GraphicalEditor
NOTE: these are not retarget actions but the ones you created in step 2

super.createActions();
action = new CopyAction((IworkbenchPart)this);
registry.registerAction(action)
getSelectionActions().add(action.getId());


4. in your editorContributor override buildActions and add retarget
actions here

//first create and register the retarget action
IWorkbenchWindow = getPage().getWorkbenchWindow();
addRetargetAction((RetargetAction)ActionFactory.COPY.create( iww));

//then add it to a menu or toolbar
override ContributeToToolBar(IToolBarManager tbm) or wherever you want
to add the actions

tbm.add(getAction(ActionFactory.COPY.getId()));

or contributeToMenu

Only way off hand I know how to use the editor contributor is by telling
the xml file to use it if you extend the point org.eclipse.ui.editors add

contributorClass = "MyContributor"

Wish I could be more help with a more direct route, but I think you got
this part fixed anyway since you are actually seeing the buttons and
menuitems.







Jesper Eskilson wrote:
> Randy Hudson wrote:
>
>> The registry should hold onto actions maintained by the Editor instance.
>> RetargetActions should only be created by the ActionBarContributor,
>> not each
>> editor instance.
>>
>> The purpose of retarget actions is to map a single
>> ActionBarContributor to
>> multiple editor instances within a given WorkbenchWindow. I think
>> actionbar
>> contributors were designed as 1-per-window-per-editor-type to reduce
>> flicker
>> and repainting when changing between multiple java CU editors. It also
>> reduces the number of native widgets created.
>
>
> Ok, thanks. The mist is clearing a little now.
>
> The problem was (I think) that I added the retarget-actions to the
> editors registry instead of using addRetargetAction().
>
> So, now I've changed my actionbar-contributor to do the following:
>
> IWorkbenchWindow window =
> PlatformUI.getWorkbench().getActiveWorkbenchWindow();
>
> addRetargetAction((RetargetAction) ActionFactory.CUT.create(window));
> addRetargetAction((RetargetAction) ActionFactory.COPY.create(window));
> addRetargetAction((RetargetAction) ActionFactory.PASTE.create(window));
>
> but now I get a ClassCastException() in
>
> org.eclipse.ui.actions.RetargetAction.propagateChange(Retarg etAction.java:195)
>
>
> Apparently this mist didn't clear enough. :-)
>
> /Jesper


--
Respectfully,


CL Gilbert

"Verily, verily, I say unto you, He that entereth not by the door() into
the sheepfold{}, but climbeth up some other *way, the same is a thief
and a robber."

GnuPG Key Fingerprint:
82A6 8893 C2A1 F64E A9AD 19AE 55B2 4CD7 80D2 0A2D

For a free Java interface to Freechess.org see
http://www.rigidsoftware.com/Chess/chess.html
Re: implementing cut/copy/paste [message #160873 is a reply to message #160071] Tue, 07 December 2004 17:05 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: jojo.virtutech.se

CL [dnoyeb] Gilbert wrote:
> Well lets go over all the steps.
>
> 1. Create Cut/Copy/Paste commands based on org.eclipse.gef.commands.Command
>
> 2. Create Cut/Copy actions based on
> org.eclipse.gef.ui.actions.SelectionAction
> paste probably wont be a selection action since its not based on selection
> be sure to properly override calculateEnabled()
> be sure in the init method or some other appropriate place to
> setID(ActionFactory.PASTE.getId());
>
>
>
> 3. Override createActions of GraphicalEditor
> NOTE: these are not retarget actions but the ones you created in step 2
>
> super.createActions();
> action = new CopyAction((IworkbenchPart)this);
> registry.registerAction(action)
> getSelectionActions().add(action.getId());
>
>
> 4. in your editorContributor override buildActions and add retarget
> actions here
>
> //first create and register the retarget action
> IWorkbenchWindow = getPage().getWorkbenchWindow();
> addRetargetAction((RetargetAction)ActionFactory.COPY.create( iww));
>
> //then add it to a menu or toolbar
> override ContributeToToolBar(IToolBarManager tbm) or wherever you want
> to add the actions
>
> tbm.add(getAction(ActionFactory.COPY.getId()));
>
> or contributeToMenu
>
> Only way off hand I know how to use the editor contributor is by telling
> the xml file to use it if you extend the point org.eclipse.ui.editors add
>
> contributorClass = "MyContributor"
>
> Wish I could be more help with a more direct route, but I think you got
> this part fixed anyway since you are actually seeing the buttons and
> menuitems.

Thanks.
Re: implementing cut/copy/paste [message #160880 is a reply to message #160071] Tue, 07 December 2004 17:13 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: jojo.virtutech.se

CL [dnoyeb] Gilbert wrote:
> Well lets go over all the steps.
>
> 1. Create Cut/Copy/Paste commands based on
> org.eclipse.gef.commands.Command
>
> 2. Create Cut/Copy actions based on
> org.eclipse.gef.ui.actions.SelectionAction paste probably wont be a
> selection action since its not based on selection be sure to properly
> override calculateEnabled() be sure in the init method or some other
> appropriate place to setID(ActionFactory.PASTE.getId());
>
>
>
> 3. Override createActions of GraphicalEditor NOTE: these are not
> retarget actions but the ones you created in step 2
>
> super.createActions(); action = new CopyAction((IworkbenchPart)this);
> registry.registerAction(action)
> getSelectionActions().add(action.getId());
>
>
> 4. in your editorContributor override buildActions and add retarget
> actions here
>
> //first create and register the retarget action IWorkbenchWindow =
> getPage().getWorkbenchWindow();

Ah. I was looking for the correct way to get a reference to the window.

> addRetargetAction((RetargetAction)ActionFactory.COPY.create( iww));
>
> //then add it to a menu or toolbar override
> ContributeToToolBar(IToolBarManager tbm) or wherever you want to add
> the actions
>
> tbm.add(getAction(ActionFactory.COPY.getId()));
>
> or contributeToMenu
>
> Only way off hand I know how to use the editor contributor is by
> telling the xml file to use it if you extend the point
> org.eclipse.ui.editors add
>
> contributorClass = "MyContributor"
>
> Wish I could be more help with a more direct route, but I think you
> got this part fixed anyway since you are actually seeing the buttons
> and menuitems.

Thanks. Now I get stuck on a strange ClassCastException in
RetargetAction.propagateChange(). The result of getSource() is cast to
(IActionBars), but according to the debugger, the source-field in the
event is of type RetargetAction.

setActionHandler(((IActionBars)event.getSource()).getGlobalA ctionHandler(getId()));

Hm.

/Jesper (puzzled)
Re: implementing cut/copy/paste [message #160897 is a reply to message #160880] Tue, 07 December 2004 23:20 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: Lamont_Gilbert.rigidsoftware.com

Jesper Eskilson wrote:
> CL [dnoyeb] Gilbert wrote:
>
>> Well lets go over all the steps.
>>
>> 1. Create Cut/Copy/Paste commands based on
>> org.eclipse.gef.commands.Command
>>
>> 2. Create Cut/Copy actions based on
>> org.eclipse.gef.ui.actions.SelectionAction paste probably wont be a
>> selection action since its not based on selection be sure to properly
>> override calculateEnabled() be sure in the init method or some other
>> appropriate place to setID(ActionFactory.PASTE.getId());
>>
>>
>>
>> 3. Override createActions of GraphicalEditor NOTE: these are not
>> retarget actions but the ones you created in step 2
>>
>> super.createActions(); action = new CopyAction((IworkbenchPart)this);
>> registry.registerAction(action)
>> getSelectionActions().add(action.getId());
>>
>>
>> 4. in your editorContributor override buildActions and add retarget
>> actions here
>>
>> //first create and register the retarget action IWorkbenchWindow =
>> getPage().getWorkbenchWindow();
>
>
> Ah. I was looking for the correct way to get a reference to the window.
>
>> addRetargetAction((RetargetAction)ActionFactory.COPY.create( iww));
>>
>> //then add it to a menu or toolbar override
>> ContributeToToolBar(IToolBarManager tbm) or wherever you want to add
>> the actions
>>
>> tbm.add(getAction(ActionFactory.COPY.getId()));
>>
>> or contributeToMenu
>>
>> Only way off hand I know how to use the editor contributor is by
>> telling the xml file to use it if you extend the point
>> org.eclipse.ui.editors add
>>
>> contributorClass = "MyContributor"
>>
>> Wish I could be more help with a more direct route, but I think you
>> got this part fixed anyway since you are actually seeing the buttons
>> and menuitems.
>
>
> Thanks. Now I get stuck on a strange ClassCastException in
> RetargetAction.propagateChange(). The result of getSource() is cast to
> (IActionBars), but according to the debugger, the source-field in the
> event is of type RetargetAction.
>
> setActionHandler(((IActionBars)event.getSource()).getGlobalA ctionHandler(getId()));
>
>
> Hm.
>
> /Jesper (puzzled)

which retarget action is it?, I mean which class exactly. Who fired the
event in question?

--
Respectfully,


CL Gilbert

"Verily, verily, I say unto you, He that entereth not by the door() into
the sheepfold{}, but climbeth up some other *way, the same is a thief
and a robber."

GnuPG Key Fingerprint:
82A6 8893 C2A1 F64E A9AD 19AE 55B2 4CD7 80D2 0A2D

For a free Java interface to Freechess.org see
http://www.rigidsoftware.com/Chess/chess.html
Re: implementing cut/copy/paste [message #161027 is a reply to message #160897] Wed, 08 December 2004 20:36 Go to previous message
Eclipse UserFriend
Originally posted by: jojo.virtutech.se

CL [dnoyeb] Gilbert wrote:

>
> which retarget action is it?, I mean which class exactly. Who fired the
> event in question?
>

I'm not sure, but I traced the problem to my context-menu provider,
which also tried to create retarget-actions. After removing them, things
started working.

Thanks for all help.

/Jesper
Previous Topic:Draw2D - Editable TextBox
Next Topic:RulerFigure#setDPU
Goto Forum:
  


Current Time: Fri Apr 26 17:45:08 GMT 2024

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

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

Back to the top