You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@struts.apache.org by rg...@apache.org on 2007/08/13 10:13:33 UTC

svn commit: r565264 - in /struts/struts2/trunk/plugins/dojo/src: main/java/org/apache/struts2/dojo/components/ main/java/org/apache/struts2/dojo/views/jsp/ui/ main/resources/template/ajax/ test/java/org/apache/struts2/dojo/views/jsp/ui/ test/resources/...

Author: rgielen
Date: Mon Aug 13 01:13:30 2007
New Revision: 565264

URL: http://svn.apache.org/viewvc?view=rev&rev=565264
Log:
WW-2108:
Make <s:tabbedpanel> being able to remember last selected tab using a cookie

Added:
    struts/struts2/trunk/plugins/dojo/src/test/java/org/apache/struts2/dojo/views/jsp/ui/TabbedPanelTagTest.java
Modified:
    struts/struts2/trunk/plugins/dojo/src/main/java/org/apache/struts2/dojo/components/TabbedPanel.java
    struts/struts2/trunk/plugins/dojo/src/main/java/org/apache/struts2/dojo/views/jsp/ui/TabbedPanelTag.java
    struts/struts2/trunk/plugins/dojo/src/main/resources/template/ajax/tabbedpanel.ftl
    struts/struts2/trunk/plugins/dojo/src/test/resources/org/apache/struts2/dojo/views/jsp/ui/TabbedPanel-1.txt
    struts/struts2/trunk/plugins/dojo/src/test/resources/org/apache/struts2/dojo/views/jsp/ui/TabbedPanel-2.txt
    struts/struts2/trunk/plugins/dojo/src/test/resources/org/apache/struts2/dojo/views/jsp/ui/TabbedPanel-3.txt

Modified: struts/struts2/trunk/plugins/dojo/src/main/java/org/apache/struts2/dojo/components/TabbedPanel.java
URL: http://svn.apache.org/viewvc/struts/struts2/trunk/plugins/dojo/src/main/java/org/apache/struts2/dojo/components/TabbedPanel.java?view=diff&rev=565264&r1=565263&r2=565264
==============================================================================
--- struts/struts2/trunk/plugins/dojo/src/main/java/org/apache/struts2/dojo/components/TabbedPanel.java (original)
+++ struts/struts2/trunk/plugins/dojo/src/main/java/org/apache/struts2/dojo/components/TabbedPanel.java Mon Aug 13 01:13:30 2007
@@ -34,6 +34,11 @@
  * <!-- START SNIPPET: javadoc -->
  * The tabbedpanel widget is primarily an AJAX component, where each tab can either be local content or remote
  * content (refreshed each time the user selects that tab).</p>
+ * If the useSelectedTabCookie attribute is set to true, the id of the selected tab is saved in a cookie on activation.
+ * When coming back to this view, the cookie is read and the tab will be activated again, unless an actual value for the
+ * selectedTab attribute is specified.</p>
+ * If you want to use the cookie feature, please be sure that you provide a unique id for your tabbedpanel component,
+ * since this will also be the identifying name component of the stored cookie.</p>
  * <!-- END SNIPPET: javadoc -->
  *
  * <p/> <b>Examples</b>
@@ -92,7 +97,8 @@
     protected String beforeSelectTabNotifyTopics;
     protected String afterSelectTabNotifyTopics;
     protected String disabledTabCssClass; 
-    
+    protected String useSelectedTabCookie;
+
     public TabbedPanel(ValueStack stack, HttpServletRequest request, HttpServletResponse response) {
         super(stack, request, response);
     }
@@ -123,7 +129,10 @@
             addParameter("afterSelectTabNotifyTopics", findString(afterSelectTabNotifyTopics));
         if (disabledTabCssClass!= null)
             addParameter("disabledTabCssClass", findString(disabledTabCssClass));
-        
+        if(useSelectedTabCookie != null) {
+            addParameter("useSelectedTabCookie", findString(useSelectedTabCookie));
+        }
+
     }
 
     @Override
