/* ========================
* 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-2003, by :
* Corporate:
* Astrium SAS
* EADS CRC
* Individual:
* Nicolas Brodu
* Jean-Baptiste Lièvremont
*
* $Id: PropertiesPanel1D.java,v 1.12 2008/09/29 08:59:59 ogor Exp $
*
* Changes
* -------
*
*/
package jsynoptic.builtin.ui;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.SwingConstants;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import jsynoptic.builtin.Abstract1DShape;
import jsynoptic.builtin.AffineTransformData;
import jsynoptic.builtin.Abstract1DShape.Abstract1DShapePropertiesNames;
import simtools.data.DataSource;
import simtools.diagram.DiagramParameters;
import simtools.shapes.StrokeParameters;
import simtools.ui.ColorMapper;
import simtools.ui.DynamicColorChooser;
import simtools.ui.JPropertiesPanel;
import simtools.ui.MenuResourceBundle;
import simtools.ui.NumberField;
import simtools.ui.GridBagPanel;
import simtools.ui.ResourceFinder;
import simtools.util.NamedProperties;
/**
* Properties panel for <code>Abstract1DShape</code>s
*
* @author Jean-Baptiste Lièvremont
* @see jsynoptic.builtin.Abstract1DShape
* @see simtools.ui.JPropertiesPanel
*/
public class PropertiesPanel1D extends JPropertiesPanel {
public static MenuResourceBundle resources = (MenuResourceBundle) ResourceFinder.get(Abstract1DShape.class);
protected JButton bdrawcolor;
protected Color noColor;
protected JCheckBox cbResize;
protected JCheckBox cbRatio;
protected JCheckBox cbdraw; // optional
protected JLabel lwidth, lheight, lthickness, lstroke, lTransformation;
protected JComboBox cbxdashes; // optional
protected JSpinner spithickness; // optional
protected SpinnerNumberModel spiModelThickness;
protected NumberField nfWidth, nfHeight;
protected Color drawColor;
protected ColorMapper drawMapper;
protected StrokeDisplay strokeDisplay;
protected DataSource drawMapperSource;
protected JButton bRemoveTransform;
protected JButton bEditTransform;
protected AffineTransformData transform;
protected JButton editTransformButton;
protected JButton delTransformButton;
protected GridBagPanel sizePanel;
protected GridBagPanel colorAndStrokePanel;
protected boolean drawEnabled;
public PropertiesPanel1D(String shapeName) {
this(true, false, shapeName);
}
public PropertiesPanel1D(boolean showResize, boolean drawOptional, String shapeName) {
this(showResize, drawOptional, true, shapeName);
}
public PropertiesPanel1D(boolean showResize, boolean drawOptional, boolean showStroke, String shapeName) {
this(showResize, drawOptional, showStroke, false, shapeName);
}
public PropertiesPanel1D(boolean showResize, boolean drawOptional, boolean showStroke, boolean showTransform,
String shapeName) {
super(shapeName);
colorAndStrokePanel = new GridBagPanel(resources.getString("ColorAndStroke"));
sizePanel = new GridBagPanel(resources.getString("Size"));
// Create contents
setColorPanel(drawOptional);
setStrokePanel(showStroke);
setSizePanel(showResize, showTransform);
addOnCurrentRow(colorAndStrokePanel);
if (showResize || showTransform) {
addOnCurrentRow(sizePanel);
}
carriageReturn();
}
protected void setColorPanel(boolean drawOptional) {
// Initialize our copy fields (allow users to cancel changes)
drawColor = Color.BLACK;
drawMapper = null;
drawMapperSource = null;
cbdraw = new JCheckBox(lineColorLabel(), true);
// display the stroke Customisation if cbdraw is selected
cbdraw.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
displayStrokeCustomisation(cbdraw.isSelected());
}
});
bdrawcolor = new JButton(" ");
noColor = bdrawcolor.getBackground();
bdrawcolor.setFocusPainted(false);
bdrawcolor.setBackground(drawColor);
bdrawcolor.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
DynamicColorChooser dialog = new DynamicColorChooser(
PropertiesPanel1D.this.getOwner(),
lineColorChooserTitle(),
null, drawColor, drawMapperSource, drawMapper);
dialog.pack();
dialog.setVisible(true);
if (dialog.isOk()){
drawColor = dialog.getColor();
bdrawcolor.setBackground(drawColor);
drawMapperSource = dialog.getSource();
drawMapper = dialog.getMapper();
if (cbdraw != null) {
cbdraw.setSelected(true);
displayStrokeCustomisation(true);
}
}
}
});
// Draw
if (drawOptional) {
colorAndStrokePanel.addOnCurrentRow(cbdraw);
} else {
colorAndStrokePanel.addOnCurrentRow(new JLabel(lineColorLabel()));
}
colorAndStrokePanel.addOnCurrentRow(bdrawcolor);
colorAndStrokePanel.carriageReturn();
}
protected void setStrokePanel(boolean showStroke) {
strokeDisplay = new StrokeDisplay();
cbxdashes = new JComboBox(StrokeDisplay.defaultstrokes);
cbxdashes.setRenderer(strokeDisplay);
lstroke = new JLabel(resources.getString("Stroke"));
lthickness = new JLabel(resources.getString("Thickness"));
spiModelThickness = new SpinnerNumberModel(1, 1, 5, 1);
spithickness = new JSpinner(spiModelThickness);
cbxdashes.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == cbxdashes) {
if (e.getActionCommand().equals("comboBoxChanged")) {
Object o = cbxdashes.getSelectedItem();
if (o instanceof StrokeParameters) {
strokeDisplay.setStrokeParams((StrokeParameters) o);
}
}
}
}
});
spithickness.addChangeListener((new ChangeListener() {
public void stateChanged(ChangeEvent e) {
if (e.getSource() == spithickness) {
strokeDisplay.getStrokeParams().setThickness(((Number) spithickness.getValue()).floatValue());
cbxdashes.repaint();
}
}
}));
// Stroke style
if (showStroke) {
colorAndStrokePanel.addOnCurrentRow(lstroke);
colorAndStrokePanel.addOnCurrentRow(cbxdashes);
colorAndStrokePanel.carriageReturn();
colorAndStrokePanel.addOnCurrentRow(lthickness);
colorAndStrokePanel.addOnCurrentRow(spithickness);
colorAndStrokePanel.carriageReturn();
}
}
/**
* Allow subclasses to overwrite this panel content easily
*
* @param sizePanel
* @param showResize
* @param showTransform
* @return
*/
protected void setSizePanel(boolean showResize, boolean showTransform) {
// Rezise properties
lwidth = new JLabel(resources.getString("Width"));
nfWidth = new NumberField(5);
nfWidth.setHorizontalAlignment(SwingConstants.RIGHT);
lheight = new JLabel(resources.getString("Height"));
nfHeight = new NumberField(5);
nfHeight.setHorizontalAlignment(SwingConstants.RIGHT);
cbResize = new JCheckBox(resources.getString("AllowResize"), false);
cbRatio = new JCheckBox(resources.getString("FixedRatio"), false);
// Transform properties
lTransformation = new JLabel(resources.getString("transformation"));
editTransformButton = resources.getButton("editTransform", new ActionListener() {
public void actionPerformed(ActionEvent e) {
NamedProperties res;
if (transform == null) {
res = AffineTransformDataPanel.showDialog(PropertiesPanel1D.this.getOwner(), resources
.getString("transformationTitle"), new AffineTransformData());
} else {
res = AffineTransformDataPanel.showDialog(PropertiesPanel1D.this.getOwner(), resources
.getString("transformationTitle"), transform);
}
if (res != null) {
if (transform == null) {
transform = new AffineTransformData();
}
String[] props = res.getPropertyNames();
if (props != null) {
for (int j = 0; j < props.length; j++) {
String pname = props[j];
Object value = res.getPropertyValue(pname);
transform.setPropertyValue(pname, value);
}
// Update transform with last related data
// source values
transform.notificationEnd(null);
}
}
delTransformButton.setEnabled(transform != null);
}
});
delTransformButton = resources.getButton("delTransform", new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (transform != null) {
transform.unsubscribeToDataNotifications();
}
delTransformButton.setEnabled(false);
transform = null;
}
});
if (showResize) {
sizePanel.addOnCurrentRow(lwidth);
sizePanel.addOnCurrentRow(nfWidth);
sizePanel.carriageReturn();
sizePanel.addOnCurrentRow(lheight);
sizePanel.addOnCurrentRow(nfHeight);
sizePanel.carriageReturn();
sizePanel.addOnCurrentRow(cbResize);
sizePanel.addOnCurrentRow(cbRatio);
sizePanel.carriageReturn();
}
if (showTransform) {
sizePanel.addOnCurrentRow(lTransformation);
sizePanel.addOnCurrentRow(editTransformButton);
sizePanel.addOnCurrentRow(delTransformButton);
sizePanel.carriageReturn();
}
}
/** Allow subclasses to override default text easily : text for the label */
protected String lineColorLabel() {
return resources.getString("LineColor");
}
/**
* Allow subclasses to override default text easily : text for the color
* chooser
*/
protected String lineColorChooserTitle() {
return resources.getString("ChooseLineColor");
}
protected void displayStrokeCustomisation(boolean display) {
drawEnabled = display;
spithickness.setEnabled(display);
cbxdashes.setEnabled(display);
lthickness.setEnabled(display);
lstroke.setEnabled(display);
bdrawcolor.setEnabled(display);
}
public String[] getPropertyNames() {
if (_propertyNames == null) {
_propertyNames = new Abstract1DShapePropertiesNames().getPropertyNames();
}
return _propertyNames;
}
/*
* (non-Javadoc)
*
* @see simtools.ui.JPropertiesPanel#getPropertyValue(java.lang.String)
*/
public Object getPropertyValue(String name) {
Object res = null;
if (name.equalsIgnoreCase("STROKE_COLOR")) {
res = ((cbdraw == null) || cbdraw.isSelected()) ? drawColor : null;
} else if (name.equalsIgnoreCase("ALLOW_RESIZE")) {
res = new Boolean(cbResize.isSelected());
} else if (name.equalsIgnoreCase("FIXED_RATIO")) {
res = new Boolean(cbRatio.isSelected());
} else if (name.equalsIgnoreCase("WIDTH")) {
res = new Integer((int) nfWidth.getLongValue());
} else if (name.equalsIgnoreCase("HEIGHT")) {
res = new Integer((int) nfHeight.getLongValue());
} else if (name.equalsIgnoreCase("DRAW_MAPPER")) {
res = drawMapper;
} else if (name.equalsIgnoreCase("DRAW_MAPPER_SOURCE")) {
res = drawMapperSource;
} else if (name.equalsIgnoreCase("STROKE_PARAMS")) {
StrokeParameters sp = (((StrokeDisplay) (cbxdashes.getSelectedItem())).getStrokeParams());
res = new StrokeParameters(sp);
((StrokeParameters) res).setThickness(((Number) spithickness.getValue()).floatValue());
} else if (name.equalsIgnoreCase("TRANSFORM")) {
res = transform;
}
return res;
}
/*
* (non-Javadoc)
*
* @see simtools.ui.JPropertiesPanel#setPropertyValue(java.lang.String,
* java.lang.Object)
*/
public void setPropertyValue(String name, Object value) {
if (name.equalsIgnoreCase("STROKE_COLOR")) {
if (value instanceof Color) {
drawColor = (Color) value;
if (cbdraw != null) {
cbdraw.setSelected(true);
}
} else {
drawColor = DiagramParameters.DEFAULT_COLOR;
if (cbdraw != null) {
cbdraw.setSelected(false);
}
}
displayStrokeCustomisation(cbdraw.isSelected());
bdrawcolor.setBackground(drawColor);
} else if (name.equalsIgnoreCase("ALLOW_RESIZE")) {
if (value instanceof Boolean) {
cbResize.setSelected(((Boolean) value).booleanValue());
}
} else if (name.equalsIgnoreCase("FIXED_RATIO")) {
if (value instanceof Boolean) {
cbRatio.setSelected(((Boolean) value).booleanValue());
}
} else if (name.equalsIgnoreCase("WIDTH")) {
if (value instanceof Integer) {
nfWidth.setValue(((Integer) value).intValue());
}
} else if (name.equalsIgnoreCase("HEIGHT")) {
if (value instanceof Integer) {
nfHeight.setValue(((Integer) value).intValue());
}
} else if (name.equalsIgnoreCase("DRAW_MAPPER")) {
if (value instanceof ColorMapper) {
drawMapper = (ColorMapper) value;
}
} else if (name.equalsIgnoreCase("DRAW_MAPPER_SOURCE")) {
if (value instanceof DataSource) {
drawMapperSource = (DataSource) value;
}
} else if (name.equalsIgnoreCase("STROKE_PARAMS")) {
if (value instanceof StrokeParameters) {
float thicknessFloatValue = ((StrokeParameters) value).thickness;
spithickness.setValue(new Integer((int) thicknessFloatValue));
for (int i = 0; i < cbxdashes.getItemCount(); i++) {
StrokeDisplay sd = (StrokeDisplay) cbxdashes.getItemAt(i);
if (sd.getStrokeParams().equalsIgnoreThickness(value)) {
cbxdashes.setSelectedIndex(i);
return;
}
}
StrokeDisplay nsd = new StrokeDisplay((StrokeParameters) value);
cbxdashes.addItem(nsd);
cbxdashes.setSelectedItem(nsd);
}
} else if (name.equalsIgnoreCase("TRANSFORM")) {
if (transform != null) {
transform.unsubscribeToDataNotifications();
transform = null;
}
if (value instanceof AffineTransformData) {
transform = (AffineTransformData) value;
}
if (delTransformButton != null) {
delTransformButton.setEnabled(transform != null);
}
}
}
}