Project

General

Profile

Download (3.05 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 java.math.BigDecimal;
12

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

    
18
/**
19
 * @author k.luther
20
 * @since May 27, 2020
21
 */
22
public class BigDecimalWithLabelElement extends NumberWithLabelElement {
23

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

    
29
    private BigDecimal start;
30
    private BigDecimal end;
31

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

    
41
        try{
42
            String pattern = "-?\\d{0,9}(.\\d{0,9})?";
43
            if (value.matches(pattern)){
44
                if ("-".equals(value)){
45
                    //accept entry
46
                }else{
47
                    BigDecimal number = new BigDecimal(value);
48
                    if((start != null && number.compareTo(start) < 0 || (end != null && number.compareTo(end) > 0))){
49
                        exception = new NumberFormatException("You entered a number that is not within the allowed bounds.");
50
                        throw exception;
51
                    }
52
                }
53
            }else{
54
                exception = new NumberFormatException("You entered a number that is not within the allowed bounds.");
55
                throw exception;
56
            }
57
        }catch(NumberFormatException e){
58
            text.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_RED));
59
            firePropertyChangeEvent(new CdmPropertyChangeEvent(this, event));
60
            exception = e;
61
            return;
62
        }
63

    
64
        exception = null;
65
        text.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));
66

    
67
        super.modifyText(event);
68
    }
69

    
70
    /**
71
     * <p>setLimits</p>
72
     *
73
     * @param numberOfDigits a int.
74
     * @param start a {@link java.lang.Integer} object.
75
     * @param end a {@link java.lang.Integer} object.
76
     */
77

    
78
    public void setLimits(int numberOfDigits, BigDecimal start, BigDecimal end){
79
        text.setTextLimit(numberOfDigits);
80
        this.start = start;
81
        this.end = end;
82
    }
83

    
84
    public BigDecimal getBigDecimal(){
85
        String text = super.getText();
86
        if (StringUtils.isBlank(text) || "-".equals(text)){
87
            return null;
88
        }
89
        try {
90
            return new BigDecimal(text);
91
        } catch (NumberFormatException e) {
92
            exception = e;
93
            return null;
94
        }
95
    }
96
}
(6-6/53)