1
|
/**
|
2
|
* Copyright (C) 2007 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
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.
|
8
|
*/
|
9
|
|
10
|
package eu.etaxonomy.taxeditor.ui.element;
|
11
|
|
12
|
import org.apache.commons.lang.StringUtils;
|
13
|
|
14
|
|
15
|
/**
|
16
|
* <p>NumberWithLabelElement class.</p>
|
17
|
*
|
18
|
* @author n.hoffmann
|
19
|
* @created Mar 22, 2010
|
20
|
*/
|
21
|
public abstract class NumberWithLabelElement extends TextWithLabelElement {
|
22
|
|
23
|
|
24
|
protected NumberFormatException exception;
|
25
|
|
26
|
/**
|
27
|
* <p>Constructor for NumberWithLabelElement.</p>
|
28
|
*
|
29
|
* @param toolkit a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory} object.
|
30
|
* @param parentElement a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement} object.
|
31
|
* @param labelString a {@link java.lang.String} object.
|
32
|
* @param initialNumber a {@link java.lang.Number} object.
|
33
|
* @param style a int.
|
34
|
*/
|
35
|
public NumberWithLabelElement(CdmFormFactory toolkit,
|
36
|
ICdmFormElement parentElement, String labelString,
|
37
|
Number initialNumber, int style) {
|
38
|
super(toolkit, parentElement, labelString, null, null, style);
|
39
|
setNumber(initialNumber);
|
40
|
}
|
41
|
|
42
|
/**
|
43
|
* <p>Constructor for NumberWithLabelElement.</p>
|
44
|
*
|
45
|
* @param toolkit a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory} object.
|
46
|
* @param parentElement a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement} object.
|
47
|
* @param labelString a {@link java.lang.String} object.
|
48
|
* @param initialNumber a {@link java.lang.Number} object.
|
49
|
* @param style a int.
|
50
|
*/
|
51
|
public NumberWithLabelElement(CdmFormFactory toolkit,
|
52
|
ICdmFormElement parentElement, String labelString,
|
53
|
Number initialNumber, Integer height, Integer length, int style) {
|
54
|
super(toolkit, parentElement, labelString, null, null, style);
|
55
|
setNumber(initialNumber);
|
56
|
}
|
57
|
|
58
|
|
59
|
/**
|
60
|
* <p>setNumber</p>
|
61
|
*
|
62
|
* @param number a {@link java.lang.Number} object.
|
63
|
*/
|
64
|
public void setNumber(Number number) {
|
65
|
super.setText(getStringRepresentation(number));
|
66
|
}
|
67
|
|
68
|
/**
|
69
|
* Get the value of this field as an {@link Integer}.
|
70
|
* @return the Integer value or null if {@link NumberFormatException} occurs.
|
71
|
*/
|
72
|
public Integer getInteger() {
|
73
|
if(super.getText()!=null){
|
74
|
String text = super.getText().trim();
|
75
|
try {
|
76
|
return StringUtils.isBlank(text) ? null : new Integer(text);
|
77
|
} catch (NumberFormatException e) {
|
78
|
exception = e;
|
79
|
}
|
80
|
}
|
81
|
return null;
|
82
|
}
|
83
|
|
84
|
/**
|
85
|
* Get the value of this field as a {@link Float}.
|
86
|
* @return the Float value or null if {@link NumberFormatException} occurs.
|
87
|
*/
|
88
|
public Float getFloat(){
|
89
|
String text = super.getText();
|
90
|
try {
|
91
|
return StringUtils.isBlank(text) ? null : new Float(text);
|
92
|
} catch (NumberFormatException e) {
|
93
|
exception = e;
|
94
|
}
|
95
|
return null;
|
96
|
}
|
97
|
|
98
|
/**
|
99
|
* Get the value of this field as an {@link Double}.
|
100
|
* @return the Double value or null if {@link NumberFormatException} occurs.
|
101
|
*/
|
102
|
public Double getDouble(){
|
103
|
String text = super.getText();
|
104
|
try {
|
105
|
return StringUtils.isBlank(text) ? null : new Double(text);
|
106
|
} catch (NumberFormatException e) {
|
107
|
exception = e;
|
108
|
}
|
109
|
return null;
|
110
|
}
|
111
|
|
112
|
private String getStringRepresentation(Object number){
|
113
|
if(number != null){
|
114
|
return number.toString();
|
115
|
}
|
116
|
return null;
|
117
|
}
|
118
|
|
119
|
|
120
|
|
121
|
|
122
|
|
123
|
/**
|
124
|
* <p>Getter for the field <code>exception</code>.</p>
|
125
|
*
|
126
|
* @return the exception
|
127
|
*/
|
128
|
public NumberFormatException getException() {
|
129
|
return exception;
|
130
|
}
|
131
|
}
|