Author: musachy
Date: Mon Mar  9 21:13:18 2009
New Revision: 751841

URL: http://svn.apache.org/viewvc?rev=751841&view=rev
Log:
Add datepicker

Added:
    
struts/sandbox/trunk/struts2-jquery-plugin/src/main/java/org/apache/struts2/jquery/components/JQueryDatepicker.java
    
struts/sandbox/trunk/struts2-jquery-plugin/src/main/java/org/apache/struts2/jquery/views/jsp/ui/JQueryDatepickerTag.java
    
struts/sandbox/trunk/struts2-jquery-plugin/src/main/resources/template/jquery-simple/datepicker.ftl
Modified:
    
struts/sandbox/trunk/struts2-jquery-plugin/src/main/resources/template/jquery-simple/head.ftl

Added: 
struts/sandbox/trunk/struts2-jquery-plugin/src/main/java/org/apache/struts2/jquery/components/JQueryDatepicker.java
URL: 
http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-jquery-plugin/src/main/java/org/apache/struts2/jquery/components/JQueryDatepicker.java?rev=751841&view=auto
==============================================================================
--- 
struts/sandbox/trunk/struts2-jquery-plugin/src/main/java/org/apache/struts2/jquery/components/JQueryDatepicker.java
 (added)
+++ 
struts/sandbox/trunk/struts2-jquery-plugin/src/main/java/org/apache/struts2/jquery/components/JQueryDatepicker.java
 Mon Mar  9 21:13:18 2009
