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