1   /**
2    * Copyright (c) 2000-2008 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.util.StringPool;
26  import com.liferay.portal.kernel.util.Validator;
27  import com.liferay.portal.theme.ThemeDisplay;
28  import com.liferay.portal.util.SessionClicks;
29  import com.liferay.portal.util.WebKeys;
30  import com.liferay.util.PwdGenerator;
31  
32  import javax.servlet.RequestDispatcher;
33  import javax.servlet.ServletContext;
34  import javax.servlet.http.HttpServletRequest;
35  import javax.servlet.http.HttpServletResponse;
36  import javax.servlet.jsp.JspException;
37  
38  import org.apache.commons.logging.Log;
39  import org.apache.commons.logging.LogFactory;
40  
41  /**
42   * <a href="ToggleTagUtil.java.html"><b><i>View Source</i></b></a>
43   *
44   * @author Brian Wing Shun Chan
45   *
46   */
47  public class ToggleTagUtil {
48  
49      public static void doEndTag(
50              String page, String id, String showImage, String hideImage,
51              String showMessage, String hideMessage, boolean defaultShowContent,
52              String stateVar, ServletContext servletContext,
53              HttpServletRequest request, HttpServletResponse response)
54          throws JspException {
55  
56          try {
57              ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
58                  WebKeys.THEME_DISPLAY);
59  
60              if (Validator.isNull(showImage) && Validator.isNull(showMessage)) {
61                  showImage =
62                      themeDisplay.getPathThemeImages() + "/arrows/01_down.png";
63              }
64  
65              if (Validator.isNull(hideImage) && Validator.isNull(hideImage)) {
66                  hideImage =
67                      themeDisplay.getPathThemeImages() + "/arrows/01_right.png";
68              }
69  
70              String defaultStateValue =
71                  defaultShowContent ? StringPool.BLANK : "none";
72              String defaultImage = defaultShowContent ? hideImage : showImage;
73              String defaultMessage =
74                  defaultShowContent ? hideMessage : showMessage;
75  
76              String clickValue = SessionClicks.get(request, id, null);
77  
78              if (defaultShowContent) {
79                  if ((clickValue != null) && (clickValue.equals("none"))) {
80                      defaultStateValue = "none";
81                      defaultImage = showImage;
82                      defaultMessage = showMessage;
83                  }
84                  else {
85                      defaultStateValue = "";
86                      defaultImage = hideImage;
87                      defaultMessage = hideMessage;
88                  }
89              }
90              else {
91                  if ((clickValue == null) || (clickValue.equals("none"))) {
92                      defaultStateValue = "none";
93                      defaultImage = showImage;
94                      defaultMessage = showMessage;
95                  }
96                  else {
97                      defaultStateValue = "";
98                      defaultImage = hideImage;
99                      defaultMessage = hideMessage;
100                 }
101             }
102 
103             if (stateVar == null) {
104                 stateVar = PwdGenerator.getPassword(PwdGenerator.KEY3, 8);
105             }
106 
107             request.setAttribute("liferay-ui:toggle:id", id);
108             request.setAttribute("liferay-ui:toggle:showImage", showImage);
109             request.setAttribute("liferay-ui:toggle:hideImage", hideImage);
110             request.setAttribute("liferay-ui:toggle:showMessage", showMessage);
111             request.setAttribute("liferay-ui:toggle:hideMessage", hideMessage);
112             request.setAttribute("liferay-ui:toggle:stateVar", stateVar);
113             request.setAttribute(
114                 "liferay-ui:toggle:defaultStateValue", defaultStateValue);
115             request.setAttribute(
116                 "liferay-ui:toggle:defaultImage", defaultImage);
117             request.setAttribute(
118                 "liferay-ui:toggle:defaultMessage", defaultMessage);
119 
120             RequestDispatcher requestDispatcher =
121                 servletContext.getRequestDispatcher(page);
122 
123             requestDispatcher.include(request, response);
124         }
125         catch (Exception e) {
126             _log.error(e, e);
127 
128             throw new JspException(e);
129         }
130     }
131 
132     private static Log _log = LogFactory.getLog(ToggleTagUtil.class);
133 
134 }