流程设计器开发(复制粘贴部分)
要在编辑器中支持复制,粘贴功能,首先,要在编辑器中选择活动节点,Eclipse工作台的菜单中复制,粘贴按钮处于激活状态,要实现这功能,我们还必须做些配置,因为编辑器没有单独的菜单栏,所有编辑器共享一个菜单栏,前面我们在定义编辑器扩展点时,有一个属性contributorClass没有指定值,它的值应该是一个实现IEditorActionBarContributor接口的类的全名,只有指定这个属性,菜单栏中的菜单项才能够对我们在编辑器中所做的操作进行相应;
首先,我们增加属性contributorClass对应的类:
package com.example.workflow.actions;
import org.eclipse.gef.ui.actions.ActionBarContributor;
import org.eclipse.ui.actions.ActionFactory;
public class WorkflowActionContributor extends ActionBarContributor {
@Override
protected void buildActions() {
}
@Override
protected void declareGlobalActionKeys() {
addGlobalActionKey(ActionFactory.COPY.getId());
}
}
在declareGlobalActionKeys增加对复制命令的申明,同时把属性contributorClass指定为这个类,包含全路径。再定义复制动作CopyAction,代码如下:
package com.example.workflow.actions;
import org.eclipse.gef.ui.actions.SelectionAction;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.actions.ActionFactory;
public class CopyAction extends SelectionAction{
public CopyAction(IWorkbenchPart part) {
super(part);
setId(ActionFactory.COPY.getId());
}
@Override
protected boolean calculateEnabled() {
return true;
}
}
注意构造函数中一定要把这个action的id设置为ActionFactory.COPY.getId(),和前面统一起来;
到这儿还没有结束,还必须在编辑器类中注册这个action,覆盖编辑器父类中的createActions()方法:
protected void createActions() {
super.createActions();
CopyAction copyAction = new CopyAction((IWorkbenchPart) this);
getActionRegistry().registerAction(copyAction);
getSelectionActions().add(copyAction.getId());
}
但是当我们在复制按钮可用情况,点击之后,我们希望把我们选择的对象,拷贝到剪切板上,这个在其他软件中已经成功实现,但要是在我们开发的流程设计器中,要实现这个功能,我们还必须自己写代码来实现,必须重写CopyAction父类中的run方法,把我们选择的对象放到剪切板上,代码如下:
public void run() {
Clipboard.getDefault().setContents(super.getSelectedObjects());
}
这样当我们点复制按钮之后,流程设计器就把我们选择的内容放到剪切板上,下一步要做的就是粘贴了。
要使粘贴按钮可用,所做的修改和复制类似,在类WorkflowActionContributor 的方法declareGlobalActionKeys中增加addGlobalActionKey(ActionFactory.PASTE.getId())
增加PasteAction类,代码如下:
package com.example.workflow.actions;
import org.eclipse.gef.ui.actions.Clipboard;
import org.eclipse.gef.ui.actions.SelectionAction;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.actions.ActionFactory;
public class PasteAction extends SelectionAction{
public PasteAction(IWorkbenchPart part) {
super(part);
setId(ActionFactory.PASTE.getId());
}
@Override
protected boolean calculateEnabled() {
if(Clipboard.getDefault().getContents()!=null){
return true;
}else{
return false;
}
}
}
这里对Paste菜单的可用性作了判断,只有当剪切板中内容不为空时,粘贴按钮才可用,否则为不可用状
态。
另外还要在编辑器中对这个action进行申明,
PasteAction pasteAction = new PasteAction((IWorkbenchPart) this);
getActionRegistry().registerAction(pasteAction);
getSelectionActions().add(pasteAction.getId());
到这里,当我们在编辑器中选择活动,选择复制之后,粘贴按钮可用,但点粘贴按钮具体做什么,我们还没有定义,要实现这个做什么功能,还必须覆盖PasteAction父类中的run方法:
public void run() {
List list = (List)Clipboard.getDefault().getContents();
for(int i=0;i
public class CopyNodeCommand extends Command {
private List copyList ;
public CopyNodeCommand(List selectedObjects) {
if (selectedObjects == null || selectedObjects.isEmpty()) {
return;
}
copyList = new ArrayList();
for (Object object : selectedObjects) {
EditPart editPart = (EditPart) object;
CopyModel node = (CopyModel) editPart.getModel();
addElement(node);
}
}
private boolean addElement(CopyModel node) {
if (!copyList.contains(node)) {
return copyList.add(node);
}
return false;
}
/*
* (non-Javadoc)
*
* @see org.eclipse.gef.commands.Command#execute()
*/
@Override
public void execute() {
Clipboard.getDefault().setContents(copyList);
}
@Override
public boolean canUndo() {
return false;
}
}
public class CutNodeCommand extends Command {
private List copyList ;
private CutModel ParentNamedModel;
public CutNodeCommand(List selectedObjects,NamedModel parent) {
if (selectedObjects == null || selectedObjects.isEmpty()) {
return;
}
ParentCutModel = parent;
copyList = new ArrayList();
for (Object object : selectedObjects) {
EditPart editPart = (EditPart) object;
CutModel node = (CutModel ) editPart.getModel();
addElement(node);
}
}
private boolean addElement(CutModel node) {
if (!copyList.contains(node)) {
return copyList.add(node);
}
return false;
}
/*
* (non-Javadoc)
*
* @see org.eclipse.gef.commands.Command#execute()
*/
@Override
public void execute() {
Clipboard.getDefault().setContents(copyList);
for(CutModel node:copyList){
ParentCutModel .getSubElements().remove(node);
}
}
@Override
public boolean canUndo() {
return false;
}
}
public class PasteNodeCommand extends Command {
private List pasteList;
private PasteModel ParentPasteModel ;
@SuppressWarnings("unchecked")
public PasteNodeCommand(PasteModel Parent) {
ParentPasteModel = Parent;
pasteList = (ArrayList<PasteModel >) Clipboard.getDefault().getContents();
}
@Override
public boolean canExecute() {
if (pasteList == null || pasteList.isEmpty()) {
return false;
}
return true;
}
@Override
public void execute() {
if (!canExecute()) {
return;
}
for (PasteModel node : pasteList) {
PasteModel newNode = EcoreUtil.copy(node);
ParentPasteModel .getSubElements().add(newNode);
}
}
@Override
public boolean canUndo() {
return !(pasteList == null || pasteList.isEmpty());
}
@Override
public void redo() {
}
@Override
public void undo() {
}
}