nodedeletecommand.java
来自「对eclipse gef进行封装,可以生成图形化编辑器」· Java 代码 · 共 235 行
JAVA
235 行
/*******************************************************************************
* $Header: /cvsroot/EOS6/work_dir/niegy/com.primeton.studio.gef.ui/src/com/primeton/studio/gef/ui/commands/NodeDeleteCommand.java,v 1.5 2006/12/30 02:45:04 niegy Exp $
* $Revision: 1.5 $
* $Date: 2006/12/30 02:45:04 $
*
*==============================================================================
*
* Copyright (c) 2001-2006 Primeton Technologies, Ltd.
* All rights reserved.
*
* Created on 2006-9-28
*******************************************************************************/
package com.primeton.studio.gef.ui.commands;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.eclipse.gef.commands.Command;
import org.eclipse.ui.PlatformUI;
import com.primeton.studio.gef.core.Node;
import com.primeton.studio.gef.core.Connection;
import com.primeton.studio.gef.core.Diagram;
import com.primeton.studio.gef.core.NodeElement;
import com.primeton.studio.gef.core.NodeElementLabel;
/**
* TODO此处填写 class 信息
*
* @author niegy (mailto:niegy@primeton.com)
*/
/*
* 修改历史
* $Log: NodeDeleteCommand.java,v $
* Revision 1.5 2006/12/30 02:45:04 niegy
* 重构代码
*
* Revision 1.4 2006/12/12 08:26:39 niegy
* 重构代码,实现一般图元的编辑框
*
* Revision 1.3 2006/12/05 05:18:59 niegy
* 修改模型,增加连线的扩展点
*
* Revision 1.2 2006/11/18 12:13:53 niegy
* 增加容器模型
*
* Revision 1.1 2006/11/17 03:15:13 niegy
* create
*
*/
public class NodeDeleteCommand extends Command {
/** Shape to remove. */
private final NodeElement child;
/** ShapeDiagram to remove from. */
private final Node parent;
/** Holds a copy of the outgoing connections of child. */
private List sourceConnections = new ArrayList();
/** Holds a copy of the incoming connections of child. */
private List targetConnections = new ArrayList();
/** True, if child was removed from its parent. */
private boolean wasRemoved;
/**
* Create a command that will remove the shape from its parent.
* @param parent the ShapesDiagram containing the child
* @param child the Shape to remove
* @throws IllegalArgumentException if any parameter is null
*/
public NodeDeleteCommand(Node parent, NodeElement child) {
if (parent == null || child == null) {
throw new IllegalArgumentException();
}
setLabel("node deletion");
this.parent = parent;
this.child = child;
}
/**
* Reconnects a List of Connections with their previous endpoints.
* @param connections a non-null List of connections
*/
private void addConnections(List connections) {
for (Iterator iter = connections.iterator(); iter.hasNext();) {
EdgeEntry entry = (EdgeEntry) iter.next();
Connection conn = entry.getLine();
conn.getSourceNode().getSourceConnections().add(conn);
conn.getTargetNode().getTargetConnections().add(conn);
}
}
/* (non-Javadoc)
* @see org.eclipse.gef.commands.Command#canUndo()
*/
public boolean canUndo() {
return wasRemoved;
}
/* (non-Javadoc)
* @see org.eclipse.gef.commands.Command#canExecute()
*/
public boolean canExecute() {
// TODO Auto-generated method stub
if(child instanceof NodeElementLabel)
return false;
else
return super.canExecute();
}
/* (non-Javadoc)
* @see org.eclipse.gef.commands.Command#execute()
*/
public void execute() {
// store a copy of incoming & outgoing connections before proceeding
// saveConnect(child.getSourceConnections(),sourceConnections);
// saveConnect(child.getTargetConnections(),targetConnections);
saveConnect(child);
redo();
}
private void saveConnect(NodeElement nodeElement){
List sourceList = nodeElement.getSourceConnections();
for (Iterator iter = sourceList.iterator(); iter.hasNext();) {
Connection element = (Connection) iter.next();
EdgeEntry entry = new EdgeEntry(element);
sourceConnections.add(entry);
}
List targetList = nodeElement.getTargetConnections();
for (Iterator iter = targetList.iterator(); iter.hasNext();) {
Connection element = (Connection) iter.next();
EdgeEntry entry = new EdgeEntry(element);
targetConnections.add(entry);
}
for (Iterator iter = nodeElement.getNodes().iterator(); iter.hasNext();) {
NodeElement childNode = (NodeElement) iter.next();
saveConnect(childNode);
}
}
/* (non-Javadoc)
* @see org.eclipse.gef.commands.Command#redo()
*/
public void redo() {
// remove the child and disconnect its connections
wasRemoved = parent.getNodes().contains(child);
if (wasRemoved) {
// removeConnections(child.getSourceConnections());
// removeConnections(child.getTargetConnections());
removeConnections(child);
delNodelabel();
parent.getNodes().remove(child);
}
}
private void delNodelabel(){
if(child.getNodeLabel() != null){
parent.getNodes().remove(child.getNodeLabel());
}
}
private void addNodelabel(){
if(child.getNodeLabel() != null){
parent.getNodes().add(child.getNodeLabel());
}
}
/**
* Disconnects a List of Connections from their endpoints.
* @param connections a non-null List of connections
*/
private void removeConnections(NodeElement nodeElement) {
List sourceConnections = nodeElement.getSourceConnections();
for (int i = sourceConnections.size()-1; i >= 0; i--) {
Connection conn = (Connection) sourceConnections.get(i);
conn.getSourceNode().getSourceConnections().remove(conn);
conn.getTargetNode().getTargetConnections().remove(conn);
}
List targetConnections = nodeElement.getTargetConnections();
for (int i = targetConnections.size()-1; i >= 0; i--) {
Connection conn = (Connection) targetConnections.get(i);
conn.getSourceNode().getSourceConnections().remove(conn);
conn.getTargetNode().getTargetConnections().remove(conn);
}
for (Iterator iter = nodeElement.getNodes().iterator(); iter.hasNext();) {
NodeElement childNode = (NodeElement) iter.next();
removeConnections(childNode);
}
}
/* (non-Javadoc)
* @see org.eclipse.gef.commands.Command#undo()
*/
public void undo() {
// add the child and reconnect its connections
try{
addConnections(sourceConnections);
addConnections(targetConnections);
if (!parent.getNodes().add(child)) {
removeConnections(child);
}else{
addNodelabel();
}
}
catch(Exception e){
e.printStackTrace();
}
}
private class EdgeEntry {
private NodeElement source;
private NodeElement target;
private Connection edge;
public EdgeEntry( Connection edge ) {
target = edge.getTargetNode();
source = edge.getSourceNode();
this.edge = edge;
}
public Connection getLine(){
return edge;
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?