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