2 * Copyright (C) 2020 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.taxeditor
.ui
.element
;
11 import java
.math
.BigDecimal
;
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
;
22 public class BigDecimalWithLabelElement
extends NumberWithLabelElement
{
25 public BigDecimalWithLabelElement(CdmFormFactory toolkit
, ICdmFormElement parentElement
, String labelString
,
26 Number initialNumber
, int style
) {
27 super(toolkit
, parentElement
, labelString
, initialNumber
, style
);
30 private BigDecimal start
;
31 private BigDecimal end
;
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
);
43 String pattern
= "-?\\d{0,9}(.\\d{0,9})?";
44 if (value
.matches(pattern
)){
45 if ("-".equals(value
)){
48 BigDecimal number
= new BigDecimal(value
);
49 if((start
!= null && number
.compareTo(start
) < 0 || (end
!= null && number
.compareTo(end
) > 0))){
50 exception
= new NumberFormatException("You entered a number that is not within the allowed bounds.");
55 exception
= new NumberFormatException("You entered a number that is not within the allowed bounds.");
58 }catch(NumberFormatException e
){
59 text
.setBackground(Display
.getCurrent().getSystemColor(SWT
.COLOR_RED
));
60 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, event
));
66 text
.setBackground(Display
.getCurrent().getSystemColor(SWT
.COLOR_WHITE
));
68 super.modifyText(event
);
74 * @param numberOfDigits a int.
75 * @param start a {@link java.lang.Integer} object.
76 * @param end a {@link java.lang.Integer} object.
79 public void setLimits(int numberOfDigits
, BigDecimal start
, BigDecimal end
){
80 text
.setTextLimit(numberOfDigits
);
85 public BigDecimal
getBigDecimal(){
86 String text
= super.getText();
87 if (StringUtils
.isBlank(text
) || "-".equals(text
)){
91 return new BigDecimal(text
);
92 } catch (NumberFormatException e
) {