/*
* Copyright (c) 2006 Alexander Podlich
*
* E-Mail : podlich@student.uni-kassel.de
* Creation Date : 04.07.2006 15:32:22
* Original Filename: org.dgpf.gui.controls.DGPFPropertyControl.java
* Version : 1.0
* Last modification: 04.07.2006
* Last modified by : Alexander Podlich
*
* License : GNU LESSER GENERAL PUBLIC LICENSE
* Version 2.1, February 1999
* You should have received a copy of this license along
* with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307, USA or download the license under
* http://www.gnu.org/copyleft/lesser.html.
*
* Warranty : This software is provided "as is" 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.
*/
package org.dgpf.gui.controls;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SpinnerNumberModel;
import javax.swing.SwingConstants;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import org.dgpf.gui.InternalFrame;
import org.dgpf.gui.components.Button;
import org.dgpf.gui.components.Spinner;
import org.dgpf.gui.utils.LanguageSwitcher;
import org.dgpf.gui.utils.Layout;
import org.dgpf.search.api.SearchState;
import org.sfc.meta.BoundedDoubleProperty;
import org.sfc.meta.Property;
import org.sfc.meta.PropertyPort;
/**
* This class creates a control for a bounded double property.
*
* @author Alexander Podlich
*/
public class BoundedDoublePropertyControl implements IControl,
ActionListener, ChangeListener
{
/**
* The serial version UID.
*/
private static final long serialVersionUID = 1L;
/**
* The control panel for editing the values.
*/
private JPanel m_controlPanel;
/**
* The label for double values, which cannot be set.
*/
private JLabel m_label;
/**
* The property.
*/
private final Property> m_property;
/**
* The button for submit.
*/
private Button m_button;
/**
* The spinner for changing the value.
*/
private Spinner m_spinner;
/**
* The label for displaying the minimal value.
*/
private JLabel m_minLabel;
/**
* The label for displaying the maximal value.
*/
private JLabel m_maxLabel;
/**
* The minimal value.
*/
private double m_minValue;
/**
* The maximal value.
*/
private double m_maxValue;
/**
* Has wether the value of this property can be written by at least one
* property port or not. true
if this property can be
* written by at least one property port, false
if it is
* read-only.
*/
private boolean m_canSet;
/**
* Has wether the value of this property can be read by at least one
* property port or not. true
if this property can be read
* by at least one property port, false
if it cannot be
* read.
*/
private boolean m_canGet;
/**
* Is true
, if it is possible to make an update for this
* bounded double property. False
if not.
*/
private boolean m_updateAble;
/**
* The parent DGPFInternalFrame
.
*/
private InternalFrame m_parent;
/**
* The default constructor.
*
* @param p_parent
* is the parent frame.
* @param p_port
* The property port.
*/
public BoundedDoublePropertyControl(InternalFrame p_parent,
PropertyPort p_port)
{
this.m_parent = p_parent;
this.m_canSet = p_port.get_property().can_set();
this.m_canGet = p_port.get_property().can_get();
setUpdateAble(true);
this.m_property = p_port.get_property();
if(this.m_canSet && this.m_canGet)
{
this.m_minValue = ((BoundedDoubleProperty) p_port.get_property())
.get_min();
this.m_maxValue = ((BoundedDoubleProperty) p_port.get_property())
.get_max();
DGPFMouseListener l_mListener = new DGPFMouseListener(this);
this.m_controlPanel = new JPanel(new GridBagLayout());
this.m_spinner = new Spinner(new SpinnerNumberModel(this.m_minValue,
this.m_minValue, this.m_maxValue, 0.01), this, l_mListener, true);
Layout.set_constraints(this.m_controlPanel, this.m_spinner, 1.0,
1.0, GridBagConstraints.NONE, GridBagConstraints.LINE_END,
GridBagConstraints.RELATIVE, new Insets(3, 3, 3, 3));
this.m_button = new Button(LanguageSwitcher.getString("btn_change"),
"btn", this, true);
Layout.set_constraints(this.m_controlPanel, this.m_button, 1.0, 1.0,
GridBagConstraints.NONE, GridBagConstraints.LINE_START,
GridBagConstraints.REMAINDER, new Insets(3, 3, 3, 3));
this.m_minLabel = new JLabel(LanguageSwitcher.getString("minValue")
+ " " + String.valueOf(this.m_minValue));
this.m_minLabel.setHorizontalAlignment(SwingConstants.CENTER);
Layout.set_constraints(this.m_controlPanel, this.m_minLabel, 1.0,
1.0, GridBagConstraints.HORIZONTAL,
GridBagConstraints.LINE_START, GridBagConstraints.REMAINDER,
new Insets(3, 3, 3, 3));
this.m_maxLabel = new JLabel(LanguageSwitcher.getString("maxValue")
+ " " + String.valueOf(this.m_maxValue));
this.m_maxLabel.setHorizontalAlignment(SwingConstants.CENTER);
Layout.set_constraints(this.m_controlPanel, this.m_maxLabel, 1.0,
1.0, GridBagConstraints.HORIZONTAL,
GridBagConstraints.LINE_START, GridBagConstraints.REMAINDER,
new Insets(3, 3, 3, 3));
}
else
if(this.m_canGet)
{
this.m_label = new JLabel();
}
}
/**
* This method makes an update of the gui element.
*
* @param p_pPort
* The property port.
* @param p_searchState
* The search state.
* @param p_index
* The array index if this property is an array property, or
* -1 if the property is no array property.
*/
public void update(PropertyPort p_pPort,
final SearchState> p_searchState, int p_index)
{
if(this.m_updateAble)
{
Object l_object = ((p_index >= 0) ? p_pPort.get(p_searchState,
p_index) : p_pPort.get(p_searchState));
if(this.m_canSet && this.m_canGet)
{
this.m_spinner.setValue(Double.valueOf(l_object.toString()));
}
else
if(this.m_canGet)
{
this.m_label.setText((l_object != null) ? l_object.toString()
: "null");
}
}
}
/**
* This method returns the internal JComponent.
*
* @return The JComponent.
*/
public JComponent get()
{
if(this.m_canSet && this.m_canGet)
{
return this.m_controlPanel;
}
else
if(this.m_canGet)
{
return this.m_label;
}
return null;
}
/**
* Invoked when an action occurs.
*
* @param p_event
* The ActionEvent.
*/
public void actionPerformed(ActionEvent p_event)
{
String l_cmd = p_event.getActionCommand();
if(l_cmd.equals("btn"))
{
this.m_parent.setProperty(this.m_property, this.m_spinner
.getDoubleValue());
setUpdateAble(true);
}
}
/**
* @param p_event
* The change event for the spinner.
*/
public void stateChanged(ChangeEvent p_event)
{
if(p_event.getSource().equals(this.m_spinner))
{
setUpdateAble(false);
}
}
/**
* True
, if this bounded double property can be updated.
* False
, if this bounded double property cannot be
* updated.
*
* @param p_updateAble
* The boolean value for this control.
*/
public void setUpdateAble(boolean p_updateAble)
{
this.m_updateAble = p_updateAble;
}
}