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:18:08 UTC

svn commit: r565265 - in /struts/struts2/branches/STRUTS_2_0_X/core/src: main/java/org/apache/struts2/components/ main/java/org/apache/struts2/views/jsp/ui/ main/resources/template/simple/ site/resources/tags/ test/java/org/apache/struts2/views/jsp/ui/...

Author: rgielen
Date: Mon Aug 13 01:18:07 2007
New Revision: 565265

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

Added:
    struts/struts2/branches/STRUTS_2_0_X/core/src/test/java/org/apache/struts2/views/jsp/ui/TabbedPanelTagTest.java
Modified:
    struts/struts2/branches/STRUTS_2_0_X/core/src/main/java/org/apache/struts2/components/TabbedPanel.java
    struts/struts2/branches/STRUTS_2_0_X/core/src/main/java/org/apache/struts2/views/jsp/ui/TabbedPanelTag.java
    struts/struts2/branches/STRUTS_2_0_X/core/src/main/resources/template/simple/tabbedpanel.ftl
    struts/struts2/branches/STRUTS_2_0_X/core/src/site/resources/tags/tabbedPanel.html
    struts/struts2/branches/STRUTS_2_0_X/core/src/test/resources/org/apache/struts2/views/jsp/ui/TabbedPanel-1.txt
    struts/struts2/branches/STRUTS_2_0_X/core/src/test/resources/org/apache/struts2/views/jsp/ui/TabbedPanel-2.txt
    struts/struts2/branches/STRUTS_2_0_X/core/src/test/resources/org/apache/struts2/views/jsp/ui/TabbedPanel-3.txt

Modified: struts/struts2/branches/STRUTS_2_0_X/core/src/main/java/org/apache/struts2/components/TabbedPanel.java
URL: http://svn.apache.org/viewvc/struts/struts2/branches/STRUTS_2_0_X/core/src/main/java/org/apache/struts2/components/TabbedPanel.java?view=diff&rev=565265&r1=565264&r2=565265
==============================================================================
--- struts/struts2/branches/STRUTS_2_0_X/core/src/main/java/org/apache/struts2/components/TabbedPanel.java (original)
+++ struts/struts2/branches/STRUTS_2_0_X/core/src/main/java/org/apache/struts2/components/TabbedPanel.java Mon Aug 13 01:18:07 2007
@@ -32,6 +32,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>
@@ -67,6 +72,7 @@
     protected String closeButton;
     protected String doLayout ;
     protected String templateCssPath;
+    protected String useSelectedTabCookie;
 
     public TabbedPanel(ValueStack stack, HttpServletRequest request, HttpServletResponse response) {
         super(stack, request, response);
@@ -92,6 +98,9 @@
         }
         if(templateCssPath != null)
             addParameter("templateCssPath", findString(templateCssPath));
