/* (non-Javadoc)
* @see simtools.diagram.DiagramComponent#copyAt(java.util.Vector, int, int)
*/
protected void copyAt(Vector v, int x, int y){
CompoundEdit compoundEdit = new CompoundEdit();
List selectedConnectors = new ArrayList();
List selectedGatedComponent = new ArrayList();
Hashtable clonedGates = new Hashtable();
for(int i=0;i<v.size();i++){
Object o=v.elementAt(i);
if (o instanceof Connection){
selectedConnectors.add(o);
} else if (o instanceof AbstractShape){
AbstractShape as=((AbstractShape)o).cloneShape();
if (o instanceof GatedComponent){
selectedGatedComponent.add(o);
// Kept the relation between the existing and the new gate
List oldGates = new ArrayList(((GatedComponent)o).getGates());
List newGates = new ArrayList(((GatedComponent)as).getGates());
for(int j=0;j<oldGates.size(); j++){
clonedGates.put(oldGates.get(j), newGates.get(j));
}
}
as.translate(x,y);
ShapesContainer.this.add(as);
compoundEdit.addEdit(new CreateEdit(ShapesContainer.this,as));
}
}
// Now Copy the connectors
for (int i = 0; i < selectedConnectors.size(); i++) {
Connection connection = (Connection) selectedConnectors.get(i);
if (connection instanceof AbstractShape){
Gate fisrtGate = (Gate) connection.getFirstEndGate();
Gate lastGate = (Gate) connection.getLastEndGate();
Gate newFg = null;
Gate newLg = null;
if ((fisrtGate!=null) && selectedGatedComponent.contains(fisrtGate.getOwner())){
newFg = (Gate) clonedGates.get(fisrtGate);
}
if ((lastGate!=null) && selectedGatedComponent.contains(lastGate.getOwner())){
newLg = (Gate) clonedGates.get(lastGate);
}
AbstractShape newConnector = ((AbstractShape)connection).cloneShape();
newConnector.translate(x,y);
if (newFg != null){
((Connection)newConnector).connect(newFg, true);
compoundEdit.addEdit(new GateConnectEdit( (Connection)newConnector, newFg, true));
}
if (newLg != null){
((Connection)newConnector).connect(newLg, false);
compoundEdit.addEdit(new GateConnectEdit( (Connection)newConnector, newLg, false));
}
// Add it ton container
ShapesContainer.this.add(newConnector);
compoundEdit.addEdit(new CreateEdit(ShapesContainer.this,newConnector));
}
}
compoundEdit.end();
fireUndoableEditUpdate(new UndoableEditEvent(this, compoundEdit));
}