Project

General

Profile

Download (2.85 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.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
 * @author k.luther
20
 * @since May 27, 2020
21
 */
22
public class BigDecimalWithLabelElement extends NumberWithLabelElement {
23

    
24

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

    
30
    private BigDecimal start;
31
    private BigDecimal end;
32

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

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

    
61
        exception = null;
62
        text.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));
63

    
64
        super.modifyText(event);
65
    }
66

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

    
75
    public void setLimits(int numberOfDigits, BigDecimal start, BigDecimal end){
76
        text.setTextLimit(numberOfDigits);
77
        this.start = start;
78
        this.end = end;
79
    }
80

    
81
    public BigDecimal getBigDecimal(){
82
        String text = super.getText();
83
        try {
84
            return StringUtils.isBlank(text) ? null : new BigDecimal(text);
85
        } catch (NumberFormatException e) {
86
            exception = e;
87
            return null;
88
        }
89
    }
90
}
(6-6/51)