Project

General

Profile

Download (4.58 KB) Statistics
| Branch: | Tag: | Revision:
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.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
import eu.etaxonomy.taxeditor.ui.campanula.compatibility.ICdmFormElement;
19

    
20

    
21
/**
22
 * <p>NumberWithLabelElement class.</p>
23
 *
24
 * @author n.hoffmann
25
 * @created Mar 22, 2010
26
 * @version 1.0
27
 */
28
public class NumberWithLabelElement extends TextWithLabelElement {
29

    
30
	private Float start;
31
	private Float end;
32

    
33
	private NumberFormatException exception;
34

    
35
	/**
36
	 * <p>Constructor for NumberWithLabelElement.</p>
37
	 *
38
	 * @param toolkit a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory} object.
39
	 * @param parentElement a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement} object.
40
	 * @param labelString a {@link java.lang.String} object.
41
	 * @param initialInteger a {@link java.lang.Integer} object.
42
	 * @param style a int.
43
	 */
44
	public NumberWithLabelElement(CdmFormFactory toolkit,
45
			ICdmFormElement parentElement, String labelString,
46
			Integer initialInteger, int style) {
47
		super(toolkit, parentElement, labelString, null, null, style);
48
		setInteger(initialInteger);
49
	}
50

    
51
	/**
52
	 * <p>Constructor for NumberWithLabelElement.</p>
53
	 *
54
	 * @param toolkit a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory} object.
55
	 * @param parentElement a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement} object.
56
	 * @param labelString a {@link java.lang.String} object.
57
	 * @param initialFloat a {@link java.lang.Float} object.
58
	 * @param style a int.
59
	 */
60
	public NumberWithLabelElement(CdmFormFactory toolkit,
61
			ICdmFormElement parentElement, String labelString,
62
			Float initialFloat, int style) {
63
		super(toolkit, parentElement, labelString, null, null, style);
64
		setFloat(initialFloat);
65
	}
66

    
67
	/**
68
	 * <p>setInteger</p>
69
	 *
70
	 * @param number a {@link java.lang.Integer} object.
71
	 */
72
	public void setInteger(Integer number) {
73
		super.setText(getStringRepresentation(number));
74
	}
75

    
76
	/**
77
	 * <p>setFloat</p>
78
	 *
79
	 * @param number a {@link java.lang.Float} object.
80
	 */
81
	public void setFloat(Float number) {
82
		super.setText(getStringRepresentation(number));
83
	}
84

    
85
	/**
86
	 * <p>getInteger</p>
87
	 *
88
	 * @return a {@link java.lang.Integer} object.
89
	 */
90
	public Integer getInteger() {
91
		String text = super.getText().trim();
92
		return text.equals("") ? 0 : new Integer(text);
93
	}
94

    
95
	/**
96
	 * <p>getFloat</p>
97
	 *
98
	 * @return a {@link java.lang.Float} object.
99
	 */
100
	public Float getFloat(){
101
		String text = super.getText();
102
		return new Float(text);
103
	}
104

    
105
	private String getStringRepresentation(Object number){
106
		if(number != null){
107
			return number.toString();
108
		}
109
		return null;
110
	}
111

    
112
	/* (non-Javadoc)
113
	 * @see eu.etaxonomy.taxeditor.forms.AbstractCdmFormElement#propertyChange(org.eclipse.jface.util.PropertyChangeEvent)
114
	 */
115
	/** {@inheritDoc} */
116
	@Override
117
	public void modifyText(ModifyEvent event) {
118
		String value = text.getText();
119
		if(CdmUtils.isEmpty(value)){
120
			text.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));
121
			return;
122
		}
123

    
124
		try{
125

    
126
			Float number = Float.parseFloat(value);
127

    
128
			if((start != null && number < start) || (end != null && number > end)){
129
				throw new NumberFormatException("You entered a number that is not within the allowed bounds.");
130
			}
131

    
132
		}catch(NumberFormatException e){
133
			text.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_RED));
134
			firePropertyChangeEvent(new CdmPropertyChangeEvent(this, e));
135
			exception = e;
136
			return;
137
		}
138

    
139
		exception = null;
140
		text.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));
141

    
142
		super.modifyText(event);
143
	}
144

    
145
	/**
146
	 * <p>setLimits</p>
147
	 *
148
	 * @param numberOfDigits a int.
149
	 * @param start a {@link java.lang.Integer} object.
150
	 * @param end a {@link java.lang.Integer} object.
151
	 */
152
	public void setLimits(int numberOfDigits, Integer start, Integer end){
153
		setLimits(numberOfDigits, start.floatValue(), end.floatValue());
154
	}
155

    
156
	/**
157
	 * <p>setLimits</p>
158
	 *
159
	 * @param numberOfDigits a int.
160
	 * @param start a {@link java.lang.Float} object.
161
	 * @param end a {@link java.lang.Float} object.
162
	 */
163
	public void setLimits(int numberOfDigits, Float start, Float end){
164
		text.setTextLimit(numberOfDigits);
165
		this.start = start;
166
		this.end = end;
167
	}
168

    
169
	/**
170
	 * <p>Getter for the field <code>exception</code>.</p>
171
	 *
172
	 * @return the exception
173
	 */
174
	public NumberFormatException getException() {
175
		return exception;
176
	}
177
}
(26-26/35)