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                                    _select = false;
099                                    _showArrow = true;
100                                    _showExpanded = false;
101                                    _showWhenSingleIcon = false;
102                                    _startPage = null;
103                            }
104                    }
105            }
106    
107            @Override
108            public int doStartTag() {
109                    HttpServletRequest request =
110                            (HttpServletRequest)pageContext.getRequest();
111    
112                    ScriptData scriptData = (ScriptData)request.getAttribute(
113                            WebKeys.AUI_SCRIPT_DATA);
114    
115                    if (scriptData != null) {
116                            scriptData.mark();
117                    }
118    
119                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
120                            WebKeys.THEME_DISPLAY);
121    
122                    if (_direction == null) {
123                            _direction = "left";
124                    }
125    
126                    if (_icon == null) {
127                            _icon = themeDisplay.getPathThemeImages() + "/common/tool.png";
128                    }
129    
130                    if (Validator.isNull(_id)) {
131                            _id = (String)request.getAttribute(
132                                    "liferay-ui:search-container-row:rowId");
133    
134                            if (Validator.isNull(_id)) {
135                                    _id = PortalUtil.generateRandomKey(
136                                            request, IconMenuTag.class.getName());
137                            }
138    
139                            _id = _id.concat("_menu");
140                    }
141    
142                    request.setAttribute("liferay-ui:icon-menu:id", _id);
143    
144                    PortletDisplay portletDisplay = themeDisplay.getPortletDisplay();
145    
146                    String namespace = portletDisplay.getNamespace();
147    
148                    _id = namespace.concat(_id);
149    
150                    request.setAttribute(
151                            "liferay-ui:icon-menu:icon-count", new IntegerWrapper());
152                    request.setAttribute(
153                            "liferay-ui:icon-menu:showWhenSingleIcon",
154                            String.valueOf(_showWhenSingleIcon));
155    
156                    return EVAL_BODY_BUFFERED;
157            }
158    
159            public void setCssClass(String cssClass) {
160                    _cssClass = cssClass;
161            }
162    
163            public void setDirection(String direction) {
164                    _direction = direction;
165            }
166    
167            public void setDisabled(boolean disabled) {
168                    _disabled = disabled;
169            }
170    
171            public void setEndPage(String endPage) {
172                    _endPage = endPage;
173            }
174    
175            public void setExtended(boolean extended) {
176                    _extended = extended;
177            }
178    
179            public void setIcon(String icon) {
180                    _icon = icon;
181            }
182    
183            public void setId(String id) {
184                    _id = id;
185            }
186    
187            public void setLocalizeMessage(boolean localizeMessage) {
188                    _localizeMessage = localizeMessage;
189            }
190    
191            public void setMaxDisplayItems(int maxDisplayItems) {
192                    if (maxDisplayItems <= 0) {
193                            maxDisplayItems = _DEFAULT_MAX_DISPLAY_ITEMS;
194                    }
195    
196                    _maxDisplayItems = maxDisplayItems;
197            }
198    
199            public void setMessage(String message) {
200                    if (message != null) {
201                            _message = message;
202                    }
203            }
204    
205            public void setSelect(boolean select) {
206                    _select = select;
207            }
208    
209            public void setShowArrow(boolean showArrow) {
210                    _showArrow = showArrow;
211            }
212    
213            public void setShowExpanded(boolean showExpanded) {
214                    _showExpanded = showExpanded;
215            }
216    
217            public void setShowWhenSingleIcon(boolean showWhenSingleIcon) {
218                    _showWhenSingleIcon = showWhenSingleIcon;
219            }
220    
221            public void setStartPage(String startPage) {
222                    _startPage = startPage;
223            }
224    
225            protected String getEndPage() {
226                    if (Validator.isNull(_endPage)) {
227                            return _END_PAGE;
228                    }
229                    else {
230                            return _endPage;
231                    }
232            }
233    
234            protected String getStartPage() {
235                    if (Validator.isNull(_startPage)) {
236                            return _START_PAGE;
237                    }
238                    else {
239                            return _startPage;
240                    }
241            }
242    
243            protected int processEndTag() throws Exception {
244                    HttpServletRequest request =
245                            (HttpServletRequest)pageContext.getRequest();
246    
247                    IntegerWrapper iconCount = (IntegerWrapper)request.getAttribute(
248                            "liferay-ui:icon-menu:icon-count");
249    
250                    request.removeAttribute("liferay-ui:icon-menu:icon-count");
251                    request.removeAttribute("liferay-ui:icon-menu:id");
252    
253                    Boolean singleIcon = (Boolean)request.getAttribute(
254                            "liferay-ui:icon-menu:single-icon");
255    
256                    request.removeAttribute("liferay-ui:icon-menu:single-icon");
257    
258                    JspWriter jspWriter = pageContext.getOut();
259    
260                    if ((iconCount != null) && (iconCount.getValue() >= 1) &&
261                            ((singleIcon == null) || _showWhenSingleIcon)) {
262    
263                            if (!FileAvailabilityUtil.isAvailable(
264                                            pageContext.getServletContext(), getStartPage())) {
265    
266                                    if (_showExpanded) {
267                                            jspWriter.write("<ul class=\"lfr-menu-expanded ");
268                                            jspWriter.write("lfr-menu-list");
269    
270                                            if (Validator.isNotNull(_cssClass)) {
271                                                    jspWriter.write(StringPool.SPACE);
272                                                    jspWriter.print(_cssClass);
273                                            }
274    
275                                            jspWriter.write("\" id=\"");
276                                            jspWriter.write(_id);
277                                            jspWriter.write("\">");
278                                    }
279                                    else {
280                                            jspWriter.write("<div class=\"btn-group lfr-icon-menu");
281    
282                                            if (Validator.isNotNull(_cssClass)) {
283                                                    jspWriter.write(StringPool.SPACE);
284                                                    jspWriter.print(_cssClass);
285                                            }
286    
287                                            if (_direction.equals("up")) {
288                                                    jspWriter.write(" dropup");
289                                            }
290    
291                                            jspWriter.write("\"><a class=\"dropdown-toggle direction-");
292                                            jspWriter.write(_direction);
293                                            jspWriter.write(" max-display-items-");
294                                            jspWriter.write(String.valueOf(_maxDisplayItems));
295    
296                                            if (_disabled) {
297                                                    jspWriter.write(" disabled");
298                                            }
299    
300                                            if (_extended) {
301                                                    jspWriter.write(" btn");
302                                            }
303    
304                                            if (_select) {
305                                                    jspWriter.write(" select");
306                                            }
307    
308                                            String message = _message;
309    
310                                            if (_localizeMessage) {
311                                                    message = LanguageUtil.get(pageContext, _message);
312                                            }
313    
314                                            jspWriter.write("\" href=\"javascript:;\" id=\"");
315                                            jspWriter.write(_id);
316                                            jspWriter.write("\" title=\"");
317                                            jspWriter.write(message);
318                                            jspWriter.write("\">");
319    
320                                            if (_showArrow && _direction.equals("left")) {
321                                                    jspWriter.write("<i class=\"caret\"></i> ");
322                                            }
323    
324                                            boolean auiImage = false;
325    
326                                            if (Validator.isNotNull(_icon)) {
327                                                    auiImage = _icon.startsWith(_AUI_PATH);
328    
329                                                    if (auiImage) {
330                                                            jspWriter.write(" <i class=\"icon-");
331                                                            jspWriter.write(
332                                                                    _icon.substring(_AUI_PATH.length()));
333                                                            jspWriter.write("\"></i>");
334                                                    }
335                                                    else {
336                                                            jspWriter.write("<img alt=\"\" src=\"");
337                                                            jspWriter.write(_icon);
338                                                            jspWriter.write("\" /> ");
339                                                    }
340                                            }
341    
342                                            if (Validator.isNotNull(message)) {
343                                                    jspWriter.write("<span class=\"lfr-icon-menu-text\">");
344                                                    jspWriter.write(message);
345                                                    jspWriter.write("</span>");
346                                            }
347    
348                                            if (_showArrow && !_direction.equals("left")) {
349                                                    jspWriter.write(" <i class=\"caret\"></i>");
350                                            }
351    
352                                            jspWriter.write("</a>");
353    
354                                            ScriptTag.doTag(
355                                                    null, "liferay-menu",
356                                                    "Liferay.Menu.register('" + _id + "');", bodyContent,
357                                                    pageContext);
358    
359                                            jspWriter.write("<ul class=\"dropdown-menu lfr-menu-list");
360                                            jspWriter.write(" direction-");
361                                            jspWriter.write(_direction);
362                                            jspWriter.write("\">");
363                                    }
364                            }
365                            else {
366                                    PortalIncludeUtil.include(pageContext, getStartPage());
367                            }
368                    }
369    
370                    writeBodyContent(jspWriter);
371    
372                    if ((iconCount != null) && (iconCount.getValue() >= 1) &&
373                            ((singleIcon == null) || _showWhenSingleIcon)) {
374    
375                            if (!FileAvailabilityUtil.isAvailable(
376                                            pageContext.getServletContext(), getEndPage())) {
377    
378                                    jspWriter.write("</ul>");
379    
380                                    if (_showExpanded) {
381                                            ScriptTag.doTag(
382                                                    null, "liferay-menu",
383                                                    "Liferay.Menu.handleFocus('#" + _id + "menu');",
384                                                    bodyContent, pageContext);
385                                    }
386                                    else {
387                                            jspWriter.write("</div>");
388                                    }
389                            }
390                            else {
391                                    PortalIncludeUtil.include(pageContext, getEndPage());
392                            }
393                    }
394    
395                    request.removeAttribute("liferay-ui:icon-menu:showWhenSingleIcon");
396    
397                    return EVAL_PAGE;
398            }
399    
400            private static final String _AUI_PATH = "../aui/";
401    
402            private static final int _DEFAULT_MAX_DISPLAY_ITEMS = GetterUtil.getInteger(
403                    PropsUtil.get(PropsKeys.ICON_MENU_MAX_DISPLAY_ITEMS));
404    
405            private static final String _END_PAGE = "/html/taglib/ui/icon_menu/end.jsp";
406    
407            private static final String _START_PAGE =
408                    "/html/taglib/ui/icon_menu/start.jsp";
409    
410            private String _cssClass;
411            private String _direction = "left";
412            private boolean _disabled;
413            private String _endPage;
414            private boolean _extended = true;
415            private String _icon;
416            private String _id;
417            private boolean _localizeMessage = true;
418            private int _maxDisplayItems = _DEFAULT_MAX_DISPLAY_ITEMS;
419            private String _message = "actions";
420            private boolean _select;
421            private boolean _showArrow = true;
422            private boolean _showExpanded;
423            private boolean _showWhenSingleIcon;
424            private String _startPage;
425    
426    }