]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/gui/DecimalNumberField.java
f1baa2a874f02de26f20f785fd9cbdbb7367aaac
[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                 super(6);
56                 setDocument(new DecimalNumberDocument(false));
57         }
58
59         /**
60          * Constructor
61          * @param inAllowNegative true to allow negative numbers
62          */
63         public DecimalNumberField(boolean inAllowNegative)
64         {
65                 super(6);
66                 setDocument(new DecimalNumberDocument(inAllowNegative));
67         }
68
69         /**
70          * @return double value
71          */
72         public double getValue()
73         {
74                 return parseValue(getText());
75         }
76
77         /**
78          * @param inValue value to set
79          */
80         public void setValue(double inValue)
81         {
82                 setText("" + inValue);
83         }
84
85         /**
86          * @param inText text to parse
87          * @return value as double
88          */
89         private static double parseValue(String inText)
90         {
91                 double value = 0.0;
92                 try {
93                         value = Double.parseDouble(inText);
94                 }
95                 catch (NumberFormatException nfe) {}
96                 if (value < 0) {
97                         value = 0;
98                 }
99                 return value;
100         }
101
102         /**
103          * Put a minimum on the minimum width
104          */
105         public Dimension getMinimumSize()
106         {
107                 Dimension dim = super.getMinimumSize();
108                 if (dim.width < 50) dim.width = 50;
109                 return dim;
110         }
111 }