Coverage Report - org.seasar.uruma.binding.value.binder.DateTimeValueBinder
 
Classes in this File Line Coverage Branch Coverage Complexity
DateTimeValueBinder
58%
15/26
50%
2/4
0
 
 1  
 /*
 2  
  * Copyright 2004-2008 the Seasar Foundation and the Others.
 3  
  *
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  *     http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
 13  
  * either express or implied. See the License for the specific language
 14  
  * governing permissions and limitations under the License.
 15  
  */
 16  
 package org.seasar.uruma.binding.value.binder;
 17  
 
 18  
 import java.text.DecimalFormat;
 19  
 import java.text.ParseException;
 20  
 import java.text.SimpleDateFormat;
 21  
 import java.util.Calendar;
 22  
 import java.util.Date;
 23  
 
 24  
 import org.eclipse.swt.widgets.DateTime;
 25  
 import org.seasar.framework.beans.PropertyDesc;
 26  
 import org.seasar.uruma.binding.value.ValueBinder;
 27  
 import org.seasar.uruma.component.UIComponent;
 28  
 
 29  
 /**
 30  
  * {@link DateTime} のための {@link ValueBinder} です。<br />
 31  
  * 
 32  
  * @author shhirose
 33  
  */
 34  8
 public class DateTimeValueBinder extends AbstractValueBinder<DateTime> {
 35  
 
 36  
     /**
 37  
      * {@link DateTimeValueBinder} を構築します。<br />
 38  
      */
 39  
     public DateTimeValueBinder() {
 40  4
         super(DateTime.class);
 41  4
     }
 42  
 
 43  
     /*
 44  
      * @see org.seasar.uruma.binding.value.binder.AbstractValueBinder#doImportValue(java.lang.Object,
 45  
      *      java.lang.Object, org.seasar.framework.beans.PropertyDesc)
 46  
      */
 47  
     @Override
 48  
     protected void doImportValue(final DateTime widget, final Object formObj,
 49  
             final PropertyDesc propDesc, final UIComponent uiComp) {
 50  
 
 51  0
         Date date = null;
 52  0
         SimpleDateFormat sdFormat = new SimpleDateFormat("yyyyMMddHHmmss");
 53  0
         DecimalFormat dFormat = new DecimalFormat("00");
 54  
         try {
 55  0
             date = sdFormat.parse(widget.getYear()
 56  
                     + dFormat.format(widget.getMonth() + 1)
 57  
                     + dFormat.format(widget.getDay())
 58  
                     + dFormat.format(widget.getHours())
 59  
                     + dFormat.format(widget.getMinutes())
 60  
                     + dFormat.format(widget.getSeconds()));
 61  0
         } catch (ParseException ex) {
 62  0
             date = new Date();
 63  0
         }
 64  
 
 65  0
         logBinding(IMPORT_VALUE, formObj, propDesc, widget, propDesc, date);
 66  0
         propDesc.setValue(formObj, date);
 67  0
     }
 68  
 
 69  
     /*
 70  
      * @see org.seasar.uruma.binding.value.binder.AbstractValueBinder#doExportValue(java.lang.Object,
 71  
      *      java.lang.Object, org.seasar.framework.beans.PropertyDesc)
 72  
      */
 73  
     @Override
 74  
     protected void doExportValue(final DateTime widget, final Object formObj,
 75  
             final PropertyDesc propDesc, final UIComponent uiComp) {
 76  8
         Object value = propDesc.getValue(formObj);
 77  8
         if (value == null || !(value instanceof Date)) {
 78  0
             value = new Date();
 79  
         }
 80  
 
 81  8
         logBinding(EXPORT_VALUE, formObj, propDesc, widget, propDesc, value);
 82  
 
 83  8
         Calendar cal = Calendar.getInstance();
 84  8
         cal.setTime((Date) value);
 85  8
         widget.setYear(cal.get(Calendar.YEAR));
 86  8
         widget.setMonth(cal.get(Calendar.MONTH));
 87  8
         widget.setDay(cal.get(Calendar.DATE));
 88  8
         widget.setHours(cal.get(Calendar.HOUR_OF_DAY));
 89  8
         widget.setMinutes(cal.get(Calendar.MINUTE));
 90  8
         widget.setSeconds(cal.get(Calendar.SECOND));
 91  8
     }
 92  
 }