📄 connectionreconnectcommand.java
字号:
/*******************************************************************************
* $Header: /cvsroot/EOS6/work_dir/niegy/com.primeton.studio.gef.ui/src/com/primeton/studio/gef/ui/commands/ConnectionReconnectCommand.java,v 1.2 2006/12/30 02:45:04 niegy Exp $
* $Revision: 1.2 $
* $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.Iterator;
import java.util.List;
import org.eclipse.gef.commands.Command;
import com.primeton.studio.gef.core.Connection;
import com.primeton.studio.gef.core.NodeElement;
/**
* TODO此处填写 class 信息
*
* @author niegy (mailto:niegy@primeton.com)
*/
/*
* 修改历史
* $Log: ConnectionReconnectCommand.java,v $
* Revision 1.2 2006/12/30 02:45:04 niegy
* 重构代码
*
* Revision 1.1 2006/11/17 03:15:13 niegy
* create
*
*/
public class ConnectionReconnectCommand extends Command {
/** The connection instance to reconnect. */
private Connection connection;
/** The new source endpoint. */
private NodeElement newSource;
/** The new target endpoint. */
private NodeElement newTarget;
/** The original source endpoint. */
private final NodeElement oldSource;
/** The original target endpoint. */
private final NodeElement oldTarget;
/**
* Instantiate a command that can reconnect a Connection instance to a different source
* or target endpoint.
* @param conn the connection instance to reconnect (non-null)
* @throws IllegalArgumentException if conn is null
*/
public ConnectionReconnectCommand(Connection conn) {
if (conn == null) {
throw new IllegalArgumentException();
}
this.connection = conn;
this.oldSource = conn.getSourceNode();
this.oldTarget = conn.getTargetNode();
}
private int getConnectIndex(List list){
for (int i = 0;i<list.size(); i++) {
Connection element = (Connection) list.get(i);
if(element.equals(connection))
return i;
}
return -1;
}
/* (non-Javadoc)
* @see org.eclipse.gef.commands.Command#canExecute()
*/
public boolean canExecute() {
boolean flag =true;
if (newSource != null) {
if(!checkSourceReconnection())
flag = false;
}
else if (newTarget != null) {
if(! checkTargetReconnection())
flag =false;
}
return flag;
}
/**
* Return true, if reconnecting the connection-instance to newSource is allowed.
*/
private boolean checkSourceReconnection() {
// connection endpoints must be different Shapes
if (newSource.equals(oldTarget)) {
return false;
}
// return false, if the connection exists already
for (Iterator iter = newSource.getSourceConnections().iterator(); iter.hasNext();) {
Connection conn = (Connection) iter.next();
// return false if a newSource -> oldTarget connection exists already
// and it is a different instance than the connection-field
if (conn.getTargetNode().equals(oldTarget) && !conn.equals(connection)) {
return false;
}
}
return true;
}
/**
* Return true, if reconnecting the connection-instance to newTarget is allowed.
*/
private boolean checkTargetReconnection() {
// connection endpoints must be different Shapes
if (newTarget.equals(oldSource)) {
return false;
}
// return false, if the connection exists already
for (Iterator iter = newTarget.getTargetConnections().iterator(); iter.hasNext();) {
Connection conn = (Connection) iter.next();
// return false if a oldSource -> newTarget connection exists already
// and it is a differenct instance that the connection-field
if (conn.getSourceNode().equals(oldSource) && !conn.equals(connection)) {
return false;
}
}
return true;
}
/**
* Reconnect the connection to newSource (if setNewSource(...) was invoked before)
* or newTarget (if setNewTarget(...) was invoked before).
*/
public void execute() {
if (newSource != null) {
oldSource.getSourceConnections().remove(connection);
connection.setSourceNode(newSource);
connection.setTargetNode(oldTarget);
newSource.getSourceConnections().add(connection);
}
else if (newTarget != null) {
oldTarget.getTargetConnections().remove(connection);
connection.setSourceNode(oldSource);
connection.setTargetNode(newTarget);
newTarget.getTargetConnections().add(connection);
}
else {
throw new IllegalStateException("Should not happen");
}
}
/**
* Set a new source endpoint for this connection.
* When execute() is invoked, the source endpoint of the connection will be attached
* to the supplied Shape instance.
* <p>
* Note: Calling this method, deactivates reconnection of the <i>target</i> endpoint.
* A single instance of this command can only reconnect either the source or the target
* endpoint.
* </p>
* @param connectionSource a non-null Shape instance, to be used as a new source endpoint
* @throws IllegalArgumentException if connectionSource is null
*/
public void setNewSource(NodeElement connectionSource) {
if (connectionSource == null) {
throw new IllegalArgumentException();
}
setLabel("move connection startpoint");
newSource = connectionSource;
newTarget = null;
}
/**
* Set a new target endpoint for this connection
* When execute() is invoked, the target endpoint of the connection will be attached
* to the supplied Shape instance.
* <p>
* Note: Calling this method, deactivates reconnection of the <i>source</i> endpoint.
* A single instance of this command can only reconnect either the source or the target
* endpoint.
* </p>
* @param connectionTarget a non-null Shape instance, to be used as a new target endpoint
* @throws IllegalArgumentException if connectionTarget is null
*/
public void setNewTarget(NodeElement connectionTarget) {
if (connectionTarget == null) {
throw new IllegalArgumentException();
}
setLabel("move connection endpoint");
newSource = null;
newTarget = connectionTarget;
}
/**
* Reconnect the connection to its original source and target endpoints.
*/
public void undo() {
if(newSource!=null)
newSource.getSourceConnections().remove(connection);
else
oldSource.getSourceConnections().remove(connection);
if(newTarget!=null)
newTarget.getTargetConnections().remove(connection);
else
oldTarget.getTargetConnections().remove(connection);
connection.setSourceNode(oldSource);
connection.setTargetNode(oldTarget);
oldSource.getSourceConnections().add(connection);
oldTarget.getTargetConnections().add(connection);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -