001    /**
002     * Copyright (c) 2000-2010 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.servlet.PortalIncludeUtil;
018    import com.liferay.portal.kernel.servlet.taglib.BaseBodyTagSupport;
019    import com.liferay.portal.kernel.util.IntegerWrapper;
020    import com.liferay.portal.kernel.util.ServerDetector;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.kernel.util.Validator;
023    import com.liferay.portal.kernel.util.WebKeys;
024    import com.liferay.portal.theme.ThemeDisplay;
025    import com.liferay.portal.util.PortalUtil;
026    
027    import javax.servlet.http.HttpServletRequest;
028    import javax.servlet.jsp.JspException;
029    
030    /**
031     * @author Brian Wing Shun Chan
032     */
033    public class IconMenuTag extends BaseBodyTagSupport {
034    
035            public int doAfterBody() {
036                    HttpServletRequest request =
037                            (HttpServletRequest)pageContext.getRequest();
038    
039                    IntegerWrapper iconCount = (IntegerWrapper)request.getAttribute(
040                            "liferay-ui:icon-menu:icon-count");
041    
042                    Boolean singleIcon = (Boolean)request.getAttribute(
043                            "liferay-ui:icon-menu:single-icon");
044    
045                    if ((iconCount != null) && (iconCount.getValue() == 1) &&
046                            (singleIcon == null)) {
047    
048                            bodyContent.clearBody();
049    
050                            request.setAttribute(
051                                    "liferay-ui:icon-menu:single-icon", Boolean.TRUE);
052    
053                            return EVAL_BODY_AGAIN;
054                    }
055                    else {
056                            return SKIP_BODY;
057                    }
058            }
059    
060            public int doEndTag() throws JspException {
061                    try {
062                            HttpServletRequest request =
063                                    (HttpServletRequest)pageContext.getRequest();
064    
065                            IntegerWrapper iconCount = (IntegerWrapper)request.getAttribute(
066                                    "liferay-ui:icon-menu:icon-count");
067    
068                            request.removeAttribute("liferay-ui:icon-menu:icon-count");
069    
070                            Boolean singleIcon = (Boolean)request.getAttribute(
071                                    "liferay-ui:icon-menu:single-icon");
072    
073                            request.removeAttribute("liferay-ui:icon-menu:single-icon");
074    
075                            if ((iconCount != null) && (iconCount.getValue() >= 1) &&
076                                    ((singleIcon == null) || _showWhenSingleIcon)) {
077    
078                                    PortalIncludeUtil.include(pageContext, getStartPage());
079                            }
080    
081                            writeBodyContent(pageContext.getOut());
082    
083                            if ((iconCount != null) && (iconCount.getValue() >= 1) &&
084                                    ((singleIcon == null) || _showWhenSingleIcon)) {
085    
086                                    PortalIncludeUtil.include(pageContext, getEndPage());
087                            }
088    
089                            request.removeAttribute("liferay-ui:icon-menu:align");
090                            request.removeAttribute("liferay-ui:icon-menu:cssClass");
091                            request.removeAttribute("liferay-ui:icon-menu:icon");
092                            request.removeAttribute("liferay-ui:icon-menu:id");
093                            request.removeAttribute("liferay-ui:icon-menu:message");
094                            request.removeAttribute("liferay-ui:icon-menu:showArrow");
095                            request.removeAttribute("liferay-ui:icon-menu:showExpanded");
096                            request.removeAttribute("liferay-ui:icon-menu:showWhenSingleIcon");
097    
098                            return EVAL_PAGE;
099                    }
100                    catch (Exception e) {
101                            throw new JspException(e);
102                    }
103                    finally {
104                            if (!ServerDetector.isResin()) {
105                                    _align = "right";
106                                    _cssClass = null;
107                                    _endPage = null;
108                                    _icon = null;
109                                    _id = null;
110                                    _message = "actions";
111                                    _showArrow = true;
112                                    _showExpanded = false;
113                                    _showWhenSingleIcon = false;
114                                    _startPage = null;
115                            }
116                    }
117            }
118    
119            public int doStartTag() {
120                    HttpServletRequest request =
121                            (HttpServletRequest)pageContext.getRequest();
122    
123                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
124                            WebKeys.THEME_DISPLAY);
125    
126                    String icon = _icon;
127    
128                    if (icon == null) {
129                            icon =  themeDisplay.getPathThemeImages() + "/common/tool.png";
130                    }
131    
132                    String id = _id;
133    
134                    if (Validator.isNull(id)) {
135                            String randomKey = PortalUtil.generateRandomKey(
136                                    request, IconMenuTag.class.getName());
137    
138                            id = randomKey + StringPool.UNDERLINE;
139                    }
140    
141                    request.setAttribute("liferay-ui:icon-menu:align", _align);
142                    request.setAttribute("liferay-ui:icon-menu:cssClass", _cssClass);
143                    request.setAttribute(
144                            "liferay-ui:icon-menu:icon-count", new IntegerWrapper());
145                    request.setAttribute("liferay-ui:icon-menu:icon", icon);
146                    request.setAttribute("liferay-ui:icon-menu:id", id);
147                    request.setAttribute("liferay-ui:icon-menu:message", _message);
148                    request.setAttribute(
149                            "liferay-ui:icon-menu:showArrow",String.valueOf(_showArrow));
150                    request.setAttribute(
151                            "liferay-ui:icon-menu:showExpanded",String.valueOf(_showExpanded));
152                    request.setAttribute(
153                            "liferay-ui:icon-menu:showWhenSingleIcon",
154                            String.valueOf(_showWhenSingleIcon));
155    
156                    return EVAL_BODY_BUFFERED;
157            }
158    
159            protected String getEndPage() {
160                    if (Validator.isNull(_endPage)) {
161                            return _END_PAGE;
162                    }
163                    else {
164                            return _endPage;
165                    }
166            }
167    
168            protected String getStartPage() {
169                    if (Validator.isNull(_startPage)) {
170                            return _START_PAGE;
171                    }
172                    else {
173                            return _startPage;
174                    }
175            }
176    
177            public void setAlign(String align) {
178                    _align = align;
179            }
180    
181            public void setCssClass(String cssClass) {
182                    _cssClass = cssClass;
183            }
184    
185            public void setEndPage(String endPage) {
186                    _endPage = endPage;
187            }
188    
189            public void setIcon(String icon) {
190                    _icon = icon;
191            }
192    
193            public void setId(String id) {
194                    _id = id;
195            }
196    
197            public void setMessage(String message) {
198                    _message = message;
199            }
200    
201            public void setShowArrow(boolean showArrow) {
202                    _showArrow = showArrow;
203            }
204    
205            public void setShowExpanded(boolean showExpanded) {
206                    _showExpanded = showExpanded;
207            }
208    
209            public void setShowWhenSingleIcon(boolean showWhenSingleIcon) {
210                    _showWhenSingleIcon = showWhenSingleIcon;
211            }
212    
213            public void setStartPage(String startPage) {
214                    _startPage = startPage;
215            }
216    
217            private static final String _END_PAGE = "/html/taglib/ui/icon_menu/end.jsp";
218    
219            private static final String _START_PAGE =
220                    "/html/taglib/ui/icon_menu/start.jsp";
221    
222            private String _align = "right";
223            private String _cssClass;
224            private String _endPage;
225            private String _icon;
226            private String _id;
227            private String _message = "actions";
228            private boolean _showArrow = true;
229            private boolean _showExpanded;
230            private boolean _showWhenSingleIcon;
231            private String _startPage;
232    
233    }