001: package net.refractions.udig.printing.ui.internal.editor.commands;
002:
003: import java.util.Iterator;
004:
005: import net.refractions.udig.printing.model.Box;
006: import net.refractions.udig.printing.model.Connection;
007: import net.refractions.udig.printing.ui.internal.Messages;
008:
009: import org.eclipse.gef.commands.Command;
010:
011: public class ConnectionReconnectCommand extends Command {
012:
013: private Box oldTarget;
014: private Box oldSource;
015: private Box newTarget;
016: private Box newSource;
017: private Connection connection;
018:
019: public ConnectionReconnectCommand(Connection connection) {
020: super ();
021: if (connection == null) {
022: throw new IllegalArgumentException(
023: Messages.ConnectionReconnectCommand_error_nullConnection);
024: }
025: this .connection = connection;
026: this .oldSource = connection.getSource();
027: this .oldTarget = connection.getTarget();
028: }
029:
030: public boolean canExecute() {
031: if (newSource != null) {
032: return checkSourceReconnection();
033: } else if (newTarget != null) {
034: return checkTargetReconnection();
035: }
036: return false;
037: }
038:
039: private boolean checkSourceReconnection() {
040: // connection endpoints must be different Shapes
041: if (newSource.equals(oldTarget)) {
042: return false;
043: }
044: // return false, if the connection exists already
045: for (Iterator iter = newSource.getSourceConnections()
046: .iterator(); iter.hasNext();) {
047: Connection conn = (Connection) iter.next();
048: // return false if a newSource -> oldTarget connection exists already
049: // and it is a different instance than the connection-field
050: if (conn.getTarget().equals(oldTarget)
051: && !conn.equals(connection)) {
052: return false;
053: }
054: }
055: return true;
056: }
057:
058: private boolean checkTargetReconnection() {
059: // connection endpoints must be different Shapes
060: if (newTarget.equals(oldSource)) {
061: return false;
062: }
063: // return false, if the connection exists already
064: for (Iterator iter = newTarget.getTargetConnections()
065: .iterator(); iter.hasNext();) {
066: Connection conn = (Connection) iter.next();
067: // return false if a oldSource -> newTarget connection exists already
068: // and it is a differenct instance that the connection-field
069: if (conn.getSource().equals(oldSource)
070: && !conn.equals(connection)) {
071: return false;
072: }
073: }
074: return true;
075: }
076:
077: public void execute() {
078: if (newSource != null) {
079: connection.reconnect(newSource, oldTarget);
080: } else if (newTarget != null) {
081: connection.reconnect(oldSource, newTarget);
082: } else {
083: throw new IllegalStateException(
084: Messages.ConnectionReconnectCommand_error_unreacheable);
085: }
086: }
087:
088: public void setNewSource(Box connectionSource) {
089: if (connectionSource == null) {
090: throw new IllegalArgumentException();
091: }
092: setLabel(Messages.ConnectionReconnectCommand_label_startpoint);
093: newSource = connectionSource;
094: newTarget = null;
095: }
096:
097: public void setNewTarget(Box connectionTarget) {
098: if (connectionTarget == null) {
099: throw new IllegalArgumentException();
100: }
101: setLabel(Messages.ConnectionReconnectCommand_label_endpoint);
102: newSource = null;
103: newTarget = connectionTarget;
104: }
105:
106: public void undo() {
107: connection.reconnect(oldSource, oldTarget);
108: }
109: }
|