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.taglib.ui;
24  
25  import com.liferay.portal.kernel.servlet.StringServletResponse;
26  import com.liferay.portal.kernel.util.BooleanWrapper;
27  import com.liferay.portal.kernel.util.MethodInvoker;
28  import com.liferay.portal.kernel.util.MethodWrapper;
29  import com.liferay.portal.kernel.util.NullWrapper;
30  import com.liferay.portal.kernel.util.PortalClassLoaderUtil;
31  import com.liferay.taglib.util.IncludeTag;
32  
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="ToggleTag.java.html"><b><i>View Source</i></b></a>
43   *
44   * @author Brian Wing Shun Chan
45   *
46   */
47  public class ToggleTag extends IncludeTag {
48  
49      public static void doTag(
50              String id, String onImage, String offImage, boolean defaultOn,
51              String stateVar, ServletContext ctx, HttpServletRequest req,
52              HttpServletResponse res)
53          throws Exception {
54  
55          doTag(_PAGE, id, onImage, offImage, defaultOn, stateVar, ctx, req, res);
56      }
57  
58      public static void doTag(
59              String page, String id, String onImage, String offImage,
60              boolean defaultOn, String stateVar, ServletContext ctx,
61              HttpServletRequest req, HttpServletResponse res)
62          throws Exception {
63  
64          ClassLoader contextClassLoader =
65              Thread.currentThread().getContextClassLoader();
66  
67          try {
68              Thread.currentThread().setContextClassLoader(
69                  PortalClassLoaderUtil.getClassLoader());
70  
71              Object pageWrapper = page;
72  
73              if (pageWrapper == null) {
74                  pageWrapper = new NullWrapper(String.class.getName());
75              }
76  
77              Object idWrapper = id;
78  
79              if (idWrapper == null) {
80                  idWrapper = new NullWrapper(String.class.getName());
81              }
82  
83              Object onImageWrapper = onImage;
84  
85              if (onImageWrapper == null) {
86                  onImageWrapper = new NullWrapper(String.class.getName());
87              }
88  
89              Object offImageWrapper = offImage;
90  
91              if (offImageWrapper == null) {
92                  offImageWrapper = new NullWrapper(String.class.getName());
93              }
94  
95              Object stateVarWrapper = stateVar;
96  
97              if (stateVarWrapper == null) {
98                  stateVarWrapper = new NullWrapper(String.class.getName());
99              }
100 
101             MethodWrapper methodWrapper = new MethodWrapper(
102                 _TAG_CLASS, _TAG_DO_END_METHOD,
103                 new Object[] {
104                     pageWrapper, idWrapper, onImageWrapper, offImageWrapper,
105                     new BooleanWrapper(defaultOn), stateVarWrapper, ctx, req,
106                     res
107                 });
108 
109             MethodInvoker.invoke(methodWrapper);
110         }
111         catch (Exception e) {
112             _log.error(e, e);
113 
114             throw e;
115         }
116         finally {
117             Thread.currentThread().setContextClassLoader(contextClassLoader);
118         }
119     }
120 
121     public int doEndTag() throws JspException {
122         try {
123             ServletContext ctx = getServletContext();
124             HttpServletRequest req = getServletRequest();
125             StringServletResponse res = getServletResponse();
126 
127             doTag(
128                 _id, _onImage, _offImage, _defaultOn, _stateVar, ctx, req, res);
129 
130             pageContext.getOut().print(res.getString());
131 
132             return EVAL_PAGE;
133         }
134         catch (Exception e) {
135             throw new JspException(e);
136         }
137     }
138 
139     public void setId(String id) {
140         _id = id;
141     }
142 
143     public void setOnImage(String onImage) {
144         _onImage = onImage;
145     }
146 
147     public void setOffImage(String offImage) {
148         _offImage = offImage;
149     }
150 
151     public void setDefaultOn(boolean defaultOn) {
152         _defaultOn = defaultOn;
153     }
154 
155     public void setStateVar(String stateVar) {
156         _stateVar = stateVar;
157     }
158 
159     protected String getDefaultPage() {
160         return _PAGE;
161     }
162 
163     private static final String _TAG_CLASS =
164         "com.liferay.portal.servlet.taglib.ui.ToggleTagUtil";
165 
166     private static final String _TAG_DO_END_METHOD = "doEndTag";
167 
168     private static final String _PAGE = "/html/taglib/ui/toggle/page.jsp";
169 
170     private static Log _log = LogFactory.getLog(ToggleTag.class);
171 
172     private String _id;
173     private String _onImage;
174     private String _offImage;
175     private boolean _defaultOn;
176     private String _stateVar;
177 
178 }