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="IconMenuTag.java.html"><b><i>View Source</i></b></a>
37   *
38   * @author Brian Wing Shun Chan
39   */
40  public class IconMenuTag extends BodyTagSupport {
41  
42      public int doStartTag() {
43          HttpServletRequest request =
44              (HttpServletRequest)pageContext.getRequest();
45  
46          request.setAttribute("liferay-ui:icon-menu:message", _message);
47          request.setAttribute(
48              "liferay-ui:icon-menu:showWhenSingleIcon",
49              String.valueOf(_showWhenSingleIcon));
50          request.setAttribute("liferay-ui:icon-menu:align", _align);
51          request.setAttribute("liferay-ui:icon-menu:cssClass", _cssClass);
52          request.setAttribute(
53              "liferay-ui:icon-menu:icon-count", new IntegerWrapper());
54  
55          return EVAL_BODY_BUFFERED;
56      }
57  
58      public int doAfterBody() {
59          BodyContent bodyContent = getBodyContent();
60  
61          _bodyContentString = bodyContent.getString();
62  
63          HttpServletRequest request =
64              (HttpServletRequest)pageContext.getRequest();
65  
66          IntegerWrapper iconCount = (IntegerWrapper)request.getAttribute(
67              "liferay-ui:icon-menu:icon-count");
68  
69          Boolean singleIcon = (Boolean)request.getAttribute(
70              "liferay-ui:icon-menu:single-icon");
71  
72          if ((iconCount != null) && (iconCount.getValue() == 1) &&
73              (singleIcon == null)) {
74  
75              bodyContent.clearBody();
76  
77              request.setAttribute(
78                  "liferay-ui:icon-menu:single-icon", Boolean.TRUE);
79  
80              return EVAL_BODY_AGAIN;
81          }
82          else {
83              return SKIP_BODY;
84          }
85      }
86  
87      public int doEndTag() throws JspException {
88          try {
89              HttpServletRequest request =
90                  (HttpServletRequest)pageContext.getRequest();
91  
92              IntegerWrapper iconCount = (IntegerWrapper)request.getAttribute(
93                  "liferay-ui:icon-menu:icon-count");
94  
95              request.removeAttribute("liferay-ui:icon-menu:icon-count");
96  
97              Boolean singleIcon = (Boolean)request.getAttribute(
98                  "liferay-ui:icon-menu:single-icon");
99  
100             request.removeAttribute("liferay-ui:icon-menu:single-icon");
101 
102             if ((iconCount != null) && (iconCount.getValue() >= 1) &&
103                 ((singleIcon == null) || _showWhenSingleIcon)) {
104 
105                 PortalIncludeUtil.include(pageContext, getStartPage());
106             }
107 
108             pageContext.getOut().print(_bodyContentString);
109 
110             if ((iconCount != null) && (iconCount.getValue() >= 1) &&
111                 ((singleIcon == null) || _showWhenSingleIcon)) {
112 
113                 PortalIncludeUtil.include(pageContext, getEndPage());
114             }
115 
116             request.removeAttribute("liferay-ui:icon-menu:message");
117             request.removeAttribute("liferay-ui:icon-menu:showWhenSingleIcon");
118             request.removeAttribute("liferay-ui:icon-menu:align");
119             request.removeAttribute("liferay-ui:icon-menu:cssClass");
120 
121             return EVAL_PAGE;
122         }
123         catch (Exception e) {
124             throw new JspException(e);
125         }
126         finally {
127             _startPage = null;
128             _endPage = null;
129             _message = "actions";
130             _showWhenSingleIcon = false;
131             _align = "right";
132             _cssClass = null;
133             _bodyContentString = StringPool.BLANK;
134         }
135     }
136 
137     public String getStartPage() {
138         if (Validator.isNull(_startPage)) {
139             return _START_PAGE;
140         }
141         else {
142             return _startPage;
143         }
144     }
145 
146     public void setStartPage(String startPage) {
147         _startPage = startPage;
148     }
149 
150     public String getEndPage() {
151         if (Validator.isNull(_endPage)) {
152             return _END_PAGE;
153         }
154         else {
155             return _endPage;
156         }
157     }
158 
159     public void setEndPage(String endPage) {
160         _endPage = endPage;
161     }
162 
163     public void setMessage(String message) {
164         _message = message;
165     }
166 
167     public void setShowWhenSingleIcon(boolean showWhenSingleIcon) {
168         _showWhenSingleIcon = showWhenSingleIcon;
169     }
170 
171     public void setAlign(String align) {
172         _align = align;
173     }
174 
175     public void setCssClass(String cssClass) {
176         _cssClass = cssClass;
177     }
178 
179     private static final String _START_PAGE =
180         "/html/taglib/ui/icon_menu/start.jsp";
181 
182     private static final String _END_PAGE = "/html/taglib/ui/icon_menu/end.jsp";
183 
184     private String _startPage;
185     private String _endPage;
186     private String _message = "actions";
187     private boolean _showWhenSingleIcon = false;
188     private String _align = "right";
189     private String _cssClass;
190     private String _bodyContentString = StringPool.BLANK;
191 
192 }