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