/* ========================
* 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-2005, by :
* Corporate:
* EADS Astrium SAS
* EADS CRC
* Individual:
* Claude Cazenave
*
* $Id: GatedSvgShape.java,v 1.2 2009/01/20 17:52:32 ogor Exp $
*
* Changes
* -------
* 08 sept 2008 : Initial public release (RO);
*
*/
package jsynoptic.plugins.circuit;
import java.awt.Point;
import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Vector;
import simtools.diagram.DiagramSelection;
import simtools.diagram.ElementSelection;
import simtools.diagram.SelectableShapeInterface;
import simtools.diagram.gate.Connection;
import simtools.diagram.gate.Gate;
import simtools.diagram.gate.GatedComponentSelection;
import simtools.shapes.AbstractShape;
import simtools.shapes.GatesHolder;
import simtools.ui.JPropertiesPanel;
import jsynoptic.plugins.circuit.ui.GatedSvgShapePropertiesPanel;
import jsynoptic.plugins.circuit.ui.GatesPropertiesPanel.GateParameters;
import jsynoptic.plugins.svg.SvgShape;
/**
* An SVG shape with a various number of input/output circuit gates
* @author zxpletran007
*
*/
public class GatedSvgShape extends SvgShape implements GatedCircuitComponent, SelectableShapeInterface{
private static final long serialVersionUID = -7395390236644692816L;
/**
* gates
*/
protected GatesHolder gateHolder;
public GatedSvgShape(int ox, int oy, int width, int height) {
super(ox, oy, width, height);
gateHolder = new GatesHolder(this);
}
public Rectangle getGateBounds(){
return new Rectangle(_ox + _x,_oy + _y - _h , _w,_h);
}
/* (non-Javadoc)
* @see jsynoptic.plugins.svg.SvgShape#cloneShape()
*/
protected AbstractShape cloneShape() {
GatedSvgShape res = (GatedSvgShape) super.cloneShape();
res.gateHolder = gateHolder.cloneGateHolder(res);
// clone gates
List gates = gateHolder.getGates();
for(int i=0;i< gates.size();i++){
Gate gate = (Gate)gates.get(i);
res.gateHolder.addGate(gate.cloneGate(res));
}
return res;
}
/**
* Create the shape gate from a list of gate parameters
* @param gatesParameters
*/
protected void setGates(List gatesParameters){
List oldGates = new ArrayList(gateHolder.getGates());
gateHolder.removeAllGate();
for(int i=0; i<gatesParameters.size();i++){
GateParameters gp = (GateParameters)gatesParameters.get(i);
CircuitGate newGate;
if (gp.isInput){
newGate = new CircuitGate(this, gp.side, gp.name, gp.isInput);
} else {
newGate = new ColorCircuitGate(this, gp.side, gp.name);
((ColorCircuitGate)newGate).setEmitterMapper(gp.cm);
((ColorCircuitGate)newGate).setEmitterDataSource(gp.ds);
}
gateHolder.addGate(newGate);
if (i<oldGates.size()){ // update gate reference and connections
CircuitGate oldGAte = (CircuitGate)oldGates.get(i);
List connections = new ArrayList(oldGAte.getConnections());
for(int j=0; j< connections.size(); j++){
Connection connection = (Connection)connections.get(j);
boolean isFirstGate = connection.getFirstEndGate() == oldGAte;
connection.disconnect(oldGAte);
connection.connect(newGate, isFirstGate);
connection.gatePositionHasChanged(newGate);
}
}
}
}
/**
* @return a vector containing gate parameters.
* Note: A vector is used to comply with JComboList constructor
*/
protected Vector getGateParametersList(){
Vector res = new Vector();
List gates = gateHolder.getGates();
for(int i=0; i<gates.size();i++){
CircuitGate cg = (CircuitGate)gates.get(i);
GateParameters gp;
if (cg instanceof ColorCircuitGate){
ColorCircuitGate ccg = (ColorCircuitGate)cg;
gp = new GateParameters(cg.getGateId(), cg.getSide(), cg.isInput, ccg.getEmitterDataSource(), ccg.getEmitterMapper());
} else {
gp = new GateParameters(cg.getGateId(), cg.getSide(), cg.isInput, null, null);
}
res.add(gp);
}
return res;
}
/* (non-Javadoc)
* @see jsynoptic.builtin.Abstract2DShape#getPanel(java.util.List)
*/
public JPropertiesPanel getPanel(List properties) {
if (properties == null){
return null;
}
if (properties.containsAll(Arrays.asList(new GatedSvgShapePropertiesNames().getPropertyNames()))){
return new GatedSvgShapePropertiesPanel(CircuitPlugin.resources.getString("GatedSvgShape"));
} else {
return super.getPanel(properties);
}
}
/* (non-Javadoc)
* @see simtools.util.NamedProperties#getPropertyNames()
*/
public String[] getPropertyNames() {
if (_propertyNames == null) {
_propertyNames = new GatedSvgShapePropertiesNames().getPropertyNames();
}
return _propertyNames;
}
/* (non-Javadoc)
* @see simtools.util.NamedProperties#getPropertyValue(java.lang.String)
*/
public Object getPropertyValue(String name) {
Object res = super.getPropertyValue(name);
if (res == null){
if ((name.equals("GATES_LIST"))){
res = getGateParametersList();
}
}
return res;
}
/* (non-Javadoc)
* @see simtools.util.NamedProperties#setPropertyValue(java.lang.String, java.lang.Object)
*/
public void setPropertyValue(String name, Object value) {
super.setPropertyValue(name, value);
if (name.equalsIgnoreCase("GATES_LIST") && value instanceof List) {
setGates( (List) value);
}
}
public static class GatedSvgShapePropertiesNames extends SvgShapePropertiesNames{
public GatedSvgShapePropertiesNames(){
super();
propertyNames.add("GATES_LIST");
}
}
// ***** Connection interface ******* //////
/* (non-Javadoc)
* @see simtools.diagram.gate.GatedComponent#addGate(simtools.diagram.gate.Gate)
*/
public void addGate(Gate gate) {
gateHolder.addGate(gate);
}
/* (non-Javadoc)
* @see simtools.diagram.gate.GatedComponent#connectionAddedAt(simtools.diagram.gate.Gate, simtools.diagram.gate.Connection)
*/
public void connectionAddedAt(Gate gate, Connection connection) {
}
/* (non-Javadoc)
* @see simtools.diagram.gate.GatedComponent#connectionRemovedAt(simtools.diagram.gate.Gate, simtools.diagram.gate.Connection)
*/
public void connectionRemovedAt(Gate gate, Connection connection) {
}
/* (non-Javadoc)
* @see simtools.diagram.gate.GatedComponent#getGateAnchor(simtools.diagram.gate.Gate)
*/
public Point getGateAnchor(Gate gate) {
return gateHolder.getGateAnchor(gate);
}
/* (non-Javadoc)
* @see simtools.diagram.gate.GatedComponent#getGateAt(int, int)
*/
public Gate getGateAt(int ox, int oy) {
return gateHolder.getGateAt(ox, oy);
}
/* (non-Javadoc)
* @see simtools.diagram.gate.GatedComponent#getGates()
*/
public List getGates() {
return gateHolder.getGates();
}
/* (non-Javadoc)
* @see simtools.diagram.gate.GatedComponent#removeGate(simtools.diagram.gate.Gate)
*/
public void removeGate(Gate gate) {
gateHolder.removeGate(gate);
}
public ElementSelection createSelection(Point shapeOrigin, DiagramSelection ds){
return new GatedComponentSelection(this, shapeOrigin, ds);
}
/* (non-Javadoc)
* @see jsynoptic.plugins.circuit.GatedCircuitComponent#signalHasBeenReceived(jsynoptic.plugins.circuit.CircuitGate)
*/
public void signalHasBeenReceived(CircuitGate gate) {
// By default do nothing
}
/* (non-Javadoc)
* @see jsynoptic.plugins.circuit.GatedCircuitComponent#signalHasBeenSent(jsynoptic.plugins.circuit.CircuitGate)
*/
public void signalHasBeenSent(CircuitGate gate) {
// By default do nothing
}
}