View Javadoc

1   /*
2    *  File: DoubleField.java 
3    *  Copyright (c) 2004-2007  Peter Kliem (Peter.Kliem@jaret.de)
4    *  A commercial license is available, see http://www.jaret.de.
5    *
6    * All rights reserved. This program and the accompanying materials
7    * are made available under the terms of the Common Public License v1.0
8    * which accompanies this distribution, and is available at
9    * http://www.eclipse.org/legal/cpl-v10.html
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   * Helper wrapping a Text to allow only editing of double values.
26   * 
27   * @author Peter Kliem
28   * @version $Id: DoubleField.java 242 2007-02-11 21:05:07Z olk $
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       * @return the digits
70       */
71      public int getDigits() {
72          return _digits;
73      }
74  
75      /**
76       * @param digits the digits to set
77       */
78      public void setDigits(int digits) {
79          _digits = digits;
80          updateNumberFormat();
81      }
82  
83      /**
84       * @return the max
85       */
86      public double getMax() {
87          return _max;
88      }
89  
90      /**
91       * @param max the max to set
92       */
93      public void setMax(double max) {
94          _max = max;
95      }
96  
97      /**
98       * @return the min
99       */
100     public double getMin() {
101         return _min;
102     }
103 
104     /**
105      * @param min the min to set
106      */
107     public void setMin(double min) {
108         _min = min;
109     }
110 
111     /**
112      * @return the text
113      */
114     public Text getText() {
115         return _text;
116     }
117 
118     /**
119      * @param text the text to set
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                 // ignore, text will no t be inserted
140             }
141         }
142         // System.out.println("characteer "+(int)e.character);
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      * @return the increment
161      */
162     public double getIncrement() {
163         return _increment;
164     }
165 
166     /**
167      * @param increment the increment to set
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                 // ignore
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                 // ignore
196             }
197             e.doit = false;
198         }
199     }
200 
201     public void keyReleased(KeyEvent e) {
202     }
203 
204 }