001    /**
002     * Copyright (c) 2000-2011 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.util.HtmlUtil;
020    import com.liferay.portal.kernel.util.MethodCache;
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     */
039    public class NavItem implements Serializable {
040    
041            public static NavItem fromLayout(RequestVars vars, Layout layout) {
042                    return new NavItem(vars, layout);
043            }
044    
045            public static List<NavItem> fromLayouts(
046                    RequestVars vars, List<Layout> layouts) {
047    
048                    if (layouts == null) {
049                            return null;
050                    }
051    
052                    List<NavItem> navItems = new ArrayList<NavItem>(layouts.size());
053    
054                    for (Layout layout : layouts) {
055                            navItems.add(fromLayout(vars, layout));
056                    }
057    
058                    return navItems;
059            }
060    
061            public NavItem(RequestVars vars, Layout layout) {
062                    _vars = vars;
063                    _layout = layout;
064            }
065    
066            public List<NavItem> getChildren() throws Exception {
067                    if (_children == null) {
068                            ThemeDisplay themeDisplay = _vars.getThemeDisplay();
069    
070                            List<Layout> layouts = _layout.getChildren(
071                                    themeDisplay.getPermissionChecker());
072    
073                            _children = fromLayouts(_vars, layouts);
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(_vars.getRequest());
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(_vars.getRequest());
104            }
105    
106            public String getResetLayoutURL() throws Exception {
107                    return _layout.getResetLayoutURL(_vars.getRequest());
108            }
109    
110            public String getResetMaxStateURL() throws Exception {
111                    return _layout.getResetMaxStateURL(_vars.getRequest());
112            }
113    
114            public String getTarget() {
115                    return _layout.getTarget();
116            }
117    
118            public String getTitle() {
119                    return _layout.getTitle(_vars.getThemeDisplay().getLocale());
120            }
121    
122            public String getUnescapedName() {
123                    return _layout.getName(_vars.getThemeDisplay().getLocale());
124            }
125    
126            public String getURL() throws Exception {
127                    return HtmlUtil.escape(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                    HttpServletRequest request = _vars.getRequest();
141    
142                    Object velocityTaglib = request.getAttribute(WebKeys.VELOCITY_TAGLIB);
143    
144                    Method method = MethodCache.get(
145                            _VELOCITY_TAGLIB_CLASS, _VELOCITY_TAGLIB_LAYOUT_ICON_METHOD,
146                            _VELOCITY_TAGLIB_LAYOUT_ICON_PARAMS);
147    
148                    method.invoke(velocityTaglib, _layout);
149            }
150    
151            public boolean isChildSelected() throws PortalException, SystemException {
152                    ThemeDisplay themeDisplay = _vars.getThemeDisplay();
153    
154                    return _layout.isChildSelected(
155                            themeDisplay.isTilesSelectable(), themeDisplay.getLayout());
156            }
157    
158            public boolean isSelected() {
159                    ThemeDisplay themeDisplay = _vars.getThemeDisplay();
160    
161                    return _layout.isSelected(
162                            themeDisplay.isTilesSelectable(), themeDisplay.getLayout(),
163                            _vars.getAncestorPlid());
164            }
165    
166            private static final String _VELOCITY_TAGLIB_CLASS =
167                    "com.liferay.taglib.util.VelocityTaglib";
168    
169            private static final String _VELOCITY_TAGLIB_LAYOUT_ICON_METHOD =
170                    "layoutIcon";
171    
172            private static final Class<?>[] _VELOCITY_TAGLIB_LAYOUT_ICON_PARAMS =
173                    new Class[] {Layout.class};
174    
175            private RequestVars _vars;
176            private Layout _layout;
177            private List<NavItem> _children;
178    
179    }