1   /**
2    * Copyright (c) 2000-2009 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="IconListTag.java.html"><b><i>View Source</i></b></a>
37   *
38   * @author Brian Wing Shun Chan
39   *
40   */
41  public class IconListTag extends BodyTagSupport {
42  
43      public int doStartTag() {
44          HttpServletRequest request =
45              (HttpServletRequest)pageContext.getRequest();
46  
47          request.setAttribute(
48              "liferay-ui:icon-list:showWhenSingleIcon",
49              String.valueOf(_showWhenSingleIcon));
50          request.setAttribute(
51              "liferay-ui:icon-list:icon-count", new IntegerWrapper());
52  
53          return EVAL_BODY_BUFFERED;
54      }
55  
56      public int doAfterBody() {
57          BodyContent bodyContent = getBodyContent();
58  
59          _bodyContentString = bodyContent.getString();
60  
61          HttpServletRequest request =
62              (HttpServletRequest)pageContext.getRequest();
63  
64          IntegerWrapper iconCount = (IntegerWrapper)request.getAttribute(
65              "liferay-ui:icon-list:icon-count");
66  
67          Boolean singleIcon = (Boolean)request.getAttribute(
68              "liferay-ui:icon-list:single-icon");
69  
70          if ((iconCount != null) && (iconCount.getValue() == 1) &&
71              (singleIcon == null)) {
72  
73              bodyContent.clearBody();
74  
75              request.setAttribute(
76                  "liferay-ui:icon-list:single-icon", Boolean.TRUE);
77  
78              return EVAL_BODY_AGAIN;
79          }
80          else {
81              return SKIP_BODY;
82          }
83      }
84  
85      public int doEndTag() throws JspException {
86          try {
87              HttpServletRequest request =
88                  (HttpServletRequest)pageContext.getRequest();
89  
90              IntegerWrapper iconCount = (IntegerWrapper)request.getAttribute(
91                  "liferay-ui:icon-list:icon-count");
92  
93              request.removeAttribute("liferay-ui:icon-list:icon-count");
94  
95              Boolean singleIcon = (Boolean)request.getAttribute(
96                  "liferay-ui:icon-list:single-icon");
97  
98              request.removeAttribute("liferay-ui:icon-list:single-icon");
99  
100             if ((iconCount != null) && (iconCount.getValue() > 1) &&
101                 ((singleIcon == null) || _showWhenSingleIcon)) {
102 
103                 PortalIncludeUtil.include(pageContext, getStartPage());
104             }
105 
106             pageContext.getOut().print(_bodyContentString);
107 
108             if ((iconCount != null) && (iconCount.getValue() > 1) &&
109                 ((singleIcon == null) || _showWhenSingleIcon)) {
110 
111                 PortalIncludeUtil.include(pageContext, getEndPage());
112             }
113 
114             request.removeAttribute("liferay-ui:icon-list:showWhenSingleIcon");
115 
116             return EVAL_PAGE;
117         }
118         catch (Exception e) {
119             throw new JspException(e);
120         }
121         finally {
122             _startPage = null;
123             _endPage = null;
124             _showWhenSingleIcon = false;
125             _bodyContentString = StringPool.BLANK;
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 setStartPage(String startPage) {
139         _startPage = startPage;
140     }
141 
142     public String getEndPage() {
143         if (Validator.isNull(_endPage)) {
144             return _END_PAGE;
145         }
146         else {
147             return _endPage;
148         }
149     }
150 
151     public void setEndPage(String endPage) {
152         _endPage = endPage;
153     }
154 
155     public void setShowWhenSingleIcon(boolean showWhenSingleIcon) {
156         _showWhenSingleIcon = showWhenSingleIcon;
157     }
158 
159     private static final String _START_PAGE =
160         "/html/taglib/ui/icon_list/start.jsp";
161 
162     private static final String _END_PAGE = "/html/taglib/ui/icon_list/end.jsp";
163 
164     private String _startPage;
165     private String _endPage;
166     private boolean _showWhenSingleIcon = false;
167     private String _bodyContentString = StringPool.BLANK;
168 
169 }