[Eclipse插件开发-003] -复制剪切粘贴拷贝

复制:


  • 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) {
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值