@@ -194,5 +203,12 @@
     @StrutsTagAttribute(description="Css class to be applied to the tab button of disabled tabs", defaultValue="strutsDisabledTab")
     public void setDisabledTabCssClass(String disabledTabCssClass) {
         this.disabledTabCssClass = disabledTabCssClass;
+    }
+
+    @StrutsTagAttribute(required = false, defaultValue = "false", description = "If set to true, the id of the last selected " +
+            "tab will be stored in cookie. If the view is rendered, it will be tried to read this cookie and activate " +
+            "the corresponding tab on success, unless overridden by the selectedTab attribute. The cookie name is \"Struts2TabbedPanel_selectedTab_\"+id.")
+    public void setUseSelectedTabCookie( String useSelectedTabCookie ) {
+        this.useSelectedTabCookie = useSelectedTabCookie;
     }
 }

Modified: struts/struts2/trunk/plugins/dojo/src/main/java/org/apache/struts2/dojo/views/jsp/ui/TabbedPanelTag.java
URL: http://svn.apache.org/viewvc/struts/struts2/trunk/plugins/dojo/src/main/java/org/apache/struts2/dojo/views/jsp/ui/TabbedPanelTag.java?view=diff&rev=565264&r1=565263&r2=565264
==============================================================================
--- struts/struts2/trunk/plugins/dojo/src/main/java/org/apache/struts2/dojo/views/jsp/ui/TabbedPanelTag.java (original)
+++ struts/struts2/trunk/plugins/dojo/src/main/java/org/apache/struts2/dojo/views/jsp/ui/TabbedPanelTag.java Mon Aug 13 01:13:30 2007
@@ -42,8 +42,9 @@
     private String templateCssPath;
     private String beforeSelectTabNotifyTopics;
     private String afterSelectTabNotifyTopics;
-    protected String disabledTabCssClass; 
-    
+    private String disabledTabCssClass;
+    private String useSelectedTabCookie;
+
     public Component getBean(ValueStack stack, HttpServletRequest req, HttpServletResponse res) {
         return new TabbedPanel(stack, req, res);
     }
@@ -59,6 +60,7 @@
         tabbedPanel.setBeforeSelectTabNotifyTopics(beforeSelectTabNotifyTopics);
         tabbedPanel.setAfterSelectTabNotifyTopics(afterSelectTabNotifyTopics);
         tabbedPanel.setDisabledTabCssClass(disabledTabCssClass);
+        tabbedPanel.setUseSelectedTabCookie(useSelectedTabCookie);
     }
 
     public void setSelectedTab(String selectedTab) {
@@ -87,5 +89,9 @@
 
     public void setDisabledTabCssClass(String disabledTabCssClass) {
         this.disabledTabCssClass = disabledTabCssClass;
+    }
+
+    public void setUseSelectedTabCookie( String useSelectedTabCookie ) {
+        this.useSelectedTabCookie = useSelectedTabCookie;
     }
 }

Modified: struts/struts2/trunk/plugins/dojo/src/main/resources/template/ajax/tabbedpanel.ftl
URL: http://svn.apache.org/viewvc/struts/struts2/trunk/plugins/dojo/src/main/resources/template/ajax/tabbedpanel.ftl?view=diff&rev=565264&r1=565263&r2=565264
==============================================================================
--- struts/struts2/trunk/plugins/dojo/src/main/resources/template/ajax/tabbedpanel.ftl (original)
+++ struts/struts2/trunk/plugins/dojo/src/main/resources/template/ajax/tabbedpanel.ftl Mon Aug 13 01:13:30 2007
@@ -21,6 +21,30 @@
  */
 -->
 <link rel="stylesheet" href="${base}/struts/TabbedPanel.css" type="text/css"/>
+<#if parameters.useSelectedTabCookie?exists && parameters.useSelectedTabCookie=="true">
+<script type="text/javascript">
+  dojo.require("dojo.io.cookie");
+  dojo.addOnLoad (
+        function() {
+            var tabContainer = dojo.widget.byId("${parameters.escapedId?html}");
+
+            <#if !(parameters.selectedTab?if_exists != "")>
+            var selectedTab = dojo.io.cookie.getCookie("Struts2TabbedPanel_selectedTab_${parameters.escapedId?html}");
+            if (selectedTab) {
+                tabContainer.selectChild(selectedTab, tabContainer.correspondingPageButton);
+            }
+
+            </#if>
+            dojo.event.connect(tabContainer, "selectChild",
+                    function (evt) {
+                        dojo.io.cookie.setCookie("Struts2TabbedPanel_selectedTab_${parameters.escapedId?html}", evt.widgetId, 1, null, null, null);
+                    }
+                )
+            }
+        );
+</script>
+</#if>
+
 <div dojoType="struts:StrutsTabContainer"
   <#if parameters.cssStyle?if_exists != "">
     style="${parameters.cssStyle?html}"<#rt/>

Added: struts/struts2/trunk/plugins/dojo/src/test/java/org/apache/struts2/dojo/views/jsp/ui/TabbedPanelTagTest.java
URL: http://svn.apache.org/viewvc/struts/struts2/trunk/plugins/dojo/src/test/java/org/apache/struts2/dojo/views/jsp/ui/TabbedPanelTagTest.java?view=auto&rev=565264
==============================================================================
--- struts/struts2/trunk/plugins/dojo/src/test/java/org/apache/struts2/dojo/views/jsp/ui/TabbedPanelTagTest.java (added)
+++ struts/struts2/trunk/plugins/dojo/src/test/java/org/apache/struts2/dojo/views/jsp/ui/TabbedPanelTagTest.java Mon Aug 13 01:13:30 2007
@@ -0,0 +1,68 @@
+/*
+ * $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.dojo.views.jsp.ui;
+
+import org.apache.struts2.views.jsp.AbstractUITagTest;
+
+/**
+ * TabbedPanelTagTest.
+ */
+public class TabbedPanelTagTest extends AbstractUITagTest {
+
+    public void testSimple() throws Exception {
+        TabbedPanelTag tag = new TabbedPanelTag();
+        tag.setPageContext(pageContext);
+
+        tag.doStartTag();
+        tag.doEndTag();
+
+        verify(TabbedPanelTag.class.getResource("TabbedPanel-1.txt"));
+
+    }
+
+    public void testCookieCodeAvailable() throws Exception {
+        TabbedPanelTag tag = new TabbedPanelTag();
+        tag.setPageContext(pageContext);
+        tag.setId("foo");
+        tag.setUseSelectedTabCookie("true");
+
+        tag.doStartTag();
+        tag.doEndTag();
+
+        verify(TabbedPanelTag.class.getResource("TabbedPanel-2.txt"));
+
+    }
+
+    public void testCookieCodeAvailableWithOverriddenSelectedTab() throws Exception {
+        TabbedPanelTag tag = new TabbedPanelTag();
+        tag.setPageContext(pageContext);
+        tag.setId("foo");
+        tag.setUseSelectedTabCookie("true");
+        tag.setSelectedTab("bar");
+
+        tag.doStartTag();
+        tag.doEndTag();
+
+        verify(TabbedPanelTag.class.getResource("TabbedPanel-3.txt"));
+
+    }
+
+}

Modified: struts/struts2/trunk/plugins/dojo/src/test/resources/org/apache/struts2/dojo/views/jsp/ui/TabbedPanel-1.txt
URL: http://svn.apache.org/viewvc/struts/struts2/trunk/plugins/dojo/src/test/resources/org/apache/struts2/dojo/views/jsp/ui/TabbedPanel-1.txt?view=diff&rev=565264&r1=565263&r2=565264
==============================================================================
--- struts/struts2/trunk/plugins/dojo/src/test/resources/org/apache/struts2/dojo/views/jsp/ui/TabbedPanel-1.txt (original)
+++ struts/struts2/trunk/plugins/dojo/src/test/resources/org/apache/struts2/dojo/views/jsp/ui/TabbedPanel-1.txt Mon Aug 13 01:13:30 2007
@@ -1,12 +1,5 @@
-<script type="text/javascript">
-  dojo.require("dojo.widget.TabContainer");
-  dojo.require("dojo.widget.LinkPane");
-  dojo.require("dojo.widget.ContentPane");
-</script>
+<linkrel="stylesheet" href="/struts/TabbedPanel.css" type="text/css"/>
 <div
-    dojoType="TabContainer"
-    selectedTab="a"
-    labelPosition="b"
-    closeButton="c"
-    doLayout="true">
-</div>
\ No newline at end of file
+  dojoType="struts:StrutsTabContainer"
+  doLayout="false">
+</div>

Modified: struts/struts2/trunk/plugins/dojo/src/test/resources/org/apache/struts2/dojo/views/jsp/ui/TabbedPanel-2.txt
URL: http://svn.apache.org/viewvc/struts/struts2/trunk/plugins/dojo/src/test/resources/org/apache/struts2/dojo/views/jsp/ui/TabbedPanel-2.txt?view=diff&rev=565264&r1=565263&r2=565264
==============================================================================
--- struts/struts2/trunk/plugins/dojo/src/test/resources/org/apache/struts2/dojo/views/jsp/ui/TabbedPanel-2.txt (original)
+++ struts/struts2/trunk/plugins/dojo/src/test/resources/org/apache/struts2/dojo/views/jsp/ui/TabbedPanel-2.txt Mon Aug 13 01:13:30 2007
@@ -1,9 +1,17 @@
+<linkrel="stylesheet" href="/struts/TabbedPanel.css" type="text/css"/>
 <script type="text/javascript">
-  dojo.require("dojo.widget.TabContainer");
-  dojo.require("dojo.widget.LinkPane");
-  dojo.require("dojo.widget.ContentPane");
+dojo.require("dojo.io.cookie");
+dojo.addOnLoad(function(){
+    var tabContainer=dojo.widget.byId("foo");
+    var selectedTab=dojo.io.cookie.getCookie("Struts2TabbedPanel_selectedTab_foo");
+    if( selectedTab ){
+        tabContainer.selectChild(selectedTab,tabContainer.correspondingPageButton);
+    }
+    dojo.event.connect(tabContainer,"selectChild",function(evt){
+        dojo.io.cookie.setCookie("Struts2TabbedPanel_selectedTab_foo",evt.widgetId,1,null,null,null);
+        }
+    )
+});
 </script>
-<div
-    dojoType="TabContainer"
-    doLayout="false">
+<div dojoType="struts:StrutsTabContainer" id="foo" doLayout="false">
 </div>

Modified: struts/struts2/trunk/plugins/dojo/src/test/resources/org/apache/struts2/dojo/views/jsp/ui/TabbedPanel-3.txt
URL: http://svn.apache.org/viewvc/struts/struts2/trunk/plugins/dojo/src/test/resources/org/apache/struts2/dojo/views/jsp/ui/TabbedPanel-3.txt?view=diff&rev=565264&r1=565263&r2=565264
==============================================================================
--- struts/struts2/trunk/plugins/dojo/src/test/resources/org/apache/struts2/dojo/views/jsp/ui/TabbedPanel-3.txt (original)
+++ struts/struts2/trunk/plugins/dojo/src/test/resources/org/apache/struts2/dojo/views/jsp/ui/TabbedPanel-3.txt Mon Aug 13 01:13:30 2007
@@ -1,10 +1,13 @@
+<linkrel="stylesheet" href="/struts/TabbedPanel.css" type="text/css"/>
 <script type="text/javascript">
-  dojo.require("dojo.widget.TabContainer");
-  dojo.require("dojo.widget.LinkPane");
-  dojo.require("dojo.widget.ContentPane");
+dojo.require("dojo.io.cookie");
+dojo.addOnLoad(function(){
+    var tabContainer=dojo.widget.byId("foo");
+    dojo.event.connect(tabContainer,"selectChild",function(evt){
+        dojo.io.cookie.setCookie("Struts2TabbedPanel_selectedTab_foo",evt.widgetId,1,null,null,null);
+        }
+    )
+});
 </script>
-<div
-    dojoType="TabContainer"
-    labelPosition="right-h"
-    doLayout="true">
+<div dojoType="struts:StrutsTabContainer" id="foo" selectedTab="bar" doLayout="false">
 </div>