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.Validator;
020    import com.liferay.taglib.aui.base.BaseNavTag;
021    import com.liferay.util.PwdGenerator;
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 NavTag extends BaseNavTag {
035    
036            @Override
037            public int doStartTag() throws JspException {
038                    if (getCollapsible()) {
039                            NavBarTag navBarTag = (NavBarTag)findAncestorWithClass(
040                                    this, NavBarTag.class);
041    
042                            if (navBarTag != null) {
043                                    StringBundler sb = navBarTag.getResponsiveButtonsSB();
044    
045                                    sb.append("<a class=\"btn btn-navbar\" id=\"");
046                                    sb.append(_getNamespacedId());
047                                    sb.append("NavbarBtn\" ");
048                                    sb.append("data-navId=\"");
049                                    sb.append(_getNamespacedId());
050                                    sb.append("\">");
051    
052                                    String icon = getIcon();
053    
054                                    if (Validator.isNull(icon)) {
055                                            sb.append("<span class=\"icon-bar\"></span>");
056                                            sb.append("<span class=\"icon-bar\"></span>");
057                                            sb.append("<span class=\"icon-bar\"></span>");
058                                    }
059                                    else {
060                                            sb.append("<i class=\"icon-");
061                                            sb.append(icon);
062                                            sb.append("\"></i>");
063                                    }
064    
065                                    sb.append("</a>");
066                            }
067                    }
068    
069                    return super.doStartTag();
070            }
071    
072            @Override
073            protected void cleanUp() {
074                    super.cleanUp();
075    
076                    _namespacedId = null;
077            }
078    
079            @Override
080            protected void setAttributes(HttpServletRequest request) {
081                    super.setAttributes(request);
082    
083                    setNamespacedAttribute(request, "id", _getNamespacedId());
084            }
085    
086            private String _getNamespacedId() {
087                    if (Validator.isNotNull(_namespacedId)) {
088                            return _namespacedId;
089                    }
090    
091                    _namespacedId = getId();
092    
093                    if (Validator.isNull(_namespacedId)) {
094                            _namespacedId = PwdGenerator.getPassword(4);
095                    }
096    
097                    HttpServletRequest request =
098                            (HttpServletRequest)pageContext.getRequest();
099    
100                    PortletResponse portletResponse = (PortletResponse)request.getAttribute(
101                            JavaConstants.JAVAX_PORTLET_RESPONSE);
102    
103                    if ((portletResponse != null) && getUseNamespace()) {
104                            _namespacedId = portletResponse.getNamespace() + _namespacedId;
105                    }
106    
107                    return _namespacedId;
108            }
109    
110            private String _namespacedId;
111    
112    }