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