1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.taglib.aui;
16  
17  import com.liferay.portal.kernel.servlet.PortalIncludeUtil;
18  import com.liferay.portal.kernel.util.ServerDetector;
19  import com.liferay.taglib.util.IncludeTag;
20  
21  import java.util.HashMap;
22  import java.util.Map;
23  
24  import javax.servlet.http.HttpServletRequest;
25  import javax.servlet.jsp.JspException;
26  import javax.servlet.jsp.tagext.DynamicAttributes;
27  
28  /**
29   * <a href="OptionTag.java.html"><b><i>View Source</i></b></a>
30   *
31   * @author Julio Camarero
32   * @author Jorge Ferrer
33   * @author Brian Wing Shun Chan
34   */
35  public class OptionTag extends IncludeTag implements DynamicAttributes {
36  
37      public int doEndTag() throws JspException {
38          try{
39              PortalIncludeUtil.include(pageContext, _END_PAGE);
40  
41              return EVAL_PAGE;
42          }
43          catch (Exception e) {
44              throw new JspException(e);
45          }
46          finally {
47              if (!ServerDetector.isResin()) {
48                  _cssClass = null;
49                  _dynamicAttributes.clear();
50                  _label = null;
51                  _selected = false;
52                  _value = null;
53              }
54          }
55      }
56  
57      public int doStartTag() throws JspException {
58          try{
59              HttpServletRequest request =
60                  (HttpServletRequest)pageContext.getRequest();
61  
62              if (_value == null) {
63                  _value = _label;
64              }
65  
66              request.setAttribute("aui:option:cssClass", _cssClass);
67              request.setAttribute(
68                  "aui:option:dynamicAttributes", _dynamicAttributes);
69              request.setAttribute("aui:option:label", _label);
70              request.setAttribute(
71                  "aui:option:selected", String.valueOf(_selected));
72              request.setAttribute("aui:option:value", _value);
73  
74              PortalIncludeUtil.include(pageContext, _START_PAGE);
75  
76              return EVAL_BODY_INCLUDE;
77          }
78          catch (Exception e) {
79              throw new JspException(e);
80          }
81      }
82  
83      public void setCssClass(String cssClass) {
84          _cssClass = cssClass;
85      }
86  
87      public void setDynamicAttribute(
88          String uri, String localName, Object value) {
89  
90          _dynamicAttributes.put(localName, value);
91      }
92  
93      public void setLabel(Object label) {
94          _label = String.valueOf(label);
95      }
96  
97      public void setSelected(boolean selected) {
98          _selected = selected;
99      }
100 
101     public void setValue(Object value) {
102         _value = String.valueOf(value);
103     }
104 
105     private static final String _END_PAGE = "/html/taglib/aui/option/end.jsp";
106 
107     private static final String _START_PAGE =
108         "/html/taglib/aui/option/start.jsp";
109 
110     private String _cssClass;
111     private Map<String, Object> _dynamicAttributes =
112         new HashMap<String, Object>();
113     private String _label;
114     private boolean _selected;
115     private String _value;
116 
117 }