/*
 *  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.fit.ui;

import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JDialog;
import java.awt.GridBagLayout;
import java.awt.Dimension;
import javax.swing.JLabel;
import java.awt.GridBagConstraints;
import javax.swing.JTextField;
import java.awt.Insets;

public class IterationControl extends JPanel {

	private static int NumberIterations = 1000;
	private static double Tolerance = 0.001;
	private static double Relaxation = 1.0;
	
	private JPanel jContentPane = null;
	private JLabel jLabel = null;
	private JTextField jIterationsTextField = null;
	private JLabel jLabel1 = null;
	private JTextField jToleranceTextField = null;
	private JLabel jLabel2 = null;
	private JTextField jRelaxationTextField = null;

	/** Static function for obtaining the fit iterations default
	 * 
	 * @return the default number of iterations to perform
	 */
	public static int GetDefaultIterations() {
		return NumberIterations; 
	}
	
	/** Static function fo obtaining the fit tolerance default
	 * 
	 * @return the default absolute tolerance of the fitting procedure
	 */
	public static double GetDefaultTolerance() {
		return Tolerance;
	}
	
	/** Static function for obtaining the relaxation factor default
	 * 
	 * @return the default relaxation factor for updating parameters during fitting
	 */
	public static double GetDefaultRelaxation() {
		return Relaxation;
	}
	
	/**
	 * This is the default constructor
	 */
	public IterationControl() {
		super();
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		GridBagConstraints gridBagConstraints21 = new GridBagConstraints();
		gridBagConstraints21.fill = GridBagConstraints.HORIZONTAL;
		gridBagConstraints21.gridy = 2;
		gridBagConstraints21.weightx = 1.0;
		gridBagConstraints21.insets = new Insets(3, 3, 3, 3);
		gridBagConstraints21.ipadx = 3;
		gridBagConstraints21.ipady = 3;
		gridBagConstraints21.gridx = 1;
		GridBagConstraints gridBagConstraints11 = new GridBagConstraints();
		gridBagConstraints11.gridx = 0;
		gridBagConstraints11.insets = new Insets(3, 3, 3, 3);
		gridBagConstraints11.ipadx = 3;
		gridBagConstraints11.ipady = 3;
		gridBagConstraints11.anchor = GridBagConstraints.WEST;
		gridBagConstraints11.gridy = 2;
		jLabel2 = new JLabel();
		jLabel2.setText("Relaxation Factor:");
		//this.setSize(200, 90);
		//this.setPreferredSize(new Dimension(200, 90));
		
		getJIterationsTextField().setText(Integer.toString(NumberIterations));
		getJToleranceTextField().setText(Double.toString(Tolerance));
		getJRelaxationTextField().setText(Double.toString(Relaxation));
		
		GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
		gridBagConstraints3.fill = java.awt.GridBagConstraints.HORIZONTAL;
		gridBagConstraints3.gridy = 1;
		gridBagConstraints3.weightx = 1.0;
		gridBagConstraints3.ipadx = 3;
		gridBagConstraints3.ipady = 3;
		gridBagConstraints3.insets = new java.awt.Insets(3,3,3,3);
		gridBagConstraints3.gridx = 1;
		GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
		gridBagConstraints2.gridx = 0;
		gridBagConstraints2.anchor = java.awt.GridBagConstraints.WEST;
		gridBagConstraints2.ipadx = 3;
		gridBagConstraints2.ipady = 3;
		gridBagConstraints2.insets = new java.awt.Insets(3,3,3,3);
		gridBagConstraints2.gridy = 1;
		jLabel1 = new JLabel();
		jLabel1.setText("Absolute Tolerance:");
		GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
		gridBagConstraints1.fill = java.awt.GridBagConstraints.HORIZONTAL;
		gridBagConstraints1.gridy = 0;
		gridBagConstraints1.weightx = 1.0;
		gridBagConstraints1.ipadx = 3;
		gridBagConstraints1.ipady = 3;
		gridBagConstraints1.insets = new java.awt.Insets(3,3,3,3);
		gridBagConstraints1.gridx = 1;
		GridBagConstraints gridBagConstraints = new GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
		gridBagConstraints.ipadx = 3;
		gridBagConstraints.ipady = 3;
		gridBagConstraints.gridheight = 1;
		gridBagConstraints.insets = new java.awt.Insets(3,3,3,3);
		gridBagConstraints.gridy = 0;
		jLabel = new JLabel();
		jLabel.setText("Maximum Iterations: ");
		this.setLayout(new GridBagLayout());
		this.add(jLabel, gridBagConstraints);
		this.add(getJIterationsTextField(), gridBagConstraints1);
		this.add(jLabel1, gridBagConstraints2);
		this.add(getJToleranceTextField(), gridBagConstraints3);
		this.add(jLabel2, gridBagConstraints11);
		this.add(getJRelaxationTextField(), gridBagConstraints21);
	
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			}
		return jContentPane;
	}

	/**
	 * This method initializes jIterationsTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJIterationsTextField() {
		if (jIterationsTextField == null) {
			jIterationsTextField = new JTextField();
		}
		return jIterationsTextField;
	}

	/**
	 * This method initializes jToleranceTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJToleranceTextField() {
		if (jToleranceTextField == null) {
			jToleranceTextField = new JTextField();
		}
		return jToleranceTextField;
	}
	
	public int getIterations() {
		int number = NumberIterations;
		try {
			number = new Integer(getJIterationsTextField().getText()).intValue();
			NumberIterations = number;
		} catch(NumberFormatException nfe) {
			number = NumberIterations;
		}
		return number;
	}
	
	public double getTolerance() {
		double number = Tolerance;
		try {
			number = new Double(getJToleranceTextField().getText()).doubleValue();
			Tolerance = number;
		} catch(NumberFormatException nfe) {
			number = Tolerance;
		}
		return number;
	}

	public double getRelaxation() {
		double number = Relaxation;
		try {
			number = new Double(getJToleranceTextField().getText()).doubleValue();
			Relaxation = number;
		} catch(NumberFormatException nfe) {
			number = Relaxation;
		}
		return number;
	}
	
	/**
	 * This method initializes jRelaxationTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJRelaxationTextField() {
		if (jRelaxationTextField == null) {
			jRelaxationTextField = new JTextField();
		}
		return jRelaxationTextField;
	}

}  //  @jve:decl-index=0:visual-constraint="98,62"
