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