1
|
// $Id$
|
2
|
/**
|
3
|
* Copyright (C) 2013 EDIT
|
4
|
* European Distributed Institute of Taxonomy
|
5
|
* http://www.e-taxonomy.eu
|
6
|
*
|
7
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
8
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
9
|
*/
|
10
|
package eu.etaxonomy.taxeditor.ui.campanula;
|
11
|
|
12
|
import org.eclipse.swt.SWT;
|
13
|
import org.eclipse.swt.events.ModifyEvent;
|
14
|
import org.eclipse.swt.widgets.Display;
|
15
|
import org.eclipse.swt.widgets.Text;
|
16
|
|
17
|
import eu.etaxonomy.cdm.common.CdmUtils;
|
18
|
import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
|
19
|
import eu.etaxonomy.taxeditor.ui.element.CdmPropertyChangeEvent;
|
20
|
import eu.etaxonomy.taxeditor.ui.section.campanula.compatibility.ICdmFormElement;
|
21
|
|
22
|
/**
|
23
|
* @author pplitzner
|
24
|
* @date 12.08.2013
|
25
|
*
|
26
|
*/
|
27
|
public class NumberFieldController extends TextFieldController {
|
28
|
private Float start;
|
29
|
private Float end;
|
30
|
|
31
|
private NumberFormatException exception;
|
32
|
|
33
|
/**
|
34
|
* <p>Constructor for NumberWithLabelElement.</p>
|
35
|
*
|
36
|
* @param toolkit a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory} object.
|
37
|
* @param parentElement a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement} object.
|
38
|
* @param labelString a {@link java.lang.String} object.
|
39
|
* @param initialInteger a {@link java.lang.Integer} object.
|
40
|
* @param style a int.
|
41
|
*/
|
42
|
public NumberFieldController(Text controlledText, CdmFormFactory formFactory, ICdmFormElement parentElement, Integer initialInteger){
|
43
|
super(controlledText, formFactory, parentElement, null, null);
|
44
|
//WindowBuilder exception handling
|
45
|
if(initialInteger==null){
|
46
|
initialInteger = 0;
|
47
|
}
|
48
|
setInteger(initialInteger);
|
49
|
}
|
50
|
|
51
|
/**
|
52
|
* <p>Constructor for NumberWithLabelElement.</p>
|
53
|
*
|
54
|
* @param toolkit a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory} object.
|
55
|
* @param parentElement a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement} object.
|
56
|
* @param labelString a {@link java.lang.String} object.
|
57
|
* @param initialFloat a {@link java.lang.Float} object.
|
58
|
* @param style a int.
|
59
|
* @wbp.parser.constructor
|
60
|
*/
|
61
|
public NumberFieldController(Text controlledText, CdmFormFactory formFactory,
|
62
|
ICdmFormElement parentElement, Float initialFloat) {
|
63
|
super(controlledText, formFactory, parentElement, null, null);
|
64
|
//WindowBuilder exception handling
|
65
|
if(initialFloat==null){
|
66
|
initialFloat = 0f;
|
67
|
}
|
68
|
setFloat(initialFloat);
|
69
|
}
|
70
|
|
71
|
/**
|
72
|
* <p>setInteger</p>
|
73
|
*
|
74
|
* @param number a {@link java.lang.Integer} object.
|
75
|
*/
|
76
|
public void setInteger(Integer number) {
|
77
|
super.setText(getStringRepresentation(number));
|
78
|
}
|
79
|
|
80
|
/**
|
81
|
* <p>setFloat</p>
|
82
|
*
|
83
|
* @param number a {@link java.lang.Float} object.
|
84
|
*/
|
85
|
public void setFloat(Float number) {
|
86
|
super.setText(getStringRepresentation(number));
|
87
|
}
|
88
|
|
89
|
/**
|
90
|
* <p>getInteger</p>
|
91
|
*
|
92
|
* @return a {@link java.lang.Integer} object.
|
93
|
*/
|
94
|
public Integer getInteger() {
|
95
|
String text = super.getText().trim();
|
96
|
return text.equals("") ? 0 : new Integer(text);
|
97
|
}
|
98
|
|
99
|
/**
|
100
|
* <p>getFloat</p>
|
101
|
*
|
102
|
* @return a {@link java.lang.Float} object.
|
103
|
*/
|
104
|
public Float getFloat(){
|
105
|
String text = super.getText();
|
106
|
return new Float(text);
|
107
|
}
|
108
|
|
109
|
private String getStringRepresentation(Object number){
|
110
|
if(number != null){
|
111
|
return number.toString();
|
112
|
}
|
113
|
return null;
|
114
|
}
|
115
|
|
116
|
/* (non-Javadoc)
|
117
|
* @see eu.etaxonomy.taxeditor.forms.AbstractCdmFormElement#propertyChange(org.eclipse.jface.util.PropertyChangeEvent)
|
118
|
*/
|
119
|
/** {@inheritDoc} */
|
120
|
@Override
|
121
|
public void modifyText(ModifyEvent event) {
|
122
|
String value = text.getText();
|
123
|
if(CdmUtils.isEmpty(value)){
|
124
|
text.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));
|
125
|
return;
|
126
|
}
|
127
|
|
128
|
try{
|
129
|
|
130
|
Float number = Float.parseFloat(value);
|
131
|
|
132
|
if((start != null && number < start) || (end != null && number > end)){
|
133
|
throw new NumberFormatException("You entered a number that is not within the allowed bounds.");
|
134
|
}
|
135
|
|
136
|
}catch(NumberFormatException e){
|
137
|
text.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_RED));
|
138
|
firePropertyChangeEvent(new CdmPropertyChangeEvent(this, e));
|
139
|
exception = e;
|
140
|
return;
|
141
|
}
|
142
|
|
143
|
exception = null;
|
144
|
text.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));
|
145
|
|
146
|
super.modifyText(event);
|
147
|
}
|
148
|
|
149
|
/**
|
150
|
* <p>setLimits</p>
|
151
|
*
|
152
|
* @param numberOfDigits a int.
|
153
|
* @param start a {@link java.lang.Integer} object.
|
154
|
* @param end a {@link java.lang.Integer} object.
|
155
|
*/
|
156
|
public void setLimits(int numberOfDigits, Integer start, Integer end){
|
157
|
setLimits(numberOfDigits, start.floatValue(), end.floatValue());
|
158
|
}
|
159
|
|
160
|
/**
|
161
|
* <p>setLimits</p>
|
162
|
*
|
163
|
* @param numberOfDigits a int.
|
164
|
* @param start a {@link java.lang.Float} object.
|
165
|
* @param end a {@link java.lang.Float} object.
|
166
|
*/
|
167
|
public void setLimits(int numberOfDigits, Float start, Float end){
|
168
|
text.setTextLimit(numberOfDigits);
|
169
|
this.start = start;
|
170
|
this.end = end;
|
171
|
}
|
172
|
|
173
|
/**
|
174
|
* <p>Getter for the field <code>exception</code>.</p>
|
175
|
*
|
176
|
* @return the exception
|
177
|
*/
|
178
|
public NumberFormatException getException() {
|
179
|
return exception;
|
180
|
}
|
181
|
}
|