View Javadoc

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  public class DateTimeValueBinder extends AbstractValueBinder<DateTime> {
35  
36      /**
37       * {@link DateTimeValueBinder} を構築します。<br />
38       */
39      public DateTimeValueBinder() {
40          super(DateTime.class);
41      }
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          Date date = null;
52          SimpleDateFormat sdFormat = new SimpleDateFormat("yyyyMMddHHmmss");
53          DecimalFormat dFormat = new DecimalFormat("00");
54          try {
55              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          } catch (ParseException ex) {
62              date = new Date();
63          }
64  
65          logBinding(IMPORT_VALUE, formObj, propDesc, widget, propDesc, date);
66          propDesc.setValue(formObj, date);
67      }
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          Object value = propDesc.getValue(formObj);
77          if (value == null || !(value instanceof Date)) {
78              value = new Date();
79          }
80  
81          logBinding(EXPORT_VALUE, formObj, propDesc, widget, propDesc, value);
82  
83          Calendar cal = Calendar.getInstance();
84          cal.setTime((Date) value);
85          widget.setYear(cal.get(Calendar.YEAR));
86          widget.setMonth(cal.get(Calendar.MONTH));
87          widget.setDay(cal.get(Calendar.DATE));
88          widget.setHours(cal.get(Calendar.HOUR_OF_DAY));
89          widget.setMinutes(cal.get(Calendar.MINUTE));
90          widget.setSeconds(cal.get(Calendar.SECOND));
91      }
92  }