Coverage Report - org.jtheque.utils.ui.DocumentLengthFilter
 
Classes in this File Line Coverage Branch Coverage Complexity
DocumentLengthFilter
0%
0/13
0%
0/2
1.2
 
 1  
 package org.jtheque.utils.ui;
 2  
 
 3  
 /*
 4  
  * This file is part of JTheque.
 5  
  *
 6  
  * JTheque is free software: you can redistribute it and/or modify
 7  
  * it under the terms of the GNU General Public License as published by
 8  
  * the Free Software Foundation, either version 3 of the License.
 9  
  *
 10  
  * JTheque is distributed in the hope that it will be useful,
 11  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 12  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 13  
  * GNU General Public License for more details.
 14  
  *
 15  
  * You should have received a copy of the GNU General Public License
 16  
  * along with JTheque.  If not, see <http://www.gnu.org/licenses/>.
 17  
  */
 18  
 
 19  
 import javax.swing.text.AttributeSet;
 20  
 import javax.swing.text.BadLocationException;
 21  
 import javax.swing.text.DocumentFilter;
 22  
 import java.awt.Toolkit;
 23  
 
 24  
 
 25  
 /**
 26  
  * A document filter used to limit the length of a field.
 27  
  *
 28  
  * @author Baptiste Wicht
 29  
  */
 30  
 public class DocumentLengthFilter extends DocumentFilter {
 31  
     private final int max;
 32  
 
 33  
     private static final int DEFAULT_LENGTH_LIMIT = 50;
 34  
 
 35  
     /**
 36  
      * Construct a new <code>DocumentLengthFilter</code> with a default maximum number of characters. By default,
 37  
      * only 50 characters are accepted.
 38  
      */
 39  
     public DocumentLengthFilter() {
 40  0
         this(DEFAULT_LENGTH_LIMIT);
 41  0
     }
 42  
 
 43  
     /**
 44  
      * Construct a new <code>DocumentLengthFilter</code> with a maximum number of characters.
 45  
      *
 46  
      * @param max The maximum number of characters permitted in the document.
 47  
      */
 48  
     public DocumentLengthFilter(int max) {
 49  0
         super();
 50  
 
 51  0
         this.max = max;
 52  0
     }
 53  
 
 54  
     @Override
 55  
     public final void insertString(DocumentFilter.FilterBypass fb, int offset, String str,
 56  
                                    AttributeSet attrs) throws BadLocationException {
 57  0
         replace(fb, offset, 0, str, attrs);
 58  0
     }
 59  
 
 60  
     @Override
 61  
     public void replace(DocumentFilter.FilterBypass fb, int offset, int length, String str,
 62  
                         AttributeSet attrs) throws BadLocationException {
 63  0
         int newLength = fb.getDocument().getLength() - length + str.length();
 64  
 
 65  0
         if (newLength <= max) {
 66  0
             fb.replace(offset, length, str, attrs);
 67  
         } else {
 68  0
             Toolkit.getDefaultToolkit().beep();
 69  
         }
 70  0
     }
 71  
 
 72  
     /**
 73  
      * Return the maximum number of characters permitted in the document.
 74  
      *
 75  
      * @return The number.
 76  
      */
 77  
     protected final int getMax() {
 78  0
         return max;
 79  
     }
 80  
 }