merged campanula branch to trunk. Main features are: BioCase Query via Imports, Deriv...
[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 import eu.etaxonomy.taxeditor.ui.campanula.compatibility.ICdmFormElement;
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.element.CdmFormFactory} object.
38 * @param parentElement a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement} object.
39 * @param labelString a {@link java.lang.String} object.
40 * @param initialNumber a {@link java.lang.Number} object.
41 * @param style a int.
42 */
43 public NumberWithLabelElement(CdmFormFactory toolkit,
44 ICdmFormElement parentElement, String labelString,
45 Number initialNumber, int style) {
46 super(toolkit, parentElement, labelString, null, null, style);
47 setNumber(initialNumber);
48 }
49
50
51 /**
52 * <p>setNumber</p>
53 *
54 * @param number a {@link java.lang.Number} object.
55 */
56 public void setNumber(Number number) {
57 super.setText(getStringRepresentation(number));
58 }
59
60 /**
61 * <p>getInteger</p>
62 *
63 * @return a {@link java.lang.Integer} object.
64 */
65 public Integer getInteger() {
66 String text = super.getText().trim();
67 return text.equals("") ? 0 : new Integer(text);
68 }
69
70 /**
71 * <p>getFloat</p>
72 *
73 * @return a {@link java.lang.Float} object.
74 */
75 public Float getFloat(){
76 String text = super.getText();
77 return new Float(text);
78 }
79
80 /**
81 * <p>getDouble</p>
82 *
83 * @return a {@link java.lang.Float} object.
84 */
85 public Double getDouble(){
86 String text = super.getText();
87 return new Double(text);
88 }
89
90 private String getStringRepresentation(Object number){
91 if(number != null){
92 return number.toString();
93 }
94 return null;
95 }
96
97 /* (non-Javadoc)
98 * @see eu.etaxonomy.taxeditor.forms.AbstractCdmFormElement#propertyChange(org.eclipse.jface.util.PropertyChangeEvent)
99 */
100 /** {@inheritDoc} */
101 @Override
102 public void modifyText(ModifyEvent event) {
103 String value = text.getText();
104 if(StringUtils.isBlank(value)){
105 text.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));
106 return;
107 }
108
109 try{
110
111 Float number = Float.parseFloat(value);
112
113 if((start != null && number < start) || (end != null && number > end)){
114 throw new NumberFormatException("You entered a number that is not within the allowed bounds.");
115 }
116
117 }catch(NumberFormatException e){
118 text.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_RED));
119 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, e));
120 exception = e;
121 return;
122 }
123
124 exception = null;
125 text.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));
126
127 super.modifyText(event);
128 }
129
130 /**
131 * <p>setLimits</p>
132 *
133 * @param numberOfDigits a int.
134 * @param start a {@link java.lang.Integer} object.
135 * @param end a {@link java.lang.Integer} object.
136 */
137 public void setLimits(int numberOfDigits, Integer start, Integer end){
138 setLimits(numberOfDigits, start.floatValue(), end.floatValue());
139 }
140
141 /**
142 * <p>setLimits</p>
143 *
144 * @param numberOfDigits a int.
145 * @param start a {@link java.lang.Float} object.
146 * @param end a {@link java.lang.Float} object.
147 */
148 public void setLimits(int numberOfDigits, Float start, Float end){
149 text.setTextLimit(numberOfDigits);
150 this.start = start;
151 this.end = end;
152 }
153
154 /**
155 * <p>Getter for the field <code>exception</code>.</p>
156 *
157 * @return the exception
158 */
159 public NumberFormatException getException() {
160 return exception;
161 }
162 }