- changed layout so that two colums are used for label and text field
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / element / CdmNumberField.java
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 01.08.2013
23 *
24 */
25 public class CdmNumberField extends CdmTextField {
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 CdmNumberField(Composite parent, FormToolkit formFactory, ICdmFormElement parentFormElement, Integer initialInteger, int style){
42 this(parent, formFactory, parentFormElement, new Float(initialInteger==null?0:initialInteger), style);
43 }
44
45 /**
46 * <p>Constructor for NumberWithLabelElement.</p>
47 *
48 * @param toolkit a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory} object.
49 * @param parentElement a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement} object.
50 * @param labelString a {@link java.lang.String} object.
51 * @param initialFloat a {@link java.lang.Float} object.
52 * @param style a int.
53 * @wbp.parser.constructor
54 */
55 public CdmNumberField(Composite parent, FormToolkit formFactory,
56 ICdmFormElement parentElement, Float initialFloat, int style) {
57 super(parent, formFactory, parentElement, null, null, style);
58 //WindowBuilder exception handling
59 if(initialFloat==null){
60 initialFloat = 0f;
61 }
62 setFloat(initialFloat);
63 }
64
65 /**
66 * <p>setInteger</p>
67 *
68 * @param number a {@link java.lang.Integer} object.
69 */
70 public void setInteger(Integer number) {
71 super.setText(getStringRepresentation(number));
72 }
73
74 /**
75 * <p>setFloat</p>
76 *
77 * @param number a {@link java.lang.Float} object.
78 */
79 public void setFloat(Float number) {
80 super.setText(getStringRepresentation(number));
81 }
82
83 /**
84 * <p>getInteger</p>
85 *
86 * @return a {@link java.lang.Integer} object.
87 */
88 public Integer getInteger() {
89 String text = super.getText().trim();
90 return text.equals("") ? 0 : new Integer(text);
91 }
92
93 /**
94 * <p>getFloat</p>
95 *
96 * @return a {@link java.lang.Float} object.
97 */
98 public Float getFloat(){
99 String text = super.getText();
100 return new Float(text);
101 }
102
103 private String getStringRepresentation(Object number){
104 if(number != null){
105 return number.toString();
106 }
107 return null;
108 }
109
110 /* (non-Javadoc)
111 * @see eu.etaxonomy.taxeditor.forms.AbstractCdmFormElement#propertyChange(org.eclipse.jface.util.PropertyChangeEvent)
112 */
113 /** {@inheritDoc} */
114 @Override
115 public void modifyText(ModifyEvent event) {
116 String value = text.getText();
117 if(CdmUtils.isEmpty(value)){
118 text.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));
119 return;
120 }
121
122 try{
123
124 Float number = Float.parseFloat(value);
125
126 if((start != null && number < start) || (end != null && number > end)){
127 throw new NumberFormatException("You entered a number that is not within the allowed bounds.");
128 }
129
130 }catch(NumberFormatException e){
131 text.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_RED));
132 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, e));
133 exception = 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 }