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