/*
 * #%L
 * fujion
 * %%
 * Copyright (C) 2008 - 2017 Regenstrief Institute, Inc.
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * #L%
 */
package org.fujion.component;

import java.util.Date;

import org.apache.commons.lang.time.FastDateFormat;
import org.fujion.annotation.Component;
import org.fujion.annotation.Component.PropertyGetter;
import org.fujion.annotation.Component.PropertySetter;
import org.fujion.common.DateUtil;

/**
 * An input box for entering dates.
 */
@Component(tag = "datebox", widgetClass = "Datebox", parentTag = "*", description = "An input box for entering dates.")
public class Datebox extends BaseInputboxComponent<Date> {

    private static final FastDateFormat serializer = FastDateFormat.getInstance("yyyy-MM-dd");

    private FastDateFormat formatter = FastDateFormat.getDateInstance(FastDateFormat.SHORT);

    private String format;

    @Override
    protected Date _toValue(String value) {
        return DateUtil.parseDate(value);
    }

    @Override
    protected String _toString(Date value) {
        return formatter.format(value);
    }

    @Override
    protected String _toClient(Date value) {
        return serializer.format(value);
    }

    /**
     * Returns the format for displaying the date.
     *
     * @return Format for displaying the date.
     * @see java.text.SimpleDateFormat
     */
    @PropertyGetter(value = "format", description = "The format for displaying the date.")
    public String getFormat() {
        return format;
    }

    /**
     * Sets the format for displaying the date.
     *
     * @param format Format for displaying the date.
     * @see java.text.SimpleDateFormat
     */
    @PropertySetter(value = "format", defaultValue = "yyyy-MM-dd", description = "The format for displaying the date.")
    public void setFormat(String format) {
        if (!areEqual(format = trimify(format), this.format)) {
            this.format = format;
            formatter = format == null ? FastDateFormat.getDateInstance(FastDateFormat.SHORT)
                    : FastDateFormat.getInstance(format);
        }
    }

}

@@ -1168,17 +1168,21 @@
 Date%3E %7B%0A
+    
 %0A
-
     priv
@@ -1268,16 +1268,20 @@
 M-dd%22);%0A
+    
 %0A    pri
@@ -1316,62 +1316,24 @@
 r = 
-FastDateFormat.getDateInstance(FastDateFormat.SHORT);%0A
+serializer;%0A    
 %0A   
@@ -1356,17 +1356,21 @@
 format;%0A
+    
 %0A
-
     @Ove
@@ -1459,33 +1459,37 @@
 e(value);%0A    %7D%0A
+    
 %0A
-
     @Override%0A  
@@ -1573,25 +1573,29 @@
 lue);%0A    %7D%0A
+    
 %0A
-
     @Overrid
@@ -1680,33 +1680,37 @@
 t(value);%0A    %7D%0A
+    
 %0A
-
     /**%0A     * R
@@ -2005,17 +2005,21 @@
 ;%0A    %7D%0A
+    
 %0A
-
     /**%0A
@@ -2404,42 +2404,8 @@
 ) %7B%0A
-            this.format = format;%0A
     
@@ -2440,16 +2440,29 @@
 = null ?
+ serializer :
  FastDat
@@ -2472,20 +2472,16 @@
 rmat.get
-Date
 Instance
@@ -2485,85 +2485,94 @@
 nce(
-FastDateFormat.SHORT)%0A                    : FastDateFormat.getInstance(format
+format);%0A            propertyChange(%22format%22, this.format, this.format = format, false
 );%0A 
@@ -2586,11 +2586,15 @@
 %7D%0A    %7D%0A
+    
 %0A%7D%0A

Fix issues with Datebox format.