Source for org.jfree.ui.IntegerDocument

   1: /* ========================================================================
   2:  * JCommon : a free general purpose class library for the Java(tm) platform
   3:  * ========================================================================
   4:  *
   5:  * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
   6:  * 
   7:  * Project Info:  http://www.jfree.org/jcommon/index.html
   8:  *
   9:  * This library is free software; you can redistribute it and/or modify it 
  10:  * under the terms of the GNU Lesser General Public License as published by 
  11:  * the Free Software Foundation; either version 2.1 of the License, or 
  12:  * (at your option) any later version.
  13:  *
  14:  * This library is distributed in the hope that it will be useful, but 
  15:  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
  16:  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
  17:  * License for more details.
  18:  *
  19:  * You should have received a copy of the GNU Lesser General Public
  20:  * License along with this library; if not, write to the Free Software
  21:  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 
  22:  * USA.  
  23:  *
  24:  * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
  25:  * in the United States and other countries.]
  26:  * 
  27:  * --------------------
  28:  * IntegerDocument.java
  29:  * --------------------
  30:  * (C) Copyright 2000-2004, by Andrzej Porebski.
  31:  *
  32:  * Original Author:  Andrzej Porebski;
  33:  * Contributor(s):   -;
  34:  *
  35:  * $Id: IntegerDocument.java,v 1.3 2005/11/16 15:58:41 taqua Exp $
  36:  *
  37:  * Changes (from 7-Nov-2001)
  38:  * -------------------------
  39:  * 07-Nov-2001 : Added to com.jrefinery.ui package (DG);
  40:  * 14-Oct-2002 : Fixed errors reported by Checkstyle (DG);
  41:  *
  42:  */
  43: 
  44: package org.jfree.ui;
  45: 
  46: import javax.swing.text.AttributeSet;
  47: import javax.swing.text.BadLocationException;
  48: import javax.swing.text.PlainDocument;
  49: 
  50: /**
  51:  * A document for editing integers.
  52:  *
  53:  * @author Andrzej Porebski
  54:  */
  55: public class IntegerDocument extends PlainDocument {
  56: 
  57:     /**
  58:      * Inserts a string.
  59:      *
  60:      * @param i  i.
  61:      * @param s  s.
  62:      * @param attributes  the attributes.
  63:      *
  64:      * @throws BadLocationException ??
  65:      */
  66:     public void insertString(final int i, final String s, final AttributeSet attributes)
  67:         throws BadLocationException {
  68: 
  69:         super.insertString(i, s, attributes);
  70:         if (s != null && (!s.equals("-") || i != 0 || s.length() >= 2)) {
  71:             try {
  72:                 Integer.parseInt(getText(0, getLength()));
  73:             }
  74:             catch (NumberFormatException e) {
  75:                 remove(i, s.length());
  76:             }
  77:         }
  78:     }
  79: 
  80: }