From 8f411c1d12fc5024cc9e0227013b9ce1f5855ea4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Gielen?= Date: Mon, 13 Aug 2007 08:13:30 +0000 Subject: [PATCH] WW-2108: Make being able to remember last selected tab using a cookie git-svn-id: https://svn.apache.org/repos/asf/struts/struts2/trunk@565264 13f79535-47bb-0310-9956-ffa450edef68 --- .../struts2/dojo/components/TabbedPanel.java | 20 +++++- .../dojo/views/jsp/ui/TabbedPanelTag.java | 10 ++- .../resources/template/ajax/tabbedpanel.ftl | 24 +++++++ .../dojo/views/jsp/ui/TabbedPanelTagTest.java | 68 +++++++++++++++++++ .../dojo/views/jsp/ui/TabbedPanel-1.txt | 15 ++-- .../dojo/views/jsp/ui/TabbedPanel-2.txt | 20 ++++-- .../dojo/views/jsp/ui/TabbedPanel-3.txt | 17 +++-- 7 files changed, 146 insertions(+), 28 deletions(-) create mode 100644 plugins/dojo/src/test/java/org/apache/struts2/dojo/views/jsp/ui/TabbedPanelTagTest.java diff --git a/plugins/dojo/src/main/java/org/apache/struts2/dojo/components/TabbedPanel.java b/plugins/dojo/src/main/java/org/apache/struts2/dojo/components/TabbedPanel.java index 3c3a8e8b1..cd0d7c84e 100644 --- a/plugins/dojo/src/main/java/org/apache/struts2/dojo/components/TabbedPanel.java +++ b/plugins/dojo/src/main/java/org/apache/struts2/dojo/components/TabbedPanel.java @@ -34,6 +34,11 @@ import com.opensymphony.xwork2.util.ValueStack; * * 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).

+ * 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.

+ * 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.

* * *

Examples @@ -92,7 +97,8 @@ public class TabbedPanel extends ClosingUIBean { 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 @@ public class TabbedPanel extends ClosingUIBean { addParameter("afterSelectTabNotifyTopics", findString(afterSelectTabNotifyTopics)); if (disabledTabCssClass!= null) addParameter("disabledTabCssClass", findString(disabledTabCssClass)); - + if(useSelectedTabCookie != null) { + addParameter("useSelectedTabCookie", findString(useSelectedTabCookie)); + } + } @Override @@ -195,4 +204,11 @@ public class TabbedPanel extends ClosingUIBean { 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; + } } diff --git a/plugins/dojo/src/main/java/org/apache/struts2/dojo/views/jsp/ui/TabbedPanelTag.java b/plugins/dojo/src/main/java/org/apache/struts2/dojo/views/jsp/ui/TabbedPanelTag.java index a7cb3ae86..f7046c04d 100644 --- a/plugins/dojo/src/main/java/org/apache/struts2/dojo/views/jsp/ui/TabbedPanelTag.java +++ b/plugins/dojo/src/main/java/org/apache/struts2/dojo/views/jsp/ui/TabbedPanelTag.java @@ -42,8 +42,9 @@ public class TabbedPanelTag extends AbstractClosingTag { 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 @@ public class TabbedPanelTag extends AbstractClosingTag { tabbedPanel.setBeforeSelectTabNotifyTopics(beforeSelectTabNotifyTopics); tabbedPanel.setAfterSelectTabNotifyTopics(afterSelectTabNotifyTopics); tabbedPanel.setDisabledTabCssClass(disabledTabCssClass); + tabbedPanel.setUseSelectedTabCookie(useSelectedTabCookie); } public void setSelectedTab(String selectedTab) { @@ -88,4 +90,8 @@ public class TabbedPanelTag extends AbstractClosingTag { public void setDisabledTabCssClass(String disabledTabCssClass) { this.disabledTabCssClass = disabledTabCssClass; } + + public void setUseSelectedTabCookie( String useSelectedTabCookie ) { + this.useSelectedTabCookie = useSelectedTabCookie; + } } diff --git a/plugins/dojo/src/main/resources/template/ajax/tabbedpanel.ftl b/plugins/dojo/src/main/resources/template/ajax/tabbedpanel.ftl index a2b66a656..88c735ca9 100644 --- a/plugins/dojo/src/main/resources/template/ajax/tabbedpanel.ftl +++ b/plugins/dojo/src/main/resources/template/ajax/tabbedpanel.ftl @@ -21,6 +21,30 @@ */ --> +<#if parameters.useSelectedTabCookie?exists && parameters.useSelectedTabCookie=="true"> + + +

style="${parameters.cssStyle?html}"<#rt/> diff --git a/plugins/dojo/src/test/java/org/apache/struts2/dojo/views/jsp/ui/TabbedPanelTagTest.java b/plugins/dojo/src/test/java/org/apache/struts2/dojo/views/jsp/ui/TabbedPanelTagTest.java new file mode 100644 index 000000000..ce9568dd6 --- /dev/null +++ b/plugins/dojo/src/test/java/org/apache/struts2/dojo/views/jsp/ui/TabbedPanelTagTest.java @@ -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")); + + } + +} diff --git a/plugins/dojo/src/test/resources/org/apache/struts2/dojo/views/jsp/ui/TabbedPanel-1.txt b/plugins/dojo/src/test/resources/org/apache/struts2/dojo/views/jsp/ui/TabbedPanel-1.txt index 43e5adf00..1b3c8b625 100644 --- a/plugins/dojo/src/test/resources/org/apache/struts2/dojo/views/jsp/ui/TabbedPanel-1.txt +++ b/plugins/dojo/src/test/resources/org/apache/struts2/dojo/views/jsp/ui/TabbedPanel-1.txt @@ -1,12 +1,5 @@ - +
-
\ No newline at end of file + dojoType="struts:StrutsTabContainer" + doLayout="false"> +
diff --git a/plugins/dojo/src/test/resources/org/apache/struts2/dojo/views/jsp/ui/TabbedPanel-2.txt b/plugins/dojo/src/test/resources/org/apache/struts2/dojo/views/jsp/ui/TabbedPanel-2.txt index 863690119..dfd41bb8f 100644 --- a/plugins/dojo/src/test/resources/org/apache/struts2/dojo/views/jsp/ui/TabbedPanel-2.txt +++ b/plugins/dojo/src/test/resources/org/apache/struts2/dojo/views/jsp/ui/TabbedPanel-2.txt @@ -1,9 +1,17 @@ + -
+
\ No newline at end of file diff --git a/plugins/dojo/src/test/resources/org/apache/struts2/dojo/views/jsp/ui/TabbedPanel-3.txt b/plugins/dojo/src/test/resources/org/apache/struts2/dojo/views/jsp/ui/TabbedPanel-3.txt index fe7436419..fac0220f1 100644 --- a/plugins/dojo/src/test/resources/org/apache/struts2/dojo/views/jsp/ui/TabbedPanel-3.txt +++ b/plugins/dojo/src/test/resources/org/apache/struts2/dojo/views/jsp/ui/TabbedPanel-3.txt @@ -1,10 +1,13 @@ + -
+
\ No newline at end of file