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 setCssClass(String cssClass) {
045                    _cssClass = cssClass;
046            }
047    
048            public void setData(Map<String, Object> data) {
049                    _data = data;
050            }
051    
052            public void setId(String id) {
053                    _id = id;
054            }
055    
056            public void setImage(String image) {
057                    _image = image;
058            }
059    
060            public void setImageHover(String imageHover) {
061                    _imageHover = imageHover;
062            }
063    
064            public void setLabel(boolean label) {
065                    _label = label;
066            }
067    
068            public void setLang(String lang) {
069                    _lang = lang;
070            }
071    
072            public void setLocalizeMessage(boolean localizeMessage) {
073                    _localizeMessage = localizeMessage;
074            }
075    
076            public void setMessage(String message) {
077                    _message = message;
078            }
079    
080            public void setMethod(String method) {
081                    _method = method;
082            }
083    
084            public void setOnClick(String onClick) {
085                    _onClick = onClick;
086            }
087    
088            public void setSrc(String src) {
089                    _src = src;
090            }
091    
092            public void setSrcHover(String srcHover) {
093                    _srcHover = srcHover;
094            }
095    
096            public void setTarget(String target) {
097                    _target = target;
098            }
099    
100            public void setToolTip(boolean toolTip) {
101                    _toolTip = toolTip;
102            }
103    
104            public void setUrl(String url) {
105                    _url = url;
106            }
107    
108            @Override
109            protected void cleanUp() {
110                    _alt = null;
111                    _cssClass = null;
112                    _data = null;
113                    _id = null;
114                    _image = null;
115                    _imageHover = null;
116                    _label = false;
117                    _lang = null;
118                    _localizeMessage = true;
119                    _message = null;
120                    _method = null;
121                    _onClick = null;
122                    _src = null;
123                    _srcHover = null;
124                    _target = null;
125                    _toolTip = false;
126                    _url = null;
127            }
128    
129            protected String getMessage() {
130                    return _message;
131            }
132    
133            @Override
134            protected String getPage() {
135                    return _PAGE;
136            }
137    
138            protected String getUrl() {
139                    return _url;
140            }
141    
142            @Override
143            protected boolean isCleanUpSetAttributes() {
144                    return _CLEAN_UP_SET_ATTRIBUTES;
145            }
146    
147            @Override
148            protected void setAttributes(HttpServletRequest request) {
149                    String id = _id;
150    
151                    if (Validator.isNull(id)) {
152                            id = (String)request.getAttribute("liferay-ui:icon-menu:id");
153    
154                            String message = _message;
155    
156                            if (Validator.isNull(message)) {
157                                    message = _image;
158                            }
159    
160                            if (Validator.isNotNull(id) && Validator.isNotNull(message)) {
161                                    id = id.concat(StringPool.UNDERLINE).concat(
162                                            FriendlyURLNormalizerUtil.normalize(message));
163    
164                                    PortletResponse portletResponse =
165                                            (PortletResponse)request.getAttribute(
166                                                    JavaConstants.JAVAX_PORTLET_RESPONSE);
167    
168                                    id = PortalUtil.getUniqueElementId(
169                                            getOriginalServletRequest(), portletResponse.getNamespace(),
170                                            id);
171                            }
172                            else {
173                                    id = PortalUtil.generateRandomKey(
174                                            request, IconTag.class.getName());
175                            }
176                    }
177    
178                    request.setAttribute("liferay-ui:icon:alt", _alt);
179                    request.setAttribute("liferay-ui:icon:cssClass", _cssClass);
180                    request.setAttribute("liferay-ui:icon:data", _data);
181                    request.setAttribute("liferay-ui:icon:id", id);
182                    request.setAttribute("liferay-ui:icon:image", _image);
183                    request.setAttribute("liferay-ui:icon:imageHover", _imageHover);
184                    request.setAttribute("liferay-ui:icon:label", String.valueOf(_label));
185                    request.setAttribute("liferay-ui:icon:lang", _lang);
186                    request.setAttribute(
187                            "liferay-ui:icon:localizeMessage",
188                            String.valueOf(_localizeMessage));
189                    request.setAttribute("liferay-ui:icon:message", _message);
190                    request.setAttribute("liferay-ui:icon:method", _method);
191                    request.setAttribute("liferay-ui:icon:onClick", _onClick);
192                    request.setAttribute("liferay-ui:icon:src", _src);
193                    request.setAttribute("liferay-ui:icon:srcHover", _srcHover);
194                    request.setAttribute("liferay-ui:icon:target", _target);
195                    request.setAttribute(
196                            "liferay-ui:icon:toolTip", String.valueOf(_toolTip));
197                    request.setAttribute("liferay-ui:icon:url", _url);
198            }
199    
200            private static final boolean _CLEAN_UP_SET_ATTRIBUTES = true;
201    
202            private static final String _PAGE = "/html/taglib/ui/icon/page.jsp";
203    
204            private String _alt;
205            private String _cssClass;
206            private Map<String, Object> _data;
207            private String _id;
208            private String _image;
209            private String _imageHover;
210            private boolean _label;
211            private String _lang;
212            private boolean _localizeMessage = true;
213            private String _message;
214            private String _method;
215            private String _onClick;
216            private String _src;
217            private String _srcHover;
218            private String _target = "_self";
219            private boolean _toolTip;
220            private String _url;
221    
222    }