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("<ul class=\"taglib-icon-list unstyled\">");
086                                    }
087                                    else {
088                                            PortalIncludeUtil.include(pageContext, _startPage);
089                                    }
090                            }
091    
092                            writeBodyContent(jspWriter);
093    
094                            if ((iconCount != null) && (iconCount.getValue() > 1) &&
095                                    ((singleIcon == null) || _showWhenSingleIcon)) {
096    
097                                    if (!FileAvailabilityUtil.isAvailable(
098                                                    pageContext.getServletContext(), getEndPage())) {
099    
100                                            jspWriter.write("</ul>");
101                                    }
102                                    else {
103                                            PortalIncludeUtil.include(pageContext, _endPage);
104                                    }
105                            }
106    
107                            request.removeAttribute("liferay-ui:icon-list:showWhenSingleIcon");
108    
109                            return EVAL_PAGE;
110                    }
111                    catch (Exception e) {
112                            throw new JspException(e);
113                    }
114                    finally {
115                            if (!ServerDetector.isResin()) {
116                                    _endPage = null;
117                                    _showWhenSingleIcon = false;
118                                    _startPage = null;
119                            }
120                    }
121            }
122    
123            @Override
124            public int doStartTag() {
125                    HttpServletRequest request =
126                            (HttpServletRequest)pageContext.getRequest();
127    
128                    request.setAttribute(
129                            "liferay-ui:icon-list:icon-count", new IntegerWrapper());
130                    request.setAttribute(
131                            "liferay-ui:icon-list:showWhenSingleIcon",
132                            String.valueOf(_showWhenSingleIcon));
133    
134                    return EVAL_BODY_BUFFERED;
135            }
136    
137            public void setEndPage(String endPage) {
138                    _endPage = endPage;
139            }
140    
141            public void setShowWhenSingleIcon(boolean showWhenSingleIcon) {
142                    _showWhenSingleIcon = showWhenSingleIcon;
143            }
144    
145            public void setStartPage(String startPage) {
146                    _startPage = startPage;
147            }
148    
149            protected String getEndPage() {
150                    if (Validator.isNull(_endPage)) {
151                            return _END_PAGE;
152                    }
153                    else {
154                            return _endPage;
155                    }
156            }
157    
158            protected String getStartPage() {
159                    if (Validator.isNull(_startPage)) {
160                            return _START_PAGE;
161                    }
162                    else {
163                            return _startPage;
164                    }
165            }
166    
167            private static final String _END_PAGE = "/html/taglib/ui/icon_list/end.jsp";
168    
169            private static final String _START_PAGE =
170                    "/html/taglib/ui/icon_list/start.jsp";
171    
172            private String _endPage;
173            private boolean _showWhenSingleIcon = false;
174            private String _startPage;
175    
176    }