1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class ToggleTagUtil {
46  
47      public static void doEndTag(
48              String page, String id, String showImage, String hideImage,
49              String showMessage, String hideMessage, boolean defaultShowContent,
50              String stateVar, ServletContext servletContext,
51              HttpServletRequest request, HttpServletResponse response)
52          throws JspException {
53  
54          try {
55              ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
56                  WebKeys.THEME_DISPLAY);
57  
58              if (Validator.isNull(showImage) && Validator.isNull(showMessage)) {
59                  showImage =
60                      themeDisplay.getPathThemeImages() + "/arrows/01_down.png";
61              }
62  
63              if (Validator.isNull(hideImage) && Validator.isNull(hideImage)) {
64                  hideImage =
65                      themeDisplay.getPathThemeImages() + "/arrows/01_right.png";
66              }
67  
68              String defaultStateValue =
69                  defaultShowContent ? StringPool.BLANK : "none";
70              String defaultImage = defaultShowContent ? hideImage : showImage;
71              String defaultMessage =
72                  defaultShowContent ? hideMessage : showMessage;
73  
74              String clickValue = SessionClicks.get(request, id, null);
75  
76              if (defaultShowContent) {
77                  if ((clickValue != null) && (clickValue.equals("none"))) {
78                      defaultStateValue = "none";
79                      defaultImage = showImage;
80                      defaultMessage = showMessage;
81                  }
82                  else {
83                      defaultStateValue = "";
84                      defaultImage = hideImage;
85                      defaultMessage = hideMessage;
86                  }
87              }
88              else {
89                  if ((clickValue == null) || (clickValue.equals("none"))) {
90                      defaultStateValue = "none";
91                      defaultImage = showImage;
92                      defaultMessage = showMessage;
93                  }
94                  else {
95                      defaultStateValue = "";
96                      defaultImage = hideImage;
97                      defaultMessage = hideMessage;
98                  }
99              }
100 
101             if (stateVar == null) {
102                 stateVar = PwdGenerator.getPassword(PwdGenerator.KEY3, 8);
103             }
104 
105             request.setAttribute("liferay-ui:toggle:id", id);
106             request.setAttribute("liferay-ui:toggle:showImage", showImage);
107             request.setAttribute("liferay-ui:toggle:hideImage", hideImage);
108             request.setAttribute("liferay-ui:toggle:showMessage", showMessage);
109             request.setAttribute("liferay-ui:toggle:hideMessage", hideMessage);
110             request.setAttribute("liferay-ui:toggle:stateVar", stateVar);
111             request.setAttribute(
112                 "liferay-ui:toggle:defaultStateValue", defaultStateValue);
113             request.setAttribute(
114                 "liferay-ui:toggle:defaultImage", defaultImage);
115             request.setAttribute(
116                 "liferay-ui:toggle:defaultMessage", defaultMessage);
117 
118             RequestDispatcher requestDispatcher =
119                 servletContext.getRequestDispatcher(page);
120 
121             requestDispatcher.include(request, response);
122         }
123         catch (Exception e) {
124             _log.error(e, e);
125 
126             throw new JspException(e);
127         }
128     }
129 
130     private static Log _log = LogFactoryUtil.getLog(ToggleTagUtil.class);
131 
132 }