1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.taglib.ui;
16  
17  import com.liferay.portal.kernel.servlet.PortalIncludeUtil;
18  import com.liferay.portal.kernel.servlet.taglib.BaseBodyTagSupport;
19  import com.liferay.portal.kernel.util.IntegerWrapper;
20  import com.liferay.portal.kernel.util.ServerDetector;
21  import com.liferay.portal.kernel.util.StringPool;
22  import com.liferay.portal.kernel.util.Validator;
23  import com.liferay.portal.kernel.util.WebKeys;
24  import com.liferay.portal.theme.ThemeDisplay;
25  import com.liferay.portal.util.PortalUtil;
26  
27  import javax.servlet.http.HttpServletRequest;
28  import javax.servlet.jsp.JspException;
29  
30  /**
31   * <a href="IconMenuTag.java.html"><b><i>View Source</i></b></a>
32   *
33   * @author Brian Wing Shun Chan
34   */
35  public class IconMenuTag extends BaseBodyTagSupport {
36  
37      public int doAfterBody() {
38          HttpServletRequest request =
39              (HttpServletRequest)pageContext.getRequest();
40  
41          IntegerWrapper iconCount = (IntegerWrapper)request.getAttribute(
42              "liferay-ui:icon-menu:icon-count");
43  
44          Boolean singleIcon = (Boolean)request.getAttribute(
45              "liferay-ui:icon-menu:single-icon");
46  
47          if ((iconCount != null) && (iconCount.getValue() == 1) &&
48              (singleIcon == null)) {
49  
50              bodyContent.clearBody();
51  
52              request.setAttribute(
53                  "liferay-ui:icon-menu:single-icon", Boolean.TRUE);
54  
55              return EVAL_BODY_AGAIN;
56          }
57          else {
58              return SKIP_BODY;
59          }
60      }
61  
62      public int doEndTag() throws JspException {
63          try {
64              HttpServletRequest request =
65                  (HttpServletRequest)pageContext.getRequest();
66  
67              IntegerWrapper iconCount = (IntegerWrapper)request.getAttribute(
68                  "liferay-ui:icon-menu:icon-count");
69  
70              request.removeAttribute("liferay-ui:icon-menu:icon-count");
71  
72              Boolean singleIcon = (Boolean)request.getAttribute(
73                  "liferay-ui:icon-menu:single-icon");
74  
75              request.removeAttribute("liferay-ui:icon-menu:single-icon");
76  
77              if ((iconCount != null) && (iconCount.getValue() >= 1) &&
78                  ((singleIcon == null) || _showWhenSingleIcon)) {
79  
80                  PortalIncludeUtil.include(pageContext, getStartPage());
81              }
82  
83              writeBodyContent(pageContext.getOut());
84  
85              if ((iconCount != null) && (iconCount.getValue() >= 1) &&
86                  ((singleIcon == null) || _showWhenSingleIcon)) {
87  
88                  PortalIncludeUtil.include(pageContext, getEndPage());
89              }
90  
91              request.removeAttribute("liferay-ui:icon-menu:align");
92              request.removeAttribute("liferay-ui:icon-menu:cssClass");
93              request.removeAttribute("liferay-ui:icon-menu:icon");
94              request.removeAttribute("liferay-ui:icon-menu:id");
95              request.removeAttribute("liferay-ui:icon-menu:message");
96              request.removeAttribute("liferay-ui:icon-menu:showArrow");
97              request.removeAttribute("liferay-ui:icon-menu:showExpanded");
98              request.removeAttribute("liferay-ui:icon-menu:showWhenSingleIcon");
99  
100             return EVAL_PAGE;
101         }
102         catch (Exception e) {
103             throw new JspException(e);
104         }
105         finally {
106             if (!ServerDetector.isResin()) {
107                 _align = "right";
108                 _cssClass = null;
109                 _endPage = null;
110                 _icon = null;
111                 _id = null;
112                 _message = "actions";
113                 _showArrow = true;
114                 _showExpanded = false;
115                 _showWhenSingleIcon = false;
116                 _startPage = null;
117             }
118         }
119     }
120 
121     public int doStartTag() {
122         HttpServletRequest request =
123             (HttpServletRequest)pageContext.getRequest();
124 
125         ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
126             WebKeys.THEME_DISPLAY);
127 
128         String icon = _icon;
129 
130         if (icon == null) {
131             icon =  themeDisplay.getPathThemeImages() + "/common/tool.png";
132         }
133 
134         String id = _id;
135 
136         if (Validator.isNull(id)) {
137             String randomKey = PortalUtil.generateRandomKey(
138                 request, IconMenuTag.class.getName());
139 
140             id = randomKey + StringPool.UNDERLINE;
141         }
142 
143         request.setAttribute("liferay-ui:icon-menu:align", _align);
144         request.setAttribute("liferay-ui:icon-menu:cssClass", _cssClass);
145         request.setAttribute(
146             "liferay-ui:icon-menu:icon-count", new IntegerWrapper());
147         request.setAttribute("liferay-ui:icon-menu:icon", icon);
148         request.setAttribute("liferay-ui:icon-menu:id", id);
149         request.setAttribute("liferay-ui:icon-menu:message", _message);
150         request.setAttribute(
151             "liferay-ui:icon-menu:showArrow",String.valueOf(_showArrow));
152         request.setAttribute(
153             "liferay-ui:icon-menu:showExpanded",String.valueOf(_showExpanded));
154         request.setAttribute(
155             "liferay-ui:icon-menu:showWhenSingleIcon",
156             String.valueOf(_showWhenSingleIcon));
157 
158         return EVAL_BODY_BUFFERED;
159     }
160 
161     protected String getEndPage() {
162         if (Validator.isNull(_endPage)) {
163             return _END_PAGE;
164         }
165         else {
166             return _endPage;
167         }
168     }
169 
170     protected String getStartPage() {
171         if (Validator.isNull(_startPage)) {
172             return _START_PAGE;
173         }
174         else {
175             return _startPage;
176         }
177     }
178 
179     public void setAlign(String align) {
180         _align = align;
181     }
182 
183     public void setCssClass(String cssClass) {
184         _cssClass = cssClass;
185     }
186 
187     public void setEndPage(String endPage) {
188         _endPage = endPage;
189     }
190 
191     public void setIcon(String icon) {
192         _icon = icon;
193     }
194 
195     public void setId(String id) {
196         _id = id;
197     }
198 
199     public void setMessage(String message) {
200         _message = message;
201     }
202 
203     public void setShowArrow(boolean showArrow) {
204         _showArrow = showArrow;
205     }
206 
207     public void setShowExpanded(boolean showExpanded) {
208         _showExpanded = showExpanded;
209     }
210 
211     public void setShowWhenSingleIcon(boolean showWhenSingleIcon) {
212         _showWhenSingleIcon = showWhenSingleIcon;
213     }
214 
215     public void setStartPage(String startPage) {
216         _startPage = startPage;
217     }
218 
219     private static final String _END_PAGE = "/html/taglib/ui/icon_menu/end.jsp";
220 
221     private static final String _START_PAGE =
222         "/html/taglib/ui/icon_menu/start.jsp";
223 
224     private String _align = "right";
225     private String _cssClass;
226     private String _endPage;
227     private String _icon;
228     private String _id;
229     private String _message = "actions";
230     private boolean _showArrow = true;
231     private boolean _showExpanded;
232     private boolean _showWhenSingleIcon;
233     private String _startPage;
234 
235 }