/* ========================
* 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: TransformTable.java,v 1.3 2008/10/22 16:02:11 cazenave Exp $
*
* Changes
* -------
* 24 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.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.swing.JComboBox;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import jsynoptic.plugins.java3d.edit.PropertyEdit;
import jsynoptic.plugins.java3d.edit.Transform3DEdit;
import jsynoptic.plugins.java3d.util.TransformValues;
public class TransformTable extends JPanel implements
PropertyEdit.UndoRedoListener,
PropertiesPanel.Editor<TransformGroup, Transform3D>, ItemListener,
PropertiesPanel.NeedsDialog {
Transform3DEdit.Group _editor;
JTable _table;
JComboBox _combo;
Frame _owner;
boolean _newValue;
public TransformTable() {
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;
}
@Override
public void setEnabled(boolean enabled) {
super.setEnabled(enabled);
_combo.setEnabled(enabled);
_table.setEnabled(enabled);
}
@Override
public void edit(PropertyEdit<TransformGroup, Transform3D> editor) {
if (_editor != null) {
_editor.removeListener(this);
}
_editor = (Transform3DEdit.Group) editor;
if (_editor != null) {
_editor.addListener(this);
TransformValues[] kind = new TransformValues[1];
double[] v = _editor.getOldValues(kind);
_table.setModel(new Model(kind[0], v));
_newValue = false;
_combo.setSelectedItem(kind[0]);
}
}
@Override
public void undoRedoPerfomed(boolean isUndo) {
TransformValues[] kind = new TransformValues[1];
double[] v = _editor.getPropertyValues(kind);
_newValue = false;
_combo.setSelectedItem(kind[0]);
_table.setModel(new Model(kind[0], v));
}
protected class Model extends AbstractTableModel {
final double[] _v;
final TransformValues _k;
public Model(TransformValues k, double[] v) {
_v = v;
_k = k;
}
@Override
public int getColumnCount() {
return 2;
}
@Override
public int getRowCount() {
return _k.getSize();
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
if (columnIndex == 0) {
return _k.getValueName(rowIndex);
} else {
// Use Float to reduce max number of digits
return Float.toString((float) _v[rowIndex]);
}
}
public boolean isCellEditable(int rowIndex, int columnIndex) {
return columnIndex == 1;
}
public void setValueAt(Object value, int row, int col) {
try {
_v[row] = Double.parseDouble(value.toString());
} catch (NumberFormatException nfe) {
_v[row] = _k.getDefaultValue(row);
}
_editor.setNewValues(_v, _k);
fireTableCellUpdated(row, col);
}
}
@Override
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) {
TransformValues k = (TransformValues) e.getItem();
double[] v = TransformValues.get(_editor.getPropertyValue(), k);
// keep column width
int w0 = _table.getColumnModel().getColumn(0).getWidth();
int w1 = _table.getColumnModel().getColumn(1).getWidth();
_table.setModel(new Model(k, 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, k);
} else {
_newValue = true;
}
}
}
@Override
public void set(String title, Frame owner) {
_owner = owner;
}
}