/*
  JTextTools.java
 
  (P)2002 Laurentiu Cristofor

  Modified by Dana Cristofor on August 2002 to receive a JTextArea
  object where error messages are displayed error messages
*/

/*

GAClust - Clustering categorical databases using genetic algorithms
Copyright (C) 2002  Dana Cristofor


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 2 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, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
USA

GAClust was written by Dana Cristofor (dana@cs.umb.edu).

*/

import javax.swing.*;

/**
 * A number of useful methods for dealing with JTextFields.
 *
 * @version 	1.0
 * @author	Laurentiu Cristofor; modified by Dana Cristofor
 */
public class JTextTools
{
  /**
   * Method for dealing with an invalid value in a JTextField. The
   * method clears the text field and sets the focus on it, after
   * which it throws an IllegalArgumentException.
   *
   * @param jTxt   the JTextField
   * @exception IllegalArgumentException   always thrown
   */
  public static void dealWithInvalidValue(JTextField jTxt)
  {
    jTxt.setText("");
    jTxt.requestFocus();
    throw new IllegalArgumentException();
  }

  /**
   * Method for getting a double value from a JTextField. If the value
   * is not valid an error message is appended to the text area and
   * then dealWithInvalidValue is called resulting in an
   * IllegalArgumentException being thrown. Otherwise the value read
   * from the text field is returned.
   *
   * @param jTxt   the JTextField
   * @param jTxtArea the JTextArea
   * @param errMessage the error message to be appended to the text area
   * @exception IllegalArgumentException   if text field contains an
   * invalid value
   */
  public static double getDouble(JTextField jTxt, JTextArea jTxtArea,
				 String errMessage)
  {
    String s = jTxt.getText();
    double value = 0.0;

    try
      {
	value = Double.parseDouble(s);
      }
    catch (NumberFormatException e)
      {
	jTxtArea.append(errMessage + "\n");
	dealWithInvalidValue(jTxt);
      }

    return value;
  }

  /**
   * Method for getting an int value from a JTextField. If the value
   * is not valid then dealWithInvalidValue is called resulting in an
   * IllegalArgumentException being thrown. Otherwise the value read
   * from the text field is returned.
   *
   * @param jTxt   the JTextField
   * @param jTxtArea the JTextArea
   * @param errMessage the error message to be appended to the text area
   * @exception IllegalArgumentException   if text field contains an
   * invalid value
   */
  public static int getInt(JTextField jTxt, JTextArea jTxtArea,
				 String errMessage)
  {
    String s = jTxt.getText();
    int value = 0;

    try
      {
        value = Integer.parseInt(s);
      }
    catch (NumberFormatException e)
      {
	jTxtArea.append(errMessage + "\n");
	dealWithInvalidValue(jTxt);
      }

    return value;
  }
}

