View Javadoc

1   /*
2    * Copyright 2000-2001,2004 The Apache Software Foundation.
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, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.apache.jetspeed.modules.parameters;
18  
19  // ECS support
20  import org.apache.ecs.html.Input;
21  import org.apache.ecs.html.IMG;
22  import org.apache.ecs.html.A;
23  import org.apache.ecs.html.Script;
24  import org.apache.ecs.ElementContainer;     
25  
26  // Java classes
27  import java.util.Map;
28  import java.util.Date;
29  import java.text.SimpleDateFormat;
30  import java.text.MessageFormat;
31  
32  // Turbine support
33  import org.apache.turbine.util.RunData;
34  
35  /***
36   *  Presentation method to show a date input field with a popup calendar. Calendar is accessed by clicking the icon
37   *  next to input field. For now, the only date format supported is mm/dd/yyyy. If value <code>$today</code> is passed, the date in the input field will default to today's date.
38   *  <p>Options:
39   *  <UL>
40   *  <LI><code>$<name>.style.formName</code> - form name where the control is displayed; default=DefaultCustomizer</LI>
41   *  <LI><code>$<name>.style.format</code> - date format to use for return value; default=mm/dd/yyyy.
42   * The date format can have three types of separators: hyphen(-), space( ), or slash(/), but must be consistent in their usage. E.g.
43   * d/m/yyyy
44   * The acceptable tokens are :
45   *   <UL>
46   *   <LI>d - day</LI>
47   *   <LI>dd - day (padded with 0 if less than 10)</LI>
48   *   <LI>m - month (in numbers)</LI>
49   *   <LI>mm - month (in numbers, padded with 0 if less than 10)</LI>
50   *   <LI>mmm - month (in words)</LI>
51   *   <li>yyyy - year</LI>
52   *   </UL> 
53   *  </LI>
54   *  </UL>
55   *  </p>
56   * 
57   * @author <a href="morciuch@apache.org">Mark Orciuch</a>
58   * @version $Id: PopupCalendar.java,v 1.3 2004/02/23 03:01:20 jford Exp $
59   */
60  public class PopupCalendar extends ParameterPresentationStyle
61  {
62  
63      public static final String PARM_FORM_NAME   = "formName";
64      public static final String PARM_FORMAT      = "format";
65  
66      /***
67       * Method returning HTML markup for a date list box
68       */
69      public String getContent(RunData data, String name, String value, Map parms)
70      {
71  
72          ElementContainer container = new ElementContainer();
73  
74          if ( value.equals("$today") )
75          {
76              Date dt = new Date(System.currentTimeMillis());
77              value = new SimpleDateFormat("M/d/yyyy").format(dt);
78          }
79  
80          container.addElement(new Script().setLanguage("JavaScript").setSrc("javascript/PopupCalendar.js"));
81  
82          container.addElement(new Input(Input.TEXT, name, value));
83  
84          IMG img = new IMG("images/cal.gif").setAlt("Click here for popup calendar").setBorder(0);
85          A a = new A(this.getJavaScript(name), img);
86  
87          container.addElement(a);
88  
89          return container.toString();
90      }
91  
92      /***
93       * Returns body of the java script event handler
94       */
95      private String getJavaScript(String fieldName)
96      {
97  
98          String formName = (String)this.getParm(PARM_FORM_NAME, "DefaultCustomizer");
99          String format = (String)this.getParm(PARM_FORMAT, "mm/dd/yyyy");
100 
101         Object[] args = {
102             formName,
103             fieldName,
104             format
105         };
106 
107         String template = "javascript: show_calendar(''{0}.{1}'',{0}.{1}.value,''{2}'');";
108 
109         return new MessageFormat(template).format(args);
110 
111     }
112 
113     /***
114      *  Test method
115      */
116     public static void main(String args[])
117     {
118 
119         PopupCalendar pc = new PopupCalendar();      
120         System.out.println(pc.getContent(null, "test", "08/01/2001", null));
121         System.out.println(pc.getContent(null, "test", "$today", null));
122 
123     }
124 
125 }