001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.jetspeed.portalsite.impl;
018:
019: import org.apache.jetspeed.om.folder.Folder;
020: import org.apache.jetspeed.om.folder.MenuOptionsDefinition;
021: import org.apache.jetspeed.om.page.Link;
022: import org.apache.jetspeed.om.page.Page;
023: import org.apache.jetspeed.page.document.Node;
024: import org.apache.jetspeed.page.document.NodeNotFoundException;
025: import org.apache.jetspeed.portalsite.MenuOption;
026: import org.apache.jetspeed.portalsite.PortalSiteRequestContext;
027:
028: /**
029: * This class implements the portal-site menu option
030: * elements constructed and returned to decorators.
031: *
032: * @author <a href="mailto:rwatler@apache.org">Randy Watler</a>
033: * @version $Id: MenuOptionImpl.java 537314 2007-05-11 23:08:36Z taylor $
034: */
035: public class MenuOptionImpl extends MenuElementImpl implements
036: MenuOption, Cloneable {
037: /**
038: * definition - menu option definition
039: */
040: private MenuOptionsDefinition definition;
041:
042: /**
043: * MenuOptionImpl - constructor
044: *
045: * @param parent containing menu implementation
046: * @param node menu option node proxy
047: * @param definition menu option definition
048: */
049: public MenuOptionImpl(MenuImpl parent, Node node,
050: MenuOptionsDefinition definition) {
051: super (parent, node);
052: this .definition = definition;
053: }
054:
055: /**
056: * getElementType - get type of menu element
057: *
058: * @return OPTION_ELEMENT_TYPE
059: */
060: public String getElementType() {
061: return OPTION_ELEMENT_TYPE;
062: }
063:
064: /**
065: * getType - get type of menu option
066: *
067: * @return FOLDER_OPTION_TYPE, PAGE_OPTION_TYPE, or
068: * LINK_OPTION_TYPE
069: */
070: public String getType() {
071: // return type of menu option node proxy
072: Node node = getNode();
073: if (node instanceof Page) {
074: return PAGE_OPTION_TYPE;
075: } else if (node instanceof Link) {
076: return LINK_OPTION_TYPE;
077: } else if (node instanceof Folder) {
078: return FOLDER_OPTION_TYPE;
079: }
080: return null;
081: }
082:
083: /**
084: * getSkin - get skin name for menu element
085: *
086: * @return skin name
087: */
088: public String getSkin() {
089: // get skin from definition, from menu option
090: // node proxy, or inherit from parent menu
091: String skin = definition.getSkin();
092: if (skin == null) {
093: Node node = getNode();
094: if (node instanceof Page) {
095: skin = ((Page) node).getSkin();
096: } else if (node instanceof Link) {
097: skin = ((Link) node).getSkin();
098: } else if (node instanceof Folder) {
099: skin = ((Folder) node).getSkin();
100: }
101: }
102: if (skin == null) {
103: skin = super .getSkin();
104: }
105: return skin;
106: }
107:
108: /**
109: * getUrl - get url of menu option
110: *
111: * @return folder, page, or link url
112: */
113: public String getUrl() {
114: return getNode().getUrl();
115: }
116:
117: /**
118: * getTarget - get target for url of menu option
119: *
120: * @return url target
121: */
122: public String getTarget() {
123: // only link nodes support target
124: Node node = getNode();
125: if (node instanceof Link) {
126: return ((Link) node).getTarget();
127: }
128: return null;
129: }
130:
131: /**
132: * getDefaultPage - get default page for a folder (if folder) of menu option
133: *
134: * @return url target
135: */
136: public String getDefaultPage() {
137: // only link nodes support target
138: Node node = getNode();
139: if (node instanceof Folder) {
140: return ((Folder) node).getDefaultPage();
141: }
142: return null;
143: }
144:
145: /**
146: * isHidden - get hidden state of menu option
147: *
148: * @return hidden state
149: */
150: public boolean isHidden() {
151: return getNode().isHidden();
152: }
153:
154: /**
155: * isSelected - return true if menu option is selected by
156: * the specified request context
157: *
158: * @param context request context
159: * @return selected state
160: */
161: public boolean isSelected(PortalSiteRequestContext context) {
162: // compare the site view url of the page or
163: // folder menu option proxy with the url of
164: // the context request profiled page proxy
165: if (context != null) {
166: // get request page
167: Page requestPage = null;
168: try {
169: requestPage = context.getPage();
170: } catch (NodeNotFoundException nnfe) {
171: } catch (SecurityException se) {
172: }
173: if (requestPage != null) {
174: // get selected status based or request page url
175: Node node = getNode();
176: if (node instanceof Page) {
177: // page urls must match the request page
178: // urls to be considered selected
179: return requestPage.getUrl().equals(node.getUrl());
180: } else if (node instanceof Folder) {
181: // folder urls must be a prefix of the
182: // request page urls to be considered
183: // selected
184: return requestPage.getUrl().startsWith(
185: node.getUrl());
186: }
187: }
188: }
189: return false;
190: }
191: }
|