/*******************************************************************************
 * GenRGenS v.1.0, Generation of Random Genomic Sequences                      *
 * Copyright (C) 2001  Alain Denise/Yann Ponty                                 *
 *                                                                             *
 * 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. *
 *******************************************************************************/

package GenRGenS.IU;

import javax.swing.*;
import javax.swing.text.*;

/**
 * La classe <code>IntField</code> represente des objets de type JTextField qui ne
 * peuvent avoir que des entiers strictement positifs dans leur champ.
 * @author  Yann Ponty
 * @version 1.0
 * @see JTextField
 * @see IntDialog
 */
public class IntField extends JTextField {


  /**
   * Constructeur de la classe IntField
  */
  public IntField()
  {
    super();
  }

  /**
   * On change le modele de document par defaut en surchargeant la methode
   * createDefaultModel. On peut ainsi redefinir le type d'action a accomplir sur
   * insertion d'une ligne de texte. En l'occurence, on refuse toutes chaine
   * contenant des lettres.
  */
  protected Document createDefaultModel()
  {
    return new IntDocument();
  }
  public class IntDocument extends PlainDocument
  {
    public void insertString(int offs, String str, AttributeSet a) throws BadLocationException
    {
      if (str == null)
      {return;}
      boolean ok = true;
      char[] upper = str.toCharArray();
      for (int i = 0; (i < upper.length)&&(ok); i++)
      {
        ok =  (upper[i]=='0')||(upper[i]=='1')||(upper[i]=='2')||(upper[i]=='3')
            ||(upper[i]=='4')||(upper[i]=='5')||(upper[i]=='6')||(upper[i]=='7')
            ||(upper[i]=='8')||(upper[i]=='9');
      }
      if (ok)
      {super.insertString(offs, new String(upper), a);}
    }
  }
}