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