Project

General

Profile

Download (2.35 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2020 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
package eu.etaxonomy.taxeditor.ui.element;
10

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

    
16
/**
17
 * @author k.luther
18
 * @since May 27, 2020
19
 */
20
public class FloatWithLabelElement extends NumberWithLabelElement {
21

    
22
    private Float start;
23
    private Float end;
24

    
25
    public FloatWithLabelElement(CdmFormFactory toolkit, ICdmFormElement parentElement, String labelString,
26
            Number initialNumber, int style) {
27
        super(toolkit, parentElement, labelString, initialNumber, style);
28
    }
29

    
30
    @Override
31
    public void modifyText(ModifyEvent event) {
32
        String value = text.getText();
33
        if(StringUtils.isBlank(value)){
34
            text.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));
35
            super.modifyText(event);
36
            return;
37
        }
38

    
39
        try{
40
            Float number = Float.parseFloat(value);
41
            if((start != null && number < start) || (end != null && number > end)){
42
                exception = new NumberFormatException("You entered a number that is not within the allowed bounds.");
43
                throw exception;
44
            }
45
        }catch(NumberFormatException e){
46
            text.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_RED));
47
            firePropertyChangeEvent(new CdmPropertyChangeEvent(this, event));
48
            exception = e;
49
            return;
50
        }
51

    
52
        exception = null;
53
        text.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));
54

    
55
        super.modifyText(event);
56
    }
57

    
58
    /**
59
     * <p>setLimits</p>
60
     *
61
     * @param numberOfDigits a int.
62
     * @param start a {@link java.lang.Float} object.
63
     * @param end a {@link java.lang.Float} object.
64
     */
65
    public void setLimits(int numberOfDigits, Float start, Float end){
66
        text.setTextLimit(numberOfDigits);
67
        this.start = start;
68
        this.end = end;
69
    }
70

    
71
    public void setLimits(int numberOfDigits, Integer start, Integer end){
72
        setLimits(numberOfDigits, start.floatValue(), end.floatValue());
73
    }
74
}
(19-19/58)