@@ -0,0 +1,141 @@
+/*
+ * $Id$
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.struts2.jquery.components;
+
+import com.opensymphony.xwork2.inject.Inject;
+import com.opensymphony.xwork2.util.ValueStack;
+import com.opensymphony.xwork2.util.logging.Logger;
+import com.opensymphony.xwork2.util.logging.LoggerFactory;
+import org.apache.struts2.jquery.JQueryPluginConstants;
+import org.apache.struts2.views.annotations.StrutsTag;
+import org.apache.struts2.views.annotations.StrutsTagAttribute;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.text.DateFormat;
+import java.text.MessageFormat;
+import java.text.SimpleDateFormat;
+import java.util.ArrayList;
+import java.util.Calendar;
+import java.util.Date;
+import java.util.List;
+
+...@strutstag(
+        name = "datepicker",
+        tldTagClass = 
"org.apache.struts2.jquery.views.jsp.ui.JQueryDatepickerTag",
+        description = "Renders a date picker",
+        allowDynamicAttributes = true)
+public class JQueryDatepicker extends JQueryTextField {
+     final protected static Logger LOG = 
LoggerFactory.getLogger(JQueryDatepicker.class);
+
+    private static final String TEMPLATE = "datepicker";
+    final private static String RFC3339_FORMAT = "yyyy-MM-dd'T'HH:mm:ss";
+    final private static String RFC3339_PATTERN = "{0,date," + RFC3339_FORMAT 
+ "}";
+    
+    //see http://docs.jquery.com/UI/Datepicker/%24.datepicker.formatDate
+    private String displayFormat;
+
+    public JQueryDatepicker(ValueStack stack, HttpServletRequest request, 
HttpServletResponse response) {
+        super(stack, request, response);
+    }
+
+    @Override
+    public void evaluateParams() {
+        super.evaluateParams();
+
+        if (displayFormat != null)
+            addParameter("displayFormat", findString(displayFormat));
+
+        if(parameters.containsKey("value")) {
+            addParameter("nameValue", parameters.get("value"));
+        } else {
+            if(parameters.containsKey("name")) {
+                addParameter("nameValue", 
format(findValue((String)parameters.get("name"))));
+            }
+        }
+    }
+
+    private String format(Object obj) {
+        if(obj == null)
+            return null;
+
+        if(obj instanceof Date) {
+            return MessageFormat.format(RFC3339_PATTERN, (Date) obj);
+        } else if(obj instanceof Calendar) {
+            return MessageFormat.format(RFC3339_PATTERN, ((Calendar) 
obj).getTime());
+        }
+        else {
+            // try to parse a date
+            String dateStr = obj.toString();
+            if(dateStr.equalsIgnoreCase("today"))
+                return MessageFormat.format(RFC3339_PATTERN, new Date());
+
+
+            Date date = null;
+            //formats used to parse the date
+            List<DateFormat> formats = new ArrayList<DateFormat>();
+            formats.add(new SimpleDateFormat(RFC3339_FORMAT));
+            formats.add(SimpleDateFormat.getTimeInstance(DateFormat.SHORT));
+            formats.add(SimpleDateFormat.getDateInstance(DateFormat.SHORT));
+            formats.add(SimpleDateFormat.getDateInstance(DateFormat.MEDIUM));
+            formats.add(SimpleDateFormat.getDateInstance(DateFormat.FULL));
+            formats.add(SimpleDateFormat.getDateInstance(DateFormat.LONG));
+            if (this.displayFormat != null) {
+                try {
+                    SimpleDateFormat displayFormat = new SimpleDateFormat(
+                        (String) getParameters().get("displayFormat"));
+                    formats.add(displayFormat);
+                } catch (Exception e) {
+                }
+            }
+
+            for (DateFormat format : formats) {
+                try {
+                    date = format.parse(dateStr);
+                    if (date != null)
+                        return MessageFormat.format(RFC3339_PATTERN, date);
+                } catch (Exception e) {
+                    //keep going
+                }
+            }
+
+           // last resource, assume already in correct/default format
+           if (LOG.isDebugEnabled())
+               LOG.debug("Unable to parse date " + dateStr);
+           return dateStr;
+        }
+    }
+
+    protected String getDefaultTemplate() {
+        return TEMPLATE;
+    }
+
+    @Override
+    @Inject(JQueryPluginConstants.DEFAULT_THEME)
+    public void setDefaultUITheme(String theme) {
+        this.defaultUITheme = theme;
+    }
+
+    @StrutsTagAttribute(description = "Format use to display the selected 
date", defaultValue = "mm/dd/yy")
+    public void setDisplayFormat(String displayFormat) {
+        this.displayFormat = displayFormat;
+    }
+}

Added: 
struts/sandbox/trunk/struts2-jquery-plugin/src/main/java/org/apache/struts2/jquery/views/jsp/ui/JQueryDatepickerTag.java
URL: 
http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-jquery-plugin/src/main/java/org/apache/struts2/jquery/views/jsp/ui/JQueryDatepickerTag.java?rev=751841&view=auto
==============================================================================
--- 
struts/sandbox/trunk/struts2-jquery-plugin/src/main/java/org/apache/struts2/jquery/views/jsp/ui/JQueryDatepickerTag.java
 (added)
+++ 
struts/sandbox/trunk/struts2-jquery-plugin/src/main/java/org/apache/struts2/jquery/views/jsp/ui/JQueryDatepickerTag.java
 Mon Mar  9 21:13:18 2009
@@ -0,0 +1,47 @@
+/*
+ * $Id$
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.struts2.jquery.views.jsp.ui;
+
+import com.opensymphony.xwork2.util.ValueStack;
+import org.apache.struts2.components.Component;
+import org.apache.struts2.jquery.components.JQueryDatepicker;
+import org.apache.struts2.views.jsp.ui.AbstractUITag;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+public class JQueryDatepickerTag extends JQueryTextFieldTag {
+    private String displayFormat;
+
+    public Component getBean(ValueStack stack, HttpServletRequest req, 
HttpServletResponse res) {
+        return new JQueryDatepicker(stack, req, res);
+    }
+
+    protected void populateParams() {
+        super.populateParams();
+        JQueryDatepicker picker = ((JQueryDatepicker) component);
+        picker.setDisplayFormat(displayFormat);
+    }
+
+    public void setDisplayFormat(String displayFormat) {
+        this.displayFormat = displayFormat;
+    }
+}

Added: 
struts/sandbox/trunk/struts2-jquery-plugin/src/main/resources/template/jquery-simple/datepicker.ftl
URL: 
http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-jquery-plugin/src/main/resources/template/jquery-simple/datepicker.ftl?rev=751841&view=auto
==============================================================================
--- 
struts/sandbox/trunk/struts2-jquery-plugin/src/main/resources/template/jquery-simple/datepicker.ftl
 (added)
+++ 
struts/sandbox/trunk/struts2-jquery-plugin/src/main/resources/template/jquery-simple/datepicker.ftl
 Mon Mar  9 21:13:18 2009
@@ -0,0 +1,65 @@
+<#--
+/*
+ * $Id: text.ftl 590812 2007-10-31 20:32:54Z apetrelli $
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+-->
+<input type="text"<#rt/>
+ name="struts.${parameters.name?default("")?html}"<#rt/>
+<#if parameters.get("size")??>
+ size="${parameters.get("size")?html}"<#rt/>
+</#if>
+<#if parameters.maxlength??>
+ maxlength="${parameters.maxlength?html}"<#rt/>
+</#if>
+<#if parameters.nameValue??>
+ value="<@s.property value="parameters.nameValue"/>"<#rt/>
+</#if>
+<#if parameters.disabled?default(false)>
+ disabled="disabled"<#rt/>
+</#if>
+<#if parameters.readonly?default(false)>
+ readonly="readonly"<#rt/>
+</#if>
+<#if parameters.tabindex??>
+ tabindex="${parameters.tabindex?html}"<#rt/>
+</#if>
+<#if parameters.id??>
+ id="${parameters.id?html}"<#rt/>
+</#if>
+<#include "/${parameters.templateDir}/simple/css.ftl" />
+<#if parameters.title??>
+ title="${parameters.title?html}"<#rt/>
+</#if>
+<#include "/${parameters.templateDir}/simple/scripting-events.ftl" />
+<#include "/${parameters.templateDir}/simple/common-attributes.ftl" />
+<#include "/${parameters.templateDir}/simple/dynamic-attributes.ftl" />
+/>
+<input type="text" name="${parameters.name?default("")?html}" 
id="${parameters.id?html}_hidden" />
+<script type="text/javascript">
+    $(function() {
+        $("#${parameters.id?html}").datepicker({
+            altField: "#${parameters.id?html}_hidden",
+            altFormat: "yy-mm-dd'T'00:00:00"
+            <#if parameters.displayFormat??>
+            ,dateFormat : "${parameters.displayFormat?html}"
+            </#if>
+        });
+    });
+</script>
\ No newline at end of file

Modified: 
struts/sandbox/trunk/struts2-jquery-plugin/src/main/resources/template/jquery-simple/head.ftl
URL: 
http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-jquery-plugin/src/main/resources/template/jquery-simple/head.ftl?rev=751841&r1=751840&r2=751841&view=diff
==============================================================================
--- 
struts/sandbox/trunk/struts2-jquery-plugin/src/main/resources/template/jquery-simple/head.ftl
 (original)
+++ 
struts/sandbox/trunk/struts2-jquery-plugin/src/main/resources/template/jquery-simple/head.ftl
 Mon Mar  9 21:13:18 2009
@@ -23,3 +23,6 @@
 <script src="${base}/struts/jqueryUtils.js" type="text/javascript"></script>
 
 <script src="http://jqueryjs.googlecode.com/files/jquery-1.3.2.js"; 
type="text/javascript"></script>
+<script src="http://jqueryui.com/latest/ui/ui.core.js"; 
type="text/javascript"></script>
+<script src="http://jqueryui.com/latest/ui/ui.datepicker.js"; 
type="text/javascript"></script>
+


Reply via email to