Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » GEF » Copy/Cut and Paste Facility
Copy/Cut and Paste Facility [message #192649] Mon, 22 August 2005 04:34 Go to next message
Eclipse UserFriend
Originally posted by: gautamn.iitk.ac.in

Hi,
Now the last thing I want to add in my editor is cut/copy and paste
facility. I want to cut,copy the nodes that i am creating and paste it on
canvas. Is there any example available for this...or if anybody
implemented it then plz tell the procedure.
Regards,
Nitin Gautam.
Re: Copy/Cut and Paste Facility [message #192682 is a reply to message #192649] Mon, 22 August 2005 15:05 Go to previous messageGo to next message
Nick Allen is currently offline Nick AllenFriend
Messages: 23
Registered: July 2009
Junior Member
Nitin Gautam wrote:

> Now the last thing I want to add in my editor is cut/copy and paste
> facility. I want to cut,copy the nodes that i am creating and paste it
> on canvas. Is there any example available for this...or if anybody
> implemented it then plz tell the procedure.

Hi Nitin-

Here is what I have done to implement cut, copy, and paste. I believe I
have included everything. If anyone has a better method, please suggest.

0. Implement a CutCommand and CopyCommand that extend Command.
Personally, I did not create a PasteCommand, as I will describe below.
You will need to take advantage of the Clipboard in the following
manner: Clipboard.getDefault().setContents(...).

1. Implement a CutAction, CopyAction, and PasteAction which extend
SelectionAction.

a. For each, implement calculateEnabled() so that the action is
implemented only when it is available. For example, you should not be
able to copy, when there are no nodes selected in the diagram.

b. For each, implement run() so that it creates an appropriate Command.
After you have created the command, execute the command using execute().

c. For PasteAction, I simply used an existing CreateCommand, rather than
implementing a new PasteCommand.

d. For the CopyAction and CutAction, you will need to use
getSelectedObjects() which gives you access to what the user has
selected on the diagram.

e. To take advantage of the standard images provided for cut, copy, and
paste. Implement init() for each action:

protected void init() {
setId(ActionFactory.PASTE.getId());
setText("Paste");
setToolTipText("Paste");
ISharedImages sharedImages =
PlatformUI.getWorkbench().getSharedImages();
setImageDescriptor(sharedImages.getImageDescriptor(
ISharedImages.IMG_TOOL_PASTE));
setDisabledImageDescriptor(sharedImages.
getImageDescriptor(
ISharedImages.IMG_TOOL_PASTE_DISABLED));
setEnabled(false);
}

2. Register those actions in GraphicalEditor.createActions().

protected void createActions() {
super.createActions(); // adds undo, redo, delete,
//select-all, save, etc actions
ActionRegistry registry = getActionRegistry();

IAction copyAction = new
CopyAction((IWorkbenchPart)this);
registry.registerAction(copyAction);
getSelectionActions().add(copyAction.getId());

// do the same for each of your actions...

3. Do something like the following in ActionBarContributor.buildActions():

// support cut/copy/paste
IWorkbenchWindow iww = getPage().getWorkbenchWindow();
addRetargetAction((RetargetAction)ActionFactory.COPY.create( iww));
addRetargetAction((RetargetAction)ActionFactory.PASTE.create (iww));
addRetargetAction((RetargetAction)ActionFactory.CUT.create(i ww));

4. Add them to the toolbar in ActionBarContributor.contributeToToolbar:

toolBarManager.add(new Separator());
toolBarManager.add(getAction(ActionFactory.COPY.getId()));
toolBarManager.add(getAction(ActionFactory.PASTE.getId()));
toolBarManager.add(getAction(ActionFactory.CUT.getId()));

5. Allow the user to use the shortcut keys for your actions by
overriding ActionBarContributor.declareGlobalActionKeys():

addGlobalActionKey(ActionFactory.CUT.getId());
addGlobalActionKey(ActionFactory.COPY.getId());
addGlobalActionKey(ActionFactory.PASTE.getId());

6. Add them to the context menu by overriding your
ContextMenuProvider.buildContextMenu with something like the following:

menu.appendToGroup(GEFActionConstants.GROUP_EDIT,
getAction(ActionFactory.DELETE.getId()));
menu.appendToGroup(GEFActionConstants.GROUP_EDIT,
getAction(ActionFactory.COPY.getId()));
menu.appendToGroup(GEFActionConstants.GROUP_EDIT,
getAction(ActionFactory.CUT.getId()));
menu.appendToGroup(GEFActionConstants.GROUP_EDIT,
getAction(ActionFactory.PASTE.getId()));

Here is the helper method getAction used above:

private IAction getAction(String actionId) {
return actionRegistry.getAction(actionId);
}



--Nick


___________________________
Nick Allen
nwa (at) bitwiser (dot) org
Re: Copy/Cut and Paste Facility [message #192690 is a reply to message #192682] Mon, 22 August 2005 15:32 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: sunil_kamath.nohotspammail.com

"Nick Allen" <nwa@bitwiser.org> wrote in message
news:decpjg$4gs$1@news.eclipse.org...
> Nitin Gautam wrote:
>
>> Now the last thing I want to add in my editor is cut/copy and paste
>> facility. I want to cut,copy the nodes that i am creating and paste it on
>> canvas. Is there any example available for this...or if anybody
>> implemented it then plz tell the procedure.
>
> Hi Nitin-
>
> Here is what I have done to implement cut, copy, and paste. I believe I
> have included everything. If anyone has a better method, please suggest.
>
> 0. Implement a CutCommand and CopyCommand that extend Command. Personally,
> I did not create a PasteCommand, as I will describe below. You will need
> to take advantage of the Clipboard in the following manner:
> Clipboard.getDefault().setContents(...).
>

I would also suggest that the CopyCommand not be executed via the
CommandStack, because normally copy does not change the dirty state of the
editor.
---
Sunil
Re: Copy/Cut and Paste Facility [message #192742 is a reply to message #192682] Mon, 22 August 2005 17:05 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: none.us.ibm.com

Great post. One comment though, you can't have a PasteAction which gets
disabled. The reason is that there is no notification when the system
clipboard gets overwritten. So you need to fail lazily when someone attempts
to paste and the data is not available.

https://bugs.eclipse.org/bugs/show_bug.cgi?id=39369
Re: Copy/Cut and Paste Facility [message #192788 is a reply to message #192742] Mon, 22 August 2005 20:57 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: sunil_kamath.nohotspammail.com

"Randy Hudson" <none@us.ibm.com> wrote in message
news:ded0ll$f79$1@news.eclipse.org...
> Great post. One comment though, you can't have a PasteAction which gets
> disabled. The reason is that there is no notification when the system
> clipboard gets overwritten. So you need to fail lazily when someone
> attempts to paste and the data is not available.
>
> https://bugs.eclipse.org/bugs/show_bug.cgi?id=39369
The way I did it was to create my own Clipboard class (similar to the GEF
clipboard)
and added an event to it which fired whenever content was copied or pasted
into it.
Then I added my Paste action as a listener to the Clipboard.
---
Sunil
Re: Copy/Cut and Paste Facility [message #192796 is a reply to message #192788] Mon, 22 August 2005 21:36 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: none.us.ibm.com

The only issue there is that if someone launches Notepad and copies some
Text, technically your paste should no longer work. Even when cheating with
singleton clipboards, the behavior should still be as though the OS has one
global clipboard.

The logic example now demonstrates this in 3.1

> "Randy Hudson" <none@us.ibm.com> wrote in message
> news:ded0ll$f79$1@news.eclipse.org...
>> Great post. One comment though, you can't have a PasteAction which gets
>> disabled. The reason is that there is no notification when the system
>> clipboard gets overwritten. So you need to fail lazily when someone
>> attempts to paste and the data is not available.
>>
>> https://bugs.eclipse.org/bugs/show_bug.cgi?id=39369
> The way I did it was to create my own Clipboard class (similar to the GEF
> clipboard)
> and added an event to it which fired whenever content was copied or pasted
> into it.
> Then I added my Paste action as a listener to the Clipboard.
> ---
> Sunil
>
Re: Copy/Cut and Paste Facility [message #192832 is a reply to message #192796] Tue, 23 August 2005 01:57 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: sunil_kamath.nohotspammail.com

"Randy Hudson" <none@us.ibm.com> wrote in message
news:dedghc$4o8$1@news.eclipse.org...
> The only issue there is that if someone launches Notepad and copies some
> Text, technically your paste should no longer work. Even when cheating
> with singleton clipboards, the behavior should still be as though the OS
> has one global clipboard.
>
> The logic example now demonstrates this in 3.1
>
The scenario you described seems to work for me...
I copied a part in my GEF diagram. The paste icon activated.
I switched to notepad and copied some text.
I switched back to my diagram. When I click on it, the paste icon
deactivated.
---
Sunil
Re: Copy/Cut and Paste Facility [message #192952 is a reply to message #192832] Tue, 23 August 2005 20:20 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: none.us.ibm.com

At what point would it have deactivated itself?? Maybe there was a free
selection change event somewhere?

"Sunil Kamath" <sunil_kamath@nohotspammail.com> wrote in message
news:dedvps$jpu$1@news.eclipse.org...
>
> "Randy Hudson" <none@us.ibm.com> wrote in message
> news:dedghc$4o8$1@news.eclipse.org...
>> The only issue there is that if someone launches Notepad and copies some
>> Text, technically your paste should no longer work. Even when cheating
>> with singleton clipboards, the behavior should still be as though the OS
>> has one global clipboard.
>>
>> The logic example now demonstrates this in 3.1
>>
> The scenario you described seems to work for me...
> I copied a part in my GEF diagram. The paste icon activated.
> I switched to notepad and copied some text.
> I switched back to my diagram. When I click on it, the paste icon
> deactivated.
> ---
> Sunil
>
Re: Copy/Cut and Paste Facility [message #192968 is a reply to message #192952] Tue, 23 August 2005 21:16 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: sunil_kamath.nohotspammail.com

"Randy Hudson" <none@us.ibm.com> wrote in message
news:deg0f2$8q6$1@news.eclipse.org...
> At what point would it have deactivated itself?? Maybe there was a free
> selection change event somewhere?
>

It deactivated when I clicked on the editor canvas. Since my Paste action is
a
WorkbenchPart action, it suppose recalculates its enablement status when the
workbench selection changes.
Still, I see your point. If some process in the background changes the
clipboard contents, my action will not
detect it. I suppose the best solution would be to have a timer which polls
the clipboard.
---
Sunil

> "Sunil Kamath" <sunil_kamath@nohotspammail.com> wrote in message
> news:dedvps$jpu$1@news.eclipse.org...
>>
>> "Randy Hudson" <none@us.ibm.com> wrote in message
>> news:dedghc$4o8$1@news.eclipse.org...
>>> The only issue there is that if someone launches Notepad and copies some
>>> Text, technically your paste should no longer work. Even when cheating
>>> with singleton clipboards, the behavior should still be as though the OS
>>> has one global clipboard.
>>>
>>> The logic example now demonstrates this in 3.1
>>>
>> The scenario you described seems to work for me...
>> I copied a part in my GEF diagram. The paste icon activated.
>> I switched to notepad and copied some text.
>> I switched back to my diagram. When I click on it, the paste icon
>> deactivated.
>> ---
>> Sunil
>>
>
>
Re: Copy/Cut and Paste Facility [message #193034 is a reply to message #192968] Wed, 24 August 2005 04:15 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: none.unknown.com

Also, you wouldn't be able to copy from workbench and paste into another
workbench. It's a minor concern though.

"Sunil Kamath" <sunil_kamath@nohotspammail.com> wrote in message
news:deg3n4$cpq$1@news.eclipse.org...
>
> "Randy Hudson" <none@us.ibm.com> wrote in message
> news:deg0f2$8q6$1@news.eclipse.org...
> > At what point would it have deactivated itself?? Maybe there was a free
> > selection change event somewhere?
> >
>
> It deactivated when I clicked on the editor canvas. Since my Paste action
is
> a
> WorkbenchPart action, it suppose recalculates its enablement status when
the
> workbench selection changes.
> Still, I see your point. If some process in the background changes the
> clipboard contents, my action will not
> detect it. I suppose the best solution would be to have a timer which
polls
> the clipboard.
> ---
> Sunil
>
> > "Sunil Kamath" <sunil_kamath@nohotspammail.com> wrote in message
> > news:dedvps$jpu$1@news.eclipse.org...
> >>
> >> "Randy Hudson" <none@us.ibm.com> wrote in message
> >> news:dedghc$4o8$1@news.eclipse.org...
> >>> The only issue there is that if someone launches Notepad and copies
some
> >>> Text, technically your paste should no longer work. Even when cheating
> >>> with singleton clipboards, the behavior should still be as though the
OS
> >>> has one global clipboard.
> >>>
> >>> The logic example now demonstrates this in 3.1
> >>>
> >> The scenario you described seems to work for me...
> >> I copied a part in my GEF diagram. The paste icon activated.
> >> I switched to notepad and copied some text.
> >> I switched back to my diagram. When I click on it, the paste icon
> >> deactivated.
> >> ---
> >> Sunil
> >>
> >
> >
>
>
Re: Copy/Cut and Paste Facility [message #193180 is a reply to message #192968] Wed, 24 August 2005 16:56 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: none.us.ibm.com

> It deactivated when I clicked on the editor canvas. Since my Paste action
> is a
> WorkbenchPart action, it suppose recalculates its enablement status when
> the workbench selection changes.
> Still, I see your point. If some process in the background changes the
> clipboard contents, my action will not

Or, if the workbench stops sending this extra selection event in the future.

> detect it. I suppose the best solution would be to have a timer which
> polls the clipboard.
> ---
> Sunil
Re: Copy/Cut and Paste Facility [message #194333 is a reply to message #192682] Thu, 01 September 2005 07:43 Go to previous message
Eclipse UserFriend
Originally posted by: gautamn.iitk.ac.in

In my copy code the problem is that no model is selected although I am
selecting more than one figure...Kindly check my copyaction code:


public class CopyAction extends SelectionAction{

private List selectionList = null;
private Clipboard clipboard = null;

protected void init() {
setId(ActionFactory.COPY.getId());
setText("Copy");
setToolTipText("Copy");
ISharedImages sharedImages =
PlatformUI.getWorkbench().getSharedImages();
setImageDescriptor(sharedImages.getImageDescriptor(
ISharedImages.IMG_TOOL_COPY));
setDisabledImageDescriptor(sharedImages.
getImageDescriptor(
ISharedImages.IMG_TOOL_COPY_DISABLED));
setEnabled(false);
}

public void run() {
ModuleVTEditor.getClipboard().setContents(selectionList);
}

/**
* @param part
*/
public CopyAction(IWorkbenchPart part) {
super(part);
// TODO Auto-generated constructor stub
}

/* (non-Javadoc)
* @see org.eclipse.gef.ui.actions.WorkbenchPartAction#calculateEnab led()
*/
protected boolean calculateEnabled() {
// TODO Auto-generated method stub
selectionList = getSelectedObjects();
System.out.println(">>>>*****@@@"+selectionList.size());

return true;

}
}
Previous Topic:GEF & EMF representing AbsoluteBendpoint in EMF
Next Topic:zoom and pan
Goto Forum:
  


Current Time: Fri Jan 24 03:34:10 GMT 2025

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

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

Back to the top