Project

General

Profile

Download (2.56 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

    
31
    private BigDecimal start;
32
    private BigDecimal end;
33

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

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

    
56
        exception = null;
57
        text.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));
58

    
59
        super.modifyText(event);
60
    }
61

    
62
    /**
63
     * <p>setLimits</p>
64
     *
65
     * @param numberOfDigits a int.
66
     * @param start a {@link java.lang.Integer} object.
67
     * @param end a {@link java.lang.Integer} object.
68
     */
69

    
70
    public void setLimits(int numberOfDigits, BigDecimal start, BigDecimal end){
71
        text.setTextLimit(numberOfDigits);
72
        this.start = start;
73
        this.end = end;
74
    }
75

    
76
    public BigDecimal getBigDecimal(){
77
        String text = super.getText();
78
        try {
79
            return StringUtils.isBlank(text) ? null : new BigDecimal(text);
80
        } catch (NumberFormatException e) {
81
            exception = e;
82
        }
83
        return null;
84
    }
85

    
86
}
(6-6/51)