1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class IconListTag extends BodyTagSupport {
42  
43      public int doAfterBody() {
44          BodyContent bodyContent = getBodyContent();
45  
46          _bodyContentString = bodyContent.getString();
47  
48          HttpServletRequest request =
49              (HttpServletRequest)pageContext.getRequest();
50  
51          IntegerWrapper iconCount = (IntegerWrapper)request.getAttribute(
52              "liferay-ui:icon-list:icon-count");
53  
54          Boolean singleIcon = (Boolean)request.getAttribute(
55              "liferay-ui:icon-list:single-icon");
56  
57          if ((iconCount != null) && (iconCount.getValue() == 1) &&
58              (singleIcon == null)) {
59  
60              bodyContent.clearBody();
61  
62              request.setAttribute(
63                  "liferay-ui:icon-list:single-icon", Boolean.TRUE);
64  
65              return EVAL_BODY_AGAIN;
66          }
67          else {
68              return SKIP_BODY;
69          }
70      }
71  
72      public int doEndTag() throws JspException {
73          try {
74              HttpServletRequest request =
75                  (HttpServletRequest)pageContext.getRequest();
76  
77              IntegerWrapper iconCount = (IntegerWrapper)request.getAttribute(
78                  "liferay-ui:icon-list:icon-count");
79  
80              request.removeAttribute("liferay-ui:icon-list:icon-count");
81  
82              Boolean singleIcon = (Boolean)request.getAttribute(
83                  "liferay-ui:icon-list:single-icon");
84  
85              request.removeAttribute("liferay-ui:icon-list:single-icon");
86  
87              if ((iconCount != null) && (iconCount.getValue() > 1) &&
88                  ((singleIcon == null) || _showWhenSingleIcon)) {
89  
90                  PortalIncludeUtil.include(pageContext, getStartPage());
91              }
92  
93              pageContext.getOut().print(_bodyContentString);
94  
95              if ((iconCount != null) && (iconCount.getValue() > 1) &&
96                  ((singleIcon == null) || _showWhenSingleIcon)) {
97  
98                  PortalIncludeUtil.include(pageContext, getEndPage());
99              }
100 
101             request.removeAttribute("liferay-ui:icon-list:showWhenSingleIcon");
102 
103             return EVAL_PAGE;
104         }
105         catch (Exception e) {
106             throw new JspException(e);
107         }
108         finally {
109             if (!ServerDetector.isResin()) {
110                 _bodyContentString = StringPool.BLANK;
111                 _endPage = null;
112                 _showWhenSingleIcon = false;
113                 _startPage = null;
114             }
115         }
116     }
117 
118     public int doStartTag() {
119         HttpServletRequest request =
120             (HttpServletRequest)pageContext.getRequest();
121 
122         request.setAttribute(
123             "liferay-ui:icon-list:icon-count", new IntegerWrapper());
124         request.setAttribute(
125             "liferay-ui:icon-list:showWhenSingleIcon",
126             String.valueOf(_showWhenSingleIcon));
127 
128         return EVAL_BODY_BUFFERED;
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 String getStartPage() {
141         if (Validator.isNull(_startPage)) {
142             return _START_PAGE;
143         }
144         else {
145             return _startPage;
146         }
147     }
148 
149     public void setEndPage(String endPage) {
150         _endPage = endPage;
151     }
152 
153     public void setShowWhenSingleIcon(boolean showWhenSingleIcon) {
154         _showWhenSingleIcon = showWhenSingleIcon;
155     }
156 
157     public void setStartPage(String startPage) {
158         _startPage = startPage;
159     }
160 
161     private static final String _END_PAGE = "/html/taglib/ui/icon_list/end.jsp";
162 
163     private static final String _START_PAGE =
164         "/html/taglib/ui/icon_list/start.jsp";
165 
166     private String _bodyContentString = StringPool.BLANK;
167     private String _endPage;
168     private boolean _showWhenSingleIcon = false;
169     private String _startPage;
170 
171 }