Made MWebDateBox and MWebNumberBox also accept Date and BigDecimal values, respectively.
authorMarco Zanon <info@marcozanon.com>
Sun, 11 Mar 2012 14:18:00 +0000 (14:18 +0000)
committerMarco Zanon <info@marcozanon.com>
Sun, 11 Mar 2012 14:18:00 +0000 (14:18 +0000)
src/java/com/marcozanon/macaco/web/ui/MWebDateBox.java
src/java/com/marcozanon/macaco/web/ui/MWebNumberBox.java

index fc89489a6e8f187516c5f60d2553114f068c4959..b9cfb14cfb557f571ba0cce722c91d1198105b16 100644 (file)
@@ -8,6 +8,7 @@ package com.marcozanon.macaco.web.ui;
 
 import com.marcozanon.macaco.conversion.MDateConverter;
 import com.marcozanon.macaco.conversion.MFormatConversionException;
+import java.util.Date;
 
 public class MWebDateBox extends MWebTextBox {
 
@@ -31,6 +32,12 @@ public class MWebDateBox extends MWebTextBox {
         this.setText(text);
     }
 
+    public MWebDateBox(MWebApplicationContext applicationContext, MDateConverter dateConverter, Date date) {
+        this(applicationContext, dateConverter);
+        //
+        this.setDate(date);
+    }
+
     /* Date converter */
 
     protected MDateConverter getDateConverterReference() {
@@ -41,6 +48,30 @@ public class MWebDateBox extends MWebTextBox {
         return this.getDateConverterReference().clone();
     }
 
+    /* Date conversion */
+
+    public void setDate(Date date) {
+        if (null == date) {
+            this.setText("");
+        }
+        else {
+            try {
+                this.setText(this.getDateConverterReference().getStringFromDate(date));
+            }
+            catch (MFormatConversionException exception) { // cannot happen
+            }
+        }
+    }
+
+    public Date getDate() throws MFormatConversionException {
+        Date date = null;
+        String text = this.getText();
+        if (!"".equals(text)) {
+            return this.getDateConverterReference().getDateFromString(text);
+        }
+        return date;
+    }
+
     /* Validation */
 
     public void validate() throws MValidationWebException {
index f8ccf26a2a1c696c83015abaa656af3eb1dd774b..958c6a4f1eb3ed0a0a3fb4a3b6f8ed9c4dbe6c55 100644 (file)
@@ -8,6 +8,7 @@ package com.marcozanon.macaco.web.ui;
 
 import com.marcozanon.macaco.conversion.MFormatConversionException;
 import com.marcozanon.macaco.conversion.MNumberConverter;
+import java.math.BigDecimal;
 
 public class MWebNumberBox extends MWebTextBox {
 
@@ -31,6 +32,12 @@ public class MWebNumberBox extends MWebTextBox {
         this.setText(text);
     }
 
+    public MWebNumberBox(MWebApplicationContext applicationContext, MNumberConverter numberConverter, BigDecimal number) {
+        this(applicationContext, numberConverter);
+        //
+        this.setNumber(number);
+    }
+
     /* Number converter */
 
     protected MNumberConverter getNumberConverterReference() {
@@ -41,6 +48,30 @@ public class MWebNumberBox extends MWebTextBox {
         return this.getNumberConverterReference().clone();
     }
 
+    /* Number conversion */
+
+    public void setNumber(BigDecimal number) {
+        if (null == number) {
+            this.setText("");
+        }
+        else {
+            try {
+                this.setText(this.getNumberConverterReference().getStringFromNumber(number));
+            }
+            catch (MFormatConversionException exception) { // cannot happen
+            }
+        }
+    }
+
+    public BigDecimal getNumber() throws MFormatConversionException {
+        BigDecimal number = null;
+        String text = this.getText();
+        if (!"".equals(text)) {
+            return this.getNumberConverterReference().getNumberFromString(text);
+        }
+        return number;
+    }
+
     /* Validation */
 
     public void validate() throws MValidationWebException {