Merge branch 'release/5.4.0'
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / element / NumberWithLabelElement.java
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 * <p>Constructor for NumberWithLabelElement.</p>
49 *
50 * @param toolkit a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory} object.
51 * @param parentElement a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement} object.
52 * @param labelString a {@link java.lang.String} object.
53 * @param initialNumber a {@link java.lang.Number} object.
54 * @param style a int.
55 */
56 public NumberWithLabelElement(CdmFormFactory toolkit,
57 ICdmFormElement parentElement, String labelString,
58 Number initialNumber, Integer height, Integer length, int style) {
59 super(toolkit, parentElement, labelString, null, null, style);
60 setNumber(initialNumber);
61 }
62
63
64 /**
65 * <p>setNumber</p>
66 *
67 * @param number a {@link java.lang.Number} object.
68 */
69 public void setNumber(Number number) {
70 super.setText(getStringRepresentation(number));
71 }
72
73 /**
74 * Get the value of this field as an {@link Integer}.
75 * @return the Integer value or null if {@link NumberFormatException} occurs.
76 */
77 public Integer getInteger() {
78 if(super.getText()!=null){
79 String text = super.getText().trim();
80 try {
81 return StringUtils.isBlank(text) ? null : new Integer(text);
82 } catch (NumberFormatException e) {
83 exception = e;
84 }
85 }
86 return null;
87 }
88
89 /**
90 * Get the value of this field as a {@link Float}.
91 * @return the Float value or null if {@link NumberFormatException} occurs.
92 */
93 public Float getFloat(){
94 String text = super.getText();
95 try {
96 return StringUtils.isBlank(text) ? null : new Float(text);
97 } catch (NumberFormatException e) {
98 exception = e;
99 }
100 return null;
101 }
102
103 /**
104 * Get the value of this field as an {@link Double}.
105 * @return the Double value or null if {@link NumberFormatException} occurs.
106 */
107 public Double getDouble(){
108 String text = super.getText();
109 try {
110 return StringUtils.isBlank(text) ? null : new Double(text);
111 } catch (NumberFormatException e) {
112 exception = e;
113 }
114 return null;
115 }
116
117 private String getStringRepresentation(Object number){
118 if(number != null){
119 return number.toString();
120 }
121 return null;
122 }
123
124 /** {@inheritDoc} */
125 @Override
126 public void modifyText(ModifyEvent event) {
127 String value = text.getText();
128 if(StringUtils.isBlank(value)){
129 text.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));
130 super.modifyText(event);
131 return;
132 }
133
134 try{
135 Float number = Float.parseFloat(value);
136 if((start != null && number < start) || (end != null && number > end)){
137 exception = new NumberFormatException("You entered a number that is not within the allowed bounds.");
138 throw exception;
139 }
140 }catch(NumberFormatException e){
141 text.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_RED));
142 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, event));
143 exception = e;
144 return;
145 }
146
147 exception = null;
148 text.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));
149
150 super.modifyText(event);
151 }
152
153 /**
154 * <p>setLimits</p>
155 *
156 * @param numberOfDigits a int.
157 * @param start a {@link java.lang.Integer} object.
158 * @param end a {@link java.lang.Integer} object.
159 */
160 public void setLimits(int numberOfDigits, Integer start, Integer end){
161 setLimits(numberOfDigits, start.floatValue(), end.floatValue());
162 }
163
164 /**
165 * <p>setLimits</p>
166 *
167 * @param numberOfDigits a int.
168 * @param start a {@link java.lang.Float} object.
169 * @param end a {@link java.lang.Float} object.
170 */
171 public void setLimits(int numberOfDigits, Float start, Float end){
172 text.setTextLimit(numberOfDigits);
173 this.start = start;
174 this.end = end;
175 }
176
177 /**
178 * <p>Getter for the field <code>exception</code>.</p>
179 *
180 * @return the exception
181 */
182 public NumberFormatException getException() {
183 return exception;
184 }
185 }