1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.theme;
16  
17  import com.liferay.portal.kernel.exception.PortalException;
18  import com.liferay.portal.kernel.exception.SystemException;
19  import com.liferay.portal.kernel.util.HtmlUtil;
20  import com.liferay.portal.kernel.util.MethodCache;
21  import com.liferay.portal.kernel.util.WebKeys;
22  import com.liferay.portal.model.Layout;
23  
24  import java.io.Serializable;
25  
26  import java.lang.reflect.Method;
27  
28  import java.util.ArrayList;
29  import java.util.List;
30  
31  import javax.servlet.http.HttpServletRequest;
32  
33  /**
34   * <a href="NavItem.java.html"><b><i>View Source</i></b></a>
35   *
36   * @author Brian Wing Shun Chan
37   */
38  public class NavItem implements Serializable {
39  
40      public static NavItem fromLayout(RequestVars vars, Layout layout) {
41          return new NavItem(vars, layout);
42      }
43  
44      public static List<NavItem> fromLayouts(
45          RequestVars vars, List<Layout> layouts) {
46  
47          if (layouts == null) {
48              return null;
49          }
50  
51          List<NavItem> navItems = new ArrayList<NavItem>(layouts.size());
52  
53          for (Layout layout : layouts) {
54              navItems.add(fromLayout(vars, layout));
55          }
56  
57          return navItems;
58      }
59  
60      public NavItem(RequestVars vars, Layout layout) {
61          _vars = vars;
62          _layout = layout;
63      }
64  
65      public Layout getLayout() {
66          return _layout;
67      }
68  
69      public boolean isChildSelected() throws PortalException, SystemException {
70          ThemeDisplay themeDisplay = _vars.getThemeDisplay();
71  
72          return _layout.isChildSelected(
73              themeDisplay.isTilesSelectable(), themeDisplay.getLayout());
74      }
75  
76      public boolean isSelected() {
77          ThemeDisplay themeDisplay = _vars.getThemeDisplay();
78  
79          return _layout.isSelected(
80              themeDisplay.isTilesSelectable(), themeDisplay.getLayout(),
81              _vars.getAncestorPlid());
82      }
83  
84      public String getName() {
85          return HtmlUtil.escape(
86              _layout.getName(_vars.getThemeDisplay().getLocale()));
87      }
88  
89      public String getTarget() {
90          return _layout.getTarget();
91      }
92  
93      public String getTitle() {
94          return _layout.getTitle(_vars.getThemeDisplay().getLocale());
95      }
96  
97      public String getURL() throws Exception {
98          return getRegularURL();
99      }
100 
101     public String getRegularURL() throws Exception {
102         return _layout.getRegularURL(_vars.getRequest());
103     }
104 
105     public String getResetMaxStateURL() throws Exception {
106         return _layout.getResetMaxStateURL(_vars.getRequest());
107     }
108 
109     public String getResetLayoutURL() throws Exception {
110         return _layout.getResetLayoutURL(_vars.getRequest());
111     }
112 
113     public List<NavItem> getChildren() throws Exception {
114         if (_children == null) {
115             ThemeDisplay themeDisplay = _vars.getThemeDisplay();
116 
117             List<Layout> layouts = _layout.getChildren(
118                 themeDisplay.getPermissionChecker());
119 
120             _children = fromLayouts(_vars, layouts);
121         }
122 
123         return _children;
124     }
125 
126     public boolean hasChildren() throws Exception {
127         if (getChildren().size() > 0) {
128             return true;
129         }
130         else {
131             return false;
132         }
133     }
134 
135     public String icon() throws Exception {
136         HttpServletRequest request = _vars.getRequest();
137 
138         Object velocityTaglib = request.getAttribute(WebKeys.VELOCITY_TAGLIB);
139 
140         Method method = MethodCache.get(
141             _VELOCITY_TAGLIB_CLASS, _VELOCITY_TAGLIB_LAYOUT_ICON_METHOD,
142             _VELOCITY_TAGLIB_LAYOUT_ICON_PARAMS);
143 
144         return (String)method.invoke(velocityTaglib, new Object[] {_layout});
145     }
146 
147     private static final String _VELOCITY_TAGLIB_CLASS =
148         "com.liferay.taglib.util.VelocityTaglib";
149 
150     private static final String _VELOCITY_TAGLIB_LAYOUT_ICON_METHOD =
151         "layoutIcon";
152 
153     private static final Class<?>[] _VELOCITY_TAGLIB_LAYOUT_ICON_PARAMS =
154         new Class[] {Layout.class};
155 
156     private RequestVars _vars;
157     private Layout _layout;
158     private List<NavItem> _children;
159 
160 }