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 at7 * 8 * http://www.apache.org/licenses/LICENSE-2.09 * 10 * Unless required by applicable law or agreed to in writing, software11 * 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 and14 * limitations under the License.15 */1617packageorg.apache.jetspeed.modules.parameters;
1819// ECS support20import org.apache.ecs.html.Input;
21import org.apache.ecs.html.IMG;
22import org.apache.ecs.html.A;
23import org.apache.ecs.html.Script;
24import org.apache.ecs.ElementContainer;
2526// Java classes27import java.util.Map;
28import java.util.Date;
29import java.text.SimpleDateFormat;
30import java.text.MessageFormat;
3132// Turbine support33import org.apache.turbine.util.RunData;
3435/***36 * Presentation method to show a date input field with a popup calendar. Calendar is accessed by clicking the icon37 * 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/yyyy44 * 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 */60publicclassPopupCalendarextendsParameterPresentationStyle61 {
6263publicstaticfinal String PARM_FORM_NAME = "formName";
64publicstaticfinal String PARM_FORMAT = "format";
6566/***67 * Method returning HTML markup for a date list box68 */69public String getContent(RunData data, String name, String value, Map parms)
70 {
7172 ElementContainer container = new ElementContainer();
7374if ( value.equals("$today") )
75 {
76 Date dt = new Date(System.currentTimeMillis());
77 value = new SimpleDateFormat("M/d/yyyy").format(dt);
78 }
7980 container.addElement(new Script().setLanguage("JavaScript").setSrc("javascript/PopupCalendar.js"));
8182 container.addElement(new Input(Input.TEXT, name, value));
8384 IMG img = new IMG("images/cal.gif").setAlt("Click here for popup calendar").setBorder(0);
85 A a = new A(this.getJavaScript(name), img);
8687 container.addElement(a);
8889return container.toString();
90 }
9192/***93 * Returns body of the java script event handler94 */95private String getJavaScript(String fieldName)
96 {
9798 String formName = (String)this.getParm(PARM_FORM_NAME, "DefaultCustomizer");
99 String format = (String)this.getParm(PARM_FORMAT, "mm/dd/yyyy");
100101 Object[] args = {
102 formName,
103 fieldName,
104 format
105 };
106107 String template = "javascript: show_calendar(''{0}.{1}'',{0}.{1}.value,''{2}'');";
108109returnnew MessageFormat(template).format(args);
110111 }
112113/***114 * Test method115 */116publicstaticvoid main(String args[])
117 {
118119PopupCalendar pc = newPopupCalendar();
120 System.out.println(pc.getContent(null, "test", "08/01/2001", null));
121 System.out.println(pc.getContent(null, "test", "$today", null));
122123 }
124125 }