1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.servlet.taglib.ui;
21  
22  import com.liferay.portal.kernel.log.Log;
23  import com.liferay.portal.kernel.log.LogFactoryUtil;
24  import com.liferay.portal.kernel.util.StringPool;
25  import com.liferay.portal.kernel.util.Validator;
26  import com.liferay.portal.theme.ThemeDisplay;
27  import com.liferay.portal.util.SessionClicks;
28  import com.liferay.portal.util.WebKeys;
29  import com.liferay.util.PwdGenerator;
30  
31  import javax.servlet.RequestDispatcher;
32  import javax.servlet.ServletContext;
33  import javax.servlet.http.HttpServletRequest;
34  import javax.servlet.http.HttpServletResponse;
35  import javax.servlet.jsp.JspException;
36  
37  /**
38   * <a href="ToggleTagUtil.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author Brian Wing Shun Chan
41   *
42   */
43  public class ToggleTagUtil {
44  
45      public static void doEndTag(
46              String page, String id, String showImage, String hideImage,
47              String showMessage, String hideMessage, boolean defaultShowContent,
48              String stateVar, ServletContext servletContext,
49              HttpServletRequest request, HttpServletResponse response)
50          throws JspException {
51  
52          try {
53              ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
54                  WebKeys.THEME_DISPLAY);
55  
56              if (Validator.isNull(showImage) && Validator.isNull(showMessage)) {
57                  showImage =
58                      themeDisplay.getPathThemeImages() + "/arrows/01_down.png";
59              }
60  
61              if (Validator.isNull(hideImage) && Validator.isNull(hideImage)) {
62                  hideImage =
63                      themeDisplay.getPathThemeImages() + "/arrows/01_right.png";
64              }
65  
66              String defaultStateValue =
67                  defaultShowContent ? StringPool.BLANK : "none";
68              String defaultImage = defaultShowContent ? hideImage : showImage;
69              String defaultMessage =
70                  defaultShowContent ? hideMessage : showMessage;
71  
72              String clickValue = SessionClicks.get(request, id, null);
73  
74              if (defaultShowContent) {
75                  if ((clickValue != null) && (clickValue.equals("none"))) {
76                      defaultStateValue = "none";
77                      defaultImage = showImage;
78                      defaultMessage = showMessage;
79                  }
80                  else {
81                      defaultStateValue = "";
82                      defaultImage = hideImage;
83                      defaultMessage = hideMessage;
84                  }
85              }
86              else {
87                  if ((clickValue == null) || (clickValue.equals("none"))) {
88                      defaultStateValue = "none";
89                      defaultImage = showImage;
90                      defaultMessage = showMessage;
91                  }
92                  else {
93                      defaultStateValue = "";
94                      defaultImage = hideImage;
95                      defaultMessage = hideMessage;
96                  }
97              }
98  
99              if (stateVar == null) {
100                 stateVar = PwdGenerator.getPassword(PwdGenerator.KEY3, 8);
101             }
102 
103             request.setAttribute("liferay-ui:toggle:id", id);
104             request.setAttribute("liferay-ui:toggle:showImage", showImage);
105             request.setAttribute("liferay-ui:toggle:hideImage", hideImage);
106             request.setAttribute("liferay-ui:toggle:showMessage", showMessage);
107             request.setAttribute("liferay-ui:toggle:hideMessage", hideMessage);
108             request.setAttribute("liferay-ui:toggle:stateVar", stateVar);
109             request.setAttribute(
110                 "liferay-ui:toggle:defaultStateValue", defaultStateValue);
111             request.setAttribute(
112                 "liferay-ui:toggle:defaultImage", defaultImage);
113             request.setAttribute(
114                 "liferay-ui:toggle:defaultMessage", defaultMessage);
115 
116             RequestDispatcher requestDispatcher =
117                 servletContext.getRequestDispatcher(page);
118 
119             requestDispatcher.include(request, response);
120         }
121         catch (Exception e) {
122             _log.error(e, e);
123 
124             throw new JspException(e);
125         }
126     }
127 
128     private static Log _log = LogFactoryUtil.getLog(ToggleTagUtil.class);
129 
130 }