Project

General

Profile

Download (4.54 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2007 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9

    
10
package eu.etaxonomy.taxeditor.ui.element;
11

    
12
import org.apache.commons.lang.StringUtils;
13
import org.eclipse.swt.SWT;
14
import org.eclipse.swt.events.ModifyEvent;
15
import org.eclipse.swt.widgets.Display;
16

    
17

    
18
/**
19
 * <p>NumberWithLabelElement class.</p>
20
 *
21
 * @author n.hoffmann
22
 * @created Mar 22, 2010
23
 * @version 1.0
24
 */
25
public class NumberWithLabelElement extends TextWithLabelElement {
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 initialNumber a {@link java.lang.Number} object.
39
	 * @param style a int.
40
	 */
41
	public NumberWithLabelElement(CdmFormFactory toolkit,
42
			ICdmFormElement parentElement, String labelString,
43
			Number initialNumber, int style) {
44
		super(toolkit, parentElement, labelString, null, null, style);
45
		setNumber(initialNumber);
46
	}
47

    
48

    
49
	/**
50
	 * <p>setNumber</p>
51
	 *
52
	 * @param number a {@link java.lang.Number} object.
53
	 */
54
	public void setNumber(Number number) {
55
		super.setText(getStringRepresentation(number));
56
	}
57

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

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

    
88
	/**
89
	 * Get the value of this field as an {@link Double}.
90
	 * @return the Double value or null if {@link NumberFormatException} occurs.
91
	 */
92
	public Double getDouble(){
93
	    String text = super.getText();
94
	    try {
95
	        return new Double(text);
96
	    } catch (NumberFormatException e) {
97
	        exception = e;
98
	    }
99
	    return null;
100
	}
101

    
102
	private String getStringRepresentation(Object number){
103
		if(number != null){
104
			return number.toString();
105
		}
106
		return null;
107
	}
108

    
109
	/* (non-Javadoc)
110
	 * @see eu.etaxonomy.taxeditor.forms.AbstractCdmFormElement#propertyChange(org.eclipse.jface.util.PropertyChangeEvent)
111
	 */
112
	/** {@inheritDoc} */
113
	@Override
114
	public void modifyText(ModifyEvent event) {
115
		String value = text.getText();
116
		if(StringUtils.isBlank(value)){
117
			text.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));
118
			super.modifyText(event);
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
				exception = new NumberFormatException("You entered a number that is not within the allowed bounds.");
128
				throw exception;
129
			}
130

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

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

    
141
		super.modifyText(event);
142
	}
143

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

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

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