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