001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.taglib.ui;
022:
023: import com.liferay.portal.kernel.servlet.StringServletResponse;
024: import com.liferay.portal.kernel.util.BooleanWrapper;
025: import com.liferay.portal.kernel.util.MethodInvoker;
026: import com.liferay.portal.kernel.util.MethodWrapper;
027: import com.liferay.portal.kernel.util.NullWrapper;
028: import com.liferay.portal.kernel.util.PortalClassLoaderUtil;
029: import com.liferay.taglib.util.IncludeTag;
030:
031: import javax.servlet.ServletContext;
032: import javax.servlet.http.HttpServletRequest;
033: import javax.servlet.http.HttpServletResponse;
034: import javax.servlet.jsp.JspException;
035:
036: import org.apache.commons.logging.Log;
037: import org.apache.commons.logging.LogFactory;
038:
039: /**
040: * <a href="ToggleTag.java.html"><b><i>View Source</i></b></a>
041: *
042: * @author Brian Wing Shun Chan
043: *
044: */
045: public class ToggleTag extends IncludeTag {
046:
047: public static void doTag(String id, String onImage,
048: String offImage, boolean defaultOn, String stateVar,
049: ServletContext ctx, HttpServletRequest req,
050: HttpServletResponse res) throws Exception {
051:
052: doTag(_PAGE, id, onImage, offImage, defaultOn, stateVar, ctx,
053: req, res);
054: }
055:
056: public static void doTag(String page, String id, String onImage,
057: String offImage, boolean defaultOn, String stateVar,
058: ServletContext ctx, HttpServletRequest req,
059: HttpServletResponse res) throws Exception {
060:
061: ClassLoader contextClassLoader = Thread.currentThread()
062: .getContextClassLoader();
063:
064: try {
065: Thread.currentThread().setContextClassLoader(
066: PortalClassLoaderUtil.getClassLoader());
067:
068: Object pageWrapper = page;
069:
070: if (pageWrapper == null) {
071: pageWrapper = new NullWrapper(String.class.getName());
072: }
073:
074: Object idWrapper = id;
075:
076: if (idWrapper == null) {
077: idWrapper = new NullWrapper(String.class.getName());
078: }
079:
080: Object onImageWrapper = onImage;
081:
082: if (onImageWrapper == null) {
083: onImageWrapper = new NullWrapper(String.class.getName());
084: }
085:
086: Object offImageWrapper = offImage;
087:
088: if (offImageWrapper == null) {
089: offImageWrapper = new NullWrapper(String.class
090: .getName());
091: }
092:
093: Object stateVarWrapper = stateVar;
094:
095: if (stateVarWrapper == null) {
096: stateVarWrapper = new NullWrapper(String.class
097: .getName());
098: }
099:
100: MethodWrapper methodWrapper = new MethodWrapper(_TAG_CLASS,
101: _TAG_DO_END_METHOD, new Object[] { pageWrapper,
102: idWrapper, onImageWrapper, offImageWrapper,
103: new BooleanWrapper(defaultOn),
104: stateVarWrapper, ctx, req, res });
105:
106: MethodInvoker.invoke(methodWrapper);
107: } catch (Exception e) {
108: _log.error(e, e);
109:
110: throw e;
111: } finally {
112: Thread.currentThread().setContextClassLoader(
113: contextClassLoader);
114: }
115: }
116:
117: public int doEndTag() throws JspException {
118: try {
119: ServletContext ctx = getServletContext();
120: HttpServletRequest req = getServletRequest();
121: StringServletResponse res = getServletResponse();
122:
123: doTag(_id, _onImage, _offImage, _defaultOn, _stateVar, ctx,
124: req, res);
125:
126: pageContext.getOut().print(res.getString());
127:
128: return EVAL_PAGE;
129: } catch (Exception e) {
130: throw new JspException(e);
131: }
132: }
133:
134: public void setId(String id) {
135: _id = id;
136: }
137:
138: public void setOnImage(String onImage) {
139: _onImage = onImage;
140: }
141:
142: public void setOffImage(String offImage) {
143: _offImage = offImage;
144: }
145:
146: public void setDefaultOn(boolean defaultOn) {
147: _defaultOn = defaultOn;
148: }
149:
150: public void setStateVar(String stateVar) {
151: _stateVar = stateVar;
152: }
153:
154: protected String getDefaultPage() {
155: return _PAGE;
156: }
157:
158: private static final String _TAG_CLASS = "com.liferay.portal.servlet.taglib.ui.ToggleTagUtil";
159:
160: private static final String _TAG_DO_END_METHOD = "doEndTag";
161:
162: private static final String _PAGE = "/html/taglib/ui/toggle/page.jsp";
163:
164: private static Log _log = LogFactory.getLog(ToggleTag.class);
165:
166: private String _id;
167: private String _onImage;
168: private String _offImage;
169: private boolean _defaultOn;
170: private String _stateVar;
171:
172: }
|