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