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