/* ==============================================
* Simtools : The tools library used in JSynoptic
* ==============================================
*
* Project Info: http://jsynoptic.sourceforge.net/index.html
*
* This library is free software; you can redistribute it and/or modify it under the terms
* of the GNU Lesser General Public License as published by the Free Software Foundation;
* either version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with this
* library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*
* (C) Copyright 1999-2003, by :
* Corporate:
* Astrium SAS
* EADS CRC
* Individual:
* Claude Cazenave
* Nicolas Brodu
*
*
* $Id: GatedComponentSelection.java,v 1.2 2009/01/09 13:18:41 ogor Exp $
*
* Changes
* -------
* 08-Sep-2008 : Initial public release (RO);
*
*/
package simtools.diagram.gate;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Shape;
import java.util.ArrayList;
import java.util.List;
import javax.swing.undo.CompoundEdit;
import javax.swing.undo.UndoableEdit;
import simtools.diagram.DiagramSelection;
import simtools.diagram.Element;
import simtools.diagram.ElementSelection;
/**
* A gated component selection.
* Manage the translation of attached connections.
* @author zxpletran007
*
*/
public class GatedComponentSelection extends ElementSelection{
protected List connectionPathEdits;
// TODO In this implementation, we keep a
// reference to the current DiagramSelection in order to
// check for each GatedComponent gates if the attached connection are selected
// If a connection is selected, the GatedComponentSelection shall not manage its translation.
protected DiagramSelection ds;
protected GatedComponent gc;
public GatedComponentSelection(GatedComponent gatedComponent, Point p, DiagramSelection ds) {
super((Element)gatedComponent, p);
this.ds = ds;
this.gc = gatedComponent;
createconnectionPathedits();
}
/* (non-Javadoc)
* @see simtools.diagram.ShapeSelection#translateShape(int, int)
*/
public void translateShape(int dx, int dy){
super.translateShape(dx, dy);
for(int i=0; i< connectionPathEdits.size(); i++) {
ConnectionPathEdit cpe = (ConnectionPathEdit)connectionPathEdits.get(i);
Connection c = cpe.connection;
if (!ds.isSelected( (Shape)c)){
Gate gate = (gc.getGates().contains(c.getFirstEndGate()))? c.getFirstEndGate() : c.getLastEndGate();
cpe.connection.gatePositionHasChanged(gate);
}
}
}
protected void createconnectionPathedits(){
connectionPathEdits = new ArrayList();
List gates = gc.getGates();
for(int i=0; i<gates.size(); i++){
Gate gate = (Gate)gates.get(i);
List connections = gate.getConnections();
for(int j=0; j<connections.size();j++){
Connection connection = ((Connection)connections.get(j));
connectionPathEdits.add(new ConnectionPathEdit(connection));
}
}
}
/* (non-Javadoc)
* @see simtools.diagram.ShapeSelection#translateShapeEnd()
*/
public UndoableEdit translateShapeEnd(){
CompoundEdit ce = new CompoundEdit();
ce.addEdit(super.translateShapeEnd());
for(int i=0; i< connectionPathEdits.size(); i++) {
ConnectionPathEdit cpe = (ConnectionPathEdit)connectionPathEdits.get(i);
Connection c = cpe.connection;
if (!ds.isSelected( (Shape)c)){
// Update the connection pathes
cpe.connection.getPath().updatePath();
// Add connection path edits to the returned coumpound edit
cpe.pathHasChanged();
ce.addEdit(cpe);
}
}
ce.end();
// Create new connection path edits
createconnectionPathedits();
return ce;
}
/* (non-Javadoc)
* @see simtools.diagram.ShapeSelection#drawBounds(java.awt.Graphics2D, simtools.shapes.AbstractShape)
*/
public void drawBounds(Graphics2D g2){
super.drawBounds(g2);
List gates = gc.getGates();
for(int j=0; j < gates.size(); j++){
((Gate)gates.get(j)).drawGate(g2);
}
}
}