001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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                                    _maxDisplayItems = _DEFAULT_MAX_DISPLAY_ITEMS;
088                                    _message = "actions";
089                                    _showArrow = true;
090                                    _showExpanded = false;
091                                    _showWhenSingleIcon = false;
092                                    _startPage = null;
093                            }
094                    }
095            }
096    
097            @Override
098            public int doStartTag() {
099                    HttpServletRequest request =
100                            (HttpServletRequest)pageContext.getRequest();
101    
102                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
103                            WebKeys.THEME_DISPLAY);
104    
105                    if (_direction == null) {
106                            if (_align.equals("left")) {
107                                    _direction = "right";
108                            }
109                            else {
110                                    _direction = "left";
111                            }
112                    }
113    
114                    if (_icon == null) {
115                            _icon = themeDisplay.getPathThemeImages() + "/common/tool.png";
116                    }
117    
118                    if (Validator.isNull(_id)) {
119                            _id = (String)request.getAttribute(
120                                    "liferay-ui:search-container-row:rowId");
121    
122                            if (Validator.isNull(_id)) {
123                                    _id = PortalUtil.generateRandomKey(
124                                            request, IconMenuTag.class.getName());
125                            }
126    
127                            _id = _id.concat("_menu");
128                    }
129    
130                    request.setAttribute("liferay-ui:icon-menu:id", _id);
131    
132                    PortletDisplay portletDisplay = themeDisplay.getPortletDisplay();
133    
134                    String namespace = portletDisplay.getNamespace();
135    
136                    _id = namespace.concat(_id);
137    
138                    request.setAttribute(
139                            "liferay-ui:icon-menu:icon-count", new IntegerWrapper());
140                    request.setAttribute(
141                            "liferay-ui:icon-menu:showWhenSingleIcon",
142                            String.valueOf(_showWhenSingleIcon));
143    
144                    return EVAL_BODY_BUFFERED;
145            }
146    
147            public void setAlign(String align) {
148                    _align = align;
149            }
150    
151            public void setCssClass(String cssClass) {
152                    _cssClass = cssClass;
153            }
154    
155            public void setDirection(String direction) {
156                    _direction = direction;
157            }
158    
159            public void setDisabled(boolean disabled) {
160                    _disabled = disabled;
161            }
162    
163            public void setEndPage(String endPage) {
164                    _endPage = endPage;
165            }
166    
167            public void setExtended(boolean extended) {
168                    _extended = extended;
169            }
170    
171            public void setIcon(String icon) {
172                    _icon = icon;
173            }
174    
175            public void setId(String id) {
176                    _id = id;
177            }
178    
179            public void setMaxDisplayItems(int maxDisplayItems) {
180                    if (maxDisplayItems <= 0) {
181                            maxDisplayItems = _DEFAULT_MAX_DISPLAY_ITEMS;
182                    }
183    
184                    _maxDisplayItems = maxDisplayItems;
185            }
186    
187            public void setMessage(String message) {
188                    if (message != null) {
189                            _message = message;
190                    }
191            }
192    
193            public void setShowArrow(boolean showArrow) {
194                    _showArrow = showArrow;
195            }
196    
197            public void setShowExpanded(boolean showExpanded) {
198                    _showExpanded = showExpanded;
199            }
200    
201            public void setShowWhenSingleIcon(boolean showWhenSingleIcon) {
202                    _showWhenSingleIcon = showWhenSingleIcon;
203            }
204    
205            public void setStartPage(String startPage) {
206                    _startPage = startPage;
207            }
208    
209            protected String getEndPage() {
210                    if (Validator.isNull(_endPage)) {
211                            return _END_PAGE;
212                    }
213                    else {
214                            return _endPage;
215                    }
216            }
217    
218            protected String getStartPage() {
219                    if (Validator.isNull(_startPage)) {
220                            return _START_PAGE;
221                    }
222                    else {
223                            return _startPage;
224                    }
225            }
226    
227            protected int processEndTag() throws Exception {
228                    HttpServletRequest request =
229                            (HttpServletRequest)pageContext.getRequest();
230    
231                    IntegerWrapper iconCount = (IntegerWrapper)request.getAttribute(
232                            "liferay-ui:icon-menu:icon-count");
233    
234                    request.removeAttribute("liferay-ui:icon-menu:icon-count");
235                    request.removeAttribute("liferay-ui:icon-menu:id");
236    
237                    Boolean singleIcon = (Boolean)request.getAttribute(
238                            "liferay-ui:icon-menu:single-icon");
239    
240                    request.removeAttribute("liferay-ui:icon-menu:single-icon");
241    
242                    JspWriter jspWriter = pageContext.getOut();
243    
244                    if ((iconCount != null) && (iconCount.getValue() >= 1) &&
245                            ((singleIcon == null) || _showWhenSingleIcon)) {
246    
247                            if (!FileAvailabilityUtil.isAvailable(
248                                            pageContext.getServletContext(), getStartPage())) {
249    
250                                    if (_showExpanded) {
251                                            jspWriter.write("<div class=\"lfr-component ");
252                                            jspWriter.write("lfr-menu-list lfr-menu-expanded align-");
253                                            jspWriter.write(_align);
254                                            jspWriter.write(" ");
255                                            jspWriter.print(_cssClass);
256                                            jspWriter.write("\" id=\"");
257                                            jspWriter.write(_id);
258                                            jspWriter.write("\">");
259                                    }
260                                    else {
261                                            jspWriter.write("<span title=\"");
262                                            jspWriter.write(LanguageUtil.get(pageContext, _message));
263                                            jspWriter.write("\"><ul class='lfr-component lfr-actions ");
264                                            jspWriter.write("align-");
265                                            jspWriter.write(_align);
266                                            jspWriter.write(" direction-");
267                                            jspWriter.write(_direction);
268                                            jspWriter.write(" max-display-items-");
269                                            jspWriter.write(String.valueOf(_maxDisplayItems));
270                                            jspWriter.write(" ");
271    
272                                            if (Validator.isNotNull(_cssClass)) {
273                                                    jspWriter.print(_cssClass);
274                                            }
275    
276                                            if (_disabled) {
277                                                    jspWriter.write(" disabled");
278                                            }
279    
280                                            if (_extended) {
281                                                    jspWriter.write(" lfr-extended");
282                                            }
283    
284                                            if (_showArrow) {
285                                                    jspWriter.write(" show-arrow");
286                                            }
287    
288                                            jspWriter.write("\' id=\"");
289                                            jspWriter.write(_id);
290                                            jspWriter.write("\">");
291                                            jspWriter.write("<li class=\"lfr-trigger\"><strong>");
292                                            jspWriter.write(
293                                                    "<a class=\"nobr\" href=\"javascript:;\" id=\"");
294                                            jspWriter.write(_id);
295                                            jspWriter.write("Button\">");
296    
297                                            if (Validator.isNotNull(_icon)) {
298                                                    jspWriter.write("<img alt=\"\" src=\"");
299                                                    jspWriter.write(_icon);
300                                                    jspWriter.write("\" />");
301                                            }
302    
303                                            jspWriter.write("<span class=\"taglib-text\">");
304                                            jspWriter.write(LanguageUtil.get(pageContext, _message));
305                                            jspWriter.write("</span></a></strong>");
306    
307                                            ScriptTag.doTag(
308                                                    null, "liferay-menu",
309                                                    "Liferay.Menu.register('" + _id + "');", bodyContent,
310                                                    pageContext);
311                                    }
312    
313                                    jspWriter.write("<ul>");
314                            }
315                            else {
316                                    PortalIncludeUtil.include(pageContext, getStartPage());
317                            }
318                    }
319    
320                    writeBodyContent(jspWriter);
321    
322                    if ((iconCount != null) && (iconCount.getValue() >= 1) &&
323                            ((singleIcon == null) || _showWhenSingleIcon)) {
324    
325                            if (!FileAvailabilityUtil.isAvailable(
326                                            pageContext.getServletContext(), getEndPage())) {
327    
328                                    jspWriter.write("</ul>");
329    
330                                    if (_showExpanded) {
331                                            jspWriter.write("</div>");
332    
333                                            ScriptTag.doTag(
334                                                    null, "liferay-menu",
335                                                    "Liferay.Menu.handleFocus('#" + _id + "menu');",
336                                                    bodyContent, pageContext);
337                                    }
338                                    else {
339                                            jspWriter.write("</li></ul></span>");
340                                    }
341                            }
342                            else {
343                                    PortalIncludeUtil.include(pageContext, getEndPage());
344                            }
345                    }
346    
347                    request.removeAttribute("liferay-ui:icon-menu:showWhenSingleIcon");
348    
349                    return EVAL_PAGE;
350            }
351    
352            private static final int _DEFAULT_MAX_DISPLAY_ITEMS = GetterUtil.getInteger(
353                    PropsUtil.get(PropsKeys.ICON_MENU_MAX_DISPLAY_ITEMS));
354    
355            private static final String _END_PAGE = "/html/taglib/ui/icon_menu/end.jsp";
356    
357            private static final String _START_PAGE =
358                    "/html/taglib/ui/icon_menu/start.jsp";
359    
360            private String _align = "right";
361            private String _cssClass;
362            private String _direction;
363            private boolean _disabled;
364            private String _endPage;
365            private boolean _extended = true;
366            private String _icon;
367            private String _id;
368            private int _maxDisplayItems = _DEFAULT_MAX_DISPLAY_ITEMS;
369            private String _message = "actions";
370            private boolean _showArrow = true;
371            private boolean _showExpanded;
372            private boolean _showWhenSingleIcon;
373            private String _startPage;
374    
375    }