Project

General

Profile

Download (4.55 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
	 * Get the value of this field as an {@link Integer}.
61
	 * @return the Integer value or null if {@link NumberFormatException} occurs.
62
	 */
63
	public Integer getInteger() {
64
	    if(super.getText()!=null){
65
	        String text = super.getText().trim();
66
	        try {
67
	            return StringUtils.isBlank(text) ? 0 : new Integer(text);
68
	        } catch (NumberFormatException e) {
69
	            exception = e;
70
	        }
71
	    }
72
		return null;
73
	}
74

    
75
	/**
76
	 * Get the value of this field as a {@link Float}.
77
	 * @return the Float value or null if {@link NumberFormatException} occurs.
78
	 */
79
	public Float getFloat(){
80
	    String text = super.getText();
81
	    try {
82
	    	return StringUtils.isBlank(text) ? 0 : new Float(text);
83
	    } catch (NumberFormatException e) {
84
	        exception = e;
85
	    }
86
	    return null;
87
	}
88

    
89
	/**
90
	 * Get the value of this field as an {@link Double}.
91
	 * @return the Double value or null if {@link NumberFormatException} occurs.
92
	 */
93
	public Double getDouble(){
94
	    String text = super.getText();
95
	    try {
96
	        return new Double(text);
97
	    } catch (NumberFormatException e) {
98
	        exception = e;
99
	    }
100
	    return null;
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(StringUtils.isBlank(value)){
118
			text.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));
119
			super.modifyText(event);
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
				exception = new NumberFormatException("You entered a number that is not within the allowed bounds.");
129
				throw exception;
130
			}
131

    
132
		}catch(NumberFormatException e){
133
			text.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_RED));
134
			firePropertyChangeEvent(new CdmPropertyChangeEvent(this, event));
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
}
(32-32/42)