1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
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 List<NavItem> getChildren() throws Exception {
64          if (_children == null) {
65              ThemeDisplay themeDisplay = _vars.getThemeDisplay();
66  
67              List<Layout> layouts = _layout.getChildren(
68                  themeDisplay.getPermissionChecker());
69  
70              _children = fromLayouts(_vars, layouts);
71          }
72  
73          return _children;
74      }
75  
76      public Layout getLayout() {
77          return _layout;
78      }
79  
80      public String getName() {
81          return HtmlUtil.escape(getUnescapedName());
82      }
83  
84      public String getRegularURL() throws Exception {
85          return _layout.getRegularURL(_vars.getRequest());
86      }
87  
88      public String getResetLayoutURL() throws Exception {
89          return _layout.getResetLayoutURL(_vars.getRequest());
90      }
91  
92      public String getResetMaxStateURL() throws Exception {
93          return _layout.getResetMaxStateURL(_vars.getRequest());
94      }
95  
96      public String getTarget() {
97          return _layout.getTarget();
98      }
99  
100     public String getTitle() {
101         return _layout.getTitle(_vars.getThemeDisplay().getLocale());
102     }
103 
104     public String getUnescapedName() {
105         return _layout.getName(_vars.getThemeDisplay().getLocale());
106     }
107 
108     public String getURL() throws Exception {
109         return HtmlUtil.escape(HtmlUtil.escapeHREF(getRegularURL()));
110     }
111 
112     public boolean hasChildren() throws Exception {
113         if (getChildren().size() > 0) {
114             return true;
115         }
116         else {
117             return false;
118         }
119     }
120 
121     public String icon() throws Exception {
122         HttpServletRequest request = _vars.getRequest();
123 
124         Object velocityTaglib = request.getAttribute(WebKeys.VELOCITY_TAGLIB);
125 
126         Method method = MethodCache.get(
127             _VELOCITY_TAGLIB_CLASS, _VELOCITY_TAGLIB_LAYOUT_ICON_METHOD,
128             _VELOCITY_TAGLIB_LAYOUT_ICON_PARAMS);
129 
130         return (String)method.invoke(velocityTaglib, new Object[] {_layout});
131     }
132 
133     public boolean isChildSelected() {
134         ThemeDisplay themeDisplay = _vars.getThemeDisplay();
135 
136         return _layout.isChildSelected(
137             themeDisplay.isTilesSelectable(), themeDisplay.getLayout());
138     }
139 
140     public boolean isSelected() {
141         ThemeDisplay themeDisplay = _vars.getThemeDisplay();
142 
143         return _layout.isSelected(
144             themeDisplay.isTilesSelectable(), themeDisplay.getLayout(),
145             _vars.getAncestorPlid());
146     }
147 
148     private static final String _VELOCITY_TAGLIB_CLASS =
149         "com.liferay.taglib.util.VelocityTaglib";
150 
151     private static final String _VELOCITY_TAGLIB_LAYOUT_ICON_METHOD =
152         "layoutIcon";
153 
154     private static final Class<?>[] _VELOCITY_TAGLIB_LAYOUT_ICON_PARAMS =
155         new Class[] {Layout.class};
156 
157     private RequestVars _vars;
158     private Layout _layout;
159     private List<NavItem> _children;
160 
161 }