001/* ---------------------------------------------------------------------------- 002 The Kiwi Toolkit - A Java Class Library 003 Copyright (C) 1998-2004 Mark A. Lindner 004 005 This library is free software; you can redistribute it and/or 006 modify it under the terms of the GNU General Public License as 007 published by the Free Software Foundation; either version 2 of the 008 License, or (at your option) any later version. 009 010 This library is distributed in the hope that it will be useful, 011 but WITHOUT ANY WARRANTY; without even the implied warranty of 012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013 General Public License for more details. 014 015 You should have received a copy of the GNU General Public License 016 along with this library; if not, write to the Free Software 017 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 018 02111-1307, USA. 019 020 The author may be contacted at: mark_a_lindner@yahoo.com 021 ---------------------------------------------------------------------------- 022 $Log: KDocument.java,v $ 023 Revision 1.6 2004/05/13 21:34:57 markl 024 comment block updates 025 026 Revision 1.5 2004/03/22 06:57:06 markl 027 new constructor added 028 029 Revision 1.4 2003/01/19 09:33:06 markl 030 Javadoc & comment header updates. 031 032 Revision 1.3 2001/03/12 04:11:43 markl 033 Source code cleanup. 034 035 Revision 1.2 1999/07/30 03:59:11 markl 036 Bug fix. 037 038 Revision 1.1 1999/07/29 06:45:09 markl 039 Initial revision 040 ---------------------------------------------------------------------------- 041*/ 042 043package kiwi.ui.model; 044 045import java.awt.*; 046import javax.swing.text.*; 047 048/** A specialization of <code>PlainDocument</code> that introduces length 049 * limits on a document. This class is useful as a way to constrain the length 050 * of input in a <code>JTextField</code> or similar text-entry component. 051 * 052 * @author Mark Lindner 053 * 054 * @see javax.swing.JTextField 055 */ 056 057public class KDocument extends PlainDocument 058 { 059 /** A constant specifying a length of 'unlimited.' */ 060 public static final int NO_LIMIT = 0; 061 private int maxLength = NO_LIMIT; 062 063 /** Construct a new <code>KDocument</code> with unlimited length. 064 */ 065 066 public KDocument() 067 { 068 } 069 070 /** Construct a new <code>KDocument</code> with the specified maximum length. 071 * 072 * @param length The maximum length for the document. The constant 073 * <code>NO_LIMIT</code> may be passed to specify unlimited length. 074 * 075 * @since Kiwi 2.0 076 */ 077 078 public KDocument(int length) 079 { 080 maxLength = length; 081 } 082 083 /** Set the maximum length that this document is allowed to have. The 084 * default length is <code>NO_LIMIT</code>, which means unlimited length. 085 * 086 * @param length The new maximum length for the document. If the document 087 * is currently longer than this length, the excess characters are deleted. 088 * The constant <code>NO_LIMIT</code> may be passed to specify unlimited 089 * length. 090 */ 091 092 public void setMaximumLength(int length) 093 { 094 maxLength = ((length < 0) ? 0 : length); 095 096 if(maxLength != NO_LIMIT) 097 truncate(); 098 } 099 100 /** Get the current maximum length for this document. 101 * 102 * @return The current maximum length, or <code>NO_LIMIT</code> if the 103 * length is unlimited. 104 */ 105 106 public int getMaximumLength() 107 { 108 return(maxLength); 109 } 110 111 /** Overridden to constrain document length. 112 */ 113 114 public void insertString(int offset, String string, AttributeSet a) 115 throws BadLocationException 116 { 117 if(maxLength == NO_LIMIT) 118 super.insertString(offset, string, a); 119 120 else if(maxLength == getLength()) 121 Toolkit.getDefaultToolkit().beep(); 122 123 else 124 { 125 super.insertString(offset, string, a); 126 truncate(); 127 } 128 } 129 130 /* Truncate excess length. */ 131 132 private void truncate() 133 { 134 int excess = getLength() - maxLength; 135 if(excess > 0) 136 { 137 try 138 { 139 remove(maxLength, excess); 140 } 141 catch(BadLocationException ex) {} 142 } 143 } 144 145 } 146 147/* end of source file */