package jsynoptic.builtin.ui;
import java.awt.Dimension;
import java.util.ArrayList;
import java.util.ResourceBundle;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import jsynoptic.builtin.Builtin;
import jsynoptic.builtin.HistoryTextShape;
import simtools.ui.JPropertiesPanel;
import simtools.ui.GridBagPanel;
import simtools.ui.ResourceFinder;
public class HistoryTextPropertiesPanel extends JPropertiesPanel{
public static ResourceBundle resources = ResourceFinder.get(HistoryTextShape.class);
protected TextPropertiesPanel textPropertiesPanel;
protected JSpinner spiHistSize;
protected SpinnerNumberModel spiModelHistSize;
public HistoryTextPropertiesPanel(String shapeName) {
super(shapeName);
addOnCurrentRow(createContents(), 2, true, true, true);
}
/**
* By overwriting this method, it is possible to create different kind of text properties panel
* @param name
* @param width
* @param height
* @return a text s properties panel
*/
protected TextPropertiesPanel createTextPropertiesPanel(boolean showTransform, String shapeName) {
return new TextPropertiesPanel(showTransform, shapeName);
}
protected JComponent createContents() {
JLabel lhistorySize = new JLabel(resources.getString("HistorySize:"));
spiModelHistSize = new SpinnerNumberModel();
spiModelHistSize.setStepSize(new Integer(1));
spiModelHistSize.setMinimum(new Integer(1));
spiHistSize = new JSpinner(spiModelHistSize);
spiHistSize.setPreferredSize(new Dimension(120, 20));
textPropertiesPanel= createTextPropertiesPanel(false, Builtin.resources.getString("History"));
// Create panel
GridBagPanel historyPanel = new GridBagPanel();
GridBagPanel historySizePanel = new GridBagPanel();
historySizePanel.addOnCurrentRow(lhistorySize);
historySizePanel.addOnCurrentRow(spiHistSize);
historySizePanel.carriageReturn();
historyPanel.addOnCurrentRow(historySizePanel, 1, true, false, true);
historyPanel.addOnCurrentRow(textPropertiesPanel, 1, true, true, true);
return historyPanel;
}
public String[] getPropertyNames() {
if (_propertyNames == null) {
_propertyNames = new HistoryTextShape.HistoryTextShapePropertiesNames().getPropertyNames();
}
return _propertyNames;
}
public void setPropertyValue(String name, Object value) {
if (name.equalsIgnoreCase("HISTORY_SIZE")) {
if (value instanceof Integer) {
spiHistSize.setValue((value));
}
} else if (name.equalsIgnoreCase("CELL_PROPERTIES")) {
if (value instanceof ArrayList) {
// Set textPropertiesPanel attributes
ArrayList cellProperties = (ArrayList) value;
for (int j = 0; j < cellProperties.size(); j++) {
Object[] property = (Object[]) cellProperties.get(j);
String pname = (String) property[0];
Object pvalue = property[1];
textPropertiesPanel.setPropertyValue(pname, pvalue);
}
}
}
}
public Object getPropertyValue(String name) {
Object res = null;
if (name.equalsIgnoreCase("HISTORY_SIZE")) {
int size = ((Number) spiHistSize.getValue()).intValue();
if (size < 1) {
size = 1; // At least one cell
}
res = new Integer(size);
}else if (name.equalsIgnoreCase("CELL_PROPERTIES")) {
ArrayList cellProperties = new ArrayList();
String[] props = textPropertiesPanel.getPropertyNames();
if (props != null) {
for (int j = 0; j < props.length; j++) {
// Each property has a name and a value
Object[] property = new Object[2];
String pname = props[j];
Object value = textPropertiesPanel.getPropertyValue(pname);
property[0] = pname;
property[1] = value;
cellProperties.add(property);
}
}
res = cellProperties;
}
return res;
}
/*
* (non-Javadoc)
*
* @see simtools.ui.JPropertiesPanel#setOwner(javax.swing.JDialog)
*/
public void setOwner(JDialog owner) {
super.setOwner(owner);
textPropertiesPanel.setOwner(owner);
}
}