/* ========================
* JSynoptic : a free Synoptic editor
* ========================
*
* Project Info: http://jsynoptic.sourceforge.net/index.html
*
* This program 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 program 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
* program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*
* (C) Copyright 2001-2007, by :
* Corporate:
* EADS Astrium
* Individual:
* Claude Cazenave
*
* $Id: TestConnection.java,v 1.2 2009/01/08 16:43:05 ogor Exp $
*
* Changes
* -------
* 2 oct. 2008 : Initial public release
*
*/
package simtools.diagram.test;
import java.awt.Point;
import java.awt.geom.Line2D;
import simtools.diagram.DiagramSelection;
import simtools.diagram.Element;
import simtools.diagram.ElementSelection;
import simtools.diagram.TranslatableShapePointsInterface;
import simtools.diagram.gate.Connection;
import simtools.diagram.gate.ConnectionPathSelection;
import simtools.diagram.gate.Gate;
import simtools.diagram.gate.Path;
import simtools.diagram.gate.StraightPath;
/**
* @author zxpletran007
*
*/
public class TestConnection extends Line2D.Double implements Element, Connection, TranslatableShapePointsInterface{
/**
* The gate attached to the first bound
*/
protected Gate fisrtGate = null;
/**
* The gate attached to the last bound
*/
protected Gate lastGate = null;
/**
* The connection path.
* Manage the connection nodes
*/
protected Path path;
public TestConnection(int x, int y, int w, int h) {
this(x, y, w, h, null, null);
}
public TestConnection(int x, int y, int w, int h, Gate fisrtGate, Gate lastGate) {
super();
//Gates
this.fisrtGate = fisrtGate;
this.lastGate = lastGate;
// Path
setPath( new StraightPath(new Point(x, y), new Point(x + w, y + h)) );
}
public TestConnection cloneTestConnection() {
TestConnection res= (TestConnection)this.clone();
// clone the path
res.path = path.clonePath();
// Cloned connection is not connected to the original connection gates
res.fisrtGate = null;
res.lastGate = null;
return res;
}
/* (non-Javadoc)
* @see java.awt.geom.Line2D#contains(double, double)
*/
public boolean contains(double x, double y){
return path.contains(x, y);
}
/* (non-Javadoc)
* @see jsynoptic.builtin.Abstract1DShape#translate(int, int)
*/
public void translate(int dx, int dy) {
path.translatePath(dx, dy);
setLine();
}
/* (non-Javadoc)
* @see simtools.diagram.gate.Connection#connect(simtools.diagram.gate.Gate, boolean)
*/
public void connect(Gate gate, boolean onfirstBound) {
// Proceed to the connection only if the target gate allows the connection with the other gate
if (gate != null && gate.allowConnection(onfirstBound? lastGate : fisrtGate)){
gate.connectTo(this);
if (onfirstBound){
fisrtGate = gate;
path.lockFirstNode(true);
}else {
lastGate = gate;
path.lockLastNode(true);
}
}
}
/* (non-Javadoc)
* @see simtools.diagram.gate.Connection#disconnect(simtools.diagram.gate.Gate)
*/
public void disconnect(Gate gate) {
if (isConnected(gate)){
gate.disconnectFrom(this);
if (gate.equals(fisrtGate)){
fisrtGate = null;
path.lockFirstNode(false);
} else {
lastGate = null;
path.lockLastNode(false);
}
}
}
/* (non-Javadoc)
* @see simtools.diagram.gate.Connection#gatePositionHasChanged(simtools.diagram.gate.Gate)
*/
public void gatePositionHasChanged(Gate gate) {
boolean isDirty = false;
if (gate == fisrtGate){
Point fn = path.getNode(0);
path.translateNode(0,fisrtGate.getAnchor().x - fn.x, fisrtGate.getAnchor().y - fn.y);
isDirty = true;
} else if(gate == lastGate){
Point ln = path.getNode(path.getNodeNumber()-1);
path.translateNode(path.getNodeNumber()-1,lastGate.getAnchor().x - ln.x, lastGate.getAnchor().y - ln.y);
isDirty = true;
}
if (isDirty){
setLine();
}
}
/* (non-Javadoc)
* @see simtools.diagram.gate.Connection#getFirstEndGate()
*/
public Gate getFirstEndGate() {
return fisrtGate;
}
/* (non-Javadoc)
* @see simtools.diagram.gate.Connection#getLastEndGate()
*/
public Gate getLastEndGate() {
return lastGate;
}
/* (non-Javadoc)
* @see simtools.diagram.gate.Connection#getPath()
*/
public Path getPath() {
return path;
}
/* (non-Javadoc)
* @see simtools.diagram.gate.Connection#isConnected(simtools.diagram.gate.Gate)
*/
public boolean isConnected(Gate gate) {
return (gate!= null && ( gate==fisrtGate || gate==lastGate));
}
/* (non-Javadoc)
* @see simtools.diagram.gate.Connection#setPath(simtools.diagram.gate.Path)
*/
public void setPath(Path path) {
this.path = path;
this.path.lockFirstNode(fisrtGate != null);
this.path.lockLastNode(lastGate != null);
setLine();
}
/* (non-Javadoc)
* @see simtools.diagram.TranslatableShapePointsInterface#translatePoint(int, int, int)
*/
public int translatePoint(int pointIndex, int dx, int dy) {
if (pointIndex % 2 == 0){ // translate a node
pointIndex = path.translateNode(pointIndex / 2, dx, dy) * 2 ;
} else { // translate a segment
pointIndex = path.translateSegment( (pointIndex / 2) , (dx!=0)? dx : dy) * 2 + 1;
}
setLine();
return pointIndex;
}
/* (non-Javadoc)
* @see simtools.diagram.SelectableShapePointsInterface#getPoint(int)
*/
public Point getPoint(int index) {
Point res;
if (index % 2 == 0){
res = path.getNode(index /2);
} else{
Point p1 = path.getNode(index/2);
Point p2 = path.getNode(index/2 + 1);
res = new Point( (p1.x + p2.x )/2, (p1.y + p2.y )/2 );
}
return res;
}
public int getPointsNumber() {
// Node and Segment translator points
return path.getNodeNumber() * 2 -1;
}
/* (non-Javadoc)
* @see simtools.diagram.SelectableShapeInterface#createSelection(java.awt.Point, simtools.diagram.DiagramSelection)
*/
public ElementSelection createSelection(Point shapeOrigin, DiagramSelection ds) {
return new ConnectionPathSelection(this,shapeOrigin);
}
protected void setLine(){
setLine(
path.getNode(0).x,
path.getNode(0).y,
path.getNode(1).x,
path.getNode(1).y
);
}
public void processShapeRemoving() {
// TODO Auto-generated method stub
}
public void processShapeRestoring() {
// TODO Auto-generated method stub
}
}