1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.theme;
24  
25  import com.liferay.portal.kernel.util.HtmlUtil;
26  import com.liferay.portal.kernel.util.MethodCache;
27  import com.liferay.portal.kernel.util.WebKeys;
28  import com.liferay.portal.model.Layout;
29  
30  import java.io.Serializable;
31  
32  import java.lang.reflect.Method;
33  
34  import java.util.ArrayList;
35  import java.util.List;
36  
37  import javax.servlet.http.HttpServletRequest;
38  
39  /**
40   * <a href="NavItem.java.html"><b><i>View Source</i></b></a>
41   *
42   * @author Brian Wing Shun Chan
43   *
44   */
45  public class NavItem implements Serializable {
46  
47      public static NavItem fromLayout(RequestVars vars, Layout layout) {
48          return new NavItem(vars, layout);
49      }
50  
51      public static List<NavItem> fromLayouts(
52          RequestVars vars, List<Layout> layouts) {
53  
54          if (layouts == null) {
55              return null;
56          }
57  
58          List<NavItem> navItems = new ArrayList<NavItem>(layouts.size());
59  
60          for (Layout layout : layouts) {
61              navItems.add(fromLayout(vars, layout));
62          }
63  
64          return navItems;
65      }
66  
67      public NavItem(RequestVars vars, Layout layout) {
68          _vars = vars;
69          _layout = layout;
70      }
71  
72      public Layout getLayout() {
73          return _layout;
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 }