001    /**
002     * Copyright (c) 2000-2012 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.portal.theme;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.templateparser.TemplateContext;
020    import com.liferay.portal.kernel.util.HtmlUtil;
021    import com.liferay.portal.kernel.util.StringUtil;
022    import com.liferay.portal.kernel.util.Validator;
023    import com.liferay.portal.kernel.util.WebKeys;
024    import com.liferay.portal.model.Layout;
025    import com.liferay.portal.util.PortalUtil;
026    
027    import java.io.Serializable;
028    
029    import java.lang.reflect.Method;
030    
031    import java.util.ArrayList;
032    import java.util.List;
033    
034    import javax.servlet.http.HttpServletRequest;
035    
036    /**
037     * @author Brian Wing Shun Chan
038     * @author Shuyang Zhou
039     */
040    public class NavItem implements Serializable {
041    
042            public static List<NavItem> fromLayouts(
043                    HttpServletRequest request, List<Layout> layouts,
044                    TemplateContext templateContext) {
045    
046                    if (layouts == null) {
047                            return null;
048                    }
049    
050                    List<NavItem> navItems = new ArrayList<NavItem>(layouts.size());
051    
052                    for (Layout layout : layouts) {
053                            navItems.add(new NavItem(request, layout, templateContext));
054                    }
055    
056                    return navItems;
057            }
058    
059            public NavItem(
060                    HttpServletRequest request, Layout layout,
061                    TemplateContext templateContext) {
062    
063                    _request = request;
064                    _themeDisplay = (ThemeDisplay)request.getAttribute(
065                            WebKeys.THEME_DISPLAY);
066                    _layout = layout;
067                    _templateContext = templateContext;
068            }
069    
070            public List<NavItem> getChildren() throws Exception {
071                    if (_children == null) {
072                            List<Layout> layouts = _layout.getChildren(
073                                    _themeDisplay.getPermissionChecker());
074    
075                            _children = fromLayouts(_request, layouts, _templateContext);
076                    }
077    
078                    return _children;
079            }
080    
081            public Layout getLayout() {
082                    return _layout;
083            }
084    
085            public String getName() {
086                    return HtmlUtil.escape(getUnescapedName());
087            }
088    
089            public String getRegularFullURL() throws Exception {
090                    String portalURL = PortalUtil.getPortalURL(_request);
091    
092                    String regularURL = getRegularURL();
093    
094                    if (StringUtil.startsWith(regularURL, portalURL) ||
095                            Validator.isUrl(regularURL)) {
096    
097                            return regularURL;
098                    }
099                    else {
100                            return portalURL.concat(regularURL);
101                    }
102            }
103    
104            public String getRegularURL() throws Exception {
105                    return _layout.getRegularURL(_request);
106            }
107    
108            public String getResetLayoutURL() throws Exception {
109                    return _layout.getResetLayoutURL(_request);
110            }
111    
112            public String getResetMaxStateURL() throws Exception {
113                    return _layout.getResetMaxStateURL(_request);
114            }
115    
116            public String getTarget() {
117                    return _layout.getTarget();
118            }
119    
120            public String getTitle() {
121                    return _layout.getTitle(_themeDisplay.getLocale());
122            }
123    
124            public String getUnescapedName() {
125                    return _layout.getName(_themeDisplay.getLocale());
126            }
127    
128            public String getURL() throws Exception {
129                    return HtmlUtil.escape(HtmlUtil.escapeHREF(getRegularFullURL()));
130            }
131    
132            public boolean hasChildren() throws Exception {
133                    if (getChildren().size() > 0) {
134                            return true;
135                    }
136                    else {
137                            return false;
138                    }
139            }
140    
141            public void icon() throws Exception {
142                    Object velocityTaglib = _templateContext.get("theme");
143    
144                    Method method = (Method)_templateContext.get(
145                            "velocityTaglib#layoutIcon");
146    
147                    method.invoke(velocityTaglib, _layout);
148            }
149    
150            public boolean isChildSelected() throws PortalException, SystemException {
151                    return _layout.isChildSelected(
152                            _themeDisplay.isTilesSelectable(), _themeDisplay.getLayout());
153            }
154    
155            public boolean isSelected() throws Exception {
156                    return _layout.isSelected(
157                            _themeDisplay.isTilesSelectable(), _themeDisplay.getLayout(),
158                            _themeDisplay.getLayout().getAncestorPlid());
159            }
160    
161            private List<NavItem> _children;
162            private Layout _layout;
163            private HttpServletRequest _request;
164            private TemplateContext _templateContext;
165            private ThemeDisplay _themeDisplay;
166    
167    }