001/*
002 * CDDL HEADER START
003 *
004 * The contents of this file are subject to the terms of the
005 * Common Development and Distribution License, Version 1.0 only
006 * (the "License").  You may not use this file except in compliance
007 * with the License.
008 *
009 * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
010 * or http://forgerock.org/license/CDDLv1.0.html.
011 * See the License for the specific language governing permissions
012 * and limitations under the License.
013 *
014 * When distributing Covered Code, include this CDDL HEADER in each
015 * file and include the License file at legal-notices/CDDLv1_0.txt.
016 * If applicable, add the following below this CDDL HEADER, with the
017 * fields enclosed by brackets "[]" replaced with your own identifying
018 * information:
019 *      Portions Copyright [yyyy] [name of copyright owner]
020 *
021 * CDDL HEADER END
022 *
023 *
024 *      Copyright 2009 Sun Microsystems, Inc.
025 *      Portions Copyright 2015 ForgeRock AS.
026 */
027package org.opends.guitools.controlpanel.ui.components;
028
029import javax.swing.text.AttributeSet;
030import javax.swing.text.BadLocationException;
031import javax.swing.text.DocumentFilter;
032import javax.swing.text.JTextComponent;
033
034/**
035 * Document filter used to update properly a text component displaying a
036 * numeric field with a limited size.
037 */
038public class NumericLimitedSizeDocumentFilter extends DocumentFilter
039{
040  private JTextComponent tf;
041  private int maxSize;
042
043  /**
044   * Constructor.
045   * @param tf the text component associated with the document.
046   * @param maxSize the maximum size.
047   */
048  public NumericLimitedSizeDocumentFilter(JTextComponent tf, int maxSize)
049  {
050    this.tf = tf;
051    this.maxSize = maxSize;
052  }
053
054  /** {@inheritDoc} */
055  public void insertString(DocumentFilter.FilterBypass fb, int offset,
056      String text, AttributeSet attr)
057  throws BadLocationException
058  {
059    int previousLength = fb.getDocument().getLength();
060    String newText = text.replaceAll("[^0-9]", "");
061    if (newText.length() > maxSize)
062    {
063      newText = newText.substring(0, maxSize);
064    }
065    if (newText.length() + previousLength > maxSize)
066    {
067      if (offset + newText.length() > maxSize)
068      {
069        int newOffset = offset + newText.length() - maxSize;
070        fb.remove(0, newOffset);
071        fb.insertString(newOffset, newText, attr);
072      }
073      else
074      {
075        fb.insertString(offset, newText, attr);
076        fb.remove(maxSize, newText.length() + previousLength - maxSize);
077      }
078    }
079    else
080    {
081      fb.insertString(offset, newText, attr);
082    }
083    updateCaretPosition(fb);
084  }
085
086  /** {@inheritDoc} */
087  public void replace(DocumentFilter.FilterBypass fb, int offset,
088      int length, String text, AttributeSet attr)
089  throws BadLocationException
090  {
091    if (length > 0)
092    {
093      fb.remove(offset, length);
094    }
095
096    insertString(fb, offset, text, attr);
097  }
098
099  private void updateCaretPosition(DocumentFilter.FilterBypass fb)
100  throws BadLocationException
101  {
102    int totalLength = fb.getDocument().getLength();
103    int caretPosition = tf.getCaretPosition();
104
105    if (totalLength >= maxSize &&
106        caretPosition == fb.getDocument().getLength())
107    {
108      tf.setCaretPosition(0);
109    }
110  }
111}