1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   * 
13   */
14  
15  package com.liferay.taglib.ui;
16  
17  import com.liferay.portal.kernel.servlet.PortalIncludeUtil;
18  import com.liferay.portal.kernel.util.IntegerWrapper;
19  import com.liferay.portal.kernel.util.StringPool;
20  import com.liferay.portal.kernel.util.Validator;
21  
22  import javax.servlet.http.HttpServletRequest;
23  import javax.servlet.jsp.JspException;
24  import javax.servlet.jsp.tagext.BodyContent;
25  import javax.servlet.jsp.tagext.BodyTagSupport;
26  
27  /**
28   * <a href="IconMenuTag.java.html"><b><i>View Source</i></b></a>
29   *
30   * @author Brian Wing Shun Chan
31   */
32  public class IconMenuTag extends BodyTagSupport {
33  
34      public int doAfterBody() {
35          BodyContent bodyContent = getBodyContent();
36  
37          _bodyContentString = bodyContent.getString();
38  
39          HttpServletRequest request =
40              (HttpServletRequest)pageContext.getRequest();
41  
42          IntegerWrapper iconCount = (IntegerWrapper)request.getAttribute(
43              "liferay-ui:icon-menu:icon-count");
44  
45          Boolean singleIcon = (Boolean)request.getAttribute(
46              "liferay-ui:icon-menu:single-icon");
47  
48          if ((iconCount != null) && (iconCount.getValue() == 1) &&
49              (singleIcon == null)) {
50  
51              bodyContent.clearBody();
52  
53              request.setAttribute(
54                  "liferay-ui:icon-menu:single-icon", Boolean.TRUE);
55  
56              return EVAL_BODY_AGAIN;
57          }
58          else {
59              return SKIP_BODY;
60          }
61      }
62  
63      public int doEndTag() throws JspException {
64          try {
65              HttpServletRequest request =
66                  (HttpServletRequest)pageContext.getRequest();
67  
68              IntegerWrapper iconCount = (IntegerWrapper)request.getAttribute(
69                  "liferay-ui:icon-menu:icon-count");
70  
71              request.removeAttribute("liferay-ui:icon-menu:icon-count");
72  
73              Boolean singleIcon = (Boolean)request.getAttribute(
74                  "liferay-ui:icon-menu:single-icon");
75  
76              request.removeAttribute("liferay-ui:icon-menu:single-icon");
77  
78              if ((iconCount != null) && (iconCount.getValue() >= 1) &&
79                  ((singleIcon == null) || _showWhenSingleIcon)) {
80  
81                  PortalIncludeUtil.include(pageContext, getStartPage());
82              }
83  
84              pageContext.getOut().print(_bodyContentString);
85  
86              if ((iconCount != null) && (iconCount.getValue() >= 1) &&
87                  ((singleIcon == null) || _showWhenSingleIcon)) {
88  
89                  PortalIncludeUtil.include(pageContext, getEndPage());
90              }
91  
92              request.removeAttribute("liferay-ui:icon-menu:align");
93              request.removeAttribute("liferay-ui:icon-menu:cssClass");
94              request.removeAttribute("liferay-ui:icon-menu:message");
95              request.removeAttribute("liferay-ui:icon-menu:showWhenSingleIcon");
96  
97              return EVAL_PAGE;
98          }
99          catch (Exception e) {
100             throw new JspException(e);
101         }
102         finally {
103             _align = "right";
104             _bodyContentString = StringPool.BLANK;
105             _cssClass = null;
106             _endPage = null;
107             _message = "actions";
108             _showWhenSingleIcon = false;
109             _startPage = null;
110         }
111     }
112 
113     public int doStartTag() {
114         HttpServletRequest request =
115             (HttpServletRequest)pageContext.getRequest();
116 
117         request.setAttribute("liferay-ui:icon-menu:align", _align);
118         request.setAttribute("liferay-ui:icon-menu:cssClass", _cssClass);
119         request.setAttribute(
120             "liferay-ui:icon-menu:icon-count", new IntegerWrapper());
121         request.setAttribute("liferay-ui:icon-menu:message", _message);
122         request.setAttribute(
123             "liferay-ui:icon-menu:showWhenSingleIcon",
124             String.valueOf(_showWhenSingleIcon));
125 
126         return EVAL_BODY_BUFFERED;
127     }
128 
129     public String getEndPage() {
130         if (Validator.isNull(_endPage)) {
131             return _END_PAGE;
132         }
133         else {
134             return _endPage;
135         }
136     }
137 
138     public String getStartPage() {
139         if (Validator.isNull(_startPage)) {
140             return _START_PAGE;
141         }
142         else {
143             return _startPage;
144         }
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 setEndPage(String endPage) {
156         _endPage = endPage;
157     }
158 
159     public void setMessage(String message) {
160         _message = message;
161     }
162 
163     public void setShowWhenSingleIcon(boolean showWhenSingleIcon) {
164         _showWhenSingleIcon = showWhenSingleIcon;
165     }
166 
167     public void setStartPage(String startPage) {
168         _startPage = startPage;
169     }
170 
171     private static final String _END_PAGE = "/html/taglib/ui/icon_menu/end.jsp";
172 
173     private static final String _START_PAGE =
174         "/html/taglib/ui/icon_menu/start.jsp";
175 
176     private String _align = "right";
177     private String _bodyContentString = StringPool.BLANK;
178     private String _cssClass;
179     private String _endPage;
180     private String _message = "actions";
181     private boolean _showWhenSingleIcon = false;
182     private String _startPage;
183 
184 }