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