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="IconMenuTag.java.html"><b><i>View Source</i></b></a>
34   *
35   * @author Brian Wing Shun Chan
36   *
37   */
38  public class IconMenuTag extends BodyTagSupport {
39  
40      public int doStartTag() {
41          HttpServletRequest request =
42              (HttpServletRequest)pageContext.getRequest();
43  
44          request.setAttribute("liferay-ui:icon-menu:message", _message);
45          request.setAttribute(
46              "liferay-ui:icon-menu:showWhenSingleIcon",
47              String.valueOf(_showWhenSingleIcon));
48          request.setAttribute("liferay-ui:icon-menu:align", _align);
49          request.setAttribute("liferay-ui:icon-menu:cssClass", _cssClass);
50          request.setAttribute(
51              "liferay-ui:icon-menu: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-menu:icon-count");
66  
67          Boolean singleIcon = (Boolean)request.getAttribute(
68              "liferay-ui:icon-menu: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-menu: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-menu:icon-count");
92  
93              request.removeAttribute("liferay-ui:icon-menu:icon-count");
94  
95              Boolean singleIcon = (Boolean)request.getAttribute(
96                  "liferay-ui:icon-menu:single-icon");
97  
98              request.removeAttribute("liferay-ui:icon-menu: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-menu:message");
115             request.removeAttribute("liferay-ui:icon-menu:showWhenSingleIcon");
116             request.removeAttribute("liferay-ui:icon-menu:align");
117             request.removeAttribute("liferay-ui:icon-menu:cssClass");
118 
119             return EVAL_PAGE;
120         }
121         catch (Exception e) {
122             throw new JspException(e);
123         }
124         finally {
125             _startPage = null;
126             _endPage = null;
127             _message = "actions";
128             _showWhenSingleIcon = false;
129             _align = "right";
130             _cssClass = null;
131             _bodyContentString = StringPool.BLANK;
132         }
133     }
134 
135     public String getStartPage() {
136         if (Validator.isNull(_startPage)) {
137             return _START_PAGE;
138         }
139         else {
140             return _startPage;
141         }
142     }
143 
144     public void setStartPage(String startPage) {
145         _startPage = startPage;
146     }
147 
148     public String getEndPage() {
149         if (Validator.isNull(_endPage)) {
150             return _END_PAGE;
151         }
152         else {
153             return _endPage;
154         }
155     }
156 
157     public void setEndPage(String endPage) {
158         _endPage = endPage;
159     }
160 
161     public void setMessage(String message) {
162         _message = message;
163     }
164 
165     public void setShowWhenSingleIcon(boolean showWhenSingleIcon) {
166         _showWhenSingleIcon = showWhenSingleIcon;
167     }
168 
169     public void setAlign(String align) {
170         _align = align;
171     }
172 
173     public void setCssClass(String cssClass) {
174         _cssClass = cssClass;
175     }
176 
177     private static final String _START_PAGE =
178         "/html/taglib/ui/icon_menu/start.jsp";
179 
180     private static final String _END_PAGE = "/html/taglib/ui/icon_menu/end.jsp";
181 
182     private String _startPage;
183     private String _endPage;
184     private String _message = "actions";
185     private boolean _showWhenSingleIcon = false;
186     private String _align = "right";
187     private String _cssClass;
188     private String _bodyContentString = StringPool.BLANK;
189 
190 }