View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.jetspeed.portalsite.impl;
18  
19  import java.util.Locale;
20  
21  import org.apache.jetspeed.om.common.GenericMetadata;
22  import org.apache.jetspeed.page.document.Node;
23  import org.apache.jetspeed.portalsite.Menu;
24  import org.apache.jetspeed.portalsite.MenuElement;
25  
26  /***
27   * This abstract class implements common features of portal-site
28   * menu elements constructed and returned to decorators.
29   * 
30   * @author <a href="mailto:rwatler@apache.org">Randy Watler</a>
31   * @version $Id: MenuElementImpl.java 516448 2007-03-09 16:25:47Z ate $
32   */
33  public abstract class MenuElementImpl implements MenuElement, Cloneable
34  {
35      /***
36       * parentMenu - parent menu implementation
37       */
38      private MenuImpl parent;
39  
40      /***
41       * node - underlying node proxy associated with this
42       *        menu element in the site view
43       */
44      private Node node;
45  
46      /***
47       * skin - inherited skin name for menu element
48       */
49      private String skin;
50  
51      /***
52       * skinInherited - flag indicating whether skin value
53       *                 has been inherited by propagating
54       *                 from parent menu
55       */
56      private boolean skinInherited;
57  
58      /***
59       * MenuElementImpl - constructor
60       *
61       * @param parent containing menu implementation
62       */
63      protected MenuElementImpl(MenuImpl parent)
64      {
65          this.parent = parent;
66      }
67  
68      /***
69       * MenuElementImpl - node proxy constructor
70       *
71       * @param parent containing menu implementation
72       * @param node menu element node proxy
73       */
74      protected MenuElementImpl(MenuImpl parent, Node node)
75      {
76          this(parent);
77          this.node = node;
78      }
79  
80      /***
81       * clone - clone this instance
82       *
83       * @return unparented copy
84       */
85      public Object clone() throws CloneNotSupportedException
86      {
87          // clone this object
88          MenuElementImpl copy = (MenuElementImpl) super.clone();
89  
90          // clear parent reference
91          copy.parent = null;
92          return copy;
93      }
94  
95      /***
96       * equals - compare menu element implementations
97       *
98       * @return equals result
99       */
100     public boolean equals(Object obj)
101     {
102         // compare menu implementation by type, url, and
103         // name, instances with no url and no name are
104         // always considered unique
105         if (this.getClass().equals(obj.getClass()))
106         {
107             String url = getUrl();
108             String name = getName();
109             if ((url != null) || (name != null))
110             {
111                 String objUrl = ((MenuElementImpl)obj).getUrl();
112                 String objName = ((MenuElementImpl)obj).getName();
113                 return ((((name == null) && (objName == null)) || ((name != null) && name.equals(objName))) &&
114                         (((url != null) && url.equals(objUrl)) || ((url == null) && (objUrl == null))));
115             }
116         }
117         return false;
118     }
119 
120     /***
121      * getElementType - get type of menu element
122      *
123      * @return MENU_ELEMENT_TYPE, OPTION_ELEMENT_TYPE, or
124      *         SEPARATOR_ELEMENT_TYPE
125      */
126     public abstract String getElementType();
127 
128     /***
129      * getParentMenu - get menu that contains menu element 
130      *
131      * @return parent menu
132      */    
133     public Menu getParentMenu()
134     {
135         return parent;
136     }
137 
138     /***
139      * setParentMenu - set menu that contains menu element 
140      *
141      * @param parentMenu parent menu
142      */    
143     protected void setParentMenu(Menu parentMenu)
144     {
145         parent = (MenuImpl)parentMenu;
146     }
147 
148     /***
149      * getName - get name of menu element used for default title
150      *
151      * @return menu element name
152      */
153     public String getName()
154     {
155         // no name by default
156         return null;
157     }
158 
159     /***
160      * getUrl - get url of menu element used for comparison
161      *
162      * @return folder, page, or link url
163      */
164     public String getUrl()
165     {
166         // no url by default
167         return null;
168     }
169 
170     /***
171      * getTitle - get default title for menu element
172      *
173      * @return title text
174      */
175     public String getTitle()
176     {
177         // return node or default title
178         if (node != null)
179         {
180             return node.getTitle();
181         }
182         return getName();
183     }
184 
185     /***
186      * getShortTitle - get default short title for menu element
187      *
188      * @return short title text
189      */
190     public String getShortTitle()
191     {
192         // return node or default short title
193         if (node != null)
194         {
195             return node.getShortTitle();
196         }
197         return getName();
198     }
199 
200     /***
201      * getTitle - get locale specific title for menu element
202      *            from metadata
203      *
204      * @param locale preferred locale
205      * @return title text
206      */
207     public String getTitle(Locale locale)
208     {
209         // return node or default title for preferred locale
210         if (node != null)
211         {
212             return node.getTitle(locale);
213         }
214         return getName();
215     }
216 
217     /***
218      * getShortTitle - get locale specific short title for menu
219      *                 element from metadata
220      *
221      * @param locale preferred locale
222      * @return short title text
223      */
224     public String getShortTitle(Locale locale)
225     {
226         // return node or default short title for preferred locale
227         if (node != null)
228         {
229             return node.getShortTitle(locale);
230         }
231         return getName();
232     }
233 
234     /***
235      * getMetadata - get generic metadata for menu element
236      *
237      * @return metadata
238      */    
239     public GenericMetadata getMetadata()
240     {
241         // return node metadata
242         if (node != null)
243         {
244             GenericMetadata metadata = node.getMetadata();
245             if (metadata != null && metadata.getFields() != null && !metadata.getFields().isEmpty())
246             {
247                 return metadata;
248             }
249         }
250         return null;
251     }
252 
253     /***
254      * getSkin - get skin name for menu element
255      *
256      * @return skin name
257      */
258     public String getSkin()
259     {
260         // no skin by default, check parent for
261         // skin value and cache locally
262         if (!skinInherited)
263         {
264             if (parent != null)
265             {
266                 skin = parent.getSkin();
267             }
268             skinInherited = true;
269         }
270         return skin;
271     }
272 
273     /***
274      * getNode - get menu element node proxy in the site view
275      *
276      * @return node proxy
277      */
278     protected Node getNode()
279     {
280         return node;
281     } 
282 
283     /***
284      * setNode - set menu element node proxy in the site view
285      *
286      * @param node node proxy
287      */
288     protected void setNode(Node node)
289     {
290         this.node = node;
291     } 
292 }