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