1
2
3
4
5
6
7
8
9
10
11 package de.jaret.util.ui;
12
13 import java.text.DecimalFormat;
14 import java.text.NumberFormat;
15 import java.text.ParseException;
16
17 import org.eclipse.swt.SWT;
18 import org.eclipse.swt.events.KeyEvent;
19 import org.eclipse.swt.events.KeyListener;
20 import org.eclipse.swt.events.VerifyEvent;
21 import org.eclipse.swt.events.VerifyListener;
22 import org.eclipse.swt.widgets.Text;
23
24
25
26
27
28
29
30 public class DoubleField implements VerifyListener, KeyListener {
31 protected int _digits = 1;
32 protected double _min = -Double.MAX_VALUE;
33 protected double _max = Double.MAX_VALUE;
34 protected Text _text;
35 protected NumberFormat _numberFormat;
36 protected double _increment = 1.0;
37
38 public DoubleField(int digits, double min, double max) {
39 _digits = digits;
40 _min = min;
41 _max = max;
42 updateNumberFormat();
43 }
44
45 public DoubleField() {
46 this(1, -Double.MAX_VALUE, Double.MAX_VALUE);
47 }
48
49 public double getValue() throws ParseException {
50 double value = ((Number) _numberFormat.parse(_text.getText())).doubleValue();
51 return value;
52 }
53
54 public void setValue(double value) {
55 if (_text != null) {
56 _text.setText(_numberFormat.format(value));
57 }
58 }
59
60 private void updateNumberFormat() {
61 if (_numberFormat == null) {
62 _numberFormat = DecimalFormat.getInstance();
63 }
64 _numberFormat.setMaximumFractionDigits(_digits);
65 _numberFormat.setGroupingUsed(false);
66 }
67
68
69
70
71 public int getDigits() {
72 return _digits;
73 }
74
75
76
77
78 public void setDigits(int digits) {
79 _digits = digits;
80 updateNumberFormat();
81 }
82
83
84
85
86 public double getMax() {
87 return _max;
88 }
89
90
91
92
93 public void setMax(double max) {
94 _max = max;
95 }
96
97
98
99
100 public double getMin() {
101 return _min;
102 }
103
104
105
106
107 public void setMin(double min) {
108 _min = min;
109 }
110
111
112
113
114 public Text getText() {
115 return _text;
116 }
117
118
119
120
121 public void setText(Text text) {
122 if (_text != null) {
123 _text.removeVerifyListener(this);
124 _text.removeKeyListener(this);
125 }
126 _text = text;
127 if (_text != null) {
128 _text.addVerifyListener(this);
129 _text.addKeyListener(this);
130 }
131 }
132
133 public void verifyText(VerifyEvent e) {
134 if (e.text.length() > 1) {
135 try {
136 _numberFormat.parse(e.text);
137 return;
138 } catch (Exception ex) {
139
140 }
141 }
142
143 if (Character.isDigit(e.character) || e.character < 32 || e.character == 127) {
144 return;
145 }
146 if (e.character == '.' || e.character == ',') {
147 if (_text.getText().indexOf('.') == -1 && _text.getText().indexOf(',') == -1) {
148 return;
149 }
150 }
151 if (e.character == '-') {
152 if (_text.getText().indexOf('-') == -1 && _text.getCaretPosition() == 0) {
153 return;
154 }
155 }
156 e.doit = false;
157 }
158
159
160
161
162 public double getIncrement() {
163 return _increment;
164 }
165
166
167
168
169 public void setIncrement(double increment) {
170 _increment = increment;
171 }
172
173 public void keyPressed(KeyEvent e) {
174 if (e.keyCode == SWT.ARROW_DOWN) {
175 try {
176 double value = getValue();
177 value -= _increment;
178 if (value < _min) {
179 value = _min;
180 }
181 setValue(value);
182 } catch (ParseException ex) {
183
184 }
185 e.doit = false;
186 } else if (e.keyCode == SWT.ARROW_UP) {
187 try {
188 double value = getValue();
189 value += _increment;
190 if (value > _max) {
191 value = _max;
192 }
193 setValue(value);
194 } catch (ParseException ex) {
195
196 }
197 e.doit = false;
198 }
199 }
200
201 public void keyReleased(KeyEvent e) {
202 }
203
204 }