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.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.template.Template;
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, Template template) {
044    
045                    if (layouts == null) {
046                            return null;
047                    }
048    
049                    List<NavItem> navItems = new ArrayList<NavItem>(layouts.size());
050    
051                    for (Layout layout : layouts) {
052                            navItems.add(new NavItem(request, layout, template));
053                    }
054    
055                    return navItems;
056            }
057    
058            public NavItem(
059                    HttpServletRequest request, Layout layout, Template template) {
060    
061                    _request = request;
062                    _themeDisplay = (ThemeDisplay)request.getAttribute(
063                            WebKeys.THEME_DISPLAY);
064                    _layout = layout;
065                    _template = template;
066            }
067    
068            public List<NavItem> getChildren() throws Exception {
069                    if (_children == null) {
070                            List<Layout> layouts = _layout.getChildren(
071                                    _themeDisplay.getPermissionChecker());
072    
073                            _children = fromLayouts(_request, layouts, _template);
074                    }
075    
076                    return _children;
077            }
078    
079            public Layout getLayout() {
080                    return _layout;
081            }
082    
083            public String getName() {
084                    return HtmlUtil.escape(getUnescapedName());
085            }
086    
087            public String getRegularFullURL() throws Exception {
088                    String portalURL = PortalUtil.getPortalURL(_request);
089    
090                    String regularURL = getRegularURL();
091    
092                    if (StringUtil.startsWith(regularURL, portalURL) ||
093                            Validator.isUrl(regularURL)) {
094    
095                            return regularURL;
096                    }
097                    else {
098                            return portalURL.concat(regularURL);
099                    }
100            }
101    
102            public String getRegularURL() throws Exception {
103                    return _layout.getRegularURL(_request);
104            }
105    
106            public String getResetLayoutURL() throws Exception {
107                    return _layout.getResetLayoutURL(_request);
108            }
109    
110            public String getResetMaxStateURL() throws Exception {
111                    return _layout.getResetMaxStateURL(_request);
112            }
113    
114            public String getTarget() {
115                    return _layout.getTarget();
116            }
117    
118            public String getTitle() {
119                    return _layout.getTitle(_themeDisplay.getLocale());
120            }
121    
122            public String getUnescapedName() {
123                    return _layout.getName(_themeDisplay.getLocale());
124            }
125    
126            public String getURL() throws Exception {
127                    return HtmlUtil.escapeHREF(getRegularFullURL());
128            }
129    
130            public boolean hasChildren() throws Exception {
131                    if (getChildren().size() > 0) {
132                            return true;
133                    }
134                    else {
135                            return false;
136                    }
137            }
138    
139            public void icon() throws Exception {
140                    Object velocityTaglib = _template.get("theme");
141    
142                    Method method = (Method)_template.get("velocityTaglib#layoutIcon");
143    
144                    method.invoke(velocityTaglib, _layout);
145            }
146    
147            public boolean isChildSelected() throws PortalException, SystemException {
148                    return _layout.isChildSelected(
149                            _themeDisplay.isTilesSelectable(), _themeDisplay.getLayout());
150            }
151    
152            public boolean isSelected() throws Exception {
153                    return _layout.isSelected(
154                            _themeDisplay.isTilesSelectable(), _themeDisplay.getLayout(),
155                            _themeDisplay.getLayout().getAncestorPlid());
156            }
157    
158            private List<NavItem> _children;
159            private Layout _layout;
160            private HttpServletRequest _request;
161            private Template _template;
162            private ThemeDisplay _themeDisplay;
163    
164    }