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