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