package jsynoptic.plugins.circuit.boxes;
import java.awt.Color;
import java.awt.Polygon;
import java.util.Arrays;
import java.util.List;
import simtools.data.DataSource;
import simtools.diagram.gate.Gate;
import simtools.shapes.AbstractShape;
import simtools.ui.ColorMapper;
import simtools.ui.JPropertiesPanel;
import simtools.ui.MenuResourceBundle;
import simtools.ui.ResourceFinder;
import jsynoptic.base.DataSourceConsumer;
import jsynoptic.plugins.circuit.CircuitGate;
import jsynoptic.plugins.circuit.CircuitPlugin;
import jsynoptic.plugins.circuit.ColorCircuitGate;
import jsynoptic.plugins.circuit.ui.EmitterPropertiesPanel;
/**
*
* This component is able to emit a colored signal
* @author zxpletran007
*
*/
public class Emitter extends CircuitBox implements DataSourceConsumer{
private static final long serialVersionUID = 570230674860211488L;
public static MenuResourceBundle resources = ResourceFinder.getMenu(CircuitPlugin.class);
protected ColorCircuitGate out;
public Emitter(int ox, int oy, int width, int height) {
super(ox, oy, width, height);
addGate(out = new ColorCircuitGate(this, Gate.EAST, "Out"));
}
/* (non-Javadoc)
* @see jsynoptic.plugins.circuit.RectangleCircuitComponent#createSymbol()
*/
protected Polygon[] createSymbol(){
Polygon[] res = new Polygon[2];
res[0] = new Polygon(new int[] {3,6,6,8,6,6,3},new int[] {4,4,3,5,7,6,6},7);
res[1] = new Polygon(new int[] {1,1,3,3,2,2,3,3},new int[] {2,8,8,7,7,3,3,2},8);
return res;
}
/*
* (non-Javadoc)
*
* @see simtools.shapes.AbstractShape#cloneShape()
*/
protected AbstractShape cloneShape() {
Emitter res = (Emitter) super.cloneShape();
if(res!=null){
res.out = (ColorCircuitGate)out.cloneGate(res);
res.addGate(res.out);
}
return res;
}
public Color getFillColor(){
return (out.getEmiterColor()!=null) ? out.getEmiterColor() : super.getFillColor();
}
public CircuitGate getOut() {
return out;
}
/**
* Emit a colored signal with the fill color of this shape to all attached gates
*/
public void emitColorSignal(Color color){
out.emitColorSignal(color);
}
/* (non-Javadoc)
* @see jsynoptic.base.DataSourceConsumer#addDataSource(simtools.data.DataSource)
*/
public boolean addDataSource(DataSource ds) {
out.setEmitterDataSource(ds);
notifyChange();
return true;
}
/* (non-Javadoc)
* @see jsynoptic.base.DataSourceConsumer#canAddDataSource(simtools.data.DataSource)
*/
public boolean canAddDataSource(DataSource d) {
return true;
}
/* (non-Javadoc)
* @see simtools.shapes.AbstractShape#getShapeName()
*/
public String getShapeName(){
return CircuitPlugin.resources.getString("Emitter");
}
/* (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 EmitterPropertiesNames().getPropertyNames()))){
return new EmitterPropertiesPanel(CircuitPlugin.resources.getString("Emitter"));
} else {
return super.getPanel(properties);
}
}
/* (non-Javadoc)
* @see simtools.shapes.AbstractShape#subscribeToDataNotifications()
*/
public void processShapeRestoring(){
out.processGateRestoring();
super.processShapeRestoring();
}
/* (non-Javadoc)
* @see simtools.shapes.AbstractShape#unsubscribeToDataNotifications()
*/
public void processShapeRemoving(){
out.processGateRemoving();
super.processShapeRemoving();
}
public void setPropertyValue(String name, Object value) {
super.setPropertyValue(name, value);
if(name.equalsIgnoreCase("EMITTER_MAPPER")) {
if (value instanceof ColorMapper) {
out.setEmitterMapper( (ColorMapper)value);
}
else{
out.setEmitterMapper(null);
}
} else if(name.equalsIgnoreCase("EMITTER_MAPPER_SOURCE")) {
out.setEmitterDataSource( (DataSource)value);
}
}
public Object getPropertyValue(String name) {
Object res = super.getPropertyValue(name); // NULL if not supported by above class
if(name.equalsIgnoreCase("EMITTER_MAPPER")) {
res = out.getEmitterMapper();
} else if(name.equalsIgnoreCase("EMITTER_MAPPER_SOURCE")) {
res = out.getEmitterDataSource();
}
return res;
}
public String[] getPropertyNames(){
if (_propertyNames==null)
_propertyNames = new EmitterPropertiesNames().getPropertyNames();
return _propertyNames;
}
/* (non-Javadoc)
* @see jsynoptic.plugins.circuit.Abstract2DCircuitComponent#signalHasChanged(jsynoptic.plugins.circuit.CircuitGate)
*/
public void signalHasBeenSent(CircuitGate gate) {
// refresh the shape
if (gate == out){
notifyChange(null);
}
super.signalHasBeenReceived(gate);
}
public static class EmitterPropertiesNames extends Abstract2DShapePropertiesNames{
private static transient String[] props = new String[] { "EMITTER_MAPPER", "EMITTER_MAPPER_SOURCE" };
public EmitterPropertiesNames(){
super();
for (int i=0;i<props.length;i++){
propertyNames.add(props[i]);
}
}
}
}