1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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="IconMenuTag.java.html"><b><i>View Source</i></b></a>
38   *
39   * @author Brian Wing Shun Chan
40   *
41   */
42  public class IconMenuTag extends BodyTagSupport {
43  
44      public int doStartTag() {
45          HttpServletRequest request =
46              (HttpServletRequest)pageContext.getRequest();
47  
48          request.setAttribute("liferay-ui:icon-menu:message", _message);
49          request.setAttribute(
50              "liferay-ui:icon-menu:showWhenSingleIcon",
51              String.valueOf(_showWhenSingleIcon));
52          request.setAttribute("liferay-ui:icon-menu:align", _align);
53          request.setAttribute("liferay-ui:icon-menu:cssClass", _cssClass);
54          request.setAttribute(
55              "liferay-ui:icon-menu:icon-count", new IntegerWrapper());
56  
57          return EVAL_BODY_BUFFERED;
58      }
59  
60      public int doAfterBody() {
61          BodyContent bodyContent = getBodyContent();
62  
63          _bodyContentString = bodyContent.getString();
64  
65          HttpServletRequest request =
66              (HttpServletRequest)pageContext.getRequest();
67  
68          IntegerWrapper iconCount = (IntegerWrapper)request.getAttribute(
69              "liferay-ui:icon-menu:icon-count");
70  
71          Boolean singleIcon = (Boolean)request.getAttribute(
72              "liferay-ui:icon-menu:single-icon");
73  
74          if ((iconCount != null) && (iconCount.getValue() == 1) &&
75              (singleIcon == null)) {
76  
77              bodyContent.clearBody();
78  
79              request.setAttribute(
80                  "liferay-ui:icon-menu:single-icon", Boolean.TRUE);
81  
82              return EVAL_BODY_AGAIN;
83          }
84          else {
85              return SKIP_BODY;
86          }
87      }
88  
89      public int doEndTag() throws JspException {
90          try {
91              HttpServletRequest request =
92                  (HttpServletRequest)pageContext.getRequest();
93  
94              IntegerWrapper iconCount = (IntegerWrapper)request.getAttribute(
95                  "liferay-ui:icon-menu:icon-count");
96  
97              request.removeAttribute("liferay-ui:icon-menu:icon-count");
98  
99              Boolean singleIcon = (Boolean)request.getAttribute(
100                 "liferay-ui:icon-menu:single-icon");
101 
102             request.removeAttribute("liferay-ui:icon-menu:single-icon");
103 
104             if ((iconCount != null) && (iconCount.getValue() >= 1) &&
105                 ((singleIcon == null) || _showWhenSingleIcon)) {
106 
107                 PortalIncludeUtil.include(pageContext, getStartPage());
108             }
109 
110             pageContext.getOut().print(_bodyContentString);
111 
112             if ((iconCount != null) && (iconCount.getValue() >= 1) &&
113                 ((singleIcon == null) || _showWhenSingleIcon)) {
114 
115                 PortalIncludeUtil.include(pageContext, getEndPage());
116             }
117 
118             request.removeAttribute("liferay-ui:icon-menu:message");
119             request.removeAttribute("liferay-ui:icon-menu:showWhenSingleIcon");
120             request.removeAttribute("liferay-ui:icon-menu:align");
121             request.removeAttribute("liferay-ui:icon-menu:cssClass");
122 
123             return EVAL_PAGE;
124         }
125         catch (Exception e) {
126             throw new JspException(e);
127         }
128         finally {
129             if (!ServerDetector.isResin()) {
130                 _startPage = null;
131                 _endPage = null;
132                 _message = "actions";
133                 _showWhenSingleIcon = false;
134                 _align = "right";
135                 _cssClass = null;
136                 _bodyContentString = StringPool.BLANK;
137             }
138         }
139     }
140 
141     public String getStartPage() {
142         if (Validator.isNull(_startPage)) {
143             return _START_PAGE;
144         }
145         else {
146             return _startPage;
147         }
148     }
149 
150     public void setStartPage(String startPage) {
151         _startPage = startPage;
152     }
153 
154     public String getEndPage() {
155         if (Validator.isNull(_endPage)) {
156             return _END_PAGE;
157         }
158         else {
159             return _endPage;
160         }
161     }
162 
163     public void setEndPage(String endPage) {
164         _endPage = endPage;
165     }
166 
167     public void setMessage(String message) {
168         _message = message;
169     }
170 
171     public void setShowWhenSingleIcon(boolean showWhenSingleIcon) {
172         _showWhenSingleIcon = showWhenSingleIcon;
173     }
174 
175     public void setAlign(String align) {
176         _align = align;
177     }
178 
179     public void setCssClass(String cssClass) {
180         _cssClass = cssClass;
181     }
182 
183     private static final String _START_PAGE =
184         "/html/taglib/ui/icon_menu/start.jsp";
185 
186     private static final String _END_PAGE = "/html/taglib/ui/icon_menu/end.jsp";
187 
188     private String _startPage;
189     private String _endPage;
190     private String _message = "actions";
191     private boolean _showWhenSingleIcon = false;
192     private String _align = "right";
193     private String _cssClass;
194     private String _bodyContentString = StringPool.BLANK;
195 
196 }