001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.taglib.aui;
016    
017    import com.liferay.portal.kernel.util.GetterUtil;
018    import com.liferay.portal.kernel.util.Validator;
019    import com.liferay.taglib.aui.base.BaseOptionTag;
020    
021    import javax.servlet.http.HttpServletRequest;
022    import javax.servlet.jsp.JspWriter;
023    
024    /**
025     * @author Julio Camarero
026     * @author Jorge Ferrer
027     * @author Brian Wing Shun Chan
028     */
029    public class OptionTag extends BaseOptionTag {
030    
031            @Override
032            protected boolean isCleanUpSetAttributes() {
033                    return _CLEAN_UP_SET_ATTRIBUTES;
034            }
035    
036            @Override
037            protected int processEndTag() throws Exception {
038                    JspWriter jspWriter = pageContext.getOut();
039    
040                    jspWriter.write("</option>");
041    
042                    return EVAL_PAGE;
043            }
044    
045            @Override
046            protected void setAttributes(HttpServletRequest request) {
047                    super.setAttributes(request);
048    
049                    Object value = getValue();
050    
051                    if (value == null) {
052                            value = getLabel();
053                    }
054    
055                    boolean selected = getSelected();
056    
057                    if (getUseModelValue()) {
058                            String selectValue = GetterUtil.getString(
059                                    (String)request.getAttribute("aui:select:value"));
060    
061                            if (Validator.isNotNull(selectValue)) {
062                                    selected = selectValue.equals(String.valueOf(value));
063                            }
064                    }
065    
066                    setNamespacedAttribute(request, "selected", String.valueOf(selected));
067                    setNamespacedAttribute(request, "value", value);
068            }
069    
070            private static final boolean _CLEAN_UP_SET_ATTRIBUTES = true;
071    
072    }