/*
 * Copyright 2004-2008 the Seasar Foundation and the Others.
 *
 * 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.
 */
package org.seasar.uruma.binding.value.binder;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.eclipse.swt.widgets.DateTime;
import org.seasar.framework.beans.PropertyDesc;
import org.seasar.uruma.binding.value.ValueBinder;
/**
 * {@link DateTime} のための {@link ValueBinder} です。
 * 
 * @author shhirose
 */
public class DateTimeValueBinder extends AbstractValueBinder {
    /**
     * {@link DateTimeValueBinder} を構築します。
     */
    public DateTimeValueBinder() {
        super(DateTime.class);
    }
    /*
     * @see org.seasar.uruma.binding.value.binder.AbstractValueBinder#doImportValue(java.lang.Object,
     *      java.lang.Object, org.seasar.framework.beans.PropertyDesc)
     */
    @Override
    protected void doImportValue(final DateTime widget, final Object formObj,
            final PropertyDesc propDesc) {
        String year = concatZero(widget.getYear(), 4);
        String month = concatZero(widget.getMonth() + 1, 2);
        String day = concatZero(widget.getDay(), 2);
        String hours = concatZero(widget.getHours(), 2);
        String minutes = concatZero(widget.getMinutes(), 2);
        String seconds = concatZero(widget.getSeconds(), 2);
        logBinding(IMPORT_VALUE, formObj, propDesc, widget, propDesc, year
                + month + day + hours + minutes + seconds);
        propDesc.setValue(formObj, year + month + day + hours + minutes
                + seconds);
    }
    /*
     * @see org.seasar.uruma.binding.value.binder.AbstractValueBinder#doExportValue(java.lang.Object,
     *      java.lang.Object, org.seasar.framework.beans.PropertyDesc)
     */
    @Override
    protected void doExportValue(final DateTime widget, final Object formObj,
            final PropertyDesc propDesc) {
        Object value = propDesc.getValue(formObj);
        SimpleDateFormat sdformat = new SimpleDateFormat("yyyyMMddHHmmss");
        String nowDate = sdformat.format(new Date());
        if (value == null) {
            value = nowDate;
        } else if (value instanceof String && ((String) value).length() == 0) {
            value = nowDate;
        } else if (((String) value).length() == 8) {
            value = new StringBuffer((String) value).append(
                    nowDate.substring(8)).toString();
        } else if (((String) value).length() == 6) {
            value = new StringBuffer(nowDate.substring(0, 8)).append(
                    (String) value).toString();
        } else if (((String) value).length() != 14) {
            value = nowDate;
        }
        logBinding(EXPORT_VALUE, formObj, propDesc, widget, propDesc, value);
        widget.setYear(Integer.parseInt(((String) value).substring(0, 4)));
        widget.setMonth(Integer.parseInt(((String) value).substring(4, 6)) - 1);
        widget.setDay(Integer.parseInt(((String) value).substring(6, 8)));
        widget.setHours(Integer.parseInt(((String) value).substring(8, 10)));
        widget.setMinutes(Integer.parseInt(((String) value).substring(10, 12)));
        widget.setSeconds(Integer.parseInt(((String) value).substring(12)));
    }
    /**
     * 数値の左側にsizeの大きさになるように0を追加する。
     * 
     * @param base
     *            元となる数値
     * @param size
     *            最終的な文字の大きさ
     * @return sizeの大きさの文字列
     */
    private String concatZero(final int base, final int size) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < size - Integer.toString(base).length(); i++) {
            sb.append("0");
        }
        return sb.append(base).toString();
    }
}