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.portletext;
016    
017    import com.liferay.portal.kernel.servlet.taglib.FileAvailabilityUtil;
018    import com.liferay.portal.kernel.util.HtmlUtil;
019    import com.liferay.portal.kernel.util.StringPool;
020    import com.liferay.portal.kernel.util.Validator;
021    import com.liferay.portal.model.Portlet;
022    import com.liferay.portal.theme.PortletDisplay;
023    import com.liferay.portal.theme.ThemeDisplay;
024    import com.liferay.portal.util.PortalUtil;
025    import com.liferay.taglib.ui.IconTag;
026    
027    import javax.servlet.http.HttpServletRequest;
028    
029    /**
030     * @author Brian Wing Shun Chan
031     * @author Shuyang Zhou
032     */
033    public class IconPortletTag extends IconTag {
034    
035            public void setPortlet(Portlet portlet) {
036                    _portlet = portlet;
037            }
038    
039            @Override
040            protected void cleanUp() {
041                    super.cleanUp();
042    
043                    _portlet = null;
044            }
045    
046            @Override
047            protected String getPage() {
048                    if (FileAvailabilityUtil.isAvailable(servletContext, _PAGE)) {
049                            return _PAGE;
050                    }
051    
052                    ThemeDisplay themeDisplay = (ThemeDisplay)pageContext.getAttribute(
053                            "themeDisplay");
054    
055                    String message = null;
056                    String src = null;
057    
058                    if (_portlet != null) {
059                            message = PortalUtil.getPortletTitle(
060                                    _portlet, pageContext.getServletContext(),
061                                    themeDisplay.getLocale());
062    
063                            if (Validator.isNotNull(_portlet.getIcon())) {
064                                    src = _portlet.getStaticResourcePath().concat(
065                                            _portlet.getIcon());
066                            }
067                            else {
068                                    src = themeDisplay.getPathContext() + "/html/icons/default.png";
069                            }
070                    }
071                    else {
072                            PortletDisplay portletDisplay = themeDisplay.getPortletDisplay();
073    
074                            if (!portletDisplay.isShowPortletIcon()) {
075                                    return null;
076                            }
077    
078                            message = portletDisplay.getTitle();
079                            src = portletDisplay.getURLPortlet();
080                    }
081    
082                    setAlt(StringPool.BLANK);
083                    setMessage(HtmlUtil.escape(message));
084                    setSrc(src);
085    
086                    return super.getPage();
087            }
088    
089            @Override
090            protected void setAttributes(HttpServletRequest request) {
091                    super.setAttributes(request);
092    
093                    request.setAttribute("liferay-portlet:icon_portlet:portlet", _portlet);
094            }
095    
096            private static final String _PAGE =
097                    "/html/taglib/portlet/icon_portlet/page.jsp";
098    
099            private Portlet _portlet;
100    
101    }