package jsynoptic.plugins.circuit.ui;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JRadioButton;
import jsynoptic.plugins.circuit.GatedSvgShape;
import simtools.data.DataSource;
import simtools.diagram.gate.Gate;
import simtools.ui.ColorMapper;
import simtools.ui.DynamicColorPanel;
import simtools.ui.JPropertiesPanel;
import simtools.ui.MenuResourceBundle;
import simtools.ui.ResourceFinder;
/**
* mMnage the properties related to a list of circuit gates
* @author zxpletran007
*
*/
public class GatesPropertiesPanel extends JPropertiesPanel implements ActionListener {
protected MenuResourceBundle resources = (MenuResourceBundle)ResourceFinder.get(GatedSvgShape.class);
/** Circuit gate list */
protected Vector gates;
/** Current selected gate **/
protected GateParameters selectectGate;
protected JComboBox cbGates;
protected JButton bGateDelete, bGateAdd;
protected JRadioButton onLeftSide, onRightSide, onTop, onBottom;
protected JLabel lgateposition;
protected DynamicColorPanel emitterPanel;
public GatesPropertiesPanel() {
super(null);
gates = new Vector();
cbGates = new JComboBox(gates);
cbGates.setMaximumRowCount(30);
cbGates.setEditable(true);
cbGates.addActionListener(this);
bGateAdd = resources.getButton("addGateButton", this);
bGateDelete = resources.getButton("deleteGateButton", this);
// Gate position
ButtonGroup position = new ButtonGroup();
onLeftSide = new JRadioButton(resources.getString("left"), true);
onRightSide = new JRadioButton(resources.getString("right"), false);
onTop = new JRadioButton(resources.getString("top"), false);
onBottom = new JRadioButton(resources.getString("bottom"), false);
position.add(onLeftSide);
position.add(onRightSide);
position.add(onTop);
position.add(onBottom);
// Emitter panel
emitterPanel = new DynamicColorPanel(this.getOwner(), resources.getString("emitterColorMapper"), true);
// Create panel
addOnCurrentRow(cbGates, 2);
addOnCurrentRow(bGateAdd);
addOnCurrentRow(bGateDelete);
carriageReturn();
lgateposition = new JLabel(resources.getString("gatePosition"));
addOnCurrentRow(lgateposition);
addOnCurrentRow(onTop);
addOnCurrentRow(onRightSide);
addOnCurrentRow(onBottom);
addOnCurrentRow(onLeftSide);
carriageReturn();
addOnCurrentRow(emitterPanel,3, true, true, true);
}
/* (non-Javadoc)
* @see simtools.ui.JPropertiesPanel#updateWarnings()
*/
public boolean updateWarnings() {
boolean res = emitterPanel.updateWarnings();
if (!res){
res = super.updateWarnings();
}
return res;
}
/* (non-Javadoc)
* @see simtools.ui.JPropertiesPanel#setOwner(javax.swing.JDialog)
*/
public void setOwner(JDialog owner) {
super.setOwner(owner);
emitterPanel.setOwner(owner);
}
public String[] getPropertyNames() {
if (_propertyNames == null) {
_propertyNames = new String[1];
_propertyNames[0] = "GATES_LIST";
}
return _propertyNames;
}
/*
* (non-Javadoc)
*
* @see simtools.util.NamedProperties#setPropertyValue(java.lang.String,
* java.lang.Object)
*/
public void setPropertyValue(String name, Object value) {
if (name.equalsIgnoreCase("GATES_LIST") && (value instanceof Vector)) {
gates.clear();
gates.addAll((Vector) value);
boolean st = gates.size() > 0;
setEnabled(st);
if (st) {
cbGates.setSelectedIndex(0);
selectectGate = (GateParameters) gates.get(0);
setGatePropertiesPanel(selectectGate);
}
}
}
/*
* (non-Javadoc)
*
* @see simtools.util.NamedProperties#getPropertyValue(java.lang.String)
*/
public Object getPropertyValue(String name) {
Object res = null;
if (name.equalsIgnoreCase("GATES_LIST")) {
// update current gate regarding AWT components
if (selectectGate != null) {
setGateProperties(selectectGate);
}
return gates;
}
return res;
}
public void setEnabled(boolean enabled) {
super.setEnabled(enabled);
cbGates.setEnabled(enabled);
bGateDelete.setEnabled(enabled);
onLeftSide.setEnabled(enabled);
onRightSide.setEnabled(enabled);
onTop.setEnabled(enabled);
onBottom.setEnabled(enabled);
lgateposition.setEnabled(enabled);
emitterPanel.setEnabled(enabled);
}
protected void setGateProperties(GateParameters cp) {
// side
if (onTop.isSelected()){
cp.side = Gate.NORTH;
} else if (onRightSide.isSelected()){
cp.side= Gate.EAST;
} else if (onBottom.isSelected()){
cp.side= Gate.SOUTH;
} else {
cp.side=Gate.WEST;
}
DataSource ds = (DataSource) emitterPanel.getPropertyValue(DynamicColorPanel.DYNAMIC_COLOR_SOURCE);
ColorMapper cm = (ColorMapper) emitterPanel.getPropertyValue(DynamicColorPanel.DYNAMIC_COLOR_MAPPER);
cp.isInput = ds==null && cm == null;
cp.ds = ds;
cp.cm = cm;
}
protected void setGatePropertiesPanel(GateParameters cp) {
// side
onTop.setSelected(cp.side == Gate.NORTH);
onRightSide.setSelected(cp.side == Gate.EAST);
onBottom.setSelected(cp.side == Gate.SOUTH);
onLeftSide.setSelected(cp.side == Gate.WEST);
emitterPanel.setPropertyValue(DynamicColorPanel.DYNAMIC_COLOR_SOURCE, cp.ds);
emitterPanel.setPropertyValue(DynamicColorPanel.DYNAMIC_COLOR_MAPPER, cp.cm);
updateWarnings();
}
/* (non-Javadoc)
* @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
*/
public void actionPerformed(ActionEvent e) {
if (e.getSource() == cbGates) {
if (e.getActionCommand().equals("comboBoxChanged")) {
if (cbGates.getSelectedItem() != null) {
if (selectectGate != null) {
if (cbGates.getSelectedItem() instanceof String) {
selectectGate.name = (String) cbGates.getSelectedItem();
} else {
setGateProperties(selectectGate); // save current gate properties
}
}
Object o = cbGates.getSelectedItem();
if (o instanceof GateParameters) {
selectectGate = (GateParameters) o;
setGatePropertiesPanel(selectectGate); // display selected gate properties
}
}
}
} else if (e.getSource() == bGateDelete) {
Object o = cbGates.getSelectedItem();
if (o instanceof GateParameters) {
GateParameters cp = (GateParameters) o;
int index = gates.indexOf(cp);
cbGates.removeItemAt(index);
if (gates.size() == 0) {
setEnabled(false);
selectectGate = null;
} else {
if (index > (gates.size() - 1)) {
index = gates.size() - 1;
}
selectectGate = (GateParameters) gates.get(index);
setGatePropertiesPanel(selectectGate);
}
}
repaint();
} else if (e.getSource() == bGateAdd) {
GateParameters cp = new GateParameters(resources.getString("gateLabel") + " " + (gates.size() + 1));
cbGates.addItem(cp);
setEnabled(true);
cbGates.setSelectedItem(cp);
if (selectectGate != null) {
setGateProperties(selectectGate);
}
selectectGate = cp;
setGatePropertiesPanel(selectectGate);
repaint();
}
}
/**
* Hold properties related to a gate
* @author zxpletran007
*/
public static class GateParameters {
public String name;
/** A gate can be either a input or an output **/
public boolean isInput;
/** The gate position: 0= on Top, 1=on right side, 2= on Bottom, 3= on left Side */
public int side;
/** The data source related to an output gate. Null if the gate is an input */
public DataSource ds;
/** The color mapper elated to an output gate. Null if the gate is an input */
public ColorMapper cm;
/**
* @param name
*/
public GateParameters(String name) {
this(name, Gate.WEST, true, null, null);
}
/**
* GateParameters constructor
* @param name
* @param side
* @param isInput
* @param ds
*/
public GateParameters(String name, int side, boolean isInput, DataSource ds, ColorMapper cm) {
this.name = name;
this.isInput = isInput;
this.side = side;
this.ds = ds;
this.cm = cm;
}
public String toString() {
return name;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#equals(java.lang.Object)
*/
public boolean equals(Object obj) {
boolean res = false;
if (obj instanceof GateParameters) {
GateParameters dobj = (GateParameters) obj;
res = ((dobj.name != null) && dobj.name.equals(name) || (dobj.name==null && name==null))
&& ((dobj.ds != null) && dobj.ds.equals(ds) || (dobj.ds==null && ds==null))
&& ((dobj.cm != null) && dobj.cm.equals(cm) || (dobj.cm==null && cm==null))
&& (dobj.side == side) && (dobj.isInput == isInput);
}
return res;
}
}
}