View Javadoc

1   /**
2    * Copyright 2009 Timothy Johnston Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
3    * file except in compliance with the License. You may obtain a copy of the License at
4    * 
5    * http://www.apache.org/licenses/LICENSE-2.0
6    * 
7    * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
8    * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
9    * specific language governing permissions and limitations under the License.
10   */
11  package com.timjohnstondev.unitconverter.view;
12  
13  import java.util.ArrayList;
14  import java.util.List;
15  import java.util.ResourceBundle;
16  import javax.swing.text.AttributeSet;
17  import javax.swing.text.BadLocationException;
18  import javax.swing.text.PlainDocument;
19  
20  /**
21   * This {@code PlainDocument} subclass has several regular expressions that the text can match to be considered a valid
22   * number. Basically the text can be in the form of a standard number but with no commas or in the form of scientific
23   * notation (#.###e#). Only digits, e and a period are allowable characters.
24   */
25  final class InputFilter extends PlainDocument
26  {
27    private ResourceBundle resources;
28    private List <Character> allowableCharacters;
29  
30    /**
31     * Default constructor to load {@code ResourceBundle}
32     * 
33     * @param allowableChars allowableChars
34     */
35    InputFilter(final String allowableChars)
36    {
37      resources = ResourceBundle.getBundle("com.timjohnstondev.unitconverter.view.View");
38      allowableCharacters = asList(allowableChars);
39    }
40  
41    @Override
42    public void insertString(final int offset, final String text, final AttributeSet attr) throws BadLocationException
43    {
44      final String oldText = getText(0, getLength());
45      final String newText = removeNonNumericChars(text);
46  
47      super.insertString(offset, newText, attr);
48  
49      if (!matchesPattern(getText(0, getLength())))
50      {
51        super.replace(0, getLength(), oldText, attr);
52      }
53    }
54  
55    private String removeNonNumericChars(final String text)
56    {
57      String number = "";
58      for (char character : text.toCharArray())
59      {
60        if (allowableCharacters.contains(character))
61        {
62          number += character;
63        }
64      }
65      return number;
66    }
67  
68    private List <Character> asList(final String characters)
69    {
70      final char[] chars = characters.toCharArray();
71      final List <Character> list = new ArrayList <Character>();
72      for (char c : chars)
73      {
74        list.add(new Character(c));
75      }
76      return list;
77    }
78  
79    private boolean matchesPattern(final String text)
80    {
81      return text.matches(resources.getString("InputFilter.inputPatternCombined"));
82    }
83  }