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.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 showImage, String hideImage, String showMessage,
51              String hideMessage, boolean defaultShowContent, String stateVar,
52              ServletContext servletContext, HttpServletRequest request,
53              HttpServletResponse response)
54          throws Exception {
55  
56          doTag(
57              _PAGE, id, showImage, hideImage, showMessage, hideMessage,
58              defaultShowContent, stateVar, servletContext, request, response);
59      }
60  
61      public static void doTag(
62              String page, String id, String showImage, String hideImage,
63              String showMessage, String hideMessage, boolean defaultShowContent,
64              String stateVar, ServletContext servletContext,
65              HttpServletRequest request, HttpServletResponse response)
66          throws Exception {
67  
68          Thread currentThread = Thread.currentThread();
69  
70          ClassLoader contextClassLoader = currentThread.getContextClassLoader();
71  
72          try {
73              currentThread.setContextClassLoader(
74                  PortalClassLoaderUtil.getClassLoader());
75  
76              Object idWrapper = id;
77  
78              if (idWrapper == null) {
79                  idWrapper = new NullWrapper(String.class.getName());
80              }
81  
82              Object showImageWrapper = showImage;
83  
84              if (showImageWrapper == null) {
85                  showImageWrapper = new NullWrapper(String.class.getName());
86              }
87  
88              Object hideImageWrapper = hideImage;
89  
90              if (hideImageWrapper == null) {
91                  hideImageWrapper = new NullWrapper(String.class.getName());
92              }
93  
94              Object showMessageWrapper = showMessage;
95  
96              if (showMessageWrapper == null) {
97                  showMessageWrapper = new NullWrapper(String.class.getName());
98              }
99  
100             Object hideMessageWrapper = hideMessage;
101 
102             if (hideMessageWrapper == null) {
103                 hideMessageWrapper = new NullWrapper(String.class.getName());
104             }
105 
106             Object stateVarWrapper = stateVar;
107 
108             if (stateVarWrapper == null) {
109                 stateVarWrapper = new NullWrapper(String.class.getName());
110             }
111 
112             MethodWrapper methodWrapper = new MethodWrapper(
113                 _TAG_CLASS, _TAG_DO_END_METHOD,
114                 new Object[] {
115                     page, idWrapper, showImageWrapper, hideImageWrapper,
116                     showMessageWrapper, hideMessageWrapper,
117                     new BooleanWrapper(defaultShowContent), stateVarWrapper,
118                     servletContext, request, response
119                 });
120 
121             MethodInvoker.invoke(methodWrapper);
122         }
123         catch (Exception e) {
124             _log.error(e, e);
125 
126             throw e;
127         }
128         finally {
129             currentThread.setContextClassLoader(contextClassLoader);
130         }
131     }
132 
133     public int doEndTag() throws JspException {
134         try {
135             ServletContext servletContext = getServletContext();
136             HttpServletRequest request = getServletRequest();
137             StringServletResponse response = getServletResponse();
138 
139             doTag(
140                 getPage(), _id, _showImage, _hideImage, _showMessage,
141                 _hideMessage, _defaultShowContent, _stateVar, servletContext,
142                 request, response);
143 
144             pageContext.getOut().print(response.getString());
145 
146             return EVAL_PAGE;
147         }
148         catch (Exception e) {
149             throw new JspException(e);
150         }
151     }
152 
153     public void setId(String id) {
154         _id = id;
155     }
156 
157     public void setShowImage(String showImage) {
158         _showImage = showImage;
159     }
160 
161     public void setHideImage(String hideImage) {
162         _hideImage = hideImage;
163     }
164 
165     public void setShowMessage(String showMessage) {
166         _showMessage = showMessage;
167     }
168 
169     public void setHideMessage(String hideMessage) {
170         _hideMessage = hideMessage;
171     }
172 
173     public void setDefaultShowContent(boolean defaultShowContent) {
174         _defaultShowContent = defaultShowContent;
175     }
176 
177     public void setStateVar(String stateVar) {
178         _stateVar = stateVar;
179     }
180 
181     protected String getDefaultPage() {
182         return _PAGE;
183     }
184 
185     private static final String _TAG_CLASS =
186         "com.liferay.portal.servlet.taglib.ui.ToggleTagUtil";
187 
188     private static final String _TAG_DO_END_METHOD = "doEndTag";
189 
190     private static final String _PAGE = "/html/taglib/ui/toggle/page.jsp";
191 
192     private static Log _log = LogFactory.getLog(ToggleTag.class);
193 
194     private String _id;
195     private String _showImage;
196     private String _hideImage;
197     private String _showMessage;
198     private String _hideMessage;
199     private boolean _defaultShowContent = true;
200     private String _stateVar;
201 
202 }