/* ========================
* 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: TransformAnimatorTable.java,v 1.3 2008/10/23 17:21:05 cazenave Exp $
*
* Changes
* -------
* 21 janv. 08 : Initial public release
*
*/
package jsynoptic.plugins.java3d.panels;
import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JComboBox;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import simtools.data.DataSource;
import simtools.ui.SourceTree;
import jsynoptic.plugins.java3d.TransformAnimator;
import jsynoptic.plugins.java3d.TransformData;
import jsynoptic.plugins.java3d.edit.PropertyEdit;
import jsynoptic.plugins.java3d.edit.TransformDataEdit;
import jsynoptic.plugins.java3d.util.TransformValues;
public class TransformAnimatorTable extends JPanel implements
PropertyEdit.UndoRedoListener,
PropertiesPanel.Editor<TransformAnimator, TransformData.TrValues>, ItemListener,
PropertiesPanel.NeedsDialog {
TransformDataEdit _editor;
JTable _table;
JComboBox _combo;
Frame _owner;
boolean _newValue;
final SourceTree _sourceTree;
public TransformAnimatorTable() {
this.setLayout(new BorderLayout());
_combo = new JComboBox(TransformValues.values());
_combo.addItemListener(this);
add(BorderLayout.NORTH, _combo);
_table = new JTable();
add(BorderLayout.CENTER, _table);
_newValue = true;
_sourceTree=SourceTree.getFromPool("SceneGraphDataChooser");
}
@Override
public void edit(PropertyEdit<TransformAnimator, TransformData.TrValues> editor) {
if (_editor != null) {
_editor.removeListener(this);
}
_editor = (TransformDataEdit) editor;
if (_editor != null) {
_editor.addListener(this);
TransformData.TrValues v = _editor.getOldValues();
_table.setModel(new Model(v));
_newValue = false;
_combo.setSelectedItem(v.kind);
}
}
@Override
public void undoRedoPerfomed(boolean isUndo) {
TransformData.TrValues v = _editor.getPropertyValue();
_newValue = false;
_combo.setSelectedItem(v.kind);
_table.setModel(new Model(v));
}
protected class Model extends AbstractTableModel {
final TransformData.TrValues _v;
public Model(TransformData.TrValues 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 _v.kind.getSize();
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
if (columnIndex == 0) {
return _v.kind.getValueName(rowIndex);
} 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]= _v.kind.getDefaultValue(row);
}
_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 itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) {
TransformValues k = (TransformValues) e.getItem();
TransformData.TrValues v=_editor.getPropertyValue();
v=new TransformData.TrValues(k,v);
// keep column width
int w0 = _table.getColumnModel().getColumn(0).getWidth();
int w1 = _table.getColumnModel().getColumn(1).getWidth();
_table.setModel(new Model(v));
// set as preferred
_table.getColumnModel().getColumn(0).setPreferredWidth(w0);
_table.getColumnModel().getColumn(1).setPreferredWidth(w1);
if (_owner != null)
_owner.pack();
if (_newValue) {
_editor.setNewValues(v);
} else {
_newValue = true;
}
}
}
@Override
public void set(String title, Frame owner) {
_owner = owner;
}
}