GEF 命令模式介绍

命令模式的结构如图:

在我们流程设计器中,实现复制粘贴功能,就是采用了命令模式,以剪切操作为例,当在编辑器中选中活动,在菜单中选择剪切,就会产生一个剪切命令(CutCommand对象)。

剪切操作所用到的类如下:

名称

说明

org.eclipse.gef.commands.Command

命令的抽象类

org.eclipse.gef.commands.Command.CompoundCommand

结合多个命令的命令的类

com.example.workflow.commands.CutCommand

剪切命令的类

com.example.workflow.model.WorkflowProcess

流程类

com.example.workflow.action.CutAction

剪切动作类

 

Command抽象类,这个类中有个execute()方法。

CompoundCommand表示结合多个命令的命令的类,此类继承了Command类,这个类的commandList属性是java.util.ArrayList类型,用来存放多个继承Command的对象,这个类在执行execute()方法时,会把commandList中所用命令的execute()方法按ArrayList中顺序执行一边,在执行undo()方法时,会把commandList中所用命令的undo ()方法按ArrayList中反序执行一边。

CutCommand类表示剪切活动命令,该类继承了Command类,该类有parentchild两个属性,parentWorkflowProcess对象,表示流程对象,childAbstractActivity对象,表示要剪切的活动对象,在构造函数中,给这两个属性赋值,指明要从哪个流程模型中删除哪个活动,在execute()方法中调用parentremoveChild()方法,把活动从流程模型中删除。

CutAction类是调用命令的类,这个类继承了org.eclipse.gef.ui.actions.SelectionAction,在这个类的run()方法中新建了CutCommandCompoundCommand对象,并且把CutCommand对象加入到CompoundCommand对象的commandList中,这样如果剪切多个活动,执行了多个CutCommand命令,但这些命令都放在commandList中,这样在用户开来,就是执行一个命令。

对照命令模式的结构图,CompoundCommandCutCommand就相当于ConcreteCommandWorkflowProcess相当于Reciever,removeChild()方法就是Recieveraction()方法,而CutAction则同时扮演了ClientInvoker角色。

CutCommand类代码:

package com.example.workflow.commands;

 

import org.eclipse.gef.commands.Command;

 

import com.example.workflow.model.AbstractActivity;

import com.example.workflow.model.WorkflowProcess;

 

public class CutCommand extends Command{

      

       private WorkflowProcess parent; 

       private AbstractActivity child;

      

       public CutCommand(WorkflowProcess parent,AbstractActivity child){

              super();

              if (parent == null || child == null) {

                     throw new IllegalArgumentException();

              }

              this.parent = parent;

              this.child = child;

              setLabel("cut activity");

       }

      

       public boolean canExecute() {

              return parent != null && child != null;

       }    

      

       public void execute() {         

              redo();

       }

 

       public void redo() {      

              parent.removeChild(child);   

       }

 

       public void undo() {

              parent.addChild(child);

       }           

 

}

CutAction类代码:

package com.example.workflow.action;

 

import java.util.List;

 

import org.eclipse.gef.EditPart;

import org.eclipse.gef.commands.Command;

import org.eclipse.gef.commands.CompoundCommand;

import org.eclipse.gef.ui.actions.Clipboard;

import org.eclipse.gef.ui.actions.SelectionAction;

import org.eclipse.jface.resource.ImageDescriptor;

import org.eclipse.ui.IWorkbenchPart;

import org.eclipse.ui.actions.ActionFactory;

 

import com.example.workflow.Activator;

import com.example.workflow.commands.CutCommand;

import com.example.workflow.model.AbstractActivity;

import com.example.workflow.model.WorkflowProcess;

import com.example.workflow.parts.WorkflowProcessEditPart;

 

public class CutAction extends SelectionAction{

      

       private List selectionList = null;

       private Clipboard clipboard = null;

       private WorkflowProcess workflowProcess;      

 

       public CutAction(IWorkbenchPart part) {

              super(part);

              setId(ActionFactory.CUT.getId());

              setText("Cut");             

              setImageDescriptor(ImageDescriptor.createFromFile(Activator.class, "icons/cut.gif"));

       }

      

       public Clipboard getClipboard() {

              return clipboard;

       }

      

       public void setClipboard(Clipboard clipboard) {

              this.clipboard = clipboard;

       }

             

       public WorkflowProcess getWorkflowProcess() {

              return workflowProcess;

       }

 

       public void setWorkflowProcess(WorkflowProcess workflowProcess) {

              this.workflowProcess = workflowProcess;

       }

 

       protected boolean calculateEnabled() {

              selectionList = super.getSelectedObjects();

              if(selectionList.isEmpty() ||

                            (selectionList.size() == 1 && selectionList.get(0) instanceof WorkflowProcessEditPart)){

                     return false;

              }

              return true;

       }

      

       public void run() {

              getClipboard().setContents(selectionList);

              execute(createCutCommand());

       }    

      

       private Command createCutCommand(){

              CompoundCommand compoundCmd = new CompoundCommand();

             

              for(int i=0;i<selectionList.size();i++){

                     EditPart part = (EditPart)selectionList.get(i);              

                     CutCommand cmd = new CutCommand(getWorkflowProcess(),(AbstractActivity)part.getModel());

                     compoundCmd.add(cmd);

              }

             

              return compoundCmd;

       }    

 

}

 

WorkflowProcess类代码:

 

package com.example.workflow.model;

 

import java.util.ArrayList;

import java.util.List;

 

/**

 * 流程模型,可以包含多个活动和转移模型

 * @author Administrator

 *

 */

public class WorkflowProcess extends ModelElement{      

      

       private static final long serialVersionUID = -5478693636480758659L;

       /** Property ID to use when a child is added to this WorkflowProcess. */

       public static final String CHILD_ADDED_PROP = "WorkflowProcess.ChildAdded";

       /** Property ID to use when a child is removed from this WorkflowProcess. */

       public static final String CHILD_REMOVED_PROP = "WorkflowProcess.ChildRemoved";    

       private List activities = new ArrayList();

      

       /**

        * Add a Activity to this WorkflowProcess.

        * @param s a non-null Activity instance

        * @return true, if the Activity was added, false otherwise

        */

       public boolean addChild(AbstractActivity a) {

              if (a != null && activities.add(a)) {

                     firePropertyChange(CHILD_ADDED_PROP, null, a);

                     return true;

              }

              return false;

       }

 

       /** Return a List of Activities in this WorkflowProcess. The returned List should not be modified. */

       public List getChildren() {

              return activities;

       }

      

       /**

        * Remove a Activity from this WorkflowProcess.

        * @param s a non-null Activity instance;

        * @return true, if the Activity was removed, false otherwise

        */

       public boolean removeChild(AbstractActivity a) {

              if (a != null && activities.remove(a)) {

                     firePropertyChange(CHILD_REMOVED_PROP, null, a);

                     return true;

              }

              return false;

       }

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值