001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.theme;
022:
023: import com.liferay.portal.kernel.util.MethodCache;
024: import com.liferay.portal.kernel.util.WebKeys;
025: import com.liferay.portal.model.Layout;
026:
027: import java.io.Serializable;
028:
029: import java.lang.reflect.Method;
030:
031: import java.util.ArrayList;
032: import java.util.List;
033:
034: import javax.servlet.http.HttpServletRequest;
035:
036: /**
037: * <a href="NavItem.java.html"><b><i>View Source</i></b></a>
038: *
039: * @author Brian Wing Shun Chan
040: *
041: */
042: public class NavItem implements Serializable {
043:
044: public static NavItem fromLayout(RequestVars vars, Layout layout) {
045: return new NavItem(vars, layout);
046: }
047:
048: public static List fromLayouts(RequestVars vars, List layouts) {
049: if (layouts == null) {
050: return null;
051: }
052:
053: List navItems = new ArrayList(layouts.size());
054:
055: for (int i = 0; i < layouts.size(); i++) {
056: Layout layout = (Layout) layouts.get(i);
057:
058: navItems.add(fromLayout(vars, layout));
059: }
060:
061: return navItems;
062: }
063:
064: public NavItem(RequestVars vars, Layout layout) {
065: _vars = vars;
066: _layout = layout;
067: }
068:
069: public Layout getLayout() {
070: return _layout;
071: }
072:
073: public boolean isSelected() {
074: ThemeDisplay themeDisplay = _vars.getThemeDisplay();
075:
076: return _layout.isSelected(themeDisplay.isTilesSelectable(),
077: themeDisplay.getLayout(), _vars.getAncestorPlid());
078: }
079:
080: public String getName() {
081: return _layout.getName(_vars.getThemeDisplay().getLocale());
082: }
083:
084: public String getTarget() {
085: return _layout.getTarget();
086: }
087:
088: public String getTitle() {
089: return _layout.getTitle(_vars.getThemeDisplay().getLocale());
090: }
091:
092: public String getURL() throws Exception {
093: return getRegularURL();
094: }
095:
096: public String getRegularURL() throws Exception {
097: return _layout.getRegularURL(_vars.getRequest());
098: }
099:
100: public String getResetMaxStateURL() throws Exception {
101: return _layout.getResetMaxStateURL(_vars.getRequest());
102: }
103:
104: public String getResetLayoutURL() throws Exception {
105: return _layout.getResetLayoutURL(_vars.getRequest());
106: }
107:
108: public List getChildren() throws Exception {
109: if (_children == null) {
110: ThemeDisplay themeDisplay = _vars.getThemeDisplay();
111:
112: List layouts = _layout.getChildren(themeDisplay
113: .getPermissionChecker());
114:
115: _children = fromLayouts(_vars, layouts);
116: }
117:
118: return _children;
119: }
120:
121: public boolean hasChildren() throws Exception {
122: if (getChildren().size() > 0) {
123: return true;
124: } else {
125: return false;
126: }
127: }
128:
129: public String icon() throws Exception {
130: HttpServletRequest req = _vars.getRequest();
131:
132: Object velocityTaglib = req
133: .getAttribute(WebKeys.VELOCITY_TAGLIB);
134:
135: Method method = MethodCache.get(_VELOCITY_TAGLIB_CLASS,
136: _VELOCITY_TAGLIB_LAYOUT_ICON_METHOD,
137: _VELOCITY_TAGLIB_LAYOUT_ICON_PARAMS);
138:
139: return (String) method.invoke(velocityTaglib,
140: new Layout[] { _layout });
141: }
142:
143: private static final String _VELOCITY_TAGLIB_CLASS = "com.liferay.taglib.util.VelocityTaglib";
144:
145: private static final String _VELOCITY_TAGLIB_LAYOUT_ICON_METHOD = "layoutIcon";
146:
147: private static final Class[] _VELOCITY_TAGLIB_LAYOUT_ICON_PARAMS = new Class[] { Layout.class };
148:
149: private RequestVars _vars;
150: private Layout _layout;
151: private List _children;
152:
153: }
|