命令模式的结构如图:

在我们流程设计器中,实现复制粘贴功能,就是采用了命令模式,以剪切操作为例,当在编辑器中选中活动,在菜单中选择“剪切”,就会产生一个剪切命令(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类,该类有parent和child两个属性,parent是WorkflowProcess对象,表示流程对象,child是AbstractActivity对象,表示要剪切的活动对象,在构造函数中,给这两个属性赋值,指明要从哪个流程模型中删除哪个活动,在execute()方法中调用parent的removeChild()方法,把活动从流程模型中删除。

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

对照命令模式的结构图,CompoundCommand和CutCommand就相当于ConcreteCommand,WorkflowProcess相当于Reciever,而removeChild()方法就是Reciever的action()方法,而CutAction则同时扮演了Client和Invoker角色。

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;

}

}

转载于:https://my.oschina.net/zhenghuazhi/blog/198856

GEF 命令模式介绍相关推荐

  1. iOS设计模式 - 命令模式

    前言: 命令对象封装了如何对目标执行指令的信息,因此客户端或调用者不必了解目标的任何细节,却仍可以对他执行任何已有的操作.通过把请求封装成对象,客 户端可 以把它参数化并置入队列或日志中,也能够支持可 ...

  2. 设计模式学习笔记(十七)——Command命令模式

    设计模式学习笔记(十七)--Command命令模式 Command命令模式介绍: Command命令模式是一种对象行为型模式,它主要解决的问题是:在软件构建过程中,"行为请求者"与 ...

  3. python 命令模式_python 设计模式之命令模式

    命令模式介绍: 在面向对象编程中,命令模式是概括所有方法信息的设计模式. 此模式对象包涵方法名,及其相关参数值. 命令模式是一个分类的观察者设计模式,在命令模式下,对象被概括为一个命令表单,此表单包涵 ...

  4. 图解Java设计模式学习笔记——行为型模式(模版方法模式、命令模式、访问者模式、迭代器模式、观察者模式、中介者模式、备忘录模式、解释器模式、状态模式、策略模式、职责链模式)

    一.模板方法模式(模板模式)--钩子方法 1.需求-豆浆制作问题 编写制作豆浆的程序,说明如下: 制作豆浆的流程选材-->添加配料-->浸泡-->放到豆浆机打碎. 通过添加不同的配料 ...

  5. 初学 Java 设计模式(十五):实战命令模式 「扫码点餐」

    一.命令模式介绍 1. 解决的问题 主要解决在系统中,行为请求者和行为实现者紧耦合的问题. 2. 定义 命令模式是一种行为设计模式,它可将请求转换为一个包含与请求相关的所有信息的独立对象.这个转换会根 ...

  6. 设计模式之命令模式详解(附应用举例实现)

    文章目录 1 命令模式介绍 2 命令模式详解 2.1 命令模式结构 2.2 命令模式实现 2.3 命令模式应用举例 3 实现命令队列 1 命令模式介绍 在现实生活中人们通过使用开关来控制一些电器的打开 ...

  7. Cisco Packet Tracer配置操作的三种命令模式

    Cisco Packet Tracer三种基本命令模式 这里我们需要讲一下三种命令模式,今后绝大部分操作都通过命令实现,所以需要熟悉命令模式 在路由器命令配置界面中演示: 三种命令模式介绍 用户模式: ...

  8. uboot流程——命令行模式以及命令处理介绍

    [uboot] (第六章)uboot流程--命令行模式以及命令处理介绍 2016年11月14日 20:39:26 阅读数:4323 以下例子都以project X项目tiny210(s5pv210平台 ...

  9. python命令行模式和交互模式区别_对命令行模式与python交互模式介绍

    命令行模式与python交互模式 1.在命令行模式下,可以执行 python 进入 Python 交互式环境,也可以执 行 python hello.py 运行一个.py 文件. 2.在 Python ...

最新文章

  1. Django-C002-深入模型,到底有多深
  2. 保护物联网的数据隐私和在线安全的7种方式
  3. 【错误记录】Flutter 混合开发报错 ( java.nio.file.FileSystemException: xxx/R.jar: 另一个程序正在使用此文件,进程无法访问。 )
  4. python笔记: 生成器
  5. 8.5-Day1T1--Asm.Def 谈笑风生
  6. 让VS2008对JQuery语法的智能感知更完美一点(转载)
  7. 耿建超英语语法---连词
  8. api接口文档生成工具apipost
  9. 拳头产品|海泰虎讯,新一代安全即时通讯系统
  10. WPF 精修篇 滑条
  11. python替换excel指定内容_Python脚本操作Excel实现批量替换功能
  12. 为了结婚领证,我做了个「一键结婚」插件
  13. 只有一行VNC server running on ’::1:5900' 没有其他输出
  14. Python核心编程16 ----- 文件的打开(读取),修改,关闭,二进制
  15. 还在为这部现象级大片热血沸腾?不如来游戏里亲身感受
  16. 树莓派瞎折腾[1]-实现简单的命令行音乐播放器
  17. 孔庆东看金庸小说的奇情怪恋
  18. android4.3 打包成image,DCloud 离线打包之Android Studio
  19. 好产品不断更新迭代,如何评估App升级效果
  20. FinTech生态丨神州信息、北京金融科技研究院联手,成立国内首个开放银行实验室

热门文章

  1. [动态规划] 洛谷P1064 金明的预算方案
  2. 胶囊网络用于推荐系统问题(MIND,CARP)
  3. origin画图_Origin作图过程中如何让图看起来更生动
  4. MTK处理器手机 解锁Bootloader 教程
  5. 如何在5个月内做出月入3万的业余项目
  6. html5 fa图标库,axure官方制作FontAwesome图标元件库V5.5.0版
  7. Cesium 1.87+实现建筑泛光效果
  8. 【笔记本电脑连接真无线 jbl flash x耳机】pin 是 000000
  9. 【Android】使用后端云Bmob实现登录、注册
  10. echarts 省市区联动地图