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.util.HtmlUtil;
18  import com.liferay.portal.kernel.util.MethodCache;
19  import com.liferay.portal.kernel.util.WebKeys;
20  import com.liferay.portal.model.Layout;
21  
22  import java.io.Serializable;
23  
24  import java.lang.reflect.Method;
25  
26  import java.util.ArrayList;
27  import java.util.List;
28  
29  import javax.servlet.http.HttpServletRequest;
30  
31  /**
32   * <a href="NavItem.java.html"><b><i>View Source</i></b></a>
33   *
34   * @author Brian Wing Shun Chan
35   */
36  public class NavItem implements Serializable {
37  
38      public static NavItem fromLayout(RequestVars vars, Layout layout) {
39          return new NavItem(vars, layout);
40      }
41  
42      public static List<NavItem> fromLayouts(
43          RequestVars vars, List<Layout> layouts) {
44  
45          if (layouts == null) {
46              return null;
47          }
48  
49          List<NavItem> navItems = new ArrayList<NavItem>(layouts.size());
50  
51          for (Layout layout : layouts) {
52              navItems.add(fromLayout(vars, layout));
53          }
54  
55          return navItems;
56      }
57  
58      public NavItem(RequestVars vars, Layout layout) {
59          _vars = vars;
60          _layout = layout;
61      }
62  
63      public Layout getLayout() {
64          return _layout;
65      }
66  
67      public boolean isChildSelected() {
68          ThemeDisplay themeDisplay = _vars.getThemeDisplay();
69  
70          return _layout.isChildSelected(
71              themeDisplay.isTilesSelectable(), themeDisplay.getLayout());
72      }
73  
74      public boolean isSelected() {
75          ThemeDisplay themeDisplay = _vars.getThemeDisplay();
76  
77          return _layout.isSelected(
78              themeDisplay.isTilesSelectable(), themeDisplay.getLayout(),
79              _vars.getAncestorPlid());
80      }
81  
82      public String getName() {
83          return HtmlUtil.escape(
84              _layout.getName(_vars.getThemeDisplay().getLocale()));
85      }
86  
87      public String getTarget() {
88          return _layout.getTarget();
89      }
90  
91      public String getTitle() {
92          return _layout.getTitle(_vars.getThemeDisplay().getLocale());
93      }
94  
95      public String getURL() throws Exception {
96          return getRegularURL();
97      }
98  
99      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 String 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         return (String)method.invoke(velocityTaglib, new Object[] {_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 }