/* ========================
* 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:
* EADS Astrium SAS
* EADS CRC
* Individual:
* Claude Cazenave
*
* $Id: PlotInformationDialog.java,v 1.10 2008/10/06 11:08:01 ogor Exp $
*
* Changes
* -------
* 10 mai 2006 : Initial public release (CC);
*
*/
package simtools.ui;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRootPane;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.border.TitledBorder;
import simtools.shapes.AbstractShape;
import simtools.shapes.PlotShape;
import simtools.shapes.ShapeListener;
/**
* A dialog box dedicated to gather information concerning plot curves.
* This information is computed on current X-axis interval.
*
* @author zxpletran007
*
*/
public class PlotInformationDialog implements ActionListener, ShapeListener{
protected JButton bok;
protected JDialog dialog=null;
protected PlotShape plotShape;
protected JTextField tfpxmin, tfpxmax,tfsxmin, tfsxmax;
double xmin, xmax;
protected HeaderPanel headerPanel;
protected CurveStatisticsPanel curveStatisticsPanel[];
protected static final Insets STANDARD_INSETS = new Insets(5, 5, 5, 5);
protected static final int FIELD_SIZE = 20;
public static MenuResourceBundle resources=ResourceFinder.getMenu(PlotInformationDialog.class);
public PlotInformationDialog(PlotShape ps){
plotShape = ps;
}
protected JPanel createButtonPanel(){
JPanel p=new JPanel();
bok = resources.getButton("OKButton", this);
p.add(bok);
return p;
}
/**
* @return JPanel containing global statistics related to plot
*/
protected JPanel createGlobalStatisticPanel(){
JPanel ret=new JPanel();
ret.setLayout(new GridBagLayout());
// Set gridBagLayout constriants
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = STANDARD_INSETS;
gbc.gridx = 0;
gbc.gridy = 0;
gbc.fill = GridBagConstraints.HORIZONTAL;
// primaryXaxis
ret.add(new JLabel(resources.getString("primaryXaxis")),gbc);
tfpxmin = new JTextField();
tfpxmin.setEditable(false);
tfpxmin.setColumns(FIELD_SIZE);
gbc.gridx++;
ret.add(tfpxmin,gbc);
tfpxmax = new JTextField();
tfpxmax.setEditable(false);
tfpxmax.setColumns(FIELD_SIZE);
gbc.gridx++;
ret.add(tfpxmax,gbc);
// secondaryXaxis
if(plotShape.getSecondaryXAxis() != null){
gbc.gridx = 0;
gbc.gridy++;
ret.add(new JLabel(resources.getString("secondaryXaxis")),gbc);
tfsxmin = new JTextField();
tfsxmin.setEditable(false);
tfsxmin.setColumns(FIELD_SIZE);
gbc.gridx++;
ret.add(tfsxmin,gbc);
tfsxmax = new JTextField();
tfsxmax.setEditable(false);
tfsxmax.setColumns(FIELD_SIZE);
gbc.gridx++;
ret.add(tfsxmax,gbc);
}
ret.setBorder(BorderFactory.createTitledBorder(null,resources.getString("globalStatistics"),TitledBorder.CENTER, TitledBorder.TOP, null));
plotShape.addListener(this);
return ret;
}
/**
* @return JPanel containing statistics related to all curves
*/
protected JPanel createCurveStatisticsPanel(){
JPanel ret=new JPanel();
ret.setBorder(BorderFactory.createTitledBorder(null,resources.getString("curvesStatistics"),TitledBorder.CENTER, TitledBorder.TOP, null));
ret.setLayout(new BoxLayout(ret,BoxLayout.Y_AXIS));
// Fisrt set xmin and xmax
upateGlobalStatistics();
// Then set curves statistics
curveStatisticsPanel = new CurveStatisticsPanel[plotShape.getCurveNumber()];
for(int i=0;i<curveStatisticsPanel.length;i++){
curveStatisticsPanel[i] = new CurveStatisticsPanel(plotShape.getCurve(i),this);
ret.add(curveStatisticsPanel[i]);
plotShape.addListener(curveStatisticsPanel[i]);
}
return ret;
}
public JDialog createDialog(Frame parent) {
if (dialog!=null){
return null;
}
dialog = new JDialog( parent, resources.getString("plotInformation"), false);
Container contentPane = dialog.getContentPane();
JTabbedPane informationPane = new JTabbedPane();
// Hearder panel
headerPanel = new HeaderPanel();
// Statistic tab
JPanel statistics2 = new JPanel(new BorderLayout());
JPanel statistics = new JPanel();
statistics.setBorder(BorderFactory.createEmptyBorder(0,10,10,10));
statistics.setLayout(new BoxLayout(statistics,BoxLayout.Y_AXIS));
JPanel p1 = new JPanel(new BorderLayout());
JLabel statTitle = new JLabel(resources.getStringValue("plotStatistics"));
statTitle.setFont(new Font("Dialog", Font.PLAIN,18));
p1.add(statTitle, BorderLayout.WEST);
statistics.add(p1);
statistics.add(createGlobalStatisticPanel());
statistics.add(createCurveStatisticsPanel());
statistics2.add(statistics, BorderLayout.NORTH);
informationPane.addTab(resources.getString("plotStatistics"),
new JScrollPane(statistics2, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER ));
contentPane.add(headerPanel, BorderLayout.NORTH);
contentPane.add(informationPane, BorderLayout.CENTER);
contentPane.add(createButtonPanel(), BorderLayout.SOUTH);
if (JDialog.isDefaultLookAndFeelDecorated()) {
boolean supportsWindowDecorations =
UIManager.getLookAndFeel().getSupportsWindowDecorations();
if (supportsWindowDecorations) {
dialog.setUndecorated(true);
dialog.getRootPane().setWindowDecorationStyle(JRootPane.QUESTION_DIALOG);
}
}
dialog.pack();
dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
dialog.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
}
});
dialog.setLocationRelativeTo(parent);
dialog.setModal(false);
return dialog;
}
/* (non-Javadoc)
* @see simtools.shapes.ShapeListener#shapeChanged(simtools.shapes.AbstractShape, java.awt.Rectangle)
*/
public void shapeChanged(AbstractShape shape, Rectangle changedArea){
upateGlobalStatistics();
}
protected void upateGlobalStatistics(){
// TODO look after secondadry X axe
xmin = plotShape.getPrimaryXAxis().getMin();
xmax = plotShape.getPrimaryXAxis().getMax();
tfpxmin.setText("Min = " + xmin);
tfpxmax.setText("Max = " + xmax);
if(plotShape.getSecondaryXAxis() != null){
double sxmin = plotShape.getSecondaryXAxis().getMin();
double sxmax = plotShape.getSecondaryXAxis().getMax();
tfsxmin.setText("Min = " + sxmin);
tfsxmax.setText("Max = " + sxmax);
}
}
/* (non-Javadoc)
* @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
*/
public void actionPerformed(ActionEvent e) {
if(e.getSource()==bok){
dispose();
}
}
/**
* When closing dialog box, unsubscribe to all notifications from vurves, plots...
*/
protected void dispose(){
plotShape.removeListener(this);
for(int i=0;i<curveStatisticsPanel.length;i++){
curveStatisticsPanel[i].dispose();
plotShape.removeListener(curveStatisticsPanel[i]);
}
dialog.dispose();
}
public String toString(){
String ret = tfpxmin.getText() + "\n" + tfpxmax.getText() + "\n";
for(int i=0;i<curveStatisticsPanel.length;i++){
ret += curveStatisticsPanel[i].toString();
}
return ret;
}
public double getXmax() {
return xmax;
}
public double getXmin() {
return xmin;
}
public HeaderPanel getHeaderPanel() {
return headerPanel;
}
}