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