Copy/Cut and Paste Facility [message #192649] |
Mon, 22 August 2005 00:34  |
Eclipse User |
|
|
|
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 11:05   |
Eclipse User |
|
|
|
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 #194333 is a reply to message #192682] |
Thu, 01 September 2005 03:43  |
Eclipse User |
|
|
|
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;
}
}
|
|
|
Powered by
FUDForum. Page generated in 0.29771 seconds