GEF 命令模式介绍
2014-02-12 00:00
260 查看
命令模式的结构如图:
在我们流程设计器中,实现复制粘贴功能,就是采用了命令模式,以剪切操作为例,当在编辑器中选中活动,在菜单中选择“剪切”,就会产生一个剪切命令(CutCommand对象)。
剪切操作所用到的类如下:
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;
}
}
在我们流程设计器中,实现复制粘贴功能,就是采用了命令模式,以剪切操作为例,当在编辑器中选中活动,在菜单中选择“剪切”,就会产生一个剪切命令(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 | 剪切动作类 |
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;
}
}
相关文章推荐
- 命令模式(Command Pattern)(一):命令模式介绍
- Vim模式介绍与命令合集
- vi编辑器工作模式及常用操作命令介绍
- GEF框架中的设计模型(命令模式)
- WPF自学入门(十一)WPF MVVM模式Command命令 WPF自学入门(十)WPF MVVM简单介绍
- 设计模式介绍:命令模式(command)
- 设计模式介绍之三:命令模式(command)
- Vi命令摘要——模式介绍
- Redis中multi,exec,watch,发布与订阅模式命令介绍笔记(9)
- 思科路由器各种命令模式介绍
- 设计模式系列六 命令模式介绍
- VMware Easy Install模式详细介绍:如何启用和禁用Easy Install模式(图文)-上【转载】
- webpack 介绍 & 安装 & 常用命令
- Slickflow.NET 开源工作流引擎基础介绍(七) -- 并行分支多实例模式实现
- Memcached 的介绍与命令使用
- Java开发中的23种设计模式详解----命令模式(Command)
- 稳扎稳打Silverlight(52) - 4.0绑定之 ICommand 命令和 MVVM 模式
- LVS DR模式搭建、keepalived+LVS搭建介绍
- 稳扎稳打Silverlight(52) - 4.0绑定之 ICommand 命令和 MVVM 模式
- 稳扎稳打Silverlight(52) - 4.0绑定之 ICommand 命令和 MVVM 模式