ref #9114 adapt some classes with URI to wrapper in taxeditor (cont.)
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / element / BigDecimalWithLabelElement.java
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
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{0,9}(.\\d{0,9})?";
44 if (value.matches(pattern)){
45 if ("-".equals(value)){
46 //accept entry
47 }else{
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.");
51 throw exception;
52 }
53 }
54 }else{
55 exception = new NumberFormatException("You entered a number that is not within the allowed bounds.");
56 throw exception;
57 }
58 }catch(NumberFormatException e){
59 text.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_RED));
60 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, event));
61 exception = e;
62 return;
63 }
64
65 exception = null;
66 text.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));
67
68 super.modifyText(event);
69 }
70
71 /**
72 * <p>setLimits</p>
73 *
74 * @param numberOfDigits a int.
75 * @param start a {@link java.lang.Integer} object.
76 * @param end a {@link java.lang.Integer} object.
77 */
78
79 public void setLimits(int numberOfDigits, BigDecimal start, BigDecimal end){
80 text.setTextLimit(numberOfDigits);
81 this.start = start;
82 this.end = end;
83 }
84
85 public BigDecimal getBigDecimal(){
86 String text = super.getText();
87 if (StringUtils.isBlank(text) || "-".equals(text)){
88 return null;
89 }
90 try {
91 return new BigDecimal(text);
92 } catch (NumberFormatException e) {
93 exception = e;
94 return null;
95 }
96 }
97 }