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