1   /**
2    * Copyright (c) 2000-2007 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 onImage, String offImage,
51              boolean defaultOn, String stateVar, ServletContext ctx,
52              HttpServletRequest req, HttpServletResponse res)
53          throws JspException {
54  
55          try {
56              ThemeDisplay themeDisplay =
57                  (ThemeDisplay)req.getAttribute(WebKeys.THEME_DISPLAY);
58  
59              if (Validator.isNull(onImage)) {
60                  onImage =
61                      themeDisplay.getPathThemeImages() + "/arrows/01_down.png";
62              }
63  
64              if (Validator.isNull(offImage)) {
65                  offImage =
66                      themeDisplay.getPathThemeImages() + "/arrows/01_right.png";
67              }
68  
69              String defaultStateValue = defaultOn ? StringPool.BLANK : "none";
70              String defaultImage = defaultOn ? onImage : offImage;
71  
72              String clickValue = SessionClicks.get(req, id, null);
73  
74              if (defaultOn) {
75                  if ((clickValue != null) && (clickValue.equals("none"))) {
76                      defaultStateValue = "none";
77                      defaultImage = offImage;
78                  }
79                  else {
80                      defaultStateValue = "";
81                      defaultImage = onImage;
82                  }
83              }
84              else {
85                  if ((clickValue == null) || (clickValue.equals("none"))) {
86                      defaultStateValue = "none";
87                      defaultImage = offImage;
88                  }
89                  else {
90                      defaultStateValue = "";
91                      defaultImage = onImage;
92                  }
93              }
94  
95              if (stateVar == null) {
96                  stateVar = PwdGenerator.getPassword(PwdGenerator.KEY3, 8);
97              }
98  
99              req.setAttribute("liferay-ui:toggle:id", id);
100             req.setAttribute("liferay-ui:toggle:onImage", onImage);
101             req.setAttribute("liferay-ui:toggle:offImage", offImage);
102             req.setAttribute("liferay-ui:toggle:stateVar", stateVar);
103             req.setAttribute(
104                 "liferay-ui:toggle:defaultStateValue", defaultStateValue);
105             req.setAttribute("liferay-ui:toggle:defaultImage", defaultImage);
106 
107             RequestDispatcher rd = ctx.getRequestDispatcher(page);
108 
109             rd.include(req, res);
110         }
111         catch (Exception e) {
112             _log.error(e, e);
113 
114             throw new JspException(e);
115         }
116     }
117 
118     private static Log _log = LogFactory.getLog(ToggleTagUtil.class);
119 
120 }