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