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