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.util.FriendlyURLNormalizerUtil;
018    import com.liferay.portal.kernel.util.JavaConstants;
019    import com.liferay.portal.kernel.util.StringPool;
020    import com.liferay.portal.kernel.util.Validator;
021    import com.liferay.portal.util.PortalUtil;
022    import com.liferay.taglib.util.IncludeTag;
023    
024    import java.util.Map;
025    
026    import javax.portlet.PortletResponse;
027    
028    import javax.servlet.http.HttpServletRequest;
029    
030    /**
031     * @author Brian Wing Shun Chan
032     */
033    public class IconTag extends IncludeTag {
034    
035            @Override
036            public int doStartTag() {
037                    return EVAL_BODY_INCLUDE;
038            }
039    
040            public void setAlt(String alt) {
041                    _alt = alt;
042            }
043    
044            public void setAriaRole(String ariaRole) {
045                    _ariaRole = ariaRole;
046            }
047    
048            public void setCssClass(String cssClass) {
049                    _cssClass = cssClass;
050            }
051    
052            public void setData(Map<String, Object> data) {
053                    _data = data;
054            }
055    
056            public void setId(String id) {
057                    _id = id;
058            }
059    
060            public void setImage(String image) {
061                    _image = image;
062            }
063    
064            public void setImageHover(String imageHover) {
065                    _imageHover = imageHover;
066            }
067    
068            public void setLabel(boolean label) {
069                    _label = label;
070            }
071    
072            public void setLang(String lang) {
073                    _lang = lang;
074            }
075    
076            public void setLinkCssClass(String linkCssClass) {
077                    _linkCssClass = linkCssClass;
078            }
079    
080            public void setLocalizeMessage(boolean localizeMessage) {
081                    _localizeMessage = localizeMessage;
082            }
083    
084            public void setMessage(String message) {
085                    _message = message;
086            }
087    
088            public void setMethod(String method) {
089                    _method = method;
090            }
091    
092            public void setOnClick(String onClick) {
093                    _onClick = onClick;
094            }
095    
096            public void setSrc(String src) {
097                    _src = src;
098            }
099    
100            public void setSrcHover(String srcHover) {
101                    _srcHover = srcHover;
102            }
103    
104            public void setTarget(String target) {
105                    _target = target;
106            }
107    
108            public void setToolTip(boolean toolTip) {
109                    _toolTip = toolTip;
110            }
111    
112            public void setUrl(String url) {
113                    _url = url;
114            }
115    
116            public void setUseDialog(boolean useDialog) {
117                    _useDialog = useDialog;
118            }
119    
120            @Override
121            protected void cleanUp() {
122                    _alt = null;
123                    _ariaRole = null;
124                    _cssClass = null;
125                    _data = null;
126                    _id = null;
127                    _image = null;
128                    _imageHover = null;
129                    _label = false;
130                    _lang = null;
131                    _linkCssClass = null;
132                    _localizeMessage = true;
133                    _message = null;
134                    _method = null;
135                    _onClick = null;
136                    _src = null;
137                    _srcHover = null;
138                    _target = null;
139                    _toolTip = false;
140                    _url = null;
141                    _useDialog = false;
142            }
143    
144            protected String getImage() {
145                    return _image;
146            }
147    
148            protected String getMessage() {
149                    return _message;
150            }
151    
152            @Override
153            protected String getPage() {
154                    return _PAGE;
155            }
156    
157            protected String getUrl() {
158                    return _url;
159            }
160    
161            @Override
162            protected boolean isCleanUpSetAttributes() {
163                    return _CLEAN_UP_SET_ATTRIBUTES;
164            }
165    
166            @Override
167            protected void setAttributes(HttpServletRequest request) {
168                    String id = _id;
169    
170                    if (Validator.isNull(id)) {
171                            id = (String)request.getAttribute("liferay-ui:icon-menu:id");
172    
173                            String message = _message;
174    
175                            if (Validator.isNull(message)) {
176                                    message = _image;
177                            }
178    
179                            if (Validator.isNotNull(id) && Validator.isNotNull(message)) {
180                                    id = id.concat(StringPool.UNDERLINE).concat(
181                                            FriendlyURLNormalizerUtil.normalize(message));
182    
183                                    PortletResponse portletResponse =
184                                            (PortletResponse)request.getAttribute(
185                                                    JavaConstants.JAVAX_PORTLET_RESPONSE);
186    
187                                    String namespace = StringPool.BLANK;
188    
189                                    if (portletResponse != null) {
190                                            namespace = portletResponse.getNamespace();
191                                    }
192    
193                                    id = PortalUtil.getUniqueElementId(
194                                            getOriginalServletRequest(), namespace, id);
195                            }
196                            else {
197                                    id = PortalUtil.generateRandomKey(
198                                            request, IconTag.class.getName());
199                            }
200                    }
201    
202                    request.setAttribute("liferay-ui:icon:alt", _alt);
203                    request.setAttribute("liferay-ui:icon:ariaRole", _ariaRole);
204                    request.setAttribute("liferay-ui:icon:cssClass", _cssClass);
205                    request.setAttribute("liferay-ui:icon:data", _data);
206                    request.setAttribute("liferay-ui:icon:id", id);
207                    request.setAttribute("liferay-ui:icon:image", _image);
208                    request.setAttribute("liferay-ui:icon:imageHover", _imageHover);
209                    request.setAttribute("liferay-ui:icon:label", String.valueOf(_label));
210                    request.setAttribute("liferay-ui:icon:lang", _lang);
211                    request.setAttribute("liferay-ui:icon:linkCssClass", _linkCssClass);
212                    request.setAttribute(
213                            "liferay-ui:icon:localizeMessage",
214                            String.valueOf(_localizeMessage));
215                    request.setAttribute("liferay-ui:icon:message", _message);
216                    request.setAttribute("liferay-ui:icon:method", _method);
217                    request.setAttribute("liferay-ui:icon:onClick", _onClick);
218                    request.setAttribute("liferay-ui:icon:src", _src);
219                    request.setAttribute("liferay-ui:icon:srcHover", _srcHover);
220                    request.setAttribute("liferay-ui:icon:target", _target);
221                    request.setAttribute(
222                            "liferay-ui:icon:toolTip", String.valueOf(_toolTip));
223                    request.setAttribute("liferay-ui:icon:url", _url);
224                    request.setAttribute(
225                            "liferay-ui:icon:useDialog", String.valueOf(_useDialog));
226            }
227    
228            private static final boolean _CLEAN_UP_SET_ATTRIBUTES = true;
229    
230            private static final String _PAGE = "/html/taglib/ui/icon/page.jsp";
231    
232            private String _alt;
233            private String _ariaRole;
234            private String _cssClass;
235            private Map<String, Object> _data;
236            private String _id;
237            private String _image;
238            private String _imageHover;
239            private boolean _label;
240            private String _lang;
241            private String _linkCssClass;
242            private boolean _localizeMessage = true;
243            private String _message;
244            private String _method;
245            private String _onClick;
246            private String _src;
247            private String _srcHover;
248            private String _target = "_self";
249            private boolean _toolTip;
250            private String _url;
251            private boolean _useDialog = false;
252    
253    }