/* ========================
* 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-2007, by :
* Corporate:
* EADS Astrium
* Individual:
* Claude Cazenave
*
* $Id: SwitchShapePropertiesPanel.java,v 1.3 2009/01/08 15:55:41 ogor Exp $
*
* Changes
* -------
* 20 oct. 08 : Initial public release
*
*/
package jsynoptic.plugins.circuit.ui;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ResourceBundle;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import jsynoptic.builtin.ui.PropertiesPanel2D;
import jsynoptic.plugins.circuit.CircuitPlugin;
import jsynoptic.plugins.circuit.switches.Switch.SwitchShapePropertiesNames;
import simtools.data.DataInfo;
import simtools.data.DataSource;
import simtools.data.DataSourcePool;
import simtools.data.DuplicateIdException;
import simtools.ui.ActionCheckBox;
import simtools.ui.FilteredSourceTree;
import simtools.ui.GridBagPanel;
import simtools.ui.ResourceFinder;
/**
* @author ronus
*
*/
public class SwitchShapePropertiesPanel extends PropertiesPanel2D {
protected JList cmlist;
protected FilteredSourceTree dstree;
protected GridBagPanel switchPositionPanel;
protected ActionCheckBox lds;
protected JLabel lCategory;
public static ResourceBundle resources = ResourceFinder.get(CircuitPlugin.class);
protected DataSource source;
protected JTextField ldynamicDsId;
/**
*
* @param shapeName
* @param switchCategories - the list of switch categories names. can be null if no categories to display
*/
public SwitchShapePropertiesPanel(String shapeName) {
super(true, true, true, true, shapeName);
setSwitchPositionPanel();
addOnCurrentRow(switchPositionPanel, 2, true, true, true);
}
public void setSwitchPositionPanel() {
lds = new ActionCheckBox(resources.getString("dataSourceUsed"), false) {
public void actionPerformed(ActionEvent e) {
ldynamicDsId.setEnabled(isSelected());
dstree.setEnabled(isSelected());
updateWarnings();
}
};
ldynamicDsId = new JTextField();
ldynamicDsId.setPreferredSize(new Dimension(200, 20));
ldynamicDsId.setEditable(true);
ldynamicDsId.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String dsId = ldynamicDsId.getText();
DataSource ds = null;
try {
ds = DataSourcePool.global.getDataSourceWithId(dsId);
} catch (DuplicateIdException e1) {
}
if (ds == null){
displayWarning(dsId + resources.getString("isNoAnExistingDataSource"));
ldynamicDsId.setText( source==null? "" : DataInfo.getId(source));
} else {
// Set source with entered id
dstree.setSelectedValue(ds);
if (canSetDataSource(ds)){
source = ds;
}
updateWarnings();
}
}
});
// Create the list and put it in a scroll pane.
cmlist = new JList();
cmlist.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
cmlist.setVisibleRowCount(5);
dstree = FilteredSourceTree.getFromPool("PropertiesPanel0");
dstree.getSourceTree().addTreeSelectionListener(new TreeSelectionListener() {
public void valueChanged(TreeSelectionEvent e) {
updateWarnings();
if (((dstree.getSelectedSourceOrCollection()) instanceof DataSource)
&& canSetDataSource((DataSource) dstree.getSelectedSourceOrCollection())) {
ldynamicDsId.setText(DataInfo.getId(dstree.getSelectedSourceOrCollection()));
}
}
});
switchPositionPanel = new GridBagPanel(resources.getString("setSwitchDynamicPosition"));
switchPositionPanel.addOnCurrentRow(lds, 2);
switchPositionPanel.addOnCurrentRow(ldynamicDsId, 1, true, false, true);
switchPositionPanel.addOnCurrentRow(dstree, 4, true, true, true);
}
protected boolean canSetDataSource(DataSource ds) {
return true;
}
/*
* (non-Javadoc)
*
* @see jsynoptic.builtin.ui.PropertiesPanel1D#updateWarnings()
*/
public boolean updateWarnings() {
boolean res = false;
if (lds.isSelected() && !(dstree.getSelectedSourceOrCollection() instanceof DataSource)) {
displayWarning(resources.getString("selectADataSource"));
res = true;
}
if (!res) {
res = super.updateWarnings();
}
return res;
}
public String[] getPropertyNames() {
if (_propertyNames == null) {
_propertyNames = new SwitchShapePropertiesNames().getPropertyNames();
}
return _propertyNames;
}
/*
* (non-Javadoc)
*
* @see simtools.ui.JPropertiesPanel#getPropertyValue(java.lang.String)
*/
public Object getPropertyValue(String name) {
Object res = super.getPropertyValue(name);
if (name.equalsIgnoreCase("SWITCH_POSITION_SOURCE")) {
Object o = dstree.getSelectedSourceOrCollection();
if (lds.isSelected() && (o instanceof DataSource) && canSetDataSource((DataSource) o)) {
res = o;
}
}
return res;
}
/*
* (non-Javadoc)
*
* @see simtools.ui.JPropertiesPanel#setPropertyValue(java.lang.String,
* java.lang.Object)
*/
public void setPropertyValue(String name, Object value) {
super.setPropertyValue(name, value);
if (name.equalsIgnoreCase("SWITCH_POSITION_SOURCE")) {
if ((value instanceof DataSource) && canSetDataSource((DataSource) value)) {
source = (DataSource) value;
ldynamicDsId.setText(DataInfo.getId(source));
lds.setSelected(true);
} else {
source = null;
lds.setSelected(false);
}
dstree.setSelectedValue(source);
lds.apply();
}
}
}