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="IconListTag.java.html"><b><i>View Source</i></b></a>
29   *
30   * @author Brian Wing Shun Chan
31   */
32  public class IconListTag 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-list:icon-count");
44  
45          Boolean singleIcon = (Boolean)request.getAttribute(
46              "liferay-ui:icon-list: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-list: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-list:icon-count");
70  
71              request.removeAttribute("liferay-ui:icon-list:icon-count");
72  
73              Boolean singleIcon = (Boolean)request.getAttribute(
74                  "liferay-ui:icon-list:single-icon");
75  
76              request.removeAttribute("liferay-ui:icon-list: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-list:showWhenSingleIcon");
93  
94              return EVAL_PAGE;
95          }
96          catch (Exception e) {
97              throw new JspException(e);
98          }
99          finally {
100             _bodyContentString = StringPool.BLANK;
101             _endPage = null;
102             _showWhenSingleIcon = false;
103             _startPage = null;
104         }
105     }
106 
107     public int doStartTag() {
108         HttpServletRequest request =
109             (HttpServletRequest)pageContext.getRequest();
110 
111         request.setAttribute(
112             "liferay-ui:icon-list:icon-count", new IntegerWrapper());
113         request.setAttribute(
114             "liferay-ui:icon-list:showWhenSingleIcon",
115             String.valueOf(_showWhenSingleIcon));
116 
117         return EVAL_BODY_BUFFERED;
118     }
119 
120     public String getEndPage() {
121         if (Validator.isNull(_endPage)) {
122             return _END_PAGE;
123         }
124         else {
125             return _endPage;
126         }
127     }
128 
129     public String getStartPage() {
130         if (Validator.isNull(_startPage)) {
131             return _START_PAGE;
132         }
133         else {
134             return _startPage;
135         }
136     }
137 
138     public void setEndPage(String endPage) {
139         _endPage = endPage;
140     }
141 
142     public void setShowWhenSingleIcon(boolean showWhenSingleIcon) {
143         _showWhenSingleIcon = showWhenSingleIcon;
144     }
145 
146     public void setStartPage(String startPage) {
147         _startPage = startPage;
148     }
149 
150     private static final String _END_PAGE = "/html/taglib/ui/icon_list/end.jsp";
151 
152     private static final String _START_PAGE =
153         "/html/taglib/ui/icon_list/start.jsp";
154 
155     private String _bodyContentString = StringPool.BLANK;
156     private String _endPage;
157     private boolean _showWhenSingleIcon = false;
158     private String _startPage;
159 
160 }