KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > input > tags > DateTimePickerTag


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.input.tags;
21
22 import javax.servlet.http.HttpServletRequest JavaDoc;
23 import javax.servlet.jsp.JspException JavaDoc;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.apache.struts.taglib.TagUtils;
28 import org.apache.struts.taglib.html.BaseFieldTag;
29 import org.apache.struts.util.MessageResources;
30
31 import com.sslexplorer.core.CoreUtil;
32 import com.sslexplorer.properties.Property;
33 import com.sslexplorer.properties.impl.profile.ProfilePropertyKey;
34 import com.sslexplorer.security.LogonControllerFactory;
35
36 /**
37  * Tag this inserts a component that allows the user to select a date and / or
38  * time.
39  *
40  * @author Brett Smith <brett@3sp.com>
41  */

42 public class DateTimePickerTag extends BaseFieldTag {
43
44     final static Log log = LogFactory.getLog(DateTimePickerTag.class);
45
46     // Protected instance variables
47

48     protected String JavaDoc dateTimeId;
49     protected String JavaDoc pattern;
50     protected boolean twentyFourHour;
51     protected boolean showTime;
52     protected boolean disabled;
53     protected String JavaDoc fragment;
54     protected String JavaDoc dateSeparator;
55     protected boolean showDate;
56     protected boolean showMonthYear;
57     protected boolean showLongMonth;
58     protected boolean showMonthYearPicker;
59     protected boolean showSeconds;
60
61     /**
62      * The message resources for this package.
63      */

64     protected static MessageResources messages = MessageResources.getMessageResources("org.apache.struts.taglib.bean.LocalStrings");
65
66     /**
67      * Constructor
68      */

69     public DateTimePickerTag() {
70         release();
71     }
72
73     /*
74      * (non-Javadoc)
75      *
76      * @see javax.servlet.jsp.tagext.BodyTagSupport#doEndTag()
77      */

78     public int doEndTag() throws JspException JavaDoc {
79         if(fragment != null) {
80             TagUtils.getInstance().write(pageContext, fragment);
81         }
82         pattern = null;
83         return (EVAL_PAGE);
84     }
85
86     /*
87      * (non-Javadoc)
88      *
89      * @see javax.servlet.jsp.tagext.BodyTagSupport#release()
90      */

91     public void release() {
92         super.release();
93         disabled = false;
94         twentyFourHour = true;
95         showTime = true;
96         pattern = null;
97         dateTimeId = null;
98         showDate = true;
99         showLongMonth = true;
100         showMonthYear = false;
101         showMonthYearPicker = true;
102         showSeconds = true;
103         dateSeparator = "/";
104         fragment = null;
105     }
106
107     /*
108      * (non-Javadoc)
109      *
110      * @see javax.servlet.jsp.tagext.BodyTagSupport#doStartTag()
111      */

112     public int doStartTag() throws JspException JavaDoc {
113         if (disabled)
114             return SKIP_PAGE;
115             
116         if(getBundle() == null || getBundle().equals("")) {
117             setBundle("navigation");
118         }
119         fragment = generateFragment();
120         return (EVAL_BODY_AGAIN);
121     }
122     
123     String JavaDoc generateFragment() throws JspException JavaDoc {
124         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
125         // <a HREF="javascript:toggleCalendar('dateFromPicker','dateFrom','ddmmyyyy',
126
//true,23)"><img SRC="../../../../../images/cal.gif" width="16" height="16"
127
//border="0" alt="Pick a date"></a><div
128
//style="position:absolute;display: none;overflow:visible;
129
//top:4px;width: 220px;left:4px;z-index:900;" id="dateFromPicker">
130
//<div/></div>
131
buf.append("<a HREF=\"#\" onclick=\"toggleCalendar('");
132         buf.append("dateTimePicker_" + dateTimeId);
133         buf.append("','");
134         buf.append(dateTimeId);
135         buf.append("','");
136         String JavaDoc datePattern = pattern;
137         if(datePattern == null) {
138             datePattern = Property.getProperty(
139                     new ProfilePropertyKey("ui.dateFormat",
140                             LogonControllerFactory.getInstance().getSessionInfo(
141                                     (HttpServletRequest JavaDoc)pageContext.getRequest())));
142             if(datePattern.contains("-")) {
143                 dateSeparator = "-";
144             }
145             else if(datePattern.contains("/")) {
146                 dateSeparator = "/";
147             }
148             datePattern = datePattern.replace(dateSeparator, "").toUpperCase();
149         }
150         buf.append(datePattern);
151         buf.append("',");
152         buf.append(showTime);
153         buf.append(",");
154         buf.append(twentyFourHour ? "24" : "12");
155         buf.append(",");
156         buf.append(showDate);
157         buf.append(",");
158         buf.append(showMonthYear);
159         buf.append(",");
160         buf.append(showLongMonth);
161         buf.append(",");
162         buf.append(showMonthYearPicker);
163         buf.append(",");
164         buf.append(showSeconds);
165         buf.append(",'");
166         buf.append(dateSeparator);
167         buf.append("','");
168         buf.append(CoreUtil.getThemePath(pageContext.getSession()));
169         buf.append("/images/year-up.gif','");
170         buf.append(CoreUtil.getThemePath(pageContext.getSession()));
171         buf.append("/images/year-down.gif','");
172         buf.append(CoreUtil.getThemePath(pageContext.getSession()));
173         buf.append("/images/close.gif');\"><img SRC=\"");
174         buf.append(CoreUtil.getThemePath(pageContext.getSession()));
175         buf.append("/images/cal.gif\" border=\"0\"></a><div style=\"position: ");
176         buf.append("absolute;display:none;overflow:visible;top:4px;width:220px;left:4px;z-index:900\"");
177         buf.append(" id=\"");
178         buf.append("dateTimePicker_" + dateTimeId);
179         buf.append("\"><div/></div>");
180         return buf.toString();
181     }
182
183     /* (non-Javadoc)
184      * @see org.apache.struts.taglib.html.BaseHandlerTag#setDisabled(boolean)
185      */

186     public void setDisabled(boolean disabled) {
187         this.disabled = disabled;
188     }
189
190     /**
191      * Set the ID of the date / time text field that should be read from
192      * and populated with the selected date.
193      *
194      * @param dateTimeId date / time text field id
195      */

196     public void setDateTimeId(String JavaDoc dateTimeId) {
197         this.dateTimeId = dateTimeId;
198     }
199
200     /**
201      * Set the pattern. The JavaScript portion of this tag currently
202      * supports dd/MM/yy, dd/MM/yyyy, MM/dd/yy, MM/dd/yyyy, dd-MMM-yyyy,
203      * dd-MMM-yy, MMM-dd-yyyy and MMM-dd-yy
204      *
205      * @param pattern pattern
206      */

207     public void setPattern(String JavaDoc pattern) {
208         this.pattern = pattern;
209     }
210
211     /**
212      * Set whether the time picker should be displayed.
213      *
214      * @param showTime show time picker
215      */

216     public void setShowTime(String JavaDoc showTime) {
217         this.showTime = Boolean.parseBoolean(showTime);
218     }
219
220     /**
221      * Set whether the time picker should be in 24 hour
222      * format. If 12 hour format then the component will
223      * display an AM / PM selection component.
224      *
225      * @param twentyFourHour twent four hour format
226      */

227     public void setTwentyFourHour(String JavaDoc twentyFourHour) {
228         this.twentyFourHour = Boolean.parseBoolean(twentyFourHour);
229     }
230
231     /**
232      * Set whether the date picker should be displayed.
233      *
234      * @param showDate show date picker
235      */

236     public void setShowDate(boolean showDate) {
237         this.showDate = showDate;
238     }
239
240     /**
241      * Set whether the long month name should be displayed.
242      *
243      * @param showLongMonth show long month name
244      */

245     public void setShowLongMonth(boolean showLongMonth) {
246         this.showLongMonth = showLongMonth;
247     }
248
249     /**
250      * Set wther the month / year subtitle should be displayed.
251      *
252      * @param showMonthYear show month year subtitle
253      */

254     public void setShowMonthYear(boolean showMonthYear) {
255         this.showMonthYear = showMonthYear;
256     }
257
258     /**
259      * Set whether the month / year picker should be displayed
260      *
261      * @param showMonthYearPicker show month year picker
262      */

263     public void setShowMonthYearPicker(boolean showMonthYearPicker) {
264         this.showMonthYearPicker = showMonthYearPicker;
265     }
266
267     /**
268      * Set whether the time picker should displayed seconds.
269      *
270      * @param showSeconds show seconds in time picker
271      */

272     public void setShowSeconds(boolean showSeconds) {
273         this.showSeconds = showSeconds;
274     }
275
276     /**
277      * Set the character to use for the date separator.
278      *
279      * @param dateSeparator date separator character
280      */

281     public void setDateSeparator(String JavaDoc dateSeparator) {
282         this.dateSeparator = dateSeparator;
283     }
284 }
285
Popular Tags