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 java.util.Locale;
020:
021: import org.apache.jetspeed.om.common.GenericMetadata;
022: import org.apache.jetspeed.page.document.Node;
023: import org.apache.jetspeed.portalsite.Menu;
024: import org.apache.jetspeed.portalsite.MenuElement;
025:
026: /**
027: * This abstract class implements common features of portal-site
028: * menu elements constructed and returned to decorators.
029: *
030: * @author <a href="mailto:rwatler@apache.org">Randy Watler</a>
031: * @version $Id: MenuElementImpl.java 516448 2007-03-09 16:25:47Z ate $
032: */
033: public abstract class MenuElementImpl implements MenuElement, Cloneable {
034: /**
035: * parentMenu - parent menu implementation
036: */
037: private MenuImpl parent;
038:
039: /**
040: * node - underlying node proxy associated with this
041: * menu element in the site view
042: */
043: private Node node;
044:
045: /**
046: * skin - inherited skin name for menu element
047: */
048: private String skin;
049:
050: /**
051: * skinInherited - flag indicating whether skin value
052: * has been inherited by propagating
053: * from parent menu
054: */
055: private boolean skinInherited;
056:
057: /**
058: * MenuElementImpl - constructor
059: *
060: * @param parent containing menu implementation
061: */
062: protected MenuElementImpl(MenuImpl parent) {
063: this .parent = parent;
064: }
065:
066: /**
067: * MenuElementImpl - node proxy constructor
068: *
069: * @param parent containing menu implementation
070: * @param node menu element node proxy
071: */
072: protected MenuElementImpl(MenuImpl parent, Node node) {
073: this (parent);
074: this .node = node;
075: }
076:
077: /**
078: * clone - clone this instance
079: *
080: * @return unparented copy
081: */
082: public Object clone() throws CloneNotSupportedException {
083: // clone this object
084: MenuElementImpl copy = (MenuElementImpl) super .clone();
085:
086: // clear parent reference
087: copy.parent = null;
088: return copy;
089: }
090:
091: /**
092: * equals - compare menu element implementations
093: *
094: * @return equals result
095: */
096: public boolean equals(Object obj) {
097: // compare menu implementation by type, url, and
098: // name, instances with no url and no name are
099: // always considered unique
100: if (this .getClass().equals(obj.getClass())) {
101: String url = getUrl();
102: String name = getName();
103: if ((url != null) || (name != null)) {
104: String objUrl = ((MenuElementImpl) obj).getUrl();
105: String objName = ((MenuElementImpl) obj).getName();
106: return ((((name == null) && (objName == null)) || ((name != null) && name
107: .equals(objName))) && (((url != null) && url
108: .equals(objUrl)) || ((url == null) && (objUrl == null))));
109: }
110: }
111: return false;
112: }
113:
114: /**
115: * getElementType - get type of menu element
116: *
117: * @return MENU_ELEMENT_TYPE, OPTION_ELEMENT_TYPE, or
118: * SEPARATOR_ELEMENT_TYPE
119: */
120: public abstract String getElementType();
121:
122: /**
123: * getParentMenu - get menu that contains menu element
124: *
125: * @return parent menu
126: */
127: public Menu getParentMenu() {
128: return parent;
129: }
130:
131: /**
132: * setParentMenu - set menu that contains menu element
133: *
134: * @param parentMenu parent menu
135: */
136: protected void setParentMenu(Menu parentMenu) {
137: parent = (MenuImpl) parentMenu;
138: }
139:
140: /**
141: * getName - get name of menu element used for default title
142: *
143: * @return menu element name
144: */
145: public String getName() {
146: // no name by default
147: return null;
148: }
149:
150: /**
151: * getUrl - get url of menu element used for comparison
152: *
153: * @return folder, page, or link url
154: */
155: public String getUrl() {
156: // no url by default
157: return null;
158: }
159:
160: /**
161: * getTitle - get default title for menu element
162: *
163: * @return title text
164: */
165: public String getTitle() {
166: // return node or default title
167: if (node != null) {
168: return node.getTitle();
169: }
170: return getName();
171: }
172:
173: /**
174: * getShortTitle - get default short title for menu element
175: *
176: * @return short title text
177: */
178: public String getShortTitle() {
179: // return node or default short title
180: if (node != null) {
181: return node.getShortTitle();
182: }
183: return getName();
184: }
185:
186: /**
187: * getTitle - get locale specific title for menu element
188: * from metadata
189: *
190: * @param locale preferred locale
191: * @return title text
192: */
193: public String getTitle(Locale locale) {
194: // return node or default title for preferred locale
195: if (node != null) {
196: return node.getTitle(locale);
197: }
198: return getName();
199: }
200:
201: /**
202: * getShortTitle - get locale specific short title for menu
203: * element from metadata
204: *
205: * @param locale preferred locale
206: * @return short title text
207: */
208: public String getShortTitle(Locale locale) {
209: // return node or default short title for preferred locale
210: if (node != null) {
211: return node.getShortTitle(locale);
212: }
213: return getName();
214: }
215:
216: /**
217: * getMetadata - get generic metadata for menu element
218: *
219: * @return metadata
220: */
221: public GenericMetadata getMetadata() {
222: // return node metadata
223: if (node != null) {
224: GenericMetadata metadata = node.getMetadata();
225: if (metadata != null && metadata.getFields() != null
226: && !metadata.getFields().isEmpty()) {
227: return metadata;
228: }
229: }
230: return null;
231: }
232:
233: /**
234: * getSkin - get skin name for menu element
235: *
236: * @return skin name
237: */
238: public String getSkin() {
239: // no skin by default, check parent for
240: // skin value and cache locally
241: if (!skinInherited) {
242: if (parent != null) {
243: skin = parent.getSkin();
244: }
245: skinInherited = true;
246: }
247: return skin;
248: }
249:
250: /**
251: * getNode - get menu element node proxy in the site view
252: *
253: * @return node proxy
254: */
255: protected Node getNode() {
256: return node;
257: }
258:
259: /**
260: * setNode - set menu element node proxy in the site view
261: *
262: * @param node node proxy
263: */
264: protected void setNode(Node node) {
265: this.node = node;
266: }
267: }
|