001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.taglib.ui;
016    
017    import com.liferay.portal.kernel.util.StringPool;
018    import com.liferay.portal.util.SessionClicks;
019    
020    import javax.servlet.http.HttpServletRequest;
021    import javax.servlet.jsp.JspException;
022    import javax.servlet.jsp.JspWriter;
023    import javax.servlet.jsp.PageContext;
024    import javax.servlet.jsp.tagext.TagSupport;
025    
026    /**
027     * @author Brian Wing Shun Chan
028     */
029    public class ToggleValueTag extends TagSupport {
030    
031            /**
032             * @deprecated
033             */
034            public static void doTag(
035                            String id, PageContext pageContext, HttpServletRequest request)
036                    throws Exception {
037    
038                    doTag(id, "block", pageContext);
039            }
040    
041            public static void doTag(
042                            String id, String defaultValue, PageContext pageContext)
043                    throws Exception {
044    
045                    HttpServletRequest request =
046                            (HttpServletRequest)pageContext.getRequest();
047    
048                    String value = SessionClicks.get(request, id, StringPool.BLANK);
049    
050                    if (value.equals(StringPool.BLANK)) {
051                            value = defaultValue;
052                    }
053    
054                    JspWriter jspWriter = pageContext.getOut();
055    
056                    jspWriter.write(value);
057            }
058    
059            @Override
060            public int doEndTag() throws JspException {
061                    try {
062                            doTag(_id, _defaultValue, pageContext);
063    
064                            return EVAL_PAGE;
065                    }
066                    catch (Exception e) {
067                            throw new JspException(e);
068                    }
069            }
070    
071            public void setDefaultValue(String defaultValue) {
072                    _defaultValue = defaultValue;
073            }
074    
075            @Override
076            public void setId(String id) {
077                    _id = id;
078            }
079    
080            private String _defaultValue = "block";
081            private String _id;
082    
083    }