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