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