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
|
import org.eclipse.swt.SWT;
|
14
|
import org.eclipse.swt.events.ModifyEvent;
|
15
|
import org.eclipse.swt.widgets.Display;
|
16
|
|
17
|
|
18
|
/**
|
19
|
* <p>NumberWithLabelElement class.</p>
|
20
|
*
|
21
|
* @author n.hoffmann
|
22
|
* @created Mar 22, 2010
|
23
|
*/
|
24
|
public class NumberWithLabelElement extends TextWithLabelElement {
|
25
|
|
26
|
private Float start;
|
27
|
private Float end;
|
28
|
|
29
|
private NumberFormatException exception;
|
30
|
|
31
|
/**
|
32
|
* <p>Constructor for NumberWithLabelElement.</p>
|
33
|
*
|
34
|
* @param toolkit a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory} object.
|
35
|
* @param parentElement a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement} object.
|
36
|
* @param labelString a {@link java.lang.String} object.
|
37
|
* @param initialNumber a {@link java.lang.Number} object.
|
38
|
* @param style a int.
|
39
|
*/
|
40
|
public NumberWithLabelElement(CdmFormFactory toolkit,
|
41
|
ICdmFormElement parentElement, String labelString,
|
42
|
Number initialNumber, int style) {
|
43
|
super(toolkit, parentElement, labelString, null, null, style);
|
44
|
setNumber(initialNumber);
|
45
|
}
|
46
|
|
47
|
|
48
|
/**
|
49
|
* <p>setNumber</p>
|
50
|
*
|
51
|
* @param number a {@link java.lang.Number} object.
|
52
|
*/
|
53
|
public void setNumber(Number number) {
|
54
|
super.setText(getStringRepresentation(number));
|
55
|
}
|
56
|
|
57
|
/**
|
58
|
* Get the value of this field as an {@link Integer}.
|
59
|
* @return the Integer value or null if {@link NumberFormatException} occurs.
|
60
|
*/
|
61
|
public Integer getInteger() {
|
62
|
if(super.getText()!=null){
|
63
|
String text = super.getText().trim();
|
64
|
try {
|
65
|
return StringUtils.isBlank(text) ? 0 : new Integer(text);
|
66
|
} catch (NumberFormatException e) {
|
67
|
exception = e;
|
68
|
}
|
69
|
}
|
70
|
return null;
|
71
|
}
|
72
|
|
73
|
/**
|
74
|
* Get the value of this field as a {@link Float}.
|
75
|
* @return the Float value or null if {@link NumberFormatException} occurs.
|
76
|
*/
|
77
|
public Float getFloat(){
|
78
|
String text = super.getText();
|
79
|
try {
|
80
|
return StringUtils.isBlank(text) ? 0 : new Float(text);
|
81
|
} catch (NumberFormatException e) {
|
82
|
exception = e;
|
83
|
}
|
84
|
return null;
|
85
|
}
|
86
|
|
87
|
/**
|
88
|
* Get the value of this field as an {@link Double}.
|
89
|
* @return the Double value or null if {@link NumberFormatException} occurs.
|
90
|
*/
|
91
|
public Double getDouble(){
|
92
|
String text = super.getText();
|
93
|
try {
|
94
|
return StringUtils.isBlank(text) ? null : new Double(text);
|
95
|
} catch (NumberFormatException e) {
|
96
|
exception = e;
|
97
|
}
|
98
|
return null;
|
99
|
}
|
100
|
|
101
|
private String getStringRepresentation(Object number){
|
102
|
if(number != null){
|
103
|
return number.toString();
|
104
|
}
|
105
|
return null;
|
106
|
}
|
107
|
|
108
|
/** {@inheritDoc} */
|
109
|
@Override
|
110
|
public void modifyText(ModifyEvent event) {
|
111
|
String value = text.getText();
|
112
|
if(StringUtils.isBlank(value)){
|
113
|
text.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));
|
114
|
super.modifyText(event);
|
115
|
return;
|
116
|
}
|
117
|
|
118
|
try{
|
119
|
Float number = Float.parseFloat(value);
|
120
|
if((start != null && number < start) || (end != null && number > end)){
|
121
|
exception = new NumberFormatException("You entered a number that is not within the allowed bounds.");
|
122
|
throw exception;
|
123
|
}
|
124
|
}catch(NumberFormatException e){
|
125
|
text.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_RED));
|
126
|
firePropertyChangeEvent(new CdmPropertyChangeEvent(this, event));
|
127
|
exception = e;
|
128
|
return;
|
129
|
}
|
130
|
|
131
|
exception = null;
|
132
|
text.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));
|
133
|
|
134
|
super.modifyText(event);
|
135
|
}
|
136
|
|
137
|
/**
|
138
|
* <p>setLimits</p>
|
139
|
*
|
140
|
* @param numberOfDigits a int.
|
141
|
* @param start a {@link java.lang.Integer} object.
|
142
|
* @param end a {@link java.lang.Integer} object.
|
143
|
*/
|
144
|
public void setLimits(int numberOfDigits, Integer start, Integer end){
|
145
|
setLimits(numberOfDigits, start.floatValue(), end.floatValue());
|
146
|
}
|
147
|
|
148
|
/**
|
149
|
* <p>setLimits</p>
|
150
|
*
|
151
|
* @param numberOfDigits a int.
|
152
|
* @param start a {@link java.lang.Float} object.
|
153
|
* @param end a {@link java.lang.Float} object.
|
154
|
*/
|
155
|
public void setLimits(int numberOfDigits, Float start, Float end){
|
156
|
text.setTextLimit(numberOfDigits);
|
157
|
this.start = start;
|
158
|
this.end = end;
|
159
|
}
|
160
|
|
161
|
/**
|
162
|
* <p>Getter for the field <code>exception</code>.</p>
|
163
|
*
|
164
|
* @return the exception
|
165
|
*/
|
166
|
public NumberFormatException getException() {
|
167
|
return exception;
|
168
|
}
|
169
|
}
|