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
062 public static List<NavItem> fromLayouts(
063 HttpServletRequest request, List<Layout> layouts, Template template) {
064
065 if (layouts == null) {
066 return null;
067 }
068
069 List<NavItem> navItems = new ArrayList<NavItem>(layouts.size());
070
071 for (Layout layout : layouts) {
072 navItems.add(new NavItem(request, layout, template));
073 }
074
075 return navItems;
076 }
077
078 public NavItem(
079 HttpServletRequest request, Layout layout, Template template) {
080
081 _request = request;
082 _themeDisplay = (ThemeDisplay)request.getAttribute(
083 WebKeys.THEME_DISPLAY);
084 _layout = layout;
085 _template = template;
086 }
087
088
096 public List<NavItem> getChildren() throws Exception {
097 if (_children == null) {
098 List<Layout> layouts = _layout.getChildren(
099 _themeDisplay.getPermissionChecker());
100
101 _children = fromLayouts(_request, layouts, _template);
102 }
103
104 return _children;
105 }
106
107
112 public Layout getLayout() {
113 return _layout;
114 }
115
116
121 public long getLayoutId() {
122 return _layout.getLayoutId();
123 }
124
125
130 public String getName() {
131 return HtmlUtil.escape(getUnescapedName());
132 }
133
134
141 public String getRegularFullURL() throws Exception {
142 String portalURL = PortalUtil.getPortalURL(_request);
143
144 String regularURL = getRegularURL();
145
146 if (StringUtil.startsWith(regularURL, portalURL) ||
147 Validator.isUrl(regularURL)) {
148
149 return regularURL;
150 }
151 else {
152 return portalURL.concat(regularURL);
153 }
154 }
155
156
162 public String getRegularURL() throws Exception {
163 return _layout.getRegularURL(_request);
164 }
165
166 public String getResetLayoutURL() throws Exception {
167 return _layout.getResetLayoutURL(_request);
168 }
169
170 public String getResetMaxStateURL() throws Exception {
171 return _layout.getResetMaxStateURL(_request);
172 }
173
174
179 public String getTarget() {
180 return _layout.getTarget();
181 }
182
183
190 public String getTitle() {
191 return _layout.getTitle(_themeDisplay.getLocale());
192 }
193
194
201 public String getUnescapedName() {
202 return _layout.getName(_themeDisplay.getLocale());
203 }
204
205
213 public String getURL() throws Exception {
214 return HtmlUtil.escapeHREF(getRegularFullURL());
215 }
216
217
225 public boolean hasChildren() throws Exception {
226 List<NavItem> children = getChildren();
227
228 if (!children.isEmpty()) {
229 return true;
230 }
231 else {
232 return false;
233 }
234 }
235
236 public void icon() throws Exception {
237 Object velocityTaglib = _template.get("theme");
238
239 Method method = (Method)_template.get("velocityTaglib_layoutIcon");
240
241 method.invoke(velocityTaglib, _layout);
242 }
243
244 public boolean isChildSelected() throws PortalException {
245 return _layout.isChildSelected(
246 _themeDisplay.isTilesSelectable(), _themeDisplay.getLayout());
247 }
248
249 public boolean isSelected() throws Exception {
250 return _layout.isSelected(
251 _themeDisplay.isTilesSelectable(), _themeDisplay.getLayout(),
252 _themeDisplay.getLayout().getAncestorPlid());
253 }
254
255 private List<NavItem> _children;
256 private final Layout _layout;
257 private final HttpServletRequest _request;
258 private final Template _template;
259 private final ThemeDisplay _themeDisplay;
260
261 }