package jsynoptic.builtin.ui;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import simtools.shapes.LimitShape;
import simtools.shapes.StrokeParameters;
import simtools.ui.JPropertiesPanel;
import simtools.ui.MenuResourceBundle;
public class LimitPropertiesPanel extends JPropertiesPanel implements ActionListener {
protected JComboBox cblimits;
protected JComboBox cbxstrokes;
protected JButton blimitcolor, blimitdelete, blimitadd;
protected JRadioButton horizontal, vertical, primary, secondary;
protected JTextField value;
protected MenuResourceBundle resources;
protected JLabel lv, lc, ls;
protected Vector limits;
protected LimitParameters currentLimit = null;
public LimitPropertiesPanel(MenuResourceBundle resources) {
super(null);
this.resources = resources;
setLayout(new BorderLayout());
// limit combo
JPanel title = new JPanel(new GridLayout());
JPanel combo = new JPanel(new FlowLayout(FlowLayout.LEFT));
limits = new Vector();
cblimits = new JComboBox(limits);
cblimits.setMaximumRowCount(30);
cblimits.setEditable(true);
cblimits.addActionListener(this);
combo.add(cblimits);
blimitadd = resources.getButton("addLimitButton", this);
combo.add(blimitadd);
blimitdelete = resources.getButton("deleteLimitButton", this);
combo.add(blimitdelete);
title.add(combo);
add(title, BorderLayout.NORTH);
add(Box.createVerticalStrut(25));
JPanel limitsProperties = new JPanel();
limitsProperties.setLayout(new BoxLayout(limitsProperties, BoxLayout.Y_AXIS));
// limit axis
JPanel axisPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
ButtonGroup direction = new ButtonGroup();
horizontal = new JRadioButton(resources.getString("horizontal"), true);
vertical = new JRadioButton(resources.getString("vertical"), true);
direction.add(horizontal);
direction.add(vertical);
axisPanel.add(horizontal);
axisPanel.add(vertical);
horizontal.addActionListener(this);
vertical.addActionListener(this);
ButtonGroup axe = new ButtonGroup();
primary = new JRadioButton(resources.getString("primary"), true);
secondary = new JRadioButton(resources.getString("secondary"), true);
axe.add(primary);
axe.add(secondary);
axisPanel.add(primary);
axisPanel.add(secondary);
primary.addActionListener(this);
secondary.addActionListener(this);
limitsProperties.add(axisPanel);
// value
JPanel limitValue = new JPanel();
limitValue.setLayout(new FlowLayout(FlowLayout.LEFT));
limitValue.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), resources
.getStringValue("limitValue")));
value = new JTextField(15);
lv = new JLabel(resources.getString("limitValue"));
limitValue.add(lv);
limitValue.add(value);
limitsProperties.add(limitValue);
limitsProperties.add(Box.createVerticalStrut(10));
// display
JPanel limitDiplay = new JPanel();
limitDiplay.setLayout(new FlowLayout(FlowLayout.LEFT));
limitDiplay.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), resources
.getStringValue("limitDisplay")));
blimitcolor = new JButton(" ");
blimitcolor.addActionListener(this);
lc = new JLabel(resources.getString("limitColor"));
limitDiplay.add(lc);
limitDiplay.add(blimitcolor);
limitDiplay.add(Box.createVerticalStrut(10));
cbxstrokes = new JComboBox(StrokeDisplay.defaultstrokes);
cbxstrokes.setRenderer(new StrokeDisplay());
cbxstrokes.addActionListener(this);
ls = new JLabel(resources.getString("limitStyle"));
limitDiplay.add(ls);
limitDiplay.add(cbxstrokes);
limitsProperties.add(limitDiplay);
JPanel s = new JPanel(new BorderLayout());
s.add(limitsProperties, BorderLayout.NORTH);
add(s, BorderLayout.CENTER);
}
public String[] getPropertyNames() {
if (_propertyNames == null) {
_propertyNames = new LimitShape.LimitShapePropertiesNames().getPropertyNames();
}
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("PLOT_LIMIT_LIST") && (value instanceof Vector)) {
limits.clear();
limits.addAll((Vector) value);
boolean st = limits.size() > 0;
setLimitPropertiesEnabled(st);
if (st) {
cblimits.setSelectedIndex(0);
currentLimit = (LimitParameters) limits.get(0);
setLimitPropertiesPanel(currentLimit);
}
}
}
protected void setLimitPropertiesEnabled(boolean enabled) {
cblimits.setEnabled(enabled);
horizontal.setEnabled(enabled);
vertical.setEnabled(enabled);
primary.setEnabled(enabled);
secondary.setEnabled(enabled);
blimitdelete.setEnabled(enabled);
blimitcolor.setEnabled(enabled);
cbxstrokes.setEnabled(enabled);
ls.setEnabled(enabled);
lc.setEnabled(enabled);
lv.setEnabled(enabled);
}
protected void setLimitProperties(LimitParameters cp) {
cp.isVertical = vertical.isSelected();
cp.secondaryAxis = secondary.isSelected();
cp.position = new Double(value.getText()).doubleValue();
}
protected void setLimitPropertiesPanel(LimitParameters cp) {
blimitcolor.setBackground(cp.color);
selectStroke(cp.strokeParams);
horizontal.setSelected(!cp.isVertical);
vertical.setSelected(cp.isVertical);
primary.setSelected(!cp.secondaryAxis);
secondary.setSelected(cp.secondaryAxis);
value.setText(new Double(cp.position).toString());
}
/*
* (non-Javadoc)
*
* @see simtools.util.NamedProperties#getPropertyValue(java.lang.String)
*/
public Object getPropertyValue(String name) {
Object res = null;
if (name.equalsIgnoreCase("PLOT_LIMIT_LIST")) {
// update current limit if exist
if (currentLimit != null) {
setLimitProperties(currentLimit);
}
return limits;
}
return res;
}
//
// Action listener interface
//
public void actionPerformed(ActionEvent e) {
if (e.getSource() == cblimits) {
if (e.getActionCommand().equals("comboBoxChanged")) {
if (cblimits.getSelectedItem() != null) {
if (currentLimit != null) {
if (cblimits.getSelectedItem() instanceof String) {
currentLimit.name = (String) cblimits.getSelectedItem();
} else {
setLimitProperties(currentLimit);
}
}
Object o = cblimits.getSelectedItem();
if (o instanceof LimitParameters) {
currentLimit = (LimitParameters) o;
setLimitPropertiesPanel(currentLimit);
}
}
}
} else if (e.getSource() == cbxstrokes) {
if (e.getActionCommand().equals("comboBoxChanged")) {
Object o = cbxstrokes.getSelectedItem();
if (o instanceof StrokeDisplay) {
StrokeDisplay sd = (StrokeDisplay) o;
if (currentLimit != null) {
currentLimit.strokeParams = sd.getStrokeParams();
}
}
}
} else if (e.getSource() == blimitcolor) {
if (currentLimit != null) {
Color c = JColorChooser.showDialog(this, resources.getStringValue("limitColorChooserTitle"),
currentLimit.color);
if (c != null) {
currentLimit.color = c;
}
blimitcolor.setBackground(currentLimit.color);
}
} else if (e.getSource() == blimitdelete) {
Object o = cblimits.getSelectedItem();
if (o instanceof LimitParameters) {
LimitParameters cp = (LimitParameters) o;
int index = limits.indexOf(cp);
cblimits.removeItemAt(index);
if (limits.size() == 0) {
setLimitPropertiesEnabled(false);
currentLimit = null;
} else {
if (index > (limits.size() - 1)) {
index = limits.size() - 1;
}
currentLimit = (LimitParameters) limits.get(index);
setLimitPropertiesPanel(currentLimit);
}
}
repaint();
} else if (e.getSource() == blimitadd) {
LimitParameters cp = new LimitParameters(resources.getString("limitLabel") + " " + (limits.size() + 1));
cblimits.addItem(cp);
setLimitPropertiesEnabled(true);
cblimits.setSelectedItem(cp);
if (currentLimit != null) {
setLimitProperties(currentLimit);
}
currentLimit = cp;
setLimitPropertiesPanel(currentLimit);
repaint();
}
}
void selectStroke(StrokeParameters dashes) {
for (int i = 0; i < cbxstrokes.getItemCount(); i++) {
StrokeDisplay sd = (StrokeDisplay) cbxstrokes.getItemAt(i);
if (sd.getStrokeParams().equals(dashes)) {
cbxstrokes.setSelectedIndex(i);
return;
}
}
StrokeDisplay nsd = new StrokeDisplay(dashes);
cbxstrokes.addItem(nsd);
cbxstrokes.setSelectedItem(nsd);
}
public static class LimitParameters {
public StrokeParameters strokeParams;
public Color color;
public String name;
public boolean isVertical;
public boolean secondaryAxis;
public double position;
public LimitParameters(String name) {
strokeParams = new StrokeParameters();
color = Color.BLACK;
isVertical = false;
secondaryAxis = false;
this.name = name;
position = 0.0;
}
public String toString() {
return name;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#equals(java.lang.Object)
*/
public boolean equals(Object obj) {
if (obj instanceof LimitParameters) {
LimitParameters dobj = (LimitParameters) obj;
if ((dobj.name != null) && dobj.name.equals(name) && (dobj.position == position)
&& (dobj.isVertical == isVertical) && (dobj.secondaryAxis == secondaryAxis)
&& dobj.strokeParams.equals(strokeParams) && dobj.color.equals(color)) {
return true;
}
}
return false;
}
}
}