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 long getLayoutId() {
084                    return _layout.getLayoutId();
085            }
086    
087            public String getName() {
088                    return HtmlUtil.escape(getUnescapedName());
089            }
090    
091            public String getRegularFullURL() throws Exception {
092                    String portalURL = PortalUtil.getPortalURL(_request);
093    
094                    String regularURL = getRegularURL();
095    
096                    if (StringUtil.startsWith(regularURL, portalURL) ||
097                            Validator.isUrl(regularURL)) {
098    
099                            return regularURL;
100                    }
101                    else {
102                            return portalURL.concat(regularURL);
103                    }
104            }
105    
106            public String getRegularURL() throws Exception {
107                    return _layout.getRegularURL(_request);
108            }
109    
110            public String getResetLayoutURL() throws Exception {
111                    return _layout.getResetLayoutURL(_request);
112            }
113    
114            public String getResetMaxStateURL() throws Exception {
115                    return _layout.getResetMaxStateURL(_request);
116            }
117    
118            public String getTarget() {
119                    return _layout.getTarget();
120            }
121    
122            public String getTitle() {
123                    return _layout.getTitle(_themeDisplay.getLocale());
124            }
125    
126            public String getUnescapedName() {
127                    return _layout.getName(_themeDisplay.getLocale());
128            }
129    
130            public String getURL() throws Exception {
131                    return HtmlUtil.escapeHREF(getRegularFullURL());
132            }
133    
134            public boolean hasChildren() throws Exception {
135                    if (getChildren().size() > 0) {
136                            return true;
137                    }
138                    else {
139                            return false;
140                    }
141            }
142    
143            public void icon() throws Exception {
144                    Object velocityTaglib = _template.get("theme");
145    
146                    Method method = (Method)_template.get("velocityTaglib_layoutIcon");
147    
148                    method.invoke(velocityTaglib, _layout);
149            }
150    
151            public boolean isChildSelected() throws PortalException, SystemException {
152                    return _layout.isChildSelected(
153                            _themeDisplay.isTilesSelectable(), _themeDisplay.getLayout());
154            }
155    
156            public boolean isSelected() throws Exception {
157                    return _layout.isSelected(
158                            _themeDisplay.isTilesSelectable(), _themeDisplay.getLayout(),
159                            _themeDisplay.getLayout().getAncestorPlid());
160            }
161    
162            private List<NavItem> _children;
163            private Layout _layout;
164            private HttpServletRequest _request;
165            private Template _template;
166            private ThemeDisplay _themeDisplay;
167    
168    }