View Javadoc
1   /*
2    * Copyright (C) 2003-2008 eXo Platform SAS.
3    *
4    * This program is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Affero General Public License
6    * as published by the Free Software Foundation; either version 3
7    * of the License, or (at your option) any later version.
8    *
9    * This program is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13   *
14   * You should have received a copy of the GNU General Public License
15   * along with this program; if not, see<http://www.gnu.org/licenses/>.
16   */
17  package org.exoplatform.ecm.webui.form.field;
18  
19  import java.text.SimpleDateFormat;
20  import java.util.Calendar;
21  import java.util.Date;
22  import java.util.GregorianCalendar;
23  
24  import org.exoplatform.ecm.webui.form.DialogFormField;
25  import org.exoplatform.webui.form.UIFormDateTimeInput;
26  import org.exoplatform.webui.form.UIFormInputBase;
27  
28  /**
29   * Created by The eXo Platform SAS
30   * @author : Hoa.Pham
31   *          hoa.pham@exoplatform.com
32   * Jun 23, 2008
33   */
34  public class UIFormCalendarField extends DialogFormField {
35  
36    public UIFormCalendarField(String name, String label, String[] args) {
37      super(name, label, args);
38    }
39  
40    @SuppressWarnings("unchecked")
41    public <T extends UIFormInputBase> T createUIFormInput() throws Exception {
42      String[] arrDate = null;
43      SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss") ;
44      Date date = null;
45      if(options == null) formatter = new SimpleDateFormat("MM/dd/yyyy") ;
46      if(defaultValue != null && defaultValue.length() > 0) {
47        try {
48          date = formatter.parse(defaultValue) ;
49          if(defaultValue.indexOf("/") > -1) arrDate = defaultValue.split("/") ;
50          String[] arrDf = formatter.format(date).split("/") ;
51          if(Integer.parseInt(arrDate[0]) != Integer.parseInt(arrDf[0])) date = new Date() ;
52        } catch(Exception e) {
53          date = null;
54        }
55      }
56      UIFormDateTimeInput uiDateTime = null;
57      if(isDisplayTime()) {
58        uiDateTime = new UIFormDateTimeInput(name, name, date) ;
59      } else {
60        uiDateTime = new UIFormDateTimeInput(name, name, date, false) ;
61      }
62      if (date != null) {
63        Calendar calendar = new GregorianCalendar();
64        calendar.setTime(date);
65        uiDateTime.setCalendar(calendar);
66      } else {
67        uiDateTime.setCalendar(null);
68      }
69      if(label != null) uiDateTime.setLabel(label);
70      return (T)uiDateTime;
71    }
72  
73    public boolean isVisible() {
74      if (visible == null) return true;
75      return "true".equalsIgnoreCase(visible);
76    }
77    public boolean isDisplayTime() { return "displaytime".equals(options); }
78  }