1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.taglib.ui;
24  
25  import com.liferay.portal.kernel.servlet.PortalIncludeUtil;
26  import com.liferay.portal.kernel.util.IntegerWrapper;
27  import com.liferay.portal.kernel.util.StringPool;
28  import com.liferay.portal.kernel.util.Validator;
29  
30  import javax.servlet.http.HttpServletRequest;
31  import javax.servlet.jsp.JspException;
32  import javax.servlet.jsp.tagext.BodyContent;
33  import javax.servlet.jsp.tagext.BodyTagSupport;
34  
35  /**
36   * <a href="IconMenuTag.java.html"><b><i>View Source</i></b></a>
37   *
38   * @author Brian Wing Shun Chan
39   *
40   */
41  public class IconMenuTag extends BodyTagSupport {
42  
43      public int doStartTag() {
44          HttpServletRequest request =
45              (HttpServletRequest)pageContext.getRequest();
46  
47          request.setAttribute("liferay-ui:icon-menu:message", _message);
48          request.setAttribute(
49              "liferay-ui:icon-menu:showWhenSingleIcon",
50              String.valueOf(_showWhenSingleIcon));
51          request.setAttribute("liferay-ui:icon-menu:align", _align);
52          request.setAttribute("liferay-ui:icon-menu:cssClass", _cssClass);
53          request.setAttribute(
54              "liferay-ui:icon-menu:icon-count", new IntegerWrapper());
55  
56          return EVAL_BODY_BUFFERED;
57      }
58  
59      public int doAfterBody() {
60          BodyContent bodyContent = getBodyContent();
61  
62          _bodyContentString = bodyContent.getString();
63  
64          HttpServletRequest request =
65              (HttpServletRequest)pageContext.getRequest();
66  
67          IntegerWrapper iconCount = (IntegerWrapper)request.getAttribute(
68              "liferay-ui:icon-menu:icon-count");
69  
70          Boolean singleIcon = (Boolean)request.getAttribute(
71              "liferay-ui:icon-menu:single-icon");
72  
73          if ((iconCount != null) && (iconCount.getValue() == 1) &&
74              (singleIcon == null)) {
75  
76              bodyContent.clearBody();
77  
78              request.setAttribute(
79                  "liferay-ui:icon-menu:single-icon", Boolean.TRUE);
80  
81              return EVAL_BODY_AGAIN;
82          }
83          else {
84              return SKIP_BODY;
85          }
86      }
87  
88      public int doEndTag() throws JspException {
89          try {
90              HttpServletRequest request =
91                  (HttpServletRequest)pageContext.getRequest();
92  
93              IntegerWrapper iconCount = (IntegerWrapper)request.getAttribute(
94                  "liferay-ui:icon-menu:icon-count");
95  
96              request.removeAttribute("liferay-ui:icon-menu:icon-count");
97  
98              Boolean singleIcon = (Boolean)request.getAttribute(
99                  "liferay-ui:icon-menu:single-icon");
100 
101             request.removeAttribute("liferay-ui:icon-menu:single-icon");
102 
103             if ((iconCount != null) && (iconCount.getValue() >= 1) &&
104                 ((singleIcon == null) || _showWhenSingleIcon)) {
105 
106                 PortalIncludeUtil.include(pageContext, getStartPage());
107             }
108 
109             pageContext.getOut().print(_bodyContentString);
110 
111             if ((iconCount != null) && (iconCount.getValue() >= 1) &&
112                 ((singleIcon == null) || _showWhenSingleIcon)) {
113 
114                 PortalIncludeUtil.include(pageContext, getEndPage());
115             }
116 
117             request.removeAttribute("liferay-ui:icon-menu:message");
118             request.removeAttribute("liferay-ui:icon-menu:showWhenSingleIcon");
119             request.removeAttribute("liferay-ui:icon-menu:align");
120             request.removeAttribute("liferay-ui:icon-menu:cssClass");
121 
122             return EVAL_PAGE;
123         }
124         catch (Exception e) {
125             throw new JspException(e);
126         }
127         finally {
128             _startPage = null;
129             _endPage = null;
130             _message = "actions";
131             _showWhenSingleIcon = false;
132             _align = "right";
133             _cssClass = null;
134             _bodyContentString = StringPool.BLANK;
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 setStartPage(String startPage) {
148         _startPage = startPage;
149     }
150 
151     public String getEndPage() {
152         if (Validator.isNull(_endPage)) {
153             return _END_PAGE;
154         }
155         else {
156             return _endPage;
157         }
158     }
159 
160     public void setEndPage(String endPage) {
161         _endPage = endPage;
162     }
163 
164     public void setMessage(String message) {
165         _message = message;
166     }
167 
168     public void setShowWhenSingleIcon(boolean showWhenSingleIcon) {
169         _showWhenSingleIcon = showWhenSingleIcon;
170     }
171 
172     public void setAlign(String align) {
173         _align = align;
174     }
175 
176     public void setCssClass(String cssClass) {
177         _cssClass = cssClass;
178     }
179 
180     private static final String _START_PAGE =
181         "/html/taglib/ui/icon_menu/start.jsp";
182 
183     private static final String _END_PAGE = "/html/taglib/ui/icon_menu/end.jsp";
184 
185     private String _startPage;
186     private String _endPage;
187     private String _message = "actions";
188     private boolean _showWhenSingleIcon = false;
189     private String _align = "right";
190     private String _cssClass;
191     private String _bodyContentString = StringPool.BLANK;
192 
193 }