+        if(useSelectedTabCookie != null) {
+            addParameter("useSelectedTabCookie", findString(useSelectedTabCookie));
+        }
     }
 
     public String getDefaultOpenTemplate() {
@@ -131,5 +140,12 @@
     @StrutsTagAttribute(description="Template css path")
     public void setTemplateCssPath(String templateCssPath) {
         this.templateCssPath = templateCssPath;
+    }
+
+    @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/branches/STRUTS_2_0_X/core/src/main/java/org/apache/struts2/views/jsp/ui/TabbedPanelTag.java
URL: http://svn.apache.org/viewvc/struts/struts2/branches/STRUTS_2_0_X/core/src/main/java/org/apache/struts2/views/jsp/ui/TabbedPanelTag.java?view=diff&rev=565265&r1=565264&r2=565265
==============================================================================
--- struts/struts2/branches/STRUTS_2_0_X/core/src/main/java/org/apache/struts2/views/jsp/ui/TabbedPanelTag.java (original)
+++ struts/struts2/branches/STRUTS_2_0_X/core/src/main/java/org/apache/struts2/views/jsp/ui/TabbedPanelTag.java Mon Aug 13 01:18:07 2007
@@ -39,6 +39,7 @@
     private String closeButton;
     private String doLayout;
     private String templateCssPath;
+    private String useSelectedTabCookie;
 
     public Component getBean(ValueStack stack, HttpServletRequest req, HttpServletResponse res) {
         return new TabbedPanel(stack, req, res);
@@ -52,6 +53,7 @@
         tabbedPanel.setDoLayout(doLayout);
         tabbedPanel.setLabelposition(labelPosition);
         tabbedPanel.setTemplateCssPath(templateCssPath);
+        tabbedPanel.setUseSelectedTabCookie(useSelectedTabCookie);
     }
 
     public void setSelectedTab(String selectedTab) {
@@ -68,5 +70,9 @@
 
     public void setTemplateCssPath(String templateCssPath) {
         this.templateCssPath = templateCssPath;
+    }
+
+    public void setUseSelectedTabCookie( String useSelectedTabCookie ) {
+        this.useSelectedTabCookie = useSelectedTabCookie;
     }
 }

Modified: struts/struts2/branches/STRUTS_2_0_X/core/src/main/resources/template/simple/tabbedpanel.ftl
URL: http://svn.apache.org/viewvc/struts/struts2/branches/STRUTS_2_0_X/core/src/main/resources/template/simple/tabbedpanel.ftl?view=diff&rev=565265&r1=565264&r2=565265
==============================================================================
--- struts/struts2/branches/STRUTS_2_0_X/core/src/main/resources/template/simple/tabbedpanel.ftl (original)
+++ struts/struts2/branches/STRUTS_2_0_X/core/src/main/resources/template/simple/tabbedpanel.ftl Mon Aug 13 01:18:07 2007
@@ -24,6 +24,27 @@
   dojo.require("dojo.widget.TabContainer");
   dojo.require("dojo.widget.LinkPane");
   dojo.require("dojo.widget.ContentPane");
+  <#if parameters.useSelectedTabCookie?exists && parameters.useSelectedTabCookie=="true">
+  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);
+                    }
+                )
+            }
+        );
+  </#if>
 </script>
 
 <div dojoType="TabContainer"

Modified: struts/struts2/branches/STRUTS_2_0_X/core/src/site/resources/tags/tabbedPanel.html
URL: http://svn.apache.org/viewvc/struts/struts2/branches/STRUTS_2_0_X/core/src/site/resources/tags/tabbedPanel.html?view=diff&rev=565265&r1=565264&r2=565265
==============================================================================
--- struts/struts2/branches/STRUTS_2_0_X/core/src/site/resources/tags/tabbedPanel.html (original)
+++ struts/struts2/branches/STRUTS_2_0_X/core/src/site/resources/tags/tabbedPanel.html Mon Aug 13 01:18:07 2007
@@ -324,6 +324,14 @@
 					<td align="left" valign="top">Set the tooltip configuration</td>
 				</tr>
 				<tr>
+					<td align="left" valign="top">useSelectedTabCookie</td>
+					<td align="left" valign="top">false</td>
+					<td align="left" valign="top">false</td>
+					<td align="left" valign="top">true</td>
+					<td align="left" valign="top">String</td>
+					<td align="left" valign="top">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.</td>
+				</tr>
+				<tr>
 					<td align="left" valign="top">value</td>
 					<td align="left" valign="top">false</td>
 					<td align="left" valign="top"></td>

Added: struts/struts2/branches/STRUTS_2_0_X/core/src/test/java/org/apache/struts2/views/jsp/ui/TabbedPanelTagTest.java
URL: http://svn.apache.org/viewvc/struts/struts2/branches/STRUTS_2_0_X/core/src/test/java/org/apache/struts2/views/jsp/ui/TabbedPanelTagTest.java?view=auto&rev=565265
==============================================================================
--- struts/struts2/branches/STRUTS_2_0_X/core/src/test/java/org/apache/struts2/views/jsp/ui/TabbedPanelTagTest.java (added)
+++ struts/struts2/branches/STRUTS_2_0_X/core/src/test/java/org/apache/struts2/views/jsp/ui/TabbedPanelTagTest.java Mon Aug 13 01:18:07 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.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/branches/STRUTS_2_0_X/core/src/test/resources/org/apache/struts2/views/jsp/ui/TabbedPanel-1.txt
URL: http://svn.apache.org/viewvc/struts/struts2/branches/STRUTS_2_0_X/core/src/test/resources/org/apache/struts2/views/jsp/ui/TabbedPanel-1.txt?view=diff&rev=565265&r1=565264&r2=565265
==============================================================================
--- struts/struts2/branches/STRUTS_2_0_X/core/src/test/resources/org/apache/struts2/views/jsp/ui/TabbedPanel-1.txt (original)
+++ struts/struts2/branches/STRUTS_2_0_X/core/src/test/resources/org/apache/struts2/views/jsp/ui/TabbedPanel-1.txt Mon Aug 13 01:18:07 2007
@@ -1,12 +1,9 @@
 <script type="text/javascript">
-  dojo.require("dojo.widget.TabContainer");
-  dojo.require("dojo.widget.LinkPane");
-  dojo.require("dojo.widget.ContentPane");
+dojo.require("dojo.widget.TabContainer");
+dojo.require("dojo.widget.LinkPane");
+dojo.require("dojo.widget.ContentPane");
 </script>
 <div
-    dojoType="TabContainer"
-    selectedTab="a"
-    labelPosition="b"
-    closeButton="c"
-    doLayout="true">
-</div>
\ No newline at end of file
+  dojoType="TabContainer"
+  doLayout="false">
+</div>

Modified: struts/struts2/branches/STRUTS_2_0_X/core/src/test/resources/org/apache/struts2/views/jsp/ui/TabbedPanel-2.txt
URL: http://svn.apache.org/viewvc/struts/struts2/branches/STRUTS_2_0_X/core/src/test/resources/org/apache/struts2/views/jsp/ui/TabbedPanel-2.txt?view=diff&rev=565265&r1=565264&r2=565265
==============================================================================
--- struts/struts2/branches/STRUTS_2_0_X/core/src/test/resources/org/apache/struts2/views/jsp/ui/TabbedPanel-2.txt (original)
+++ struts/struts2/branches/STRUTS_2_0_X/core/src/test/resources/org/apache/struts2/views/jsp/ui/TabbedPanel-2.txt Mon Aug 13 01:18:07 2007
@@ -1,9 +1,19 @@
 <script type="text/javascript">
-  dojo.require("dojo.widget.TabContainer");
-  dojo.require("dojo.widget.LinkPane");
-  dojo.require("dojo.widget.ContentPane");
+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="TabContainer" id="foo" doLayout="false">
 </div>

Modified: struts/struts2/branches/STRUTS_2_0_X/core/src/test/resources/org/apache/struts2/views/jsp/ui/TabbedPanel-3.txt
URL: http://svn.apache.org/viewvc/struts/struts2/branches/STRUTS_2_0_X/core/src/test/resources/org/apache/struts2/views/jsp/ui/TabbedPanel-3.txt?view=diff&rev=565265&r1=565264&r2=565265
==============================================================================
--- struts/struts2/branches/STRUTS_2_0_X/core/src/test/resources/org/apache/struts2/views/jsp/ui/TabbedPanel-3.txt (original)
+++ struts/struts2/branches/STRUTS_2_0_X/core/src/test/resources/org/apache/struts2/views/jsp/ui/TabbedPanel-3.txt Mon Aug 13 01:18:07 2007
@@ -1,10 +1,15 @@
 <script type="text/javascript">
-  dojo.require("dojo.widget.TabContainer");
-  dojo.require("dojo.widget.LinkPane");
-  dojo.require("dojo.widget.ContentPane");
+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="TabContainer" id="foo" selectedTab="bar" doLayout="false">
 </div>