1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.taglib.ui;
16  
17  import com.liferay.portal.kernel.servlet.PortalIncludeUtil;
18  import com.liferay.portal.kernel.servlet.taglib.BaseBodyTagSupport;
19  import com.liferay.portal.kernel.util.IntegerWrapper;
20  import com.liferay.portal.kernel.util.ServerDetector;
21  import com.liferay.portal.kernel.util.Validator;
22  
23  import javax.servlet.http.HttpServletRequest;
24  import javax.servlet.jsp.JspException;
25  
26  /**
27   * <a href="IconListTag.java.html"><b><i>View Source</i></b></a>
28   *
29   * @author Brian Wing Shun Chan
30   */
31  public class IconListTag extends BaseBodyTagSupport {
32  
33      public int doAfterBody() {
34  
35          HttpServletRequest request =
36              (HttpServletRequest)pageContext.getRequest();
37  
38          IntegerWrapper iconCount = (IntegerWrapper)request.getAttribute(
39              "liferay-ui:icon-list:icon-count");
40  
41          Boolean singleIcon = (Boolean)request.getAttribute(
42              "liferay-ui:icon-list:single-icon");
43  
44          if ((iconCount != null) && (iconCount.getValue() == 1) &&
45              (singleIcon == null)) {
46  
47              bodyContent.clearBody();
48  
49              request.setAttribute(
50                  "liferay-ui:icon-list:single-icon", Boolean.TRUE);
51  
52              return EVAL_BODY_AGAIN;
53          }
54          else {
55              return SKIP_BODY;
56          }
57      }
58  
59      public int doEndTag() throws JspException {
60          try {
61              HttpServletRequest request =
62                  (HttpServletRequest)pageContext.getRequest();
63  
64              IntegerWrapper iconCount = (IntegerWrapper)request.getAttribute(
65                  "liferay-ui:icon-list:icon-count");
66  
67              request.removeAttribute("liferay-ui:icon-list:icon-count");
68  
69              Boolean singleIcon = (Boolean)request.getAttribute(
70                  "liferay-ui:icon-list:single-icon");
71  
72              request.removeAttribute("liferay-ui:icon-list:single-icon");
73  
74              if ((iconCount != null) && (iconCount.getValue() > 1) &&
75                  ((singleIcon == null) || _showWhenSingleIcon)) {
76  
77                  PortalIncludeUtil.include(pageContext, getStartPage());
78              }
79  
80              writeBodyContent(pageContext.getOut());
81  
82              if ((iconCount != null) && (iconCount.getValue() > 1) &&
83                  ((singleIcon == null) || _showWhenSingleIcon)) {
84  
85                  PortalIncludeUtil.include(pageContext, getEndPage());
86              }
87  
88              request.removeAttribute("liferay-ui:icon-list:showWhenSingleIcon");
89  
90              return EVAL_PAGE;
91          }
92          catch (Exception e) {
93              throw new JspException(e);
94          }
95          finally {
96              if (!ServerDetector.isResin()) {
97                  _endPage = null;
98                  _showWhenSingleIcon = false;
99                  _startPage = null;
100             }
101         }
102     }
103 
104     public int doStartTag() {
105         HttpServletRequest request =
106             (HttpServletRequest)pageContext.getRequest();
107 
108         request.setAttribute(
109             "liferay-ui:icon-list:icon-count", new IntegerWrapper());
110         request.setAttribute(
111             "liferay-ui:icon-list:showWhenSingleIcon",
112             String.valueOf(_showWhenSingleIcon));
113 
114         return EVAL_BODY_BUFFERED;
115     }
116 
117     protected String getEndPage() {
118         if (Validator.isNull(_endPage)) {
119             return _END_PAGE;
120         }
121         else {
122             return _endPage;
123         }
124     }
125 
126     protected String getStartPage() {
127         if (Validator.isNull(_startPage)) {
128             return _START_PAGE;
129         }
130         else {
131             return _startPage;
132         }
133     }
134 
135     public void setEndPage(String endPage) {
136         _endPage = endPage;
137     }
138 
139     public void setShowWhenSingleIcon(boolean showWhenSingleIcon) {
140         _showWhenSingleIcon = showWhenSingleIcon;
141     }
142 
143     public void setStartPage(String startPage) {
144         _startPage = startPage;
145     }
146 
147     private static final String _END_PAGE = "/html/taglib/ui/icon_list/end.jsp";
148 
149     private static final String _START_PAGE =
150         "/html/taglib/ui/icon_list/start.jsp";
151 
152     private String _endPage;
153     private boolean _showWhenSingleIcon = false;
154     private String _startPage;
155 
156 }