001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.taglib.ui;
016    
017    import com.liferay.portal.kernel.servlet.PortalIncludeUtil;
018    import com.liferay.portal.kernel.servlet.taglib.BaseBodyTagSupport;
019    import com.liferay.portal.kernel.util.IntegerWrapper;
020    import com.liferay.portal.kernel.util.ServerDetector;
021    import com.liferay.portal.kernel.util.Validator;
022    
023    import javax.servlet.http.HttpServletRequest;
024    import javax.servlet.jsp.JspException;
025    
026    /**
027     * @author Brian Wing Shun Chan
028     */
029    public class IconListTag extends BaseBodyTagSupport {
030    
031            public int doAfterBody() {
032                    HttpServletRequest request =
033                            (HttpServletRequest)pageContext.getRequest();
034    
035                    IntegerWrapper iconCount = (IntegerWrapper)request.getAttribute(
036                            "liferay-ui:icon-list:icon-count");
037    
038                    Boolean singleIcon = (Boolean)request.getAttribute(
039                            "liferay-ui:icon-list:single-icon");
040    
041                    if ((iconCount != null) && (iconCount.getValue() == 1) &&
042                            (singleIcon == null)) {
043    
044                            bodyContent.clearBody();
045    
046                            request.setAttribute(
047                                    "liferay-ui:icon-list:single-icon", Boolean.TRUE);
048    
049                            return EVAL_BODY_AGAIN;
050                    }
051                    else {
052                            return SKIP_BODY;
053                    }
054            }
055    
056            public int doEndTag() throws JspException {
057                    try {
058                            HttpServletRequest request =
059                                    (HttpServletRequest)pageContext.getRequest();
060    
061                            IntegerWrapper iconCount = (IntegerWrapper)request.getAttribute(
062                                    "liferay-ui:icon-list:icon-count");
063    
064                            request.removeAttribute("liferay-ui:icon-list:icon-count");
065    
066                            Boolean singleIcon = (Boolean)request.getAttribute(
067                                    "liferay-ui:icon-list:single-icon");
068    
069                            request.removeAttribute("liferay-ui:icon-list:single-icon");
070    
071                            if ((iconCount != null) && (iconCount.getValue() > 1) &&
072                                    ((singleIcon == null) || _showWhenSingleIcon)) {
073    
074                                    PortalIncludeUtil.include(pageContext, getStartPage());
075                            }
076    
077                            writeBodyContent(pageContext.getOut());
078    
079                            if ((iconCount != null) && (iconCount.getValue() > 1) &&
080                                    ((singleIcon == null) || _showWhenSingleIcon)) {
081    
082                                    PortalIncludeUtil.include(pageContext, getEndPage());
083                            }
084    
085                            request.removeAttribute("liferay-ui:icon-list:showWhenSingleIcon");
086    
087                            return EVAL_PAGE;
088                    }
089                    catch (Exception e) {
090                            throw new JspException(e);
091                    }
092                    finally {
093                            if (!ServerDetector.isResin()) {
094                                    _endPage = null;
095                                    _showWhenSingleIcon = false;
096                                    _startPage = null;
097                            }
098                    }
099            }
100    
101            public int doStartTag() {
102                    HttpServletRequest request =
103                            (HttpServletRequest)pageContext.getRequest();
104    
105                    request.setAttribute(
106                            "liferay-ui:icon-list:icon-count", new IntegerWrapper());
107                    request.setAttribute(
108                            "liferay-ui:icon-list:showWhenSingleIcon",
109                            String.valueOf(_showWhenSingleIcon));
110    
111                    return EVAL_BODY_BUFFERED;
112            }
113    
114            protected String getEndPage() {
115                    if (Validator.isNull(_endPage)) {
116                            return _END_PAGE;
117                    }
118                    else {
119                            return _endPage;
120                    }
121            }
122    
123            protected String getStartPage() {
124                    if (Validator.isNull(_startPage)) {
125                            return _START_PAGE;
126                    }
127                    else {
128                            return _startPage;
129                    }
130            }
131    
132            public void setEndPage(String endPage) {
133                    _endPage = endPage;
134            }
135    
136            public void setShowWhenSingleIcon(boolean showWhenSingleIcon) {
137                    _showWhenSingleIcon = showWhenSingleIcon;
138            }
139    
140            public void setStartPage(String startPage) {
141                    _startPage = startPage;
142            }
143    
144            private static final String _END_PAGE = "/html/taglib/ui/icon_list/end.jsp";
145    
146            private static final String _START_PAGE =
147                    "/html/taglib/ui/icon_list/start.jsp";
148    
149            private String _endPage;
150            private boolean _showWhenSingleIcon = false;
151            private String _startPage;
152    
153    }