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