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