/*
 *  Draco Econometrics
 *  Copyright (C) 2006-2007 Approximatrix, LLC
 *  http://www.approximatrix.com
 *  
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.approximatrix.draco.plot.ui;

import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.FlowLayout;
import javax.swing.JTabbedPane;
import javax.swing.JLabel;
import java.awt.GridBagLayout;
import javax.swing.JCheckBox;
import java.awt.GridBagConstraints;
import java.util.Iterator;
import java.util.Map;

import com.approximatrix.charting.Title;
import com.approximatrix.charting.render.*;
import com.approximatrix.charting.swing.*;
import com.approximatrix.draco.plot.ui.proppanels.BarPropPanel;
import com.approximatrix.draco.plot.ui.proppanels.SeriesPropPanel;
import com.approximatrix.draco.application.ChildFrame;

import javax.swing.JTextField;
import javax.swing.event.*;
/*
 * Created on Jan 26, 2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */

/**
 * @author armstrongj
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class PlotProperties extends JDialog {

	private static final int PLOT_TYPE_BAR = 1;
	private static final int PLOT_TYPE_SCATTER = 2;
	
	private javax.swing.JPanel jContentPane = null;

	private JPanel jPanel = null;
	private JButton close_plot_props_button = null;
	private JTabbedPane props_tabbed_pane = null;
	private JPanel series_properties = null;
	private JPanel bar_properties = null;
	
	private BufferedChartPanel plot_panel = null;
	
	private JPanel graph_properties = null;
	private JLabel jLabel1 = null;
	private JTextField title_textfield = null;
	private JLabel jLabel2 = null;
	private JTextField xaxis_label_textfield = null;
	private JLabel jLabel3 = null;
	private JTextField yaxis_label_textfield = null;
	private JPanel scale_properties = null;
	private JPanel xaxis_panel = null;
	private JPanel yaxis_panel = null;
	private JLabel jLabel4 = null;
	private JLabel jLabel5 = null;
	private JTextField x_min_value = null;
	private JTextField x_max_value = null;
	private JLabel jLabel6 = null;
	private JLabel jLabel7 = null;
	private JTextField y_min_value = null;
	private JTextField y_max_value = null;
	private JCheckBox plot_autoscale_checkbox = null;
	private JButton apply_scaling_button = null;
	
	/**
	 * This is the default constructor
	 */
	public PlotProperties(PlotDialog parent) {
		super(parent.getMainWindow(),true);
		
		plot_panel = parent.getPlotter().getPlotPanel();
		
		initialize();
		
		title_textfield.setText(plot_panel.getTitle().getText());
		xaxis_label_textfield.setText(plot_panel.getCoordSystem().getXAxisUnit());
		yaxis_label_textfield.setText(plot_panel.getCoordSystem().getYAxisUnit());
		
		if(plot_autoscale_checkbox.isSelected()) {
			xaxis_panel.setVisible(true);
			yaxis_panel.setVisible(true);
			apply_scaling_button.setVisible(true);
		}
		
		if(plot_autoscale_checkbox.isSelected() ||
				(plot_panel.getChartDataModel().getManualMaximumValue() == null &&
				 plot_panel.getChartDataModel().getManualMinimumValue() == null)) {
			//System.out.println("Auto");
			
			y_max_value.setText(new Double(getMaxYValue()).toString());
			y_min_value.setText(new Double(getMinYValue()).toString());
			
			x_min_value.setText(new Double(getMinXValue()).toString());
			x_max_value.setText(new Double(getMaxXValue()).toString());
			
			xaxis_panel.setVisible(false);
			yaxis_panel.setVisible(false);
			apply_scaling_button.setVisible(false);
		} else {
			xaxis_panel.setVisible(true);
			yaxis_panel.setVisible(true);
			apply_scaling_button.setVisible(true);
			
			Number reset = plot_panel.getChartDataModel().getManualMaximumValue();
			if(reset != null)
				y_max_value.setText(reset.toString());
			else
				y_max_value.setText("0");
			
			reset = plot_panel.getChartDataModel().getManualMinimumValue();
			if(reset != null)
				y_min_value.setText(reset.toString());
			else 
				y_min_value.setText("0");
			
			double value = plot_panel.getChartDataModel().getManualMaximumColumnValue();
			x_max_value.setText(new Double(value).toString());
			
			value = plot_panel.getChartDataModel().getManualMinimumColumnValue();
			x_min_value.setText(new Double(value).toString());
		}
		
		xaxis_panel.doLayout();
		yaxis_panel.doLayout();
		getScale_properties().doLayout();
		
		this.setVisible(true);
	}
	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
		this.setTitle("Plot Properties");
		this.setSize(355, 286);
		this.setContentPane(getJContentPane());
		this.setLocationRelativeTo(plot_panel);
	}
	
	private int getPlotType() {
		Map renderers = plot_panel.getChartRenderer();
        for(Iterator<AbstractChartRenderer>iter = renderers.values().iterator();iter.hasNext();) {
        	AbstractChartRenderer now = iter.next();
        	if(now == null) {
        		continue;
        	} else if(now.getClass().equals(MultiScatterChartRenderer.class)) {
        		return PLOT_TYPE_SCATTER;	
        	} else if(now.getClass().equals(BarChartRenderer.class)) {
        		return PLOT_TYPE_BAR;
        	}
        }
        return -1;
	}
	
	
	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private javax.swing.JPanel getJContentPane() {
		if(jContentPane == null) {
			jContentPane = new javax.swing.JPanel();
			jContentPane.setLayout(new java.awt.BorderLayout());
			jContentPane.add(getJPanel(), java.awt.BorderLayout.SOUTH);
			jContentPane.add(getProps_tabbed_pane(), java.awt.BorderLayout.CENTER);
		}
		return jContentPane;
	}
	/**
	 * This method initializes jPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */    
	private JPanel getJPanel() {
		if (jPanel == null) {
			java.awt.FlowLayout flowLayout2 = new FlowLayout();
			jPanel = new JPanel();
			jPanel.setLayout(flowLayout2);
			flowLayout2.setAlignment(java.awt.FlowLayout.RIGHT);
			jPanel.add(getClose_plot_props_button(), null);
		}
		return jPanel;
	}
	/**
	 * This method initializes jButton	
	 * 	
	 * @return javax.swing.JButton	
	 */    
	private JButton getClose_plot_props_button() {
		if (close_plot_props_button == null) {
			close_plot_props_button = new JButton();
			close_plot_props_button.setText("Close");
			close_plot_props_button.addMouseListener(new java.awt.event.MouseAdapter() { 
				public void mouseClicked(java.awt.event.MouseEvent e) {    
					dispose();
				}
			});
		}
		return close_plot_props_button;
	}
	/**
	 * This method initializes jTabbedPane	
	 * 	
	 * @return javax.swing.JTabbedPane	
	 */    
	private JTabbedPane getProps_tabbed_pane() {
		if (props_tabbed_pane == null) {
			props_tabbed_pane = new JTabbedPane();
			
			if(getPlotType() == PLOT_TYPE_SCATTER) {
				props_tabbed_pane.addTab("Series Properties", null, getSeries_properties(), null);
			} else if(getPlotType() == PLOT_TYPE_BAR) {
				props_tabbed_pane.addTab("Series Properties", null, getBar_properties(), null);
			}
			
			props_tabbed_pane.addTab("Graph Properties", null, getGraph_properties(), null);
			props_tabbed_pane.addTab("Scaling", null, getScale_properties(), null);
		}
		return props_tabbed_pane;
	}
	
	private JPanel getBar_properties() {
		if(bar_properties == null) {
			bar_properties = new BarPropPanel(plot_panel);
		}
		return bar_properties;
	}
	
	/**
	 * This method initializes jPanel1	
	 * 	
	 * @return javax.swing.JPanel	
	 */    
	private JPanel getSeries_properties() {
		if (series_properties == null) {
			series_properties = new SeriesPropPanel(plot_panel);
		}
		return series_properties;
	}
	
	/**
	 * This method initializes jPanel3	
	 * 	
	 * @return javax.swing.JPanel	
	 */    
	private JPanel getGraph_properties() {
		if (graph_properties == null) {
			jLabel3 = new JLabel();
			jLabel2 = new JLabel();
			jLabel1 = new JLabel();
			GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
			GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
			GridBagConstraints gridBagConstraints31 = new GridBagConstraints();
			GridBagConstraints gridBagConstraints41 = new GridBagConstraints();
			GridBagConstraints gridBagConstraints5 = new GridBagConstraints();
			GridBagConstraints gridBagConstraints6 = new GridBagConstraints();
			graph_properties = new JPanel();
			graph_properties.setLayout(new GridBagLayout());
			gridBagConstraints1.gridx = 0;
			gridBagConstraints1.gridy = 0;
			jLabel1.setText("Graph Title:");
			gridBagConstraints2.gridx = 1;
			gridBagConstraints2.gridy = 0;
			gridBagConstraints2.weightx = 1.0;
			gridBagConstraints2.fill = java.awt.GridBagConstraints.HORIZONTAL;
			gridBagConstraints31.gridx = 0;
			gridBagConstraints31.gridy = 1;
			jLabel2.setText("X Axis Label:");
			gridBagConstraints41.gridx = 1;
			gridBagConstraints41.gridy = 1;
			gridBagConstraints41.weightx = 1.0;
			gridBagConstraints41.fill = java.awt.GridBagConstraints.HORIZONTAL;
			gridBagConstraints5.gridx = 0;
			gridBagConstraints5.gridy = 2;
			jLabel3.setText("Y Axis Label:");
			gridBagConstraints6.gridx = 1;
			gridBagConstraints6.gridy = 2;
			gridBagConstraints6.weightx = 1.0;
			gridBagConstraints6.fill = java.awt.GridBagConstraints.HORIZONTAL;
			graph_properties.add(jLabel1, gridBagConstraints1);
			graph_properties.add(getTitle_textfield(), gridBagConstraints2);
			graph_properties.add(jLabel2, gridBagConstraints31);
			graph_properties.add(getXaxis_label_textfield(), gridBagConstraints41);
			graph_properties.add(jLabel3, gridBagConstraints5);
			graph_properties.add(getYaxis_label_textfield(), gridBagConstraints6);
		}
		return graph_properties;
	}
	/**
	 * This method initializes jTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */    
	private JTextField getTitle_textfield() {
		if (title_textfield == null) {
			title_textfield = new JTextField();
			title_textfield.getDocument().addDocumentListener(new DocumentListener() {
				public void changedUpdate(DocumentEvent e) {
					plot_panel.setTitle(new Title(title_textfield.getText()));
					plot_panel.repaint();
				}
				public void removeUpdate(DocumentEvent e) {
					plot_panel.setTitle(new Title(title_textfield.getText()));
					plot_panel.repaint();
				}
				public void insertUpdate(DocumentEvent e) {
					plot_panel.setTitle(new Title(title_textfield.getText()));
					plot_panel.repaint();
				}
			});
		}
		return title_textfield;
	}
	/**
	 * This method initializes jTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */    
	private JTextField getXaxis_label_textfield() {
		if (xaxis_label_textfield == null) {
			xaxis_label_textfield = new JTextField();
			xaxis_label_textfield.getDocument().addDocumentListener(new DocumentListener() {
				public void changedUpdate(DocumentEvent e) {
					plot_panel.getCoordSystem().setXAxisUnit(xaxis_label_textfield.getText());
					plot_panel.repaint();
				}
				public void removeUpdate(DocumentEvent e) {
					plot_panel.getCoordSystem().setXAxisUnit(xaxis_label_textfield.getText());
					plot_panel.repaint();
				}
				public void insertUpdate(DocumentEvent e) {
					plot_panel.getCoordSystem().setXAxisUnit(xaxis_label_textfield.getText());
					plot_panel.repaint();
				}
			});
		}
		return xaxis_label_textfield;
	}
	/**
	 * This method initializes jTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */    
	private JTextField getYaxis_label_textfield() {
		if (yaxis_label_textfield == null) {
			yaxis_label_textfield = new JTextField();
			yaxis_label_textfield.getDocument().addDocumentListener(new DocumentListener() {
				public void changedUpdate(DocumentEvent e) {
					plot_panel.getCoordSystem().setYAxisUnit(yaxis_label_textfield.getText());
					plot_panel.repaint();
				}
				public void removeUpdate(DocumentEvent e) {
					plot_panel.getCoordSystem().setYAxisUnit(yaxis_label_textfield.getText());
					plot_panel.repaint();
				}
				public void insertUpdate(DocumentEvent e) {
					plot_panel.getCoordSystem().setYAxisUnit(yaxis_label_textfield.getText());
					plot_panel.repaint();
				}
			});
		}
		return yaxis_label_textfield;
	}
	
	/**
	 * This method initializes jPanel3	
	 * 	
	 * @return javax.swing.JPanel	
	 */    
	private JPanel getScale_properties() {
		if (scale_properties == null) {
			GridBagConstraints gridBagConstraints14 = new GridBagConstraints();
			GridBagConstraints gridBagConstraints13 = new GridBagConstraints();
			GridBagConstraints gridBagConstraints12 = new GridBagConstraints();
			GridBagConstraints gridBagConstraints11 = new GridBagConstraints();
			GridBagConstraints gridBagConstraints10 = new GridBagConstraints();
			scale_properties = new JPanel();
			scale_properties.setLayout(new GridBagLayout());
			gridBagConstraints10.gridx = 0;
			gridBagConstraints10.gridy = 0;
			gridBagConstraints10.ipadx = 0;
			gridBagConstraints10.ipady = 0;
			gridBagConstraints10.insets = new java.awt.Insets(0,0,2,0);
			gridBagConstraints11.gridx = 0;
			gridBagConstraints11.gridy = 1;
			gridBagConstraints11.ipadx = 229;
			gridBagConstraints11.ipady = -7;
			gridBagConstraints11.insets = new java.awt.Insets(3,0,2,0);
			gridBagConstraints11.gridheight = 1;
			gridBagConstraints11.fill = java.awt.GridBagConstraints.VERTICAL;
			gridBagConstraints12.gridx = 0;
			gridBagConstraints12.gridy = 2;
			gridBagConstraints12.ipadx = 229;
			gridBagConstraints12.ipady = -7;
			gridBagConstraints12.insets = new java.awt.Insets(3,0,2,0);
			gridBagConstraints12.fill = java.awt.GridBagConstraints.VERTICAL;
			gridBagConstraints13.gridx = 0;
			gridBagConstraints13.gridy = 3;
			gridBagConstraints14.gridx = 0;
			gridBagConstraints14.gridy = 3;
			scale_properties.add(getPlot_autoscale_checkbox(), gridBagConstraints10);
			scale_properties.add(getXaxis_panel(), gridBagConstraints11);
			scale_properties.add(getYaxis_panel(), gridBagConstraints12);
			scale_properties.add(getApply_scaling_button(), gridBagConstraints14);
			scale_properties.add(getApply_scaling_button(), gridBagConstraints13);
		}
		return scale_properties;
	}
	/**
	 * This method initializes jPanel3	
	 * 	
	 * @return javax.swing.JPanel	
	 */    
	private JPanel getXaxis_panel() {
		if (xaxis_panel == null) {
			jLabel5 = new JLabel();
			jLabel4 = new JLabel();
			GridBagConstraints gridBagConstraints21 = new GridBagConstraints();
			GridBagConstraints gridBagConstraints32 = new GridBagConstraints();
			GridBagConstraints gridBagConstraints42 = new GridBagConstraints();
			GridBagConstraints gridBagConstraints51 = new GridBagConstraints();
			xaxis_panel = new JPanel();
			xaxis_panel.setLayout(new GridBagLayout());
			xaxis_panel.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.LOWERED), "X Axis", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, null, null));
			xaxis_panel.setPreferredSize(new java.awt.Dimension(113,80));
			gridBagConstraints21.gridx = 0;
			gridBagConstraints21.gridy = 0;
			gridBagConstraints21.anchor = java.awt.GridBagConstraints.WEST;
			jLabel4.setText("Minimum Value: ");
			gridBagConstraints32.gridx = 0;
			gridBagConstraints32.gridy = 1;
			gridBagConstraints32.anchor = java.awt.GridBagConstraints.WEST;
			jLabel5.setText("Maximum Value: ");
			gridBagConstraints42.gridx = 1;
			gridBagConstraints42.gridy = 0;
			gridBagConstraints42.weightx = 1.0;
			gridBagConstraints42.fill = java.awt.GridBagConstraints.HORIZONTAL;
			gridBagConstraints51.gridx = 1;
			gridBagConstraints51.gridy = 1;
			gridBagConstraints51.weightx = 1.0;
			gridBagConstraints51.fill = java.awt.GridBagConstraints.HORIZONTAL;
			xaxis_panel.add(jLabel4, gridBagConstraints21);
			xaxis_panel.add(jLabel5, gridBagConstraints32);
			xaxis_panel.add(getX_min_value(), gridBagConstraints42);
			xaxis_panel.add(getX_max_value(), gridBagConstraints51);
		}
		return xaxis_panel;
	}
	/**
	 * This method initializes jPanel4	
	 * 	
	 * @return javax.swing.JPanel	
	 */    
	private JPanel getYaxis_panel() {
		if (yaxis_panel == null) {
			jLabel7 = new JLabel();
			jLabel6 = new JLabel();
			GridBagConstraints gridBagConstraints61 = new GridBagConstraints();
			GridBagConstraints gridBagConstraints7 = new GridBagConstraints();
			GridBagConstraints gridBagConstraints8 = new GridBagConstraints();
			GridBagConstraints gridBagConstraints9 = new GridBagConstraints();
			yaxis_panel = new JPanel();
			yaxis_panel.setLayout(new GridBagLayout());
			yaxis_panel.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.LOWERED), "Y Axis", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, null, null));
			gridBagConstraints61.gridx = 0;
			gridBagConstraints61.gridy = 0;
			gridBagConstraints61.anchor = java.awt.GridBagConstraints.WEST;
			jLabel6.setText("Minimum Value: ");
			gridBagConstraints7.gridx = 0;
			gridBagConstraints7.gridy = 1;
			gridBagConstraints7.anchor = java.awt.GridBagConstraints.WEST;
			jLabel7.setText("Maximum Value: ");
			gridBagConstraints8.gridx = 1;
			gridBagConstraints8.gridy = 0;
			gridBagConstraints8.weightx = 1.0;
			gridBagConstraints8.fill = java.awt.GridBagConstraints.HORIZONTAL;
			gridBagConstraints9.gridx = 1;
			gridBagConstraints9.gridy = 1;
			gridBagConstraints9.weightx = 1.0;
			gridBagConstraints9.fill = java.awt.GridBagConstraints.HORIZONTAL;
			yaxis_panel.add(jLabel6, gridBagConstraints61);
			yaxis_panel.add(jLabel7, gridBagConstraints7);
			yaxis_panel.add(getY_min_value(), gridBagConstraints8);
			yaxis_panel.add(getY_max_value(), gridBagConstraints9);
		}
		return yaxis_panel;
	}
	/**
	 * This method initializes jTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */    
	private JTextField getX_min_value() {
		if (x_min_value == null) {
			x_min_value = new JTextField();
		}
		return x_min_value;
	}
	/**
	 * This method initializes jTextField1	
	 * 	
	 * @return javax.swing.JTextField	
	 */    
	private JTextField getX_max_value() {
		if (x_max_value == null) {
			x_max_value = new JTextField();
		}
		return x_max_value;
	}
	/**
	 * This method initializes jTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */    
	private JTextField getY_min_value() {
		if (y_min_value == null) {
			y_min_value = new JTextField();
		}
		return y_min_value;
	}
	/**
	 * This method initializes jTextField1	
	 * 	
	 * @return javax.swing.JTextField	
	 */    
	private JTextField getY_max_value() {
		if (y_max_value == null) {
			y_max_value = new JTextField();
		}
		return y_max_value;
	}
	/**
	 * This method initializes jCheckBox	
	 * 	
	 * @return javax.swing.JCheckBox	
	 */    
	private JCheckBox getPlot_autoscale_checkbox() {
		if (plot_autoscale_checkbox == null) {
			plot_autoscale_checkbox = new JCheckBox();
			plot_autoscale_checkbox.setText("Enable Plot Autoscaling");
			//plot_autoscale_checkbox.setSelected(true);
			
			plot_autoscale_checkbox.setSelected(plot_panel.getChartDataModel().isAutoScale() && 
                    							! plot_panel.getChartDataModel().isManualScale());
			
			plot_autoscale_checkbox.addItemListener(new java.awt.event.ItemListener() { 
				public void itemStateChanged(java.awt.event.ItemEvent e) {    
					if(plot_autoscale_checkbox.isSelected()) {
						plot_panel.getChartDataModel().setAutoScale(true);
						plot_panel.getChartDataModel().setManualScale(false);
						xaxis_panel.setVisible(false);
						yaxis_panel.setVisible(false);
						apply_scaling_button.setVisible(false);
					} else {
						plot_panel.getChartDataModel().setAutoScale(false);
						plot_panel.getChartDataModel().setManualScale(true);
						
						xaxis_panel.setVisible(true);
						yaxis_panel.setVisible(true);
						apply_scaling_button.setVisible(true);
						
						rescale();
					}
					plot_panel.repaint();
				}
			});
		}
		return plot_autoscale_checkbox;
	}

	public double getMaxXValue() {
		double running_value = Double.MIN_VALUE;
		Object this_val;
		
		for(int set=0;set<plot_panel.getChartDataModel().getDataSetNumber();set++) {
			int i=0;
			while((this_val=plot_panel.getChartDataModel().getColumnValueAt(set,i)) != null) {
				if(plot_panel.getChartDataModel().isColumnNumeric()) {
					if(running_value < ((Number)this_val).doubleValue()) {
						running_value = ((Number)this_val).doubleValue();
					}
				}
				i++;
				if(i>=plot_panel.getChartDataModel().getDataSetLength(set)) break;
			}
		}
		return running_value;
	}
	
	public double getMinXValue() {
		double running_value = Double.MAX_VALUE;
		Object this_val;
		
		for(int set=0;set<plot_panel.getChartDataModel().getDataSetNumber();set++) {
			int i=0;
			while((this_val=plot_panel.getChartDataModel().getColumnValueAt(set,i)) != null) {
				if(plot_panel.getChartDataModel().isColumnNumeric()) {
					if(running_value > ((Number)this_val).doubleValue()) {
						running_value = ((Number)this_val).doubleValue();
					}
				}
				i++;
				if(i>=plot_panel.getChartDataModel().getDataSetLength(set)) break;
			}
		}
		return running_value;
	}
	
	public double getMaxYValue() {
		double running_value = Double.MIN_VALUE;
		for(int i=0;i<plot_panel.getChartDataModel().getDataSetNumber();i++) {
			for(int j=0;j<plot_panel.getChartDataModel().getDataSetLength(i);j++){
				Number this_val = plot_panel.getChartDataModel().getValueAt(i,j);
				if(this_val != null) {
					if(running_value < this_val.doubleValue()) {
						running_value = this_val.doubleValue();
					}
				}
			}
			
		}
		return running_value;
	}
	
	public double getMinYValue() {
		double running_value = Double.MAX_VALUE;
		for(int i=0;i<plot_panel.getChartDataModel().getDataSetNumber();i++) {
			for(int j=0;j<plot_panel.getChartDataModel().getDataSetLength(i);j++){
				Number this_val = plot_panel.getChartDataModel().getValueAt(i,j);
				if(this_val != null) {
					if(running_value > this_val.doubleValue()) {
						running_value = this_val.doubleValue();
					}
				}
			}
			
		}
		return running_value;
	}

	/**
	 * This method initializes jButton	
	 * 	
	 * @return javax.swing.JButton	
	 */    
	private JButton getApply_scaling_button() {
		if (apply_scaling_button == null) {
			apply_scaling_button = new JButton();
			apply_scaling_button.setText("Apply Scaling");
			apply_scaling_button.addActionListener(new java.awt.event.ActionListener() { 
				public void actionPerformed(java.awt.event.ActionEvent e) {    
					
					rescale();
					
					plot_panel.repaint();
					int i = 0;
					AbstractChartRenderer acr = plot_panel.getChartRenderer(i);
					while(acr == null) {
						i++;
						acr = plot_panel.getChartRenderer(i);
					}
				}
			});
		}
		return apply_scaling_button;
	}
	
	private void rescale() {
		try {
			double reset = new Double(y_min_value.getText()).doubleValue();
			plot_panel.getChartDataModel().setMinimumValue(new Double(reset));
		} catch (NumberFormatException nfe) {
			Number reset = plot_panel.getChartDataModel().getManualMinimumValue();
			if(reset != null)
				y_min_value.setText(reset.toString());
			else
				y_min_value.setText("0.0");
		}
		
		try {
			double reset = new Double(y_max_value.getText()).doubleValue();
			plot_panel.getChartDataModel().setMaximumValue(new Double(reset));
		} catch (NumberFormatException nfe) {
			Number reset = plot_panel.getChartDataModel().getManualMinimumValue();
			if(reset != null)
				y_max_value.setText(reset.toString());
			else
				y_max_value.setText("0.0");
		}
		
		try {
			double reset = new Double(x_max_value.getText()).doubleValue();
			plot_panel.getChartDataModel().setMaximumColumnValue(new Double(reset).doubleValue());
		} catch (NumberFormatException nfe) {
			Number reset = plot_panel.getChartDataModel().getManualMinimumValue();
			if(reset != null)
				x_max_value.setText(reset.toString());
			else
				x_max_value.setText("0.0");
		}
		
		try {
			double reset = new Double(x_min_value.getText()).doubleValue();
			plot_panel.getChartDataModel().setMinimumColumnValue(new Double(reset).doubleValue());
		} catch (NumberFormatException nfe) {
			Number reset = plot_panel.getChartDataModel().getManualMinimumValue();
			if(reset != null)
				x_min_value.setText(reset.toString());
			else
				x_min_value.setText("0.0");
		}
	}
	
}  //  @jve:decl-index=0:visual-constraint="10,10"
