83e229bd4272bfeab55ed24bfa02f51964cc156c
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / element / NumberWithLabelElement.java
1 // $Id$
2 /**
3 * Copyright (C) 2007 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
11 package eu.etaxonomy.taxeditor.ui.element;
12
13 import org.apache.commons.lang.StringUtils;
14 import org.eclipse.swt.SWT;
15 import org.eclipse.swt.events.ModifyEvent;
16 import org.eclipse.swt.widgets.Display;
17
18
19 /**
20 * <p>NumberWithLabelElement class.</p>
21 *
22 * @author n.hoffmann
23 * @created Mar 22, 2010
24 * @version 1.0
25 */
26 public class NumberWithLabelElement extends TextWithLabelElement {
27
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 initialNumber a {@link java.lang.Number} object.
40 * @param style a int.
41 */
42 public NumberWithLabelElement(CdmFormFactory toolkit,
43 ICdmFormElement parentElement, String labelString,
44 Number initialNumber, int style) {
45 super(toolkit, parentElement, labelString, null, null, style);
46 setNumber(initialNumber);
47 }
48
49
50 /**
51 * <p>setNumber</p>
52 *
53 * @param number a {@link java.lang.Number} object.
54 */
55 public void setNumber(Number number) {
56 super.setText(getStringRepresentation(number));
57 }
58
59 /**
60 * Get the value of this field as an {@link Integer}.
61 * @return the Integer value or null if {@link NumberFormatException} occurs.
62 */
63 public Integer getInteger() {
64 String text = super.getText().trim();
65 try {
66 return text.equals("") ? 0 : new Integer(text);
67 } catch (NumberFormatException e) {
68 exception = e;
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 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 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 /* (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(StringUtils.isBlank(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 exception = new NumberFormatException("You entered a number that is not within the allowed bounds.");
126 throw exception;
127 }
128
129 }catch(NumberFormatException e){
130 text.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_RED));
131 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, event));
132 exception = e;
133 return;
134 }
135
136 exception = null;
137 text.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));
138
139 super.modifyText(event);
140 }
141
142 /**
143 * <p>setLimits</p>
144 *
145 * @param numberOfDigits a int.
146 * @param start a {@link java.lang.Integer} object.
147 * @param end a {@link java.lang.Integer} object.
148 */
149 public void setLimits(int numberOfDigits, Integer start, Integer end){
150 setLimits(numberOfDigits, start.floatValue(), end.floatValue());
151 }
152
153 /**
154 * <p>setLimits</p>
155 *
156 * @param numberOfDigits a int.
157 * @param start a {@link java.lang.Float} object.
158 * @param end a {@link java.lang.Float} object.
159 */
160 public void setLimits(int numberOfDigits, Float start, Float end){
161 text.setTextLimit(numberOfDigits);
162 this.start = start;
163 this.end = end;
164 }
165
166 /**
167 * <p>Getter for the field <code>exception</code>.</p>
168 *
169 * @return the exception
170 */
171 public NumberFormatException getException() {
172 return exception;
173 }
174 }