/* ========================
* 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: Tuple3fAnimatorTable.java,v 1.2 2008/10/22 22:36:46 cazenave Exp $
*
* Changes
* -------
* 21 janv. 08 : Initial public release
*
*/
package jsynoptic.plugins.java3d.panels;
import java.awt.BorderLayout;
import java.awt.Frame;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import jsynoptic.plugins.java3d.Tuple3fAnimator;
import jsynoptic.plugins.java3d.Values;
import jsynoptic.plugins.java3d.edit.PropertyEdit;
import jsynoptic.plugins.java3d.edit.Tuple3fDataEdit;
import simtools.data.DataSource;
import simtools.ui.SourceTree;
public class Tuple3fAnimatorTable extends JPanel implements
PropertyEdit.UndoRedoListener,
PropertiesPanel.Editor<Tuple3fAnimator, Values>,
PropertiesPanel.NeedsDialog {
Tuple3fDataEdit _editor;
JTable _table;
Frame _owner;
boolean _newValue;
final SourceTree _sourceTree;
public Tuple3fAnimatorTable() {
this.setLayout(new BorderLayout());
_table = new JTable();
add(BorderLayout.CENTER, _table);
_newValue = true;
_sourceTree=SourceTree.getFromPool("SceneGraphDataChooser");
}
@Override
public void edit(PropertyEdit<Tuple3fAnimator, Values> editor) {
if (_editor != null) {
_editor.removeListener(this);
}
_editor = (Tuple3fDataEdit) editor;
if (_editor != null) {
_editor.addListener(this);
Values v = _editor.getOldValues();
_table.setModel(new Model(v));
_newValue = false;
}
}
@Override
public void undoRedoPerfomed(boolean isUndo) {
Values v = _editor.getPropertyValue();
_newValue = false;
_table.setModel(new Model(v));
}
protected class Model extends AbstractTableModel {
final Values _v;
public Model(Values v) {
_v = v;
}
@Override
public int getColumnCount() {
return 4;
}
public Class<?> getColumnClass(int columnIndex) {
if (columnIndex == 2) {
return Boolean.class;
}
return super.getColumnClass(columnIndex);
}
@Override
public int getRowCount() {
return 3;
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
if (columnIndex == 0) {
return rowIndex==0 ? "X" : rowIndex==1 ? "Y" : "Z";
} else if(columnIndex ==1){
// Use Float to reduce max number of digits
return Float.toString((float) _v.doubleValues[rowIndex]);
} else if(columnIndex ==2){
DataSource d=_v.data[rowIndex];
return Boolean.valueOf(d!=null);
} else if(columnIndex == 3){
DataSource d=_v.data[rowIndex];
return (d==null) ? "" : d.getInformation().toString();
}
throw new RuntimeException("Invalid column index");
}
public boolean isCellEditable(int rowIndex, int columnIndex) {
if (columnIndex == 0 || columnIndex == 3) {
return false;
}
if (columnIndex == 2) {
boolean edit = getValueAt(rowIndex, columnIndex).equals(Boolean.TRUE);
if (!edit) {
edit = _sourceTree.getSelectedSourceOrCollection() instanceof DataSource;
}
return edit;
}
return (_v.data[rowIndex] == null);
}
public void setValueAt(Object value, int row, int col) {
if(col ==1){
try {
_v.doubleValues[row]= Double.parseDouble(value.toString());
} catch (NumberFormatException nfe) {
_v.doubleValues[row]= 0.;
}
_editor.setNewValues(_v);
fireTableCellUpdated(row, col);
} else if(col == 2){
if (value.equals(Boolean.TRUE)) {
Object o = _sourceTree.getSelectedSourceOrCollection();
if (o instanceof DataSource) {
_v.data[row]= (DataSource) o;
_editor.setNewValues(_v);
fireTableCellUpdated(row, 3);
}
} else {
_v.data[row]=null;
_editor.setNewValues(_v);
fireTableCellUpdated(row, 3);
}
}
else{
throw new RuntimeException("Invalid column index");
}
}
}
@Override
public void set(String title, Frame owner) {
_owner = owner;
}
}