Project

General

Profile

Download (3.89 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.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
	 * <p>getInteger</p>
61
	 *
62
	 * @return a {@link java.lang.Integer} object.
63
	 */
64
	public Integer getInteger() {
65
		String text = super.getText().trim();
66
		return text.equals("") ? 0 : new Integer(text);
67
	}
68

    
69
	/**
70
	 * <p>getFloat</p>
71
	 *
72
	 * @return a {@link java.lang.Float} object.
73
	 */
74
	public Float getFloat(){
75
		String text = super.getText();
76
		return new Float(text);
77
	}
78

    
79
	/**
80
	 * <p>getDouble</p>
81
	 *
82
	 * @return a {@link java.lang.Float} object.
83
	 */
84
	public Double getDouble(){
85
		String text = super.getText();
86
		return new Double(text);
87
	}
88
	
89
	private String getStringRepresentation(Object number){
90
		if(number != null){
91
			return number.toString();
92
		}
93
		return null;
94
	}
95

    
96
	/* (non-Javadoc)
97
	 * @see eu.etaxonomy.taxeditor.forms.AbstractCdmFormElement#propertyChange(org.eclipse.jface.util.PropertyChangeEvent)
98
	 */
99
	/** {@inheritDoc} */
100
	@Override
101
	public void modifyText(ModifyEvent event) {
102
		String value = text.getText();
103
		if(StringUtils.isBlank(value)){
104
			text.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));
105
			return;
106
		}
107

    
108
		try{
109

    
110
			Float number = Float.parseFloat(value);
111

    
112
			if((start != null && number < start) || (end != null && number > end)){
113
				throw new NumberFormatException("You entered a number that is not within the allowed bounds.");
114
			}
115

    
116
		}catch(NumberFormatException e){
117
			text.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_RED));
118
			firePropertyChangeEvent(new CdmPropertyChangeEvent(this, e));
119
			exception = e;
120
			return;
121
		}
122

    
123
		exception = null;
124
		text.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));
125

    
126
		super.modifyText(event);
127
	}
128

    
129
	/**
130
	 * <p>setLimits</p>
131
	 *
132
	 * @param numberOfDigits a int.
133
	 * @param start a {@link java.lang.Integer} object.
134
	 * @param end a {@link java.lang.Integer} object.
135
	 */
136
	public void setLimits(int numberOfDigits, Integer start, Integer end){
137
		setLimits(numberOfDigits, start.floatValue(), end.floatValue());
138
	}
139

    
140
	/**
141
	 * <p>setLimits</p>
142
	 *
143
	 * @param numberOfDigits a int.
144
	 * @param start a {@link java.lang.Float} object.
145
	 * @param end a {@link java.lang.Float} object.
146
	 */
147
	public void setLimits(int numberOfDigits, Float start, Float end){
148
		text.setTextLimit(numberOfDigits);
149
		this.start = start;
150
		this.end = end;
151
	}
152

    
153
	/**
154
	 * <p>Getter for the field <code>exception</code>.</p>
155
	 *
156
	 * @return the exception
157
	 */
158
	public NumberFormatException getException() {
159
		return exception;
160
	}
161
}
(30-30/39)