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.language.LanguageUtil;
018    import com.liferay.portal.kernel.servlet.PortalIncludeUtil;
019    import com.liferay.portal.kernel.servlet.taglib.BaseBodyTagSupport;
020    import com.liferay.portal.kernel.servlet.taglib.FileAvailabilityUtil;
021    import com.liferay.portal.kernel.util.GetterUtil;
022    import com.liferay.portal.kernel.util.IntegerWrapper;
023    import com.liferay.portal.kernel.util.PropsKeys;
024    import com.liferay.portal.kernel.util.PropsUtil;
025    import com.liferay.portal.kernel.util.ServerDetector;
026    import com.liferay.portal.kernel.util.StringPool;
027    import com.liferay.portal.kernel.util.Validator;
028    import com.liferay.portal.kernel.util.WebKeys;
029    import com.liferay.portal.theme.PortletDisplay;
030    import com.liferay.portal.theme.ThemeDisplay;
031    import com.liferay.portal.util.PortalUtil;
032    import com.liferay.taglib.aui.ScriptTag;
033    
034    import javax.servlet.http.HttpServletRequest;
035    import javax.servlet.jsp.JspException;
036    import javax.servlet.jsp.JspWriter;
037    import javax.servlet.jsp.tagext.BodyTag;
038    
039    /**
040     * @author Brian Wing Shun Chan
041     * @author Shuyang Zhou
042     */
043    public class IconMenuTag extends BaseBodyTagSupport implements BodyTag {
044    
045            @Override
046            public int doAfterBody() {
047                    HttpServletRequest request =
048                            (HttpServletRequest)pageContext.getRequest();
049    
050                    IntegerWrapper iconCount = (IntegerWrapper)request.getAttribute(
051                            "liferay-ui:icon-menu:icon-count");
052    
053                    Boolean singleIcon = (Boolean)request.getAttribute(
054                            "liferay-ui:icon-menu:single-icon");
055    
056                    if ((iconCount != null) && (iconCount.getValue() == 1) &&
057                            (singleIcon == null)) {
058    
059                            bodyContent.clearBody();
060    
061                            request.setAttribute(
062                                    "liferay-ui:icon-menu:single-icon", Boolean.TRUE);
063    
064                            return EVAL_BODY_AGAIN;
065                    }
066                    else {
067                            return SKIP_BODY;
068                    }
069            }
070    
071            @Override
072            public int doEndTag() throws JspException {
073                    try {
074                            return processEndTag();
075                    }
076                    catch (Exception e) {
077                            throw new JspException(e);
078                    }
079                    finally {
080                            if (!ServerDetector.isResin()) {
081                                    _align = "right";
082                                    _cssClass = null;
083                                    _direction = null;
084                                    _endPage = null;
085                                    _extended = true;
086                                    _icon = null;
087                                    _id = null;
088                                    _maxDisplayItems = _DEFAULT_MAX_DISPLAY_ITEMS;
089                                    _message = "actions";
090                                    _showArrow = true;
091                                    _showExpanded = false;
092                                    _showWhenSingleIcon = false;
093                                    _startPage = null;
094                            }
095                    }
096            }
097    
098            @Override
099            public int doStartTag() {
100                    HttpServletRequest request =
101                            (HttpServletRequest)pageContext.getRequest();
102    
103                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
104                            WebKeys.THEME_DISPLAY);
105    
106                    if (_direction == null) {
107                            if (_align.equals("left")) {
108                                    _direction = "right";
109                            }
110                            else {
111                                    _direction = "left";
112                            }
113                    }
114    
115                    if (_icon == null) {
116                            _icon = themeDisplay.getPathThemeImages() + "/common/tool.png";
117                    }
118    
119                    if (Validator.isNull(_id)) {
120                            String randomKey = PortalUtil.generateRandomKey(
121                                    request, IconMenuTag.class.getName());
122    
123                            _id = randomKey + StringPool.UNDERLINE + "menu";
124                    }
125                    else {
126                            PortletDisplay portletDisplay = themeDisplay.getPortletDisplay();
127    
128                            String namespace = portletDisplay.getNamespace();
129    
130                            _id = namespace.concat(_id);
131                    }
132    
133                    request.setAttribute(
134                            "liferay-ui:icon-menu:icon-count", new IntegerWrapper());
135                    request.setAttribute(
136                            "liferay-ui:icon-menu:showWhenSingleIcon",
137                            String.valueOf(_showWhenSingleIcon));
138    
139                    return EVAL_BODY_BUFFERED;
140            }
141    
142            public void setAlign(String align) {
143                    _align = align;
144            }
145    
146            public void setCssClass(String cssClass) {
147                    _cssClass = cssClass;
148            }
149    
150            public void setDirection(String direction) {
151                    _direction = direction;
152            }
153    
154            public void setDisabled(boolean disabled) {
155                    _disabled = disabled;
156            }
157    
158            public void setEndPage(String endPage) {
159                    _endPage = endPage;
160            }
161    
162            public void setExtended(boolean extended) {
163                    _extended = extended;
164            }
165    
166            public void setIcon(String icon) {
167                    _icon = icon;
168            }
169    
170            public void setId(String id) {
171                    _id = id;
172            }
173    
174            public void setMaxDisplayItems(int maxDisplayItems) {
175                    if (maxDisplayItems <= 0) {
176                            maxDisplayItems = _DEFAULT_MAX_DISPLAY_ITEMS;
177                    }
178    
179                    _maxDisplayItems = maxDisplayItems;
180            }
181    
182            public void setMessage(String message) {
183                    _message = message;
184            }
185    
186            public void setShowArrow(boolean showArrow) {
187                    _showArrow = showArrow;
188            }
189    
190            public void setShowExpanded(boolean showExpanded) {
191                    _showExpanded = showExpanded;
192            }
193    
194            public void setShowWhenSingleIcon(boolean showWhenSingleIcon) {
195                    _showWhenSingleIcon = showWhenSingleIcon;
196            }
197    
198            public void setStartPage(String startPage) {
199                    _startPage = startPage;
200            }
201    
202            protected String getEndPage() {
203                    if (Validator.isNull(_endPage)) {
204                            return _END_PAGE;
205                    }
206                    else {
207                            return _endPage;
208                    }
209            }
210    
211            protected String getStartPage() {
212                    if (Validator.isNull(_startPage)) {
213                            return _START_PAGE;
214                    }
215                    else {
216                            return _startPage;
217                    }
218            }
219    
220            protected int processEndTag() throws Exception {
221                    HttpServletRequest request =
222                            (HttpServletRequest)pageContext.getRequest();
223    
224                    IntegerWrapper iconCount = (IntegerWrapper)request.getAttribute(
225                            "liferay-ui:icon-menu:icon-count");
226    
227                    request.removeAttribute("liferay-ui:icon-menu:icon-count");
228    
229                    Boolean singleIcon = (Boolean)request.getAttribute(
230                            "liferay-ui:icon-menu:single-icon");
231    
232                    request.removeAttribute("liferay-ui:icon-menu:single-icon");
233    
234                    JspWriter jspWriter = pageContext.getOut();
235    
236                    if ((iconCount != null) && (iconCount.getValue() >= 1) &&
237                            ((singleIcon == null) || _showWhenSingleIcon)) {
238    
239                            if (!FileAvailabilityUtil.isAvailable(
240                                            pageContext.getServletContext(), getStartPage())) {
241    
242                                    if (_showExpanded) {
243                                            jspWriter.write("<div class=\"lfr-component ");
244                                            jspWriter.write("lfr-menu-list lfr-menu-expanded align-");
245                                            jspWriter.write(_align);
246                                            jspWriter.write(" ");
247                                            jspWriter.print(_cssClass);
248                                            jspWriter.write("\" id=\"");
249                                            jspWriter.write(_id);
250                                            jspWriter.write("\">");
251                                    }
252                                    else {
253                                            jspWriter.write("<span title=\"");
254                                            jspWriter.write(LanguageUtil.get(pageContext, _message));
255                                            jspWriter.write("\"><ul class='lfr-component lfr-actions ");
256                                            jspWriter.write("align-");
257                                            jspWriter.write(_align);
258                                            jspWriter.write(" direction-");
259                                            jspWriter.write(_direction);
260                                            jspWriter.write(" max-display-items-");
261                                            jspWriter.write(String.valueOf(_maxDisplayItems));
262                                            jspWriter.write(" ");
263    
264                                            if (Validator.isNotNull(_cssClass)) {
265                                                    jspWriter.print(_cssClass);
266                                            }
267    
268                                            if (_disabled) {
269                                                    jspWriter.write(" disabled");
270                                            }
271    
272                                            if (_extended) {
273                                                    jspWriter.write(" lfr-extended");
274                                            }
275    
276                                            if (_showArrow) {
277                                                    jspWriter.write(" show-arrow");
278                                            }
279    
280                                            jspWriter.write("\' id=\"");
281                                            jspWriter.write(_id);
282                                            jspWriter.write("\">");
283                                            jspWriter.write("<li class=\"lfr-trigger\"><strong>");
284                                            jspWriter.write("<a class=\"nobr\" href=\"javascript:;\">");
285    
286                                            if (Validator.isNotNull(_icon)) {
287                                                    jspWriter.write("<img alt=\"\" src=\"");
288                                                    jspWriter.write(_icon);
289                                                    jspWriter.write("\" />");
290                                            }
291    
292                                            jspWriter.write("<span class=\"taglib-text\">");
293                                            jspWriter.write(LanguageUtil.get(pageContext, _message));
294                                            jspWriter.write("</span></a></strong>");
295    
296                                            ScriptTag.doTag(
297                                                    null, "liferay-menu",
298                                                    "Liferay.Menu.register('" + _id + "');", bodyContent,
299                                                    pageContext);
300                                    }
301    
302                                    jspWriter.write("<ul>");
303                            }
304                            else {
305                                    PortalIncludeUtil.include(pageContext, getStartPage());
306                            }
307                    }
308    
309                    writeBodyContent(jspWriter);
310    
311                    if ((iconCount != null) && (iconCount.getValue() >= 1) &&
312                            ((singleIcon == null) || _showWhenSingleIcon)) {
313    
314                            if (!FileAvailabilityUtil.isAvailable(
315                                            pageContext.getServletContext(), getEndPage())) {
316    
317                                    jspWriter.write("</ul>");
318    
319                                    if (_showExpanded) {
320                                            jspWriter.write("</div>");
321    
322                                            ScriptTag.doTag(
323                                                    null, "liferay-menu",
324                                                    "Liferay.Menu.handleFocus('#" + _id + "menu');",
325                                                    bodyContent, pageContext);
326                                    }
327                                    else {
328                                            jspWriter.write("</li></ul></span>");
329                                    }
330                            }
331                            else {
332                                    PortalIncludeUtil.include(pageContext, getEndPage());
333                            }
334                    }
335    
336                    request.removeAttribute("liferay-ui:icon-menu:showWhenSingleIcon");
337    
338                    return EVAL_PAGE;
339            }
340    
341            private static final int _DEFAULT_MAX_DISPLAY_ITEMS = GetterUtil.getInteger(
342                    PropsUtil.get(PropsKeys.ICON_MENU_MAX_DISPLAY_ITEMS));
343    
344            private static final String _END_PAGE = "/html/taglib/ui/icon_menu/end.jsp";
345    
346            private static final String _START_PAGE =
347                    "/html/taglib/ui/icon_menu/start.jsp";
348    
349            private String _align = "right";
350            private String _cssClass;
351            private String _direction;
352            private boolean _disabled;
353            private String _endPage;
354            private boolean _extended = true;
355            private String _icon;
356            private String _id;
357            private int _maxDisplayItems = _DEFAULT_MAX_DISPLAY_ITEMS;
358            private String _message = "actions";
359            private boolean _showArrow = true;
360            private boolean _showExpanded;
361            private boolean _showWhenSingleIcon;
362            private String _startPage;
363    
364    }