/** * Constructs a connection set containing the specified connections and * updates the set of changed edges. */ public ConnectionSet(Set connections) { setConnections(connections); Iterator it = connections.iterator(); while (it.hasNext()) { Connection conn = (Connection) it.next(); edges.add(conn.getEdge()); } }
/** * Returns the source or target of the specified edge in this connection set * or null if the connection set contains no corresponding entry for the * edge. */ public Object getPort(Object edge, boolean source) { if (edges.contains(edge)) { Iterator it = connections.iterator(); while (it.hasNext()) { Connection c = (Connection) it.next(); if (c.getEdge() == edge && c.isSource() == source) return c.getPort(); } } return null; }
/** * Two connections are equal if they represent the source or target of * the same edge. That is, if * <p> * c1.edge == c2.edge && c1.isSource == c2.isSource. */ public boolean equals(Object obj) { if (obj instanceof Connection) { Connection other = (Connection) obj; return (other.getEdge().equals(edge) && other.isSource() == isSource); } return false; }
/** * Applies <code>connectionSet</code> to the model. Returns a connection * set that may be used to undo this change. */ protected ConnectionSet handleConnectionSet(ConnectionSet cs) { if (cs != null) { ConnectionSet csundo = new ConnectionSet(); Iterator it = cs.connections(); while (it.hasNext()) { ConnectionSet.Connection c = (ConnectionSet.Connection) it .next(); Object edge = c.getEdge(); if (c.isSource()) csundo.connect(edge, getSource(edge), true); else csundo.connect(edge, getTarget(edge), false); handleConnection(c, false); } // When removing edges it is possible that an edge is // removed in a later step which has been added in a // previous connection establishment (set semantic). // Therefore, we first need to remove all old connections // and then add all new connections in two steps. it = cs.connections(); while (it.hasNext()) handleConnection((ConnectionSet.Connection) it.next(), true); return csundo; } return null; }
/** * Creates a new connection set based on this connection set, where the * edges, and ports are mapped using <code>map</code>. If a port is not * found, the old port is used. If both, the edge and the port are not in * <code>map</code>, the entry is ignored. * <p> * <strong>Note: </strong> Consequently, unselected edges are only * reconnected at the first "paste" after a "cut", because in this case the * ConnectionSet is not cloned. */ public ConnectionSet clone(Map map) { ConnectionSet cs = new ConnectionSet(); Iterator it = connections(); while (it.hasNext()) { // Shortcut Vars Connection c = (Connection) it.next(); Object edge = map.get(c.getEdge()); Object port = c.getPort(); if (port != null) port = map.get(port); // New Port if (edge != null && port != null) cs.connect(edge, port, c.isSource()); // Old Port else if (edge != null) cs.connect(edge, c.getPort(), c.isSource()); } return cs; }
/** * Inserts the specified connection into the model. */ protected void handleConnection(ConnectionSet.Connection c, boolean establish) { Object edge = c.getEdge(); Object port = (establish) ? c.getPort() : (c.isSource()) ? getSource(edge) : getTarget(edge); connect(edge, port, c.isSource(), establish); }