1
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
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 }