/* ========================
* 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-2005, by :
* Corporate:
* Astrium SAS
* Individual:
* Ronan Ogor
*
* $Id: PropertiesTransformDataUI.java,v 1.10 2008/09/29 10:29:07 ogor Exp $
*
* Changes
* -------
* 29 juin 2005 : Creation date (RO);
*
*/
package syn3d.ui;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableColumn;
import simtools.data.DataSource;
import simtools.ui.ColorMapper;
import simtools.ui.DynamicColorChooser;
import simtools.ui.MenuResourceBundle;
import simtools.ui.ResourceFinder;
import syn3d.base.ActiveNode;
import syn3d.data.PropertyData;
import syn3d.data.TransformData;
import syn3d.nodes.PropertiesTransformGroupNode.ColorMapperManager;
/**
* This UI allows the user to set a variable number of properties about a node. THe trasform property is
* the default property
* @author ogor
*/
public class PropertiesTransformDataUI extends TransformDataUI implements ItemListener, ActionListener {
protected PropertyData propertyData[];
protected PropertyData propertyDataCopy[];
protected JTable propertiesTable[];
protected JCheckBox propertiesCheckBox[];
// Color Managment :
protected JComboBox colorCombo;
protected JPanel colorPanel;
protected ColorMapperPanel colorMapperPanel;
protected ColorMapperManager colorMapperManager;
protected int colorTransformType;
protected JTable colorTablePanel;
protected PropertyData colorDataCopy;
public static final int RGBCANALS=0;
public static final int COLORMAPER=1;
public static MenuResourceBundle propertyDataResources = ResourceFinder.getMenu(PropertyData.class);
public PropertiesTransformDataUI(ActiveNode node, TransformData td, PropertyData pd[], boolean transformAvailable, ColorMapperManager cmm ){
super(node, td, transformAvailable);
propertyData = pd;
colorMapperPanel = null;
colorMapperManager = cmm;
this.colorTransformType= ( (colorMapperManager==null) || (!colorMapperManager.isColorMapperEnabled()))? RGBCANALS : COLORMAPER;
propertyDataCopy = new PropertyData[propertyData.length];
try{
for (int i=0; i<propertyData.length; i++)
propertyDataCopy[i] = (PropertyData)propertyData[i].clone();
} catch (CloneNotSupportedException e) {
throw new IllegalArgumentException();
}
}
protected JPanel createDataPanel(){
//JPanel p= new JPanel
JPanel p= new JPanel(new BorderLayout());
// Transform property
if (transformAvailable)
p.add(createTransformDataPanel(), BorderLayout.CENTER);
// Other properties
//JPanel pProp= new JPanel(new GridLayout(propertyData.length,1));
JPanel pProp= new JPanel();
pProp.setLayout(new BoxLayout(pProp, BoxLayout.Y_AXIS));
propertiesTable = new JTable[propertyData.length];
propertiesCheckBox = new JCheckBox[propertyData.length];
for (int i=0; i<propertyData.length; i++){
JPanel pb= new JPanel(new BorderLayout());
JPanel namePanel= new JPanel(new GridLayout());
propertiesCheckBox[i] = resources.getCheckBox("editable",this);
JLabel p1=new JLabel(propertyData[i].getName());
namePanel.add(propertiesCheckBox[i]);
namePanel.add(p1);
pb.add(namePanel, BorderLayout.NORTH);
// Color properties can be set by canals RGB or by a color mapper
if (propertyData[i].getName().startsWith(propertyDataResources.getString("ColorPropriety"))){
colorCombo=new JComboBox();
colorCombo.addItem(propertyDataResources.getString("RGBcanals"));
colorCombo.addItem(propertyDataResources.getString("ColorMapper"));
colorCombo.setSelectedIndex(colorTransformType);
colorCombo.addItemListener(this);
namePanel.add(colorCombo);
// Manage color with a R, G, B properties
colorDataCopy = propertyDataCopy[i];
colorTablePanel = createPropertyTableDataPanel(propertyData[i], propertyDataCopy[i]);
propertiesTable[i] = colorTablePanel;
// or manage color with a mapper
colorMapperPanel = new ColorMapperPanel(colorMapperManager);
colorPanel = new JPanel(new GridLayout());
if (colorTransformType==COLORMAPER){
colorPanel.add(colorMapperPanel, BorderLayout.NORTH);
}else if (colorTransformType==RGBCANALS){
colorPanel.add(colorTablePanel, BorderLayout.NORTH);
}
pb.add(colorPanel, BorderLayout.CENTER);
}
else if ( (propertyData[i].getName().startsWith(propertyDataResources.getString("VisibilityPropriety"))) || (propertyData[i].getName().startsWith(propertyDataResources.getString("AxisPropriety")))){
JTable table = createPropertyCheckBoxTableDataPanel(propertyData[i], propertyDataCopy[i]);
pb.add(table, BorderLayout.CENTER);
propertiesTable[i] = table;
}
else{
JTable table = createPropertyTableDataPanel(propertyData[i], propertyDataCopy[i]);
pb.add(table, BorderLayout.CENTER);
propertiesTable[i] = table;
}
pProp.add(pb);
}
if (propertyData.length==0)
pProp.add(new JTextField("No additional properties available"));
if (transformAvailable)
p.add(pProp, BorderLayout.SOUTH);
else
p.add(pProp, BorderLayout.CENTER);
return p;
}
protected JTable createPropertyTableDataPanel(PropertyData propertyData, PropertyData propertyDataCopy){
JTable table=new JTable(new PropertyTableModel( dstree, propertyDataCopy));
TableColumn c;
c=table.getColumn("2");
c.setPreferredWidth(10);
c=table.getColumn("3");
return table;
}
protected JTable createPropertyCheckBoxTableDataPanel(PropertyData propertyData, PropertyData propertyDataCopy){
JTable table=new JTable(new PropertyBooleanTableModel( dstree, propertyDataCopy));
TableColumn c;
c=table.getColumn("2");
c.setPreferredWidth(10);
c=table.getColumn("3");
return table;
}
public void actionPerformed(ActionEvent e) {
super.actionPerformed(e);
for (int i=0; i<propertyData.length; i++)
setPropertyEditable(i, propertiesCheckBox[i].isSelected());
updateEditable();
dialog.repaint();
}
/* (non-Javadoc)
* @see java.awt.event.ItemListener#itemStateChanged(java.awt.event.ItemEvent)
*/
public void itemStateChanged(ItemEvent e) {
if (e.getSource()!= colorCombo)
super.itemStateChanged(e);
else{
int newType = (colorCombo.getSelectedIndex());
if ((e.getStateChange() == ItemEvent.SELECTED)
&& (newType != colorTransformType)) {
colorTransformType = newType;
// Remove previous panel
colorPanel.removeAll();
if (colorTransformType==COLORMAPER){
colorMapperManager.setColorMapperEnabled(true);
colorDataCopy.setDataSource(0,null);
colorDataCopy.setDataSource(1,null);
colorDataCopy.setDataSource(2,null);
colorPanel.add(colorMapperPanel);
}else if (colorTransformType==RGBCANALS){
colorMapperPanel.cancel();
colorPanel.add(colorTablePanel);
}
dialog.pack();
dialog.repaint();
}
}
}
public void updateEditable(){
super.updateEditable();
for (int i=0; i<propertyData.length; i++) {
updateEditable(i);
}
if (colorCombo!=null) colorCombo.setEnabled(colorDataCopy.isEditable());
}
public void updateEditable(int index){
propertiesCheckBox[index].setSelected(propertyDataCopy[index].isEditable());
propertiesTable[index].setEnabled(propertyDataCopy[index].isEditable());
propertiesTable[index].setGridColor( (propertyDataCopy[index].isEditable())? Color.BLACK : Color.GRAY);
propertiesTable[index].setForeground((propertyDataCopy[index].isEditable())? Color.BLACK : Color.GRAY);
}
public void setPropertyEditable(int index, boolean b){
propertyDataCopy[index].setEditable(b);
}
public void updateAllData(){
super.updateAllData();
if (colorMapperPanel!=null)
colorMapperPanel.apply();
if ((colorDataCopy!=null) && (colorTransformType ==COLORMAPER)){
float floatValues[] = new float[3];
colorMapperManager.getColor().getRGBColorComponents(floatValues);
colorDataCopy.setValue(0, (double) floatValues[0] );
colorDataCopy.setValue(1, (double) floatValues[1]);
colorDataCopy.setValue(2, (double) floatValues[2]);
}
for (int i=0; i<propertyData.length; i++){
propertyData[i].updateFrom(propertyDataCopy[i]);
}
}
public void dispose() {
super.dispose();
for (int i=0; i<propertyData.length; i++){
propertyDataCopy[i].dispose();
}
}
public void synchronizeDataPanel(){
super.synchronizeDataPanel();
for (int i=0; i<propertyData.length; i++){
TableCellEditor tce=propertiesTable[i].getCellEditor();
if(tce!=null){
tce.stopCellEditing();
}
}
}
/**
* @author ogor
* Used for Color Mapper interface
*/
protected class ColorMapperPanel extends JPanel {
protected JButton bColor;
protected Color noColor;
protected ColorMapperManager colorMapperManager;
// copy fields to allow cancelling dialog
protected Color color;
protected DataSource colorMapperSource;
protected ColorMapper colorMapper;
public ColorMapperPanel(ColorMapperManager cmm) {
setLayout(new GridLayout(1,2));
colorMapperManager = cmm;
color = colorMapperManager.getColor();
colorMapperSource = colorMapperManager.getColorMapperSource();
colorMapper = colorMapperManager.getColorMapper();
add(new JLabel(propertyDataResources.getString("Color")));
add(bColor = new JButton(" "));
noColor = bColor.getBackground();
bColor.setFocusPainted(false);
bColor.setBackground(color);
bColor.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
DynamicColorChooser dialog = new DynamicColorChooser(
new JDialog(),
resources.getString("SelectAColor"),
null,color,colorMapperSource,colorMapper);
if (dialog.isOk()){
color = dialog.getColor();
bColor.setBackground(color);
DataSource colorTempDataSource = dialog.getSource();
colorMapper = dialog.getMapper();
if (colorTempDataSource!=null){
if (colorMapperSource!=null)
colorMapperSource.removeEndNotificationListener(colorMapperManager);
colorMapperSource = colorTempDataSource;
colorMapperSource.addEndNotificationListener(colorMapperManager);
}
}
}
});
}
public void apply() {
if (color!=null)
colorMapperManager.setColor(color);
colorMapperManager.setColorMapperSource(colorMapperSource);
colorMapperManager.setColorMapper(colorMapper);
}
public void cancel(){
colorMapperManager.setColorMapperEnabled(false);
if (colorMapperManager.getColorMapperSource() != null)
colorMapperManager.getColorMapperSource().removeEndNotificationListener(colorMapperManager);
}
}
}