/* ========================
* 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-2008, by :
* Corporate:
* EADS Astrium
* Individual:
* Claude Cazenave
*
* $Id: DataAnimatorSelector.java,v 1.2 2008/11/05 18:14:52 cazenave Exp $
*
* Changes
* -------
* 24 oct. 2008 : Initial public release
*
*/
package jsynoptic.plugins.java3d.panels;
import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import javax.swing.JCheckBox;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import jsynoptic.plugins.java3d.DataAnimator;
import jsynoptic.plugins.java3d.Values;
import jsynoptic.plugins.java3d.edit.DataEdit;
import jsynoptic.plugins.java3d.edit.PropertyEdit;
import simtools.data.DataSource;
import simtools.ui.SourceTree;
/**
*/
public class DataAnimatorSelector extends JPanel implements
PropertyEdit.UndoRedoListener,
PropertiesPanel.Editor<DataAnimator, Values>,
PropertiesPanel.NeedsDialog{
static final String DEFAULT_TEXT = "";
DataEdit _editor;
Frame _owner;
boolean _newValue;
final SourceTree _sourceTree;
JTextField _tfValue;
JCheckBox _cb;
JTextField _tfName;
Values _v;
public DataAnimatorSelector() {
this.setLayout(new BorderLayout());
JPanel p=new JPanel();
_tfValue=new JTextField(14);
_tfName=new JTextField(20);
_tfName.setEnabled(false);
p.add(_tfValue);
p.add(_cb=new JCheckBox());
p.add(_tfName);
add(BorderLayout.CENTER, p);
_newValue = true;
_sourceTree=SourceTree.getFromPool("SceneGraphDataChooser");
_tfValue.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
readText();
}});
_tfValue.addFocusListener(new FocusListener(){
@Override
public void focusLost(FocusEvent e) {
readText();
}
@Override
public void focusGained(FocusEvent e) {
}});
_cb.addChangeListener(new ChangeListener(){
@Override
public void stateChanged(ChangeEvent e) {
readCheckBox();
}
});
}
@Override
public void undoRedoPerfomed(boolean isUndo) {
_v = _editor.getPropertyValue();
_newValue = false;
update();
}
@Override
public void edit(PropertyEdit<DataAnimator, Values> editor) {
if (_editor != null) {
_editor.removeListener(this);
}
_editor = (DataEdit) editor;
if (_editor != null) {
_editor.addListener(this);
_v = _editor.getOldValues();
update();
_newValue = false;
}
}
@Override
public void set(String title, Frame owner) {
_owner = owner;
}
void update() {
if(_v.isIntegerValues()){
_tfValue.setText(""+ _v.intValues[0]);
}
else{
// Use Float to reduce max number of digits
_tfValue.setText(Float.toString((float) _v.doubleValues[0]));
}
DataSource d=_v.data[0];
_cb.setSelected(d!=null);
_tfValue.setEnabled(d==null);
_tfName.setText((d==null) ? "" : d.getInformation().toString());
}
void readCheckBox(){
Object o = _sourceTree.getSelectedSourceOrCollection();
if (_cb.isSelected() && o instanceof DataSource) {
DataSource d=(DataSource) o;
_tfName.setText(d.getInformation().toString());
_tfValue.setEnabled(false);
_v.data[0]= d;
_editor.setNewValues(_v);
} else {
_tfName.setText("");
_tfValue.setEnabled(true);
_v.data[0]=null;
_editor.setNewValues(_v);
}
}
void readText(){
String t;
if(_v.isIntegerValues()){
t=""+_v.intValues[0];
try{
_v.intValues[0]=Integer.parseInt(_tfValue.getText());
_editor.setNewValues(_v);
}
catch(NumberFormatException ne){
_tfValue.setText(t);
}
}
else{
// Use Float to reduce max number of digits
t=Float.toString((float) _v.doubleValues[0]);
try{
_v.doubleValues[0]=Double.parseDouble(_tfValue.getText());
_editor.setNewValues(_v);
}
catch(NumberFormatException ne){
_tfValue.setText(t);
}
}
}
}