001    /**
002     * Copyright (c) 2000-2010 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.WebKeys;
022    import com.liferay.portal.model.Layout;
023    
024    import java.io.Serializable;
025    
026    import java.lang.reflect.Method;
027    
028    import java.util.ArrayList;
029    import java.util.List;
030    
031    import javax.servlet.http.HttpServletRequest;
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 Layout getLayout() {
064                    return _layout;
065            }
066    
067            public boolean isChildSelected() throws PortalException, SystemException {
068                    ThemeDisplay themeDisplay = _vars.getThemeDisplay();
069    
070                    return _layout.isChildSelected(
071                            themeDisplay.isTilesSelectable(), themeDisplay.getLayout());
072            }
073    
074            public boolean isSelected() {
075                    ThemeDisplay themeDisplay = _vars.getThemeDisplay();
076    
077                    return _layout.isSelected(
078                            themeDisplay.isTilesSelectable(), themeDisplay.getLayout(),
079                            _vars.getAncestorPlid());
080            }
081    
082            public String getName() {
083                    return HtmlUtil.escape(
084                            _layout.getName(_vars.getThemeDisplay().getLocale()));
085            }
086    
087            public String getTarget() {
088                    return _layout.getTarget();
089            }
090    
091            public String getTitle() {
092                    return _layout.getTitle(_vars.getThemeDisplay().getLocale());
093            }
094    
095            public String getURL() throws Exception {
096                    return getRegularURL();
097            }
098    
099            public String getRegularURL() throws Exception {
100                    return _layout.getRegularURL(_vars.getRequest());
101            }
102    
103            public String getResetMaxStateURL() throws Exception {
104                    return _layout.getResetMaxStateURL(_vars.getRequest());
105            }
106    
107            public String getResetLayoutURL() throws Exception {
108                    return _layout.getResetLayoutURL(_vars.getRequest());
109            }
110    
111            public List<NavItem> getChildren() throws Exception {
112                    if (_children == null) {
113                            ThemeDisplay themeDisplay = _vars.getThemeDisplay();
114    
115                            List<Layout> layouts = _layout.getChildren(
116                                    themeDisplay.getPermissionChecker());
117    
118                            _children = fromLayouts(_vars, layouts);
119                    }
120    
121                    return _children;
122            }
123    
124            public boolean hasChildren() throws Exception {
125                    if (getChildren().size() > 0) {
126                            return true;
127                    }
128                    else {
129                            return false;
130                    }
131            }
132    
133            public void icon() throws Exception {
134                    HttpServletRequest request = _vars.getRequest();
135    
136                    Object velocityTaglib = request.getAttribute(WebKeys.VELOCITY_TAGLIB);
137    
138                    Method method = MethodCache.get(
139                            _VELOCITY_TAGLIB_CLASS, _VELOCITY_TAGLIB_LAYOUT_ICON_METHOD,
140                            _VELOCITY_TAGLIB_LAYOUT_ICON_PARAMS);
141    
142                    method.invoke(velocityTaglib, _layout);
143            }
144    
145            private static final String _VELOCITY_TAGLIB_CLASS =
146                    "com.liferay.taglib.util.VelocityTaglib";
147    
148            private static final String _VELOCITY_TAGLIB_LAYOUT_ICON_METHOD =
149                    "layoutIcon";
150    
151            private static final Class<?>[] _VELOCITY_TAGLIB_LAYOUT_ICON_PARAMS =
152                    new Class[] {Layout.class};
153    
154            private RequestVars _vars;
155            private Layout _layout;
156            private List<NavItem> _children;
157    
158    }