1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.taglib.ui;
21  
22  import com.liferay.portal.kernel.servlet.PortalIncludeUtil;
23  import com.liferay.portal.kernel.util.IntegerWrapper;
24  import com.liferay.portal.kernel.util.StringPool;
25  import com.liferay.portal.kernel.util.Validator;
26  
27  import javax.servlet.http.HttpServletRequest;
28  import javax.servlet.jsp.JspException;
29  import javax.servlet.jsp.tagext.BodyContent;
30  import javax.servlet.jsp.tagext.BodyTagSupport;
31  
32  /**
33   * <a href="IconListTag.java.html"><b><i>View Source</i></b></a>
34   *
35   * @author Brian Wing Shun Chan
36   *
37   */
38  public class IconListTag extends BodyTagSupport {
39  
40      public int doStartTag() {
41          HttpServletRequest request =
42              (HttpServletRequest)pageContext.getRequest();
43  
44          request.setAttribute(
45              "liferay-ui:icon-list:showWhenSingleIcon",
46              String.valueOf(_showWhenSingleIcon));
47          request.setAttribute(
48              "liferay-ui:icon-list:icon-count", new IntegerWrapper());
49  
50          return EVAL_BODY_BUFFERED;
51      }
52  
53      public int doAfterBody() {
54          BodyContent bodyContent = getBodyContent();
55  
56          _bodyContentString = bodyContent.getString();
57  
58          HttpServletRequest request =
59              (HttpServletRequest)pageContext.getRequest();
60  
61          IntegerWrapper iconCount = (IntegerWrapper)request.getAttribute(
62              "liferay-ui:icon-list:icon-count");
63  
64          Boolean singleIcon = (Boolean)request.getAttribute(
65              "liferay-ui:icon-list:single-icon");
66  
67          if ((iconCount != null) && (iconCount.getValue() == 1) &&
68              (singleIcon == null)) {
69  
70              bodyContent.clearBody();
71  
72              request.setAttribute(
73                  "liferay-ui:icon-list:single-icon", Boolean.TRUE);
74  
75              return EVAL_BODY_AGAIN;
76          }
77          else {
78              return SKIP_BODY;
79          }
80      }
81  
82      public int doEndTag() throws JspException {
83          try {
84              HttpServletRequest request =
85                  (HttpServletRequest)pageContext.getRequest();
86  
87              IntegerWrapper iconCount = (IntegerWrapper)request.getAttribute(
88                  "liferay-ui:icon-list:icon-count");
89  
90              request.removeAttribute("liferay-ui:icon-list:icon-count");
91  
92              Boolean singleIcon = (Boolean)request.getAttribute(
93                  "liferay-ui:icon-list:single-icon");
94  
95              request.removeAttribute("liferay-ui:icon-list:single-icon");
96  
97              if ((iconCount != null) && (iconCount.getValue() > 1) &&
98                  ((singleIcon == null) || _showWhenSingleIcon)) {
99  
100                 PortalIncludeUtil.include(pageContext, getStartPage());
101             }
102 
103             pageContext.getOut().print(_bodyContentString);
104 
105             if ((iconCount != null) && (iconCount.getValue() > 1) &&
106                 ((singleIcon == null) || _showWhenSingleIcon)) {
107 
108                 PortalIncludeUtil.include(pageContext, getEndPage());
109             }
110 
111             request.removeAttribute("liferay-ui:icon-list:showWhenSingleIcon");
112 
113             return EVAL_PAGE;
114         }
115         catch (Exception e) {
116             throw new JspException(e);
117         }
118         finally {
119             _startPage = null;
120             _endPage = null;
121             _showWhenSingleIcon = false;
122             _bodyContentString = StringPool.BLANK;
123         }
124     }
125 
126     public String getStartPage() {
127         if (Validator.isNull(_startPage)) {
128             return _START_PAGE;
129         }
130         else {
131             return _startPage;
132         }
133     }
134 
135     public void setStartPage(String startPage) {
136         _startPage = startPage;
137     }
138 
139     public String getEndPage() {
140         if (Validator.isNull(_endPage)) {
141             return _END_PAGE;
142         }
143         else {
144             return _endPage;
145         }
146     }
147 
148     public void setEndPage(String endPage) {
149         _endPage = endPage;
150     }
151 
152     public void setShowWhenSingleIcon(boolean showWhenSingleIcon) {
153         _showWhenSingleIcon = showWhenSingleIcon;
154     }
155 
156     private static final String _START_PAGE =
157         "/html/taglib/ui/icon_list/start.jsp";
158 
159     private static final String _END_PAGE = "/html/taglib/ui/icon_list/end.jsp";
160 
161     private String _startPage;
162     private String _endPage;
163     private boolean _showWhenSingleIcon = false;
164     private String _bodyContentString = StringPool.BLANK;
165 
166 }