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.JavaConstants;
018    import com.liferay.portal.kernel.util.StringBundler;
019    import com.liferay.portal.kernel.util.StringUtil;
020    import com.liferay.portal.kernel.util.Validator;
021    import com.liferay.taglib.aui.base.BaseNavBarSearchTag;
022    
023    import javax.portlet.PortletResponse;
024    
025    import javax.servlet.http.HttpServletRequest;
026    import javax.servlet.jsp.JspException;
027    
028    /**
029     * @author Eduardo Lundgren
030     * @author Bruno Basto
031     * @author Nathan Cavanaugh
032     * @author Julio Camarero
033     */
034    public class NavBarSearchTag extends BaseNavBarSearchTag {
035    
036            @Override
037            public int doStartTag() throws JspException {
038                    NavBarTag navBarTag = (NavBarTag)findAncestorWithClass(
039                            this, NavBarTag.class);
040    
041                    if (navBarTag != null) {
042                            StringBundler sb = navBarTag.getResponsiveButtonsSB();
043    
044                            sb.append("<a class=\"btn btn-navbar\" id=\"");
045                            sb.append(_getNamespacedId());
046                            sb.append("NavbarBtn\" data-navId=\"");
047                            sb.append(_getNamespacedId());
048                            sb.append("\" tabindex=\"0\">");
049                            sb.append("<i class=\"icon-search\"></i></a>");
050                    }
051    
052                    return super.doStartTag();
053            }
054    
055            @Override
056            protected void cleanUp() {
057                    super.cleanUp();
058    
059                    _namespacedId = null;
060            }
061    
062            @Override
063            protected void setAttributes(HttpServletRequest request) {
064                    super.setAttributes(request);
065    
066                    setNamespacedAttribute(request, "id", _getNamespacedId());
067            }
068    
069            private String _getNamespacedId() {
070                    if (Validator.isNotNull(_namespacedId)) {
071                            return _namespacedId;
072                    }
073    
074                    _namespacedId = getId();
075    
076                    if (Validator.isNull(_namespacedId)) {
077                            _namespacedId = StringUtil.randomId();
078                    }
079    
080                    HttpServletRequest request =
081                            (HttpServletRequest)pageContext.getRequest();
082    
083                    PortletResponse portletResponse = (PortletResponse)request.getAttribute(
084                            JavaConstants.JAVAX_PORTLET_RESPONSE);
085    
086                    if (portletResponse != null) {
087                            _namespacedId = portletResponse.getNamespace() + _namespacedId;
088                    }
089    
090                    return _namespacedId;
091            }
092    
093            private String _namespacedId;
094    
095    }