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 isChildSelected() {
77          ThemeDisplay themeDisplay = _vars.getThemeDisplay();
78  
79          return _layout.isChildSelected(
80              themeDisplay.isTilesSelectable(), themeDisplay.getLayout());
81      }
82  
83      public boolean isSelected() {
84          ThemeDisplay themeDisplay = _vars.getThemeDisplay();
85  
86          return _layout.isSelected(
87              themeDisplay.isTilesSelectable(), themeDisplay.getLayout(),
88              _vars.getAncestorPlid());
89      }
90  
91      public String getName() {
92          return HtmlUtil.escape(
93              _layout.getName(_vars.getThemeDisplay().getLocale()));
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 getURL() throws Exception {
105         return getRegularURL();
106     }
107 
108     public String getRegularURL() throws Exception {
109         return _layout.getRegularURL(_vars.getRequest());
110     }
111 
112     public String getResetMaxStateURL() throws Exception {
113         return _layout.getResetMaxStateURL(_vars.getRequest());
114     }
115 
116     public String getResetLayoutURL() throws Exception {
117         return _layout.getResetLayoutURL(_vars.getRequest());
118     }
119 
120     public List<NavItem> getChildren() throws Exception {
121         if (_children == null) {
122             ThemeDisplay themeDisplay = _vars.getThemeDisplay();
123 
124             List<Layout> layouts = _layout.getChildren(
125                 themeDisplay.getPermissionChecker());
126 
127             _children = fromLayouts(_vars, layouts);
128         }
129 
130         return _children;
131     }
132 
133     public boolean hasChildren() throws Exception {
134         if (getChildren().size() > 0) {
135             return true;
136         }
137         else {
138             return false;
139         }
140     }
141 
142     public String icon() throws Exception {
143         HttpServletRequest request = _vars.getRequest();
144 
145         Object velocityTaglib = request.getAttribute(WebKeys.VELOCITY_TAGLIB);
146 
147         Method method = MethodCache.get(
148             _VELOCITY_TAGLIB_CLASS, _VELOCITY_TAGLIB_LAYOUT_ICON_METHOD,
149             _VELOCITY_TAGLIB_LAYOUT_ICON_PARAMS);
150 
151         return (String)method.invoke(velocityTaglib, new Object[] {_layout});
152     }
153 
154     private static final String _VELOCITY_TAGLIB_CLASS =
155         "com.liferay.taglib.util.VelocityTaglib";
156 
157     private static final String _VELOCITY_TAGLIB_LAYOUT_ICON_METHOD =
158         "layoutIcon";
159 
160     private static final Class<?>[] _VELOCITY_TAGLIB_LAYOUT_ICON_PARAMS =
161         new Class[] {Layout.class};
162 
163     private RequestVars _vars;
164     private Layout _layout;
165     private List<NavItem> _children;
166 
167 }