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.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                            ScriptData scriptData = (ScriptData)request.getAttribute(
062                                    WebKeys.AUI_SCRIPT_DATA);
063    
064                            if (scriptData != null) {
065                                    scriptData.reset();
066                            }
067    
068                            request.setAttribute(
069                                    "liferay-ui:icon-menu:single-icon", Boolean.TRUE);
070    
071                            return EVAL_BODY_AGAIN;
072                    }
073                    else {
074                            return SKIP_BODY;
075                    }
076            }
077    
078            @Override
079            public int doEndTag() throws JspException {
080                    try {
081                            return processEndTag();
082                    }
083                    catch (Exception e) {
084                            throw new JspException(e);
085                    }
086                    finally {
087                            if (!ServerDetector.isResin()) {
088                                    _align = "right";
089                                    _cssClass = null;
090                                    _direction = null;
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                            if (_align.equals("left")) {
123                                    _direction = "right";
124                            }
125                            else {
126                                    _direction = "left";
127                            }
128                    }
129    
130                    if (_icon == null) {
131                            _icon = themeDisplay.getPathThemeImages() + "/common/tool.png";
132                    }
133    
134                    if (Validator.isNull(_id)) {
135                            _id = (String)request.getAttribute(
136                                    "liferay-ui:search-container-row:rowId");
137    
138                            if (Validator.isNull(_id)) {
139                                    _id = PortalUtil.generateRandomKey(
140                                            request, IconMenuTag.class.getName());
141                            }
142    
143                            _id = _id.concat("_menu");
144                    }
145    
146                    request.setAttribute("liferay-ui:icon-menu:id", _id);
147    
148                    PortletDisplay portletDisplay = themeDisplay.getPortletDisplay();
149    
150                    String namespace = portletDisplay.getNamespace();
151    
152                    _id = namespace.concat(_id);
153    
154                    request.setAttribute(
155                            "liferay-ui:icon-menu:icon-count", new IntegerWrapper());
156                    request.setAttribute(
157                            "liferay-ui:icon-menu:showWhenSingleIcon",
158                            String.valueOf(_showWhenSingleIcon));
159    
160                    return EVAL_BODY_BUFFERED;
161            }
162    
163            public void setAlign(String align) {
164                    _align = align;
165            }
166    
167            public void setCssClass(String cssClass) {
168                    _cssClass = cssClass;
169            }
170    
171            public void setDirection(String direction) {
172                    _direction = direction;
173            }
174    
175            public void setDisabled(boolean disabled) {
176                    _disabled = disabled;
177            }
178    
179            public void setEndPage(String endPage) {
180                    _endPage = endPage;
181            }
182    
183            public void setExtended(boolean extended) {
184                    _extended = extended;
185            }
186    
187            public void setIcon(String icon) {
188                    _icon = icon;
189            }
190    
191            public void setId(String id) {
192                    _id = id;
193            }
194    
195            public void setLocalizeMessage(boolean localizeMessage) {
196                    _localizeMessage = localizeMessage;
197            }
198    
199            public void setMaxDisplayItems(int maxDisplayItems) {
200                    if (maxDisplayItems <= 0) {
201                            maxDisplayItems = _DEFAULT_MAX_DISPLAY_ITEMS;
202                    }
203    
204                    _maxDisplayItems = maxDisplayItems;
205            }
206    
207            public void setMessage(String message) {
208                    if (message != null) {
209                            _message = message;
210                    }
211            }
212    
213            public void setShowArrow(boolean showArrow) {
214                    _showArrow = showArrow;
215            }
216    
217            public void setShowExpanded(boolean showExpanded) {
218                    _showExpanded = showExpanded;
219            }
220    
221            public void setShowWhenSingleIcon(boolean showWhenSingleIcon) {
222                    _showWhenSingleIcon = showWhenSingleIcon;
223            }
224    
225            public void setStartPage(String startPage) {
226                    _startPage = startPage;
227            }
228    
229            protected String getEndPage() {
230                    if (Validator.isNull(_endPage)) {
231                            return _END_PAGE;
232                    }
233                    else {
234                            return _endPage;
235                    }
236            }
237    
238            protected String getStartPage() {
239                    if (Validator.isNull(_startPage)) {
240                            return _START_PAGE;
241                    }
242                    else {
243                            return _startPage;
244                    }
245            }
246    
247            protected int processEndTag() throws Exception {
248                    HttpServletRequest request =
249                            (HttpServletRequest)pageContext.getRequest();
250    
251                    IntegerWrapper iconCount = (IntegerWrapper)request.getAttribute(
252                            "liferay-ui:icon-menu:icon-count");
253    
254                    request.removeAttribute("liferay-ui:icon-menu:icon-count");
255                    request.removeAttribute("liferay-ui:icon-menu:id");
256    
257                    Boolean singleIcon = (Boolean)request.getAttribute(
258                            "liferay-ui:icon-menu:single-icon");
259    
260                    request.removeAttribute("liferay-ui:icon-menu:single-icon");
261    
262                    JspWriter jspWriter = pageContext.getOut();
263    
264                    if ((iconCount != null) && (iconCount.getValue() >= 1) &&
265                            ((singleIcon == null) || _showWhenSingleIcon)) {
266    
267                            if (!FileAvailabilityUtil.isAvailable(
268                                            pageContext.getServletContext(), getStartPage())) {
269    
270                                    if (_showExpanded) {
271                                            jspWriter.write("<div class=\"lfr-component ");
272                                            jspWriter.write("lfr-menu-list lfr-menu-expanded align-");
273                                            jspWriter.write(_align);
274                                            jspWriter.write(" ");
275                                            jspWriter.print(_cssClass);
276                                            jspWriter.write("\" id=\"");
277                                            jspWriter.write(_id);
278                                            jspWriter.write("\">");
279                                    }
280                                    else {
281                                            String message = _message;
282    
283                                            if (_localizeMessage) {
284                                                    message = LanguageUtil.get(pageContext, _message);
285                                            }
286    
287                                            jspWriter.write("<span title=\"");
288                                            jspWriter.write(message);
289                                            jspWriter.write("\"><ul class='lfr-component lfr-actions ");
290                                            jspWriter.write("align-");
291                                            jspWriter.write(_align);
292                                            jspWriter.write(" direction-");
293                                            jspWriter.write(_direction);
294                                            jspWriter.write(" max-display-items-");
295                                            jspWriter.write(String.valueOf(_maxDisplayItems));
296                                            jspWriter.write(" ");
297    
298                                            if (Validator.isNotNull(_cssClass)) {
299                                                    jspWriter.print(_cssClass);
300                                            }
301    
302                                            if (_disabled) {
303                                                    jspWriter.write(" disabled");
304                                            }
305    
306                                            if (_extended) {
307                                                    jspWriter.write(" lfr-extended");
308                                            }
309    
310                                            if (_showArrow) {
311                                                    jspWriter.write(" show-arrow");
312                                            }
313    
314                                            jspWriter.write("\' id=\"");
315                                            jspWriter.write(_id);
316                                            jspWriter.write("\">");
317                                            jspWriter.write("<li class=\"lfr-trigger\"><strong>");
318                                            jspWriter.write(
319                                                    "<a class=\"nobr\" href=\"javascript:;\" id=\"");
320                                            jspWriter.write(_id);
321                                            jspWriter.write("Button\">");
322    
323                                            if (Validator.isNotNull(_icon)) {
324                                                    jspWriter.write("<img alt=\"\" src=\"");
325                                                    jspWriter.write(_icon);
326                                                    jspWriter.write("\" />");
327                                            }
328    
329                                            jspWriter.write("<span class=\"taglib-text\">");
330                                            jspWriter.write(message);
331                                            jspWriter.write("</span></a></strong>");
332    
333                                            ScriptTag.doTag(
334                                                    null, "liferay-menu",
335                                                    "Liferay.Menu.register('" + _id + "');", bodyContent,
336                                                    pageContext);
337                                    }
338    
339                                    jspWriter.write("<ul>");
340                            }
341                            else {
342                                    PortalIncludeUtil.include(pageContext, getStartPage());
343                            }
344                    }
345    
346                    writeBodyContent(jspWriter);
347    
348                    if ((iconCount != null) && (iconCount.getValue() >= 1) &&
349                            ((singleIcon == null) || _showWhenSingleIcon)) {
350    
351                            if (!FileAvailabilityUtil.isAvailable(
352                                            pageContext.getServletContext(), getEndPage())) {
353    
354                                    jspWriter.write("</ul>");
355    
356                                    if (_showExpanded) {
357                                            jspWriter.write("</div>");
358    
359                                            ScriptTag.doTag(
360                                                    null, "liferay-menu",
361                                                    "Liferay.Menu.handleFocus('#" + _id + "menu');",
362                                                    bodyContent, pageContext);
363                                    }
364                                    else {
365                                            jspWriter.write("</li></ul></span>");
366                                    }
367                            }
368                            else {
369                                    PortalIncludeUtil.include(pageContext, getEndPage());
370                            }
371                    }
372    
373                    request.removeAttribute("liferay-ui:icon-menu:showWhenSingleIcon");
374    
375                    return EVAL_PAGE;
376            }
377    
378            private static final int _DEFAULT_MAX_DISPLAY_ITEMS = GetterUtil.getInteger(
379                    PropsUtil.get(PropsKeys.ICON_MENU_MAX_DISPLAY_ITEMS));
380    
381            private static final String _END_PAGE = "/html/taglib/ui/icon_menu/end.jsp";
382    
383            private static final String _START_PAGE =
384                    "/html/taglib/ui/icon_menu/start.jsp";
385    
386            private String _align = "right";
387            private String _cssClass;
388            private String _direction;
389            private boolean _disabled;
390            private String _endPage;
391            private boolean _extended = true;
392            private String _icon;
393            private String _id;
394            private boolean _localizeMessage = true;
395            private int _maxDisplayItems = _DEFAULT_MAX_DISPLAY_ITEMS;
396            private String _message = "actions";
397            private boolean _showArrow = true;
398            private boolean _showExpanded;
399            private boolean _showWhenSingleIcon;
400            private String _startPage;
401    
402    }