1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.taglib.ui;
21  
22  import com.liferay.portal.kernel.log.Log;
23  import com.liferay.portal.kernel.log.LogFactoryUtil;
24  import com.liferay.portal.kernel.servlet.StringServletResponse;
25  import com.liferay.portal.kernel.util.BooleanWrapper;
26  import com.liferay.portal.kernel.util.MethodInvoker;
27  import com.liferay.portal.kernel.util.MethodWrapper;
28  import com.liferay.portal.kernel.util.NullWrapper;
29  import com.liferay.portal.kernel.util.PortalClassLoaderUtil;
30  import com.liferay.taglib.util.IncludeTag;
31  
32  import javax.servlet.ServletContext;
33  import javax.servlet.http.HttpServletRequest;
34  import javax.servlet.http.HttpServletResponse;
35  import javax.servlet.jsp.JspException;
36  
37  /**
38   * <a href="ToggleTag.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author Brian Wing Shun Chan
41   *
42   */
43  public class ToggleTag extends IncludeTag {
44  
45      public static void doTag(
46              String id, String showImage, String hideImage, String showMessage,
47              String hideMessage, boolean defaultShowContent, String stateVar,
48              ServletContext servletContext, HttpServletRequest request,
49              HttpServletResponse response)
50          throws Exception {
51  
52          doTag(
53              _PAGE, id, showImage, hideImage, showMessage, hideMessage,
54              defaultShowContent, stateVar, servletContext, request, response);
55      }
56  
57      public static void doTag(
58              String page, String id, String showImage, String hideImage,
59              String showMessage, String hideMessage, boolean defaultShowContent,
60              String stateVar, ServletContext servletContext,
61              HttpServletRequest request, HttpServletResponse response)
62          throws Exception {
63  
64          Thread currentThread = Thread.currentThread();
65  
66          ClassLoader contextClassLoader = currentThread.getContextClassLoader();
67  
68          try {
69              currentThread.setContextClassLoader(
70                  PortalClassLoaderUtil.getClassLoader());
71  
72              Object idWrapper = id;
73  
74              if (idWrapper == null) {
75                  idWrapper = new NullWrapper(String.class.getName());
76              }
77  
78              Object showImageWrapper = showImage;
79  
80              if (showImageWrapper == null) {
81                  showImageWrapper = new NullWrapper(String.class.getName());
82              }
83  
84              Object hideImageWrapper = hideImage;
85  
86              if (hideImageWrapper == null) {
87                  hideImageWrapper = new NullWrapper(String.class.getName());
88              }
89  
90              Object showMessageWrapper = showMessage;
91  
92              if (showMessageWrapper == null) {
93                  showMessageWrapper = new NullWrapper(String.class.getName());
94              }
95  
96              Object hideMessageWrapper = hideMessage;
97  
98              if (hideMessageWrapper == null) {
99                  hideMessageWrapper = new NullWrapper(String.class.getName());
100             }
101 
102             Object stateVarWrapper = stateVar;
103 
104             if (stateVarWrapper == null) {
105                 stateVarWrapper = new NullWrapper(String.class.getName());
106             }
107 
108             MethodWrapper methodWrapper = new MethodWrapper(
109                 _TAG_CLASS, _TAG_DO_END_METHOD,
110                 new Object[] {
111                     page, idWrapper, showImageWrapper, hideImageWrapper,
112                     showMessageWrapper, hideMessageWrapper,
113                     new BooleanWrapper(defaultShowContent), stateVarWrapper,
114                     servletContext, request, response
115                 });
116 
117             MethodInvoker.invoke(methodWrapper);
118         }
119         catch (Exception e) {
120             _log.error(e, e);
121 
122             throw e;
123         }
124         finally {
125             currentThread.setContextClassLoader(contextClassLoader);
126         }
127     }
128 
129     public int doEndTag() throws JspException {
130         try {
131             ServletContext servletContext = getServletContext();
132             HttpServletRequest request = getServletRequest();
133             StringServletResponse response = getServletResponse();
134 
135             doTag(
136                 getPage(), _id, _showImage, _hideImage, _showMessage,
137                 _hideMessage, _defaultShowContent, _stateVar, servletContext,
138                 request, response);
139 
140             pageContext.getOut().print(response.getString());
141 
142             return EVAL_PAGE;
143         }
144         catch (Exception e) {
145             throw new JspException(e);
146         }
147     }
148 
149     public void setId(String id) {
150         _id = id;
151     }
152 
153     public void setShowImage(String showImage) {
154         _showImage = showImage;
155     }
156 
157     public void setHideImage(String hideImage) {
158         _hideImage = hideImage;
159     }
160 
161     public void setShowMessage(String showMessage) {
162         _showMessage = showMessage;
163     }
164 
165     public void setHideMessage(String hideMessage) {
166         _hideMessage = hideMessage;
167     }
168 
169     public void setDefaultShowContent(boolean defaultShowContent) {
170         _defaultShowContent = defaultShowContent;
171     }
172 
173     public void setStateVar(String stateVar) {
174         _stateVar = stateVar;
175     }
176 
177     protected String getDefaultPage() {
178         return _PAGE;
179     }
180 
181     private static final String _TAG_CLASS =
182         "com.liferay.portal.servlet.taglib.ui.ToggleTagUtil";
183 
184     private static final String _TAG_DO_END_METHOD = "doEndTag";
185 
186     private static final String _PAGE = "/html/taglib/ui/toggle/page.jsp";
187 
188     private static Log _log = LogFactoryUtil.getLog(ToggleTag.class);
189 
190     private String _id;
191     private String _showImage;
192     private String _hideImage;
193     private String _showMessage;
194     private String _hideMessage;
195     private boolean _defaultShowContent = true;
196     private String _stateVar;
197 
198 }