1   /**
2    * Copyright (c) 2000-2007 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.MethodCache;
26  import com.liferay.portal.kernel.util.WebKeys;
27  import com.liferay.portal.model.Layout;
28  
29  import java.io.Serializable;
30  
31  import java.lang.reflect.Method;
32  
33  import java.util.ArrayList;
34  import java.util.List;
35  
36  import javax.servlet.http.HttpServletRequest;
37  
38  /**
39   * <a href="NavItem.java.html"><b><i>View Source</i></b></a>
40   *
41   * @author Brian Wing Shun Chan
42   *
43   */
44  public class NavItem implements Serializable {
45  
46      public static NavItem fromLayout(RequestVars vars, Layout layout) {
47          return new NavItem(vars, layout);
48      }
49  
50      public static List fromLayouts(RequestVars vars, List layouts) {
51          if (layouts == null) {
52              return null;
53          }
54  
55          List navItems = new ArrayList(layouts.size());
56  
57          for (int i = 0; i < layouts.size(); i++) {
58              Layout layout = (Layout)layouts.get(i);
59  
60              navItems.add(fromLayout(vars, layout));
61          }
62  
63          return navItems;
64      }
65  
66      public NavItem(RequestVars vars, Layout layout) {
67          _vars = vars;
68          _layout = layout;
69      }
70  
71      public Layout getLayout() {
72          return _layout;
73      }
74  
75      public boolean isSelected() {
76          ThemeDisplay themeDisplay = _vars.getThemeDisplay();
77  
78          return _layout.isSelected(
79              themeDisplay.isTilesSelectable(), themeDisplay.getLayout(),
80              _vars.getAncestorPlid());
81      }
82  
83      public String getName() {
84          return _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 getChildren() throws Exception {
112         if (_children == null) {
113             ThemeDisplay themeDisplay = _vars.getThemeDisplay();
114 
115             List 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 req = _vars.getRequest();
135 
136         Object velocityTaglib = req.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 Layout[] {_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 _children;
157 
158 }