]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/gui/DecimalNumberField.java
Version 16, February 2014
[GpsPrune.git] / tim / prune / gui / DecimalNumberField.java
1 package tim.prune.gui;
2
3 import java.awt.Dimension;
4
5 import javax.swing.JTextField;
6 import javax.swing.text.AttributeSet;
7 import javax.swing.text.BadLocationException;
8 import javax.swing.text.PlainDocument;
9
10 /**
11  * text field for holding a decimal number with validation
12  * - doesn't allow certain characters such as a-z to be entered
13  */
14 public class DecimalNumberField extends JTextField
15 {
16         /**
17          * Inner class to act as document for validation
18          */
19         protected static class DecimalNumberDocument extends PlainDocument
20         {
21                 private boolean _allowNegative = false;
22
23                 /** constructor */
24                 DecimalNumberDocument(boolean inAllowNegative) {
25                         _allowNegative = inAllowNegative;
26                 }
27
28                 /**
29                  * Override the insert string method
30                  * @param offs offset
31                  * @param str string
32                  * @param a attributes
33                  * @throws BadLocationException on insert failure
34                  */
35                 public void insertString(int offs, String str, AttributeSet a)
36                         throws BadLocationException
37                 {
38                         char[] source = str.toCharArray();
39                         char[] result = new char[source.length];
40                         int j = 0;
41                         for (int i = 0; i < result.length; i++) {
42                                 if (!Character.isLetter(source[i]) && (_allowNegative || source[i] != '-') && source[i] != ' ') // no letters, no minus sign or space
43                                         result[j++] = source[i];
44                         }
45                         super.insertString(offs, new String(result, 0, j), a);
46                 }
47         }
48
49
50         /**
51          * Constructor
52          */
53         public DecimalNumberField()
54         {
55                 this(false);
56         }
57
58         /**
59          * Constructor
60          * @param inAllowNegative true to allow negative numbers
61          */
62         public DecimalNumberField(boolean inAllowNegative)
63         {
64                 super(6);
65                 setDocument(new DecimalNumberDocument(inAllowNegative));
66         }
67
68         /**
69          * @return double value
70          */
71         public double getValue()
72         {
73                 return parseValue(getText());
74         }
75
76         /**
77          * @param inValue value to set
78          */
79         public void setValue(double inValue)
80         {
81                 setText("" + inValue);
82         }
83
84         /**
85          * @param inText text to parse
86          * @return value as double
87          */
88         private static double parseValue(String inText)
89         {
90                 double value = 0.0;
91                 try {
92                         value = Double.parseDouble(inText);
93                 }
94                 catch (NumberFormatException nfe) {}
95                 if (value < 0) {
96                         value = 0;
97                 }
98                 return value;
99         }
100
101         /**
102          * Put a minimum on the minimum width
103          */
104         public Dimension getMinimumSize()
105         {
106                 Dimension dim = super.getMinimumSize();
107                 if (dim.width < 50) dim.width = 50;
108                 return dim;
109         }
110 }