fixing NPE for empty float values #4448
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / element / NumberWithLabelElement.java
index d441b0b2fe51f9fabbfab3d6d0abad4cc0f09e3c..72cc267f39235518d477ffa8715b0502f82a9744 100644 (file)
 
 package eu.etaxonomy.taxeditor.ui.element;
 
+import org.apache.commons.lang.StringUtils;
 import org.eclipse.swt.SWT;
 import org.eclipse.swt.events.ModifyEvent;
 import org.eclipse.swt.widgets.Display;
 
-import eu.etaxonomy.cdm.common.CdmUtils;
-import eu.etaxonomy.taxeditor.ui.campanula.compatibility.ICdmFormElement;
-
 
 /**
  * <p>NumberWithLabelElement class.</p>
@@ -59,35 +57,49 @@ public class NumberWithLabelElement extends TextWithLabelElement {
        }
 
        /**
-        * <p>getInteger</p>
-        *
-        * @return a {@link java.lang.Integer} object.
+        * Get the value of this field as an {@link Integer}.
+        * @return the Integer value or null if {@link NumberFormatException} occurs.
         */
        public Integer getInteger() {
-               String text = super.getText().trim();
-               return text.equals("") ? 0 : new Integer(text);
+           if(super.getText()!=null){
+               String text = super.getText().trim();
+               try {
+                   return StringUtils.isBlank(text) ? 0 : new Integer(text);
+               } catch (NumberFormatException e) {
+                   exception = e;
+               }
+           }
+               return null;
        }
 
        /**
-        * <p>getFloat</p>
-        *
-        * @return a {@link java.lang.Float} object.
+        * Get the value of this field as a {@link Float}.
+        * @return the Float value or null if {@link NumberFormatException} occurs.
         */
        public Float getFloat(){
-               String text = super.getText();
-               return new Float(text);
+           String text = super.getText();
+           try {
+               return StringUtils.isBlank(text) ? 0 : new Float(text);
+           } catch (NumberFormatException e) {
+               exception = e;
+           }
+           return null;
        }
 
        /**
-        * <p>getDouble</p>
-        *
-        * @return a {@link java.lang.Float} object.
+        * Get the value of this field as an {@link Double}.
+        * @return the Double value or null if {@link NumberFormatException} occurs.
         */
        public Double getDouble(){
-               String text = super.getText();
-               return new Double(text);
+           String text = super.getText();
+           try {
+               return new Double(text);
+           } catch (NumberFormatException e) {
+               exception = e;
+           }
+           return null;
        }
-       
+
        private String getStringRepresentation(Object number){
                if(number != null){
                        return number.toString();
@@ -102,22 +114,24 @@ public class NumberWithLabelElement extends TextWithLabelElement {
        @Override
        public void modifyText(ModifyEvent event) {
                String value = text.getText();
-               if(CdmUtils.isEmpty(value)){
+               if(StringUtils.isBlank(value)){
                        text.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));
+                       super.modifyText(event);
                        return;
                }
 
                try{
 
-                       Float number = Float.parseFloat(value);
+                       Float number = Float.parseFloat(value);
 
                        if((start != null && number < start) || (end != null && number > end)){
-                               throw new NumberFormatException("You entered a number that is not within the allowed bounds.");
+                               exception = new NumberFormatException("You entered a number that is not within the allowed bounds.");
+                               throw exception;
                        }
 
                }catch(NumberFormatException e){
                        text.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_RED));
-                       firePropertyChangeEvent(new CdmPropertyChangeEvent(this, e));
+                       firePropertyChangeEvent(new CdmPropertyChangeEvent(this, event));
                        exception = e;
                        return;
                }