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("liferay-ui:icon-menu:align", _align);
49          request.setAttribute(
50              "liferay-ui:icon-menu:icon-count", new IntegerWrapper());
51  
52          return EVAL_BODY_BUFFERED;
53      }
54  
55      public int doAfterBody() {
56          BodyContent bodyContent = getBodyContent();
57  
58          _bodyContentString = bodyContent.getString();
59  
60          HttpServletRequest request =
61              (HttpServletRequest)pageContext.getRequest();
62  
63          IntegerWrapper iconCount = (IntegerWrapper)request.getAttribute(
64              "liferay-ui:icon-menu:icon-count");
65  
66          Boolean singleIcon = (Boolean)request.getAttribute(
67              "liferay-ui:icon-menu:single-icon");
68  
69          if ((iconCount != null) && (iconCount.getValue() == 1) &&
70              (singleIcon == null)) {
71  
72              bodyContent.clearBody();
73  
74              request.setAttribute(
75                  "liferay-ui:icon-menu:single-icon", Boolean.TRUE);
76  
77              return EVAL_BODY_AGAIN;
78          }
79          else {
80              return SKIP_BODY;
81          }
82      }
83  
84      public int doEndTag() throws JspException {
85          try {
86              HttpServletRequest request =
87                  (HttpServletRequest)pageContext.getRequest();
88  
89              IntegerWrapper iconCount = (IntegerWrapper)request.getAttribute(
90                  "liferay-ui:icon-menu:icon-count");
91  
92              request.removeAttribute("liferay-ui:icon-menu:icon-count");
93  
94              Boolean singleIcon = (Boolean)request.getAttribute(
95                  "liferay-ui:icon-menu:single-icon");
96  
97              request.removeAttribute("liferay-ui:icon-menu:single-icon");
98  
99              if ((iconCount != null) && (iconCount.getValue() > 1) &&
100                 (singleIcon == null)) {
101 
102                 PortalIncludeUtil.include(pageContext, getStartPage());
103             }
104 
105             pageContext.getOut().print(_bodyContentString);
106 
107             if ((iconCount != null) && (iconCount.getValue() > 1) &&
108                 (singleIcon == null)) {
109 
110                 PortalIncludeUtil.include(pageContext, getEndPage());
111             }
112 
113             return EVAL_PAGE;
114         }
115         catch (Exception e) {
116             throw new JspException(e);
117         }
118     }
119 
120     public String getStartPage() {
121         if (Validator.isNull(_startPage)) {
122             return _START_PAGE;
123         }
124         else {
125             return _startPage;
126         }
127     }
128 
129     public void setStartPage(String startPage) {
130         _startPage = startPage;
131     }
132 
133     public String getEndPage() {
134         if (Validator.isNull(_endPage)) {
135             return _END_PAGE;
136         }
137         else {
138             return _endPage;
139         }
140     }
141 
142     public void setEndPage(String endPage) {
143         _endPage = endPage;
144     }
145 
146     public void setMessage(String message) {
147         _message = message;
148     }
149 
150     public void setAlign(String align) {
151         _align = align;
152     }
153 
154     private static final String _START_PAGE =
155         "/html/taglib/ui/icon_menu/start.jsp";
156 
157     private static final String _END_PAGE = "/html/taglib/ui/icon_menu/end.jsp";
158 
159     private String _startPage;
160     private String _endPage;
161     private String _message = "actions";
162     private String _align = "right";
163     private String _bodyContentString = StringPool.BLANK;
164 
165 }