复制:
Command:
public class CopyNodeCommand extends Command { private ArrayList<Node> list = new ArrayList<Node>(); public boolean addElement(Node node) { if (!list.contains(node)) { return list.add(node); } return false; } @Override public boolean canExecute() { if (list == null || list.isEmpty()) return false; Iterator<Node> it = list.iterator(); while (it.hasNext()) { if (!isCopyableNode(it.next())) return false; } return true; } @Override public void execute() { if (canExecute()) Clipboard.getDefault().setContents(list); } @Override public boolean canUndo() { return false; } public boolean isCopyableNode(Node node) { if (node instanceof Service || node instanceof Employe) return true; return false; } }
Action
public class CopyNodeCommand extends Command { private ArrayList<Node> list = new ArrayList<Node>(); public boolean addElement(Node node) { if (!list.contains(node)) { return list.add(node); } return false; } @Override public boolean canExecute() { if (list == null || list.isEmpty()) return false; Iterator<Node> it = list.iterator(); while (it.hasNext()) { if (!isCopyableNode(it.next())) return false; } return true; } @Override public void execute() { if (canExecute()) Clipboard.getDefault().setContents(list); } @Override public boolean canUndo() { return false; } public boolean isCopyableNode(Node node) { if (node instanceof Service || node instanceof Employe) return true; return false; } }粘贴
Command
public class PasteNodeCommand extends Command { private HashMap<Node, Node> list = new HashMap<Node, Node>(); @Override public boolean canExecute() { ArrayList<Node> bList = (ArrayList<Node>) Clipboard.getDefault().getContents(); if (bList == null || bList.isEmpty()) return false; Iterator<Node> it = bList.iterator(); while (it.hasNext()) { Node node = (Node)it.next(); if (isPastableNode(node)) { list.put(node, null); } } return true; } @Override public void execute() { if (!canExecute()) return ; Iterator<Node> it = list.keySet().iterator(); while (it.hasNext()) { Node node = (Node)it.next(); try { if (node instanceof Service) { Service srv = (Service) node; Service clone = (Service) srv.clone(); list.put(node, clone); } else if (node instanceof Employe) { Employe emp = (Employe) node; Employe clone = (Employe) emp.clone(); list.put(node, clone); } } catch (CloneNotSupportedException e) { e.printStackTrace(); } } redo(); } @Override public void redo() { Iterator<Node> it = list.values().iterator(); while (it.hasNext()) { Node node = it.next(); if (isPastableNode(node)) { node.getParent().addChild(node); } } } @Override public boolean canUndo() { return !(list.isEmpty()); } @Override public void undo() { Iterator<Node> it = list.values().iterator(); while (it.hasNext()) { Node node = it.next(); if (isPastableNode(node)) { node.getParent().removeChild(node); } } } public boolean isPastableNode(Node node) { if (node instanceof Service || node instanceof Employe) return true; return false; } }
Action
public class PasteNodeAction extends SelectionAction { public PasteNodeAction(IWorkbenchPart part) { super(part); // force calculateEnabled() to be called in every context setLazyEnablementCalculation(true); } protected void init() { super.init(); ISharedImages sharedImages = PlatformUI.getWorkbench().getSharedImages(); setText("Paste"); setId(ActionFactory.PASTE.getId()); setHoverImageDescriptor(sharedImages.getImageDescriptor(ISharedImages.IMG_TOOL_PASTE)); setImageDescriptor(sharedImages.getImageDescriptor(ISharedImages.IMG_TOOL_PASTE)); setDisabledImageDescriptor(sharedImages.getImageDescriptor(ISharedImages.IMG_TOOL_PASTE_DISABLED)); setEnabled(false); } private Command createPasteCommand() { return new PasteNodeCommand(); } @Override protected boolean calculateEnabled() { Command command = createPasteCommand(); return command != null && command.canExecute(); } @Override public void run() { Command command = createPasteCommand(); if (command != null && command.canExecute()) execute(command); } }
Model
public class Employe extends Node { @Override public Object clone() throws CloneNotSupportedException { Employe emp = new Employe(); emp.setName(this.getName()); emp.setParent(this.getParent()); emp.setPrenom(this.prenom); emp.setLayout(new Rectangle(getLayout().x + 10, getLayout().y + 10, getLayout().width, getLayout().height)); return emp; } } public class Service extends Node { @Override public Object clone() throws CloneNotSupportedException { Service srv = new Service(); srv.setColor(this.color); srv.setEtage(this.etage); srv.setName(this.getName()); srv.setParent(this.getParent()); srv.setLayout(new Rectangle( getLayout().x + 10, getLayout().y + 10, getLayout().width, getLayout().height)); Iterator<Node> it = this.getChildrenArray().iterator(); while (it.hasNext()) { Node node = it.next(); if (node instanceof Employe) { Employe child = (Employe)node; Node clone = (Node)child.clone(); srv.addChild(clone); clone.setLayout(child.getLayout()); } } return srv; } }
GraphicalEditorActionBarContributor
public class MyGraphicalEditorActionBarContributor extends ActionBarContributor { protected void buildActions() { IWorkbenchWindow iww = getPage().getWorkbenchWindow(); // ... addRetargetAction((RetargetAction)ActionFactory.COPY.create(iww)); addRetargetAction((RetargetAction)ActionFactory.PASTE.create(iww)); // ... } // ... public void contributeToToolBar(IToolBarManager toolBarManager) { // ... toolBarManager.add(getAction(ActionFactory.COPY.getId())); toolBarManager.add(getAction(ActionFactory.PASTE.getId())); // ... } }
GraphicalEditor
public class MyGraphicalEditor extends GraphicalEditorWithPalette { // ... protected class OutlinePage extends ContentOutlinePage { public void createControl(Composite parent) { // ... IActionBars bars = getSite().getActionBars(); ActionRegistry ar = getActionRegistry(); // ... bars.setGlobalActionHandler(ActionFactory.COPY.getId(), ar.getAction(ActionFactory.COPY.getId())); bars.setGlobalActionHandler(ActionFactory.PASTE.getId(), ar.getAction(ActionFactory.PASTE.getId())); // ... } // ... } // ... public void createActions() { super.createActions(); ActionRegistry registry = getActionRegistry(); // ... action = new CopyNodeAction(this); registry.registerAction(action); getSelectionActions().add(action.getId()); action = new PasteNodeAction(this); registry.registerAction(action); getSelectionActions().add(action.getId()); } }
ApplicationActionBarAdvisor
public class ApplicationActionBarAdvisor extends ActionBarAdvisor { public ApplicationActionBarAdvisor(IActionBarConfigurer configurer) { super(configurer); } protected IWorkbenchAction makeAction(IWorkbenchWindow window, ActionFactory af) { IWorkbenchAction action = af.create(window); register(action); return action; } protected void makeActions(IWorkbenchWindow window) { makeAction(window, ActionFactory.UNDO); makeAction(window, ActionFactory.REDO); makeAction(window, ActionFactory.COPY); makeAction(window, ActionFactory.PASTE); } protected void fillMenuBar(IMenuManager menuBar) { } }
[Eclipse插件开发-003] -复制剪切粘贴拷贝
于 2021-03-29 13:16:06 首次发布

786

被折叠的 条评论
为什么被折叠?



