001    /**
002     * Copyright (c) 2000-2013 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.servlet.taglib.FileAvailabilityUtil;
020    import com.liferay.portal.kernel.util.IntegerWrapper;
021    import com.liferay.portal.kernel.util.ServerDetector;
022    import com.liferay.portal.kernel.util.Validator;
023    
024    import javax.servlet.http.HttpServletRequest;
025    import javax.servlet.jsp.JspException;
026    import javax.servlet.jsp.JspWriter;
027    import javax.servlet.jsp.tagext.BodyTag;
028    
029    /**
030     * @author Brian Wing Shun Chan
031     * @author Shuyang Zhou
032     */
033    public class IconListTag extends BaseBodyTagSupport implements BodyTag {
034    
035            @Override
036            public int doAfterBody() {
037                    HttpServletRequest request =
038                            (HttpServletRequest)pageContext.getRequest();
039    
040                    IntegerWrapper iconCount = (IntegerWrapper)request.getAttribute(
041                            "liferay-ui:icon-list:icon-count");
042    
043                    Boolean singleIcon = (Boolean)request.getAttribute(
044                            "liferay-ui:icon-list:single-icon");
045    
046                    if ((iconCount != null) && (iconCount.getValue() == 1) &&
047                            (singleIcon == null)) {
048    
049                            bodyContent.clearBody();
050    
051                            request.setAttribute(
052                                    "liferay-ui:icon-list:single-icon", Boolean.TRUE);
053    
054                            return EVAL_BODY_AGAIN;
055                    }
056                    else {
057                            return SKIP_BODY;
058                    }
059            }
060    
061            @Override
062            public int doEndTag() throws JspException {
063                    try {
064                            HttpServletRequest request =
065                                    (HttpServletRequest)pageContext.getRequest();
066    
067                            IntegerWrapper iconCount = (IntegerWrapper)request.getAttribute(
068                                    "liferay-ui:icon-list:icon-count");
069    
070                            request.removeAttribute("liferay-ui:icon-list:icon-count");
071    
072                            Boolean singleIcon = (Boolean)request.getAttribute(
073                                    "liferay-ui:icon-list:single-icon");
074    
075                            request.removeAttribute("liferay-ui:icon-list:single-icon");
076    
077                            JspWriter jspWriter = pageContext.getOut();
078    
079                            if ((iconCount != null) && (iconCount.getValue() > 1) &&
080                                    ((singleIcon == null) || _showWhenSingleIcon)) {
081    
082                                    if (!FileAvailabilityUtil.isAvailable(
083                                                    pageContext.getServletContext(), getStartPage())) {
084    
085                                            jspWriter.write(
086                                                    "<ul class=\"lfr-component taglib-icon-list\">");
087                                    }
088                                    else {
089                                            PortalIncludeUtil.include(pageContext, _startPage);
090                                    }
091                            }
092    
093                            writeBodyContent(jspWriter);
094    
095                            if ((iconCount != null) && (iconCount.getValue() > 1) &&
096                                    ((singleIcon == null) || _showWhenSingleIcon)) {
097    
098                                    if (!FileAvailabilityUtil.isAvailable(
099                                                    pageContext.getServletContext(), getEndPage())) {
100    
101                                            jspWriter.write("</ul>");
102                                    }
103                                    else {
104                                            PortalIncludeUtil.include(pageContext, _endPage);
105                                    }
106                            }
107    
108                            request.removeAttribute("liferay-ui:icon-list:showWhenSingleIcon");
109    
110                            return EVAL_PAGE;
111                    }
112                    catch (Exception e) {
113                            throw new JspException(e);
114                    }
115                    finally {
116                            if (!ServerDetector.isResin()) {
117                                    _endPage = null;
118                                    _showWhenSingleIcon = false;
119                                    _startPage = null;
120                            }
121                    }
122            }
123    
124            @Override
125            public int doStartTag() {
126                    HttpServletRequest request =
127                            (HttpServletRequest)pageContext.getRequest();
128    
129                    request.setAttribute(
130                            "liferay-ui:icon-list:icon-count", new IntegerWrapper());
131                    request.setAttribute(
132                            "liferay-ui:icon-list:showWhenSingleIcon",
133                            String.valueOf(_showWhenSingleIcon));
134    
135                    return EVAL_BODY_BUFFERED;
136            }
137    
138            public void setEndPage(String endPage) {
139                    _endPage = endPage;
140            }
141    
142            public void setShowWhenSingleIcon(boolean showWhenSingleIcon) {
143                    _showWhenSingleIcon = showWhenSingleIcon;
144            }
145    
146            public void setStartPage(String startPage) {
147                    _startPage = startPage;
148            }
149    
150            protected String getEndPage() {
151                    if (Validator.isNull(_endPage)) {
152                            return _END_PAGE;
153                    }
154                    else {
155                            return _endPage;
156                    }
157            }
158    
159            protected String getStartPage() {
160                    if (Validator.isNull(_startPage)) {
161                            return _START_PAGE;
162                    }
163                    else {
164                            return _startPage;
165                    }
166            }
167    
168            private static final String _END_PAGE = "/html/taglib/ui/icon_list/end.jsp";
169    
170            private static final String _START_PAGE =
171                    "/html/taglib/ui/icon_list/start.jsp";
172    
173            private String _endPage;
174            private boolean _showWhenSingleIcon = false;
175            private String _startPage;
176    
177    }