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    
033                    HttpServletRequest request =
034                            (HttpServletRequest)pageContext.getRequest();
035    
036                    IntegerWrapper iconCount = (IntegerWrapper)request.getAttribute(
037                            "liferay-ui:icon-list:icon-count");
038    
039                    Boolean singleIcon = (Boolean)request.getAttribute(
040                            "liferay-ui:icon-list:single-icon");
041    
042                    if ((iconCount != null) && (iconCount.getValue() == 1) &&
043                            (singleIcon == null)) {
044    
045                            bodyContent.clearBody();
046    
047                            request.setAttribute(
048                                    "liferay-ui:icon-list:single-icon", Boolean.TRUE);
049    
050                            return EVAL_BODY_AGAIN;
051                    }
052                    else {
053                            return SKIP_BODY;
054                    }
055            }
056    
057            public int doEndTag() throws JspException {
058                    try {
059                            HttpServletRequest request =
060                                    (HttpServletRequest)pageContext.getRequest();
061    
062                            IntegerWrapper iconCount = (IntegerWrapper)request.getAttribute(
063                                    "liferay-ui:icon-list:icon-count");
064    
065                            request.removeAttribute("liferay-ui:icon-list:icon-count");
066    
067                            Boolean singleIcon = (Boolean)request.getAttribute(
068                                    "liferay-ui:icon-list:single-icon");
069    
070                            request.removeAttribute("liferay-ui:icon-list:single-icon");
071    
072                            if ((iconCount != null) && (iconCount.getValue() > 1) &&
073                                    ((singleIcon == null) || _showWhenSingleIcon)) {
074    
075                                    PortalIncludeUtil.include(pageContext, getStartPage());
076                            }
077    
078                            writeBodyContent(pageContext.getOut());
079    
080                            if ((iconCount != null) && (iconCount.getValue() > 1) &&
081                                    ((singleIcon == null) || _showWhenSingleIcon)) {
082    
083                                    PortalIncludeUtil.include(pageContext, getEndPage());
084                            }
085    
086                            request.removeAttribute("liferay-ui:icon-list:showWhenSingleIcon");
087    
088                            return EVAL_PAGE;
089                    }
090                    catch (Exception e) {
091                            throw new JspException(e);
092                    }
093                    finally {
094                            if (!ServerDetector.isResin()) {
095                                    _endPage = null;
096                                    _showWhenSingleIcon = false;
097                                    _startPage = null;
098                            }
099                    }
100            }
101    
102            public int doStartTag() {
103                    HttpServletRequest request =
104                            (HttpServletRequest)pageContext.getRequest();
105    
106                    request.setAttribute(
107                            "liferay-ui:icon-list:icon-count", new IntegerWrapper());
108                    request.setAttribute(
109                            "liferay-ui:icon-list:showWhenSingleIcon",
110                            String.valueOf(_showWhenSingleIcon));
111    
112                    return EVAL_BODY_BUFFERED;
113            }
114    
115            protected String getEndPage() {
116                    if (Validator.isNull(_endPage)) {
117                            return _END_PAGE;
118                    }
119                    else {
120                            return _endPage;
121                    }
122            }
123    
124            protected String getStartPage() {
125                    if (Validator.isNull(_startPage)) {
126                            return _START_PAGE;
127                    }
128                    else {
129                            return _startPage;
130                    }
131            }
132    
133            public void setEndPage(String endPage) {
134                    _endPage = endPage;
135            }
136    
137            public void setShowWhenSingleIcon(boolean showWhenSingleIcon) {
138                    _showWhenSingleIcon = showWhenSingleIcon;
139            }
140    
141            public void setStartPage(String startPage) {
142                    _startPage = startPage;
143            }
144    
145            private static final String _END_PAGE = "/html/taglib/ui/icon_list/end.jsp";
146    
147            private static final String _START_PAGE =
148                    "/html/taglib/ui/icon_list/start.jsp";
149    
150            private String _endPage;
151            private boolean _showWhenSingleIcon = false;
152            private String _startPage;
153    
154    }