/* ========================
* 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-2004, by :
* Corporate:
* EADS Corporate Research Center
* Individual:
* Nicolas Brodu
*
* $Id: ShapeNode.java,v 1.17 2008/09/29 10:06:38 ogor Exp $
*
* Changes
* -------
* 19-Mar-2004 : Creation date (NB);
*
*/
package syn3d.nodes;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import jsynoptic.builtin.ui.PropertiesPanel1D;
import simtools.data.DataSource;
import simtools.data.DataSourcePool;
import simtools.data.EndNotificationListener;
import simtools.ui.ColorMapper;
import simtools.ui.DynamicColorChooser;
import syn3d.base.ActiveNode;
/**
*
*/
public class ShapeNode extends ActiveNode implements Serializable, EndNotificationListener{
static final long serialVersionUID = -5639874357133632692L;
public static final int SHADING_MODE = 1;
public static final int FLAT_MODE = 2;
public static final int FILLED_MODE = 3;
public static final int WIREFRAME_MODE = 4;
protected int mode = WIREFRAME_MODE; //SHADING_MODE;
protected boolean highlighted = false;
protected Color baseColor, highlightColor;
protected transient DataSource baseColorMapperSource, highlightColorMapperSource;
protected ColorMapper baseColorMapper, highlightColorMapper;
public ShapeNode(ActiveNode parent) {
super(parent);
baseColor = Color.lightGray;
highlightColor = Color.red;
}
protected static List actions = new ArrayList();
static {
actions.addAll(Arrays.asList(new String[] {
NodeResourcesManager.resources.getString("Properties"),
}));
}
public List getActions() {
return actions;
}
public void doAction(Object action) {
if (action==null) return;
if (action.equals(NodeResourcesManager.resources.getString("Properties"))) {
configure();
}
}
public void configure() {
configure(true);
}
public void configure(boolean showName) {
PropertiesPanel panel = createPanel(showName);
int res = JOptionPane.showConfirmDialog(null, panel,
NodeResourcesManager.resources.getString("Properties"),
JOptionPane.OK_CANCEL_OPTION,
JOptionPane.PLAIN_MESSAGE);
if (res == JOptionPane.OK_OPTION) {
panel.apply(); // subclases may hook actions there
}
}
protected static Icon icon = NodeResourcesManager.resources.getIcon("shapeIcon");
public Icon getIcon() {
return icon;
}
public void highlight(boolean on, Object parameter) {
super.highlight(on, parameter);
highlighted = on;
}
public void restoreReferences(ActiveNode parent) {
// TODO Auto-generated method stub
super.restoreReferences(parent);
}
public boolean saveChildren() {
// TODO Auto-generated method stub
return super.saveChildren();
}
/** Factory for subclasses */
protected PropertiesPanel createPanel(boolean showName) {
return new PropertiesPanel(showName);
}
protected class PropertiesPanel extends JPanel {
protected JTextField tfName;
protected JRadioButton rbShading, rbWireframe, rbFlat, rbFilled;
protected JButton bBaseColor, bHighlightColor;
protected Color noColor;
// copy fields to allow cancelling dialog
protected Color baseColor, highlightColor;
protected DataSource baseColorMapperSource, highlightColorMapperSource;
protected ColorMapper baseColorMapper, highlightColorMapper;
public PropertiesPanel(boolean showName) {
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
// init from outer
baseColor = ShapeNode.this.baseColor;
highlightColor = ShapeNode.this.highlightColor;
baseColorMapperSource = ShapeNode.this.baseColorMapperSource;
highlightColorMapperSource = ShapeNode.this.highlightColorMapperSource;
baseColorMapper = ShapeNode.this.baseColorMapper;
highlightColorMapper = ShapeNode.this.highlightColorMapper;
Box box = Box.createHorizontalBox();
box.add(new JLabel(NodeResourcesManager.resources.getString("Name")));
box.add(Box.createHorizontalGlue());
box.add(tfName = new JTextField(name));
if (showName) add(box);
box = Box.createHorizontalBox();
box.add(new JLabel(NodeResourcesManager.resources.getString("DisplayMode")));
box.add(Box.createHorizontalGlue());
box.add(rbShading = new JRadioButton(NodeResourcesManager.resources.getString("Shading"), mode==SHADING_MODE));
box.add(rbFlat = new JRadioButton(NodeResourcesManager.resources.getString("Flat"), mode==SHADING_MODE));
box.add(rbFilled = new JRadioButton(NodeResourcesManager.resources.getString("Filled"), mode==FILLED_MODE));
box.add(rbWireframe = new JRadioButton(NodeResourcesManager.resources.getString("Wireframe"), mode==WIREFRAME_MODE));
ButtonGroup bg = new ButtonGroup();
bg.add(rbShading);
bg.add(rbFlat);
bg.add(rbFilled);
bg.add(rbWireframe);
add(box);
box = Box.createHorizontalBox();
box.add(new JLabel(NodeResourcesManager.resources.getString("BaseColor")));
box.add(Box.createHorizontalGlue());
box.add(bBaseColor = new JButton(" "));
noColor = bBaseColor.getBackground();
bBaseColor.setFocusPainted(false);
bBaseColor.setBackground(baseColor);
add(box);
box = Box.createHorizontalBox();
box.add(new JLabel(NodeResourcesManager.resources.getString("HighlightColor")));
box.add(Box.createHorizontalGlue());
box.add(bHighlightColor = new JButton(" "));
bHighlightColor.setFocusPainted(false);
bHighlightColor.setBackground(highlightColor);
add(box);
bBaseColor.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
DynamicColorChooser dialog = new DynamicColorChooser(
new JDialog(),
NodeResourcesManager.resources.getString("SelectAColor"),
null,baseColor,baseColorMapperSource,baseColorMapper);
dialog.pack();
dialog.setVisible(true);
if (dialog.isOk()){
baseColor = dialog.getColor();
bBaseColor.setBackground(baseColor);
DataSource baseColorTempDataSource = dialog.getSource();
baseColorMapper = dialog.getMapper();
if (baseColorTempDataSource!=null){
if (baseColorMapperSource!=null)
baseColorMapperSource.removeEndNotificationListener(ShapeNode.this);
baseColorMapperSource = baseColorTempDataSource;
baseColorMapperSource.addEndNotificationListener(ShapeNode.this);
}
}
}
});
bHighlightColor.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
DynamicColorChooser dialog = new DynamicColorChooser(
new JDialog(),
NodeResourcesManager.resources.getString("SelectAColor"),
null,highlightColor,highlightColorMapperSource,highlightColorMapper);
if (dialog.isOk()){
highlightColor = dialog.getColor();
bHighlightColor.setBackground(highlightColor);
DataSource highlightColorTempDataSource = dialog.getSource();
highlightColorMapper = dialog.getMapper();
if (highlightColorTempDataSource!=null){
if (highlightColorMapperSource!=null)
highlightColorMapperSource.removeEndNotificationListener(ShapeNode.this);
highlightColorMapperSource = highlightColorTempDataSource;
highlightColorMapperSource.addEndNotificationListener(ShapeNode.this);
}
}
}
});
}
public void apply() {
setName(tfName.getText());
if (rbShading.isSelected()) mode = SHADING_MODE;
if (rbWireframe.isSelected()) mode = WIREFRAME_MODE;
if (rbFilled.isSelected()) mode = FILLED_MODE;
if (rbFlat.isSelected()) mode = FLAT_MODE;
ShapeNode.this.baseColor = baseColor;
ShapeNode.this.highlightColor = highlightColor;
ShapeNode.this.baseColorMapperSource = baseColorMapperSource;
ShapeNode.this.highlightColorMapperSource = highlightColorMapperSource;
ShapeNode.this.baseColorMapper = baseColorMapper;
ShapeNode.this.highlightColorMapper = highlightColorMapper;
}
}
public void notificationEnd(Object referer){
if (referer ==baseColorMapperSource){
Color c = null;
if (baseColorMapper!=null) {
c = (Color)baseColorMapper.getPaint(baseColorMapperSource);
if ( (c!=null) && !(c.equals(baseColor))){
baseColor = c;
notifyInternalChange();
}
}
}
else if (referer ==highlightColorMapperSource){
Color c = null;
if (highlightColor!=null) {
c = (Color)highlightColorMapper.getPaint(highlightColorMapperSource);
if ( (c!=null) && !(c.equals(highlightColor))){
highlightColor = c;
notifyInternalChange();
}
}
}
}
/**
* @return Returns the baseColor.
*/
public Color getBaseColor() {
return baseColor;
}
/**
* @param baseColor The baseColor to set.
*/
public void setBaseColor(Color baseColor) {
this.baseColor = baseColor;
notifyInternalChange();
}
/**
* @return Returns the highlightColor.
*/
public Color getHighlightColor() {
return highlightColor;
}
/**
* @param highlightColor The highlightColor to set.
*/
public void setHighlightColor(Color highlightColor) {
this.highlightColor = highlightColor;
notifyInternalChange();
}
/**
* @return Returns the mode.
*/
public int getMode() {
return mode;
}
/**
* @param mode The mode to set.
*/
public void setMode(int mode) {
this.mode = mode;
notifyInternalChange();
}
/**
* Utility method to set this shape properties to the same as another shape
*/
public void duplicateProperties(ShapeNode node) {
mode = node.mode;
baseColor = node.baseColor;
highlightColor = node.highlightColor;
baseColorMapperSource = node.baseColorMapperSource;
highlightColorMapperSource = node.highlightColorMapperSource;
baseColorMapper = node.baseColorMapper;
highlightColorMapper = node.highlightColorMapper;
// subclasses implementation may use the new values before propagation
notifyInternalChange();
}
private void writeObject(java.io.ObjectOutputStream out) throws java.io.IOException {
out.defaultWriteObject();
DataSourcePool.global.writeDataSource(out, baseColorMapperSource);
DataSourcePool.global.writeDataSource(out, highlightColorMapperSource);
}
private void readObject(java.io.ObjectInputStream in) throws java.lang.ClassNotFoundException, java.io.IOException {
in.defaultReadObject();
if (baseColorMapper != null) {
baseColorMapper = ColorMapper.updateColorMapper(baseColorMapper);
}
if (highlightColorMapper != null) {
highlightColorMapper = ColorMapper.updateColorMapper(highlightColorMapper);
}
baseColorMapperSource = DataSourcePool.global.readDataSource(in);
highlightColorMapperSource = DataSourcePool.global.readDataSource(in);
}
}