File: WholeNumberField.java

package info (click to toggle)
gpsprune 26.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,824 kB
  • sloc: java: 52,154; sh: 25; makefile: 21; python: 15
file content (109 lines) | stat: -rw-r--r-- 2,442 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package tim.prune.gui;

import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;

/**
 * text field for holding a single integer with validation
 */
public class WholeNumberField extends JTextField
{
	/**
	 * Inner class to act as document for validation
	 */
	protected static class WholeNumberDocument extends PlainDocument
	{
		/** Num digits to allow */
		private int _maxDigits = 0;

		/**
		 * Constructor
		 * @param inMaxDigits max digits to allow
		 */
		public WholeNumberDocument(int inMaxDigits) {
			_maxDigits = inMaxDigits;
		}


		/**
		 * Override the insert string method
		 * @param offs offset
		 * @param str string
		 * @param a attributes
		 * @throws BadLocationException on insert failure
		 */
		public void insertString(int offs, String str, AttributeSet a)
			throws BadLocationException
		{
			if (getLength() >= _maxDigits) return;
			char[] source = str.toCharArray();
			char[] result = new char[source.length];
			int j = 0;
			for (int i = 0; i < result.length && j < _maxDigits; i++)
			{
				if (Character.isDigit(source[i])) {
					result[j++] = source[i];
				}
			}
			super.insertString(offs, new String(result, 0, j), a);
		}
	}


	/**
	 * Constructor
	 * @param inMaxDigits max digits to allow
	 */
	public WholeNumberField(int inMaxDigits)
	{
		super(inMaxDigits);
		setDocument(new WholeNumberDocument(inMaxDigits));
		getDocument().addDocumentListener(new DocumentListener() {
			public void removeUpdate(DocumentEvent arg0) {fireActionPerformed();}
			public void insertUpdate(DocumentEvent arg0) {fireActionPerformed();}
			public void changedUpdate(DocumentEvent arg0) {fireActionPerformed();}
		});
	}

	/**
	 * @return true if there is no text in the field
	 */
	public boolean isEmpty() {
		return getText().isEmpty();
	}

	/**
	 * @return integer value
	 */
	public int getValue() {
		return parseValue(getText());
	}

	/**
	 * @param inValue value to set
	 */
	public void setValue(int inValue) {
		setText("" + inValue);
	}

	/**
	 * @param inText text to parse
	 * @return value as integer
	 */
	private static int parseValue(String inText)
	{
		int value = 0;
		try {
			value = Integer.parseInt(inText);
		}
		catch (NumberFormatException nfe) {}
		if (value < 0) {
			value = 0;
		}
		return value;
	}
}