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