1 8 package com.nightlabs.editor2d.actions; 9 10 import org.eclipse.draw2d.geometry.Point; 11 import org.eclipse.gef.EditPart; 12 import org.eclipse.gef.commands.Command; 13 import org.eclipse.gef.requests.CreateRequest; 14 import org.eclipse.gef.requests.CreationFactory; 15 import org.eclipse.gef.ui.actions.Clipboard; 16 import org.eclipse.gef.ui.actions.SelectionAction; 17 import org.eclipse.ui.IWorkbenchPart; 18 import org.eclipse.ui.actions.ActionFactory; 19 20 import com.nightlabs.editor2d.EditorPlugin; 21 22 28 29 30 public abstract class PasteTemplateAction 31 extends SelectionAction 32 { 33 34 38 public PasteTemplateAction(IWorkbenchPart editor) { 39 super(editor); 40 } 41 42 47 protected boolean calculateEnabled() { 48 Command command = null; 49 if (getClipboardContents() != null) 50 command = createPasteCommand(); 51 return command != null && command.canExecute(); 52 } 53 54 59 protected Command createPasteCommand() { 60 if (getSelectedObjects() == null || getSelectedObjects().isEmpty()) 61 return null; 62 CreateRequest request = new CreateRequest(); 63 request.setFactory(getFactory(getClipboardContents())); 64 request.setLocation(getPasteLocation()); 65 Object obj = getSelectedObjects().get(0); 66 if (obj instanceof EditPart) 67 return ((EditPart)obj).getCommand(request); 68 return null; 69 } 70 71 75 protected Object getClipboardContents() { 76 return Clipboard.getDefault().getContents(); 77 } 78 79 85 protected abstract CreationFactory getFactory(Object template); 86 87 protected abstract Point getPasteLocation(); 88 89 92 protected void init() { 93 setId(ActionFactory.PASTE.getId()); 94 setText(EditorPlugin.getResourceString("action_paste_label")); 95 } 96 97 100 public void run() { 101 execute(createPasteCommand()); 102 } 103 } 104 | Popular Tags |