001
014
015 package com.liferay.portal.theme;
016
017 import com.liferay.portal.kernel.exception.PortalException;
018 import com.liferay.portal.kernel.template.Template;
019 import com.liferay.portal.kernel.util.HtmlUtil;
020 import com.liferay.portal.kernel.util.StringUtil;
021 import com.liferay.portal.kernel.util.Validator;
022 import com.liferay.portal.kernel.util.WebKeys;
023 import com.liferay.portal.model.Layout;
024 import com.liferay.portal.util.PortalUtil;
025
026 import java.io.Serializable;
027
028 import java.lang.reflect.Method;
029
030 import java.util.ArrayList;
031 import java.util.List;
032
033 import javax.servlet.http.HttpServletRequest;
034
035
043 public class NavItem implements Serializable {
044
045
061 public static List<NavItem> fromLayouts(
062 HttpServletRequest request, List<Layout> layouts, Template template) {
063
064 if (layouts == null) {
065 return null;
066 }
067
068 List<NavItem> navItems = new ArrayList<NavItem>(layouts.size());
069
070 for (Layout layout : layouts) {
071 navItems.add(new NavItem(request, layout, template));
072 }
073
074 return navItems;
075 }
076
077 public NavItem(
078 HttpServletRequest request, Layout layout, Template template) {
079
080 _request = request;
081 _themeDisplay = (ThemeDisplay)request.getAttribute(
082 WebKeys.THEME_DISPLAY);
083 _layout = layout;
084 _template = template;
085 }
086
087
095 public List<NavItem> getChildren() throws Exception {
096 if (_children == null) {
097 List<Layout> layouts = _layout.getChildren(
098 _themeDisplay.getPermissionChecker());
099
100 _children = fromLayouts(_request, layouts, _template);
101 }
102
103 return _children;
104 }
105
106
111 public Layout getLayout() {
112 return _layout;
113 }
114
115
120 public long getLayoutId() {
121 return _layout.getLayoutId();
122 }
123
124
129 public String getName() {
130 return HtmlUtil.escape(getUnescapedName());
131 }
132
133
140 public String getRegularFullURL() throws Exception {
141 String portalURL = PortalUtil.getPortalURL(_request);
142
143 String regularURL = getRegularURL();
144
145 if (StringUtil.startsWith(regularURL, portalURL) ||
146 Validator.isUrl(regularURL)) {
147
148 return regularURL;
149 }
150 else {
151 return portalURL.concat(regularURL);
152 }
153 }
154
155
161 public String getRegularURL() throws Exception {
162 return _layout.getRegularURL(_request);
163 }
164
165 public String getResetLayoutURL() throws Exception {
166 return _layout.getResetLayoutURL(_request);
167 }
168
169 public String getResetMaxStateURL() throws Exception {
170 return _layout.getResetMaxStateURL(_request);
171 }
172
173
178 public String getTarget() {
179 return _layout.getTarget();
180 }
181
182
189 public String getTitle() {
190 return _layout.getTitle(_themeDisplay.getLocale());
191 }
192
193
200 public String getUnescapedName() {
201 return _layout.getName(_themeDisplay.getLocale());
202 }
203
204
212 public String getURL() throws Exception {
213 return HtmlUtil.escapeHREF(getRegularFullURL());
214 }
215
216
224 public boolean hasChildren() throws Exception {
225 List<NavItem> children = getChildren();
226
227 if (!children.isEmpty()) {
228 return true;
229 }
230 else {
231 return false;
232 }
233 }
234
235 public void icon() throws Exception {
236 Object velocityTaglib = _template.get("theme");
237
238 Method method = (Method)_template.get("velocityTaglib_layoutIcon");
239
240 method.invoke(velocityTaglib, _layout);
241 }
242
243 public boolean isChildSelected() throws PortalException {
244 return _layout.isChildSelected(
245 _themeDisplay.isTilesSelectable(), _themeDisplay.getLayout());
246 }
247
248 public boolean isSelected() throws Exception {
249 return _layout.isSelected(
250 _themeDisplay.isTilesSelectable(), _themeDisplay.getLayout(),
251 _themeDisplay.getLayout().getAncestorPlid());
252 }
253
254 private List<NavItem> _children;
255 private final Layout _layout;
256 private final HttpServletRequest _request;
257 private final Template _template;
258 private final ThemeDisplay _themeDisplay;
259
260 }