2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
10 package eu
.etaxonomy
.taxeditor
.ui
.element
;
12 import org
.apache
.commons
.lang
.StringUtils
;
13 import org
.eclipse
.swt
.SWT
;
14 import org
.eclipse
.swt
.events
.ModifyEvent
;
15 import org
.eclipse
.swt
.widgets
.Display
;
19 * <p>NumberWithLabelElement class.</p>
22 * @created Mar 22, 2010
25 public class NumberWithLabelElement
extends TextWithLabelElement
{
30 private NumberFormatException exception
;
33 * <p>Constructor for NumberWithLabelElement.</p>
35 * @param toolkit a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory} object.
36 * @param parentElement a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement} object.
37 * @param labelString a {@link java.lang.String} object.
38 * @param initialNumber a {@link java.lang.Number} object.
41 public NumberWithLabelElement(CdmFormFactory toolkit
,
42 ICdmFormElement parentElement
, String labelString
,
43 Number initialNumber
, int style
) {
44 super(toolkit
, parentElement
, labelString
, null, null, style
);
45 setNumber(initialNumber
);
52 * @param number a {@link java.lang.Number} object.
54 public void setNumber(Number number
) {
55 super.setText(getStringRepresentation(number
));
59 * Get the value of this field as an {@link Integer}.
60 * @return the Integer value or null if {@link NumberFormatException} occurs.
62 public Integer
getInteger() {
63 if(super.getText()!=null){
64 String text
= super.getText().trim();
66 return StringUtils
.isBlank(text
) ?
0 : new Integer(text
);
67 } catch (NumberFormatException e
) {
75 * Get the value of this field as a {@link Float}.
76 * @return the Float value or null if {@link NumberFormatException} occurs.
78 public Float
getFloat(){
79 String text
= super.getText();
81 return StringUtils
.isBlank(text
) ?
0 : new Float(text
);
82 } catch (NumberFormatException e
) {
89 * Get the value of this field as an {@link Double}.
90 * @return the Double value or null if {@link NumberFormatException} occurs.
92 public Double
getDouble(){
93 String text
= super.getText();
95 return new Double(text
);
96 } catch (NumberFormatException e
) {
102 private String
getStringRepresentation(Object number
){
104 return number
.toString();
110 * @see eu.etaxonomy.taxeditor.forms.AbstractCdmFormElement#propertyChange(org.eclipse.jface.util.PropertyChangeEvent)
114 public void modifyText(ModifyEvent event
) {
115 String value
= text
.getText();
116 if(StringUtils
.isBlank(value
)){
117 text
.setBackground(Display
.getCurrent().getSystemColor(SWT
.COLOR_WHITE
));
118 super.modifyText(event
);
124 Float number
= Float
.parseFloat(value
);
126 if((start
!= null && number
< start
) || (end
!= null && number
> end
)){
127 exception
= new NumberFormatException("You entered a number that is not within the allowed bounds.");
131 }catch(NumberFormatException e
){
132 text
.setBackground(Display
.getCurrent().getSystemColor(SWT
.COLOR_RED
));
133 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, event
));
139 text
.setBackground(Display
.getCurrent().getSystemColor(SWT
.COLOR_WHITE
));
141 super.modifyText(event
);
147 * @param numberOfDigits a int.
148 * @param start a {@link java.lang.Integer} object.
149 * @param end a {@link java.lang.Integer} object.
151 public void setLimits(int numberOfDigits
, Integer start
, Integer end
){
152 setLimits(numberOfDigits
, start
.floatValue(), end
.floatValue());
158 * @param numberOfDigits a int.
159 * @param start a {@link java.lang.Float} object.
160 * @param end a {@link java.lang.Float} object.
162 public void setLimits(int numberOfDigits
, Float start
, Float end
){
163 text
.setTextLimit(numberOfDigits
);
169 * <p>Getter for the field <code>exception</code>.</p>
171 * @return the exception
173 public NumberFormatException
getException() {