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 org.apache.jetspeed.om.folder.Folder;
20  import org.apache.jetspeed.om.folder.MenuOptionsDefinition;
21  import org.apache.jetspeed.om.page.Link;
22  import org.apache.jetspeed.om.page.Page;
23  import org.apache.jetspeed.page.document.Node;
24  import org.apache.jetspeed.page.document.NodeNotFoundException;
25  import org.apache.jetspeed.portalsite.MenuOption;
26  import org.apache.jetspeed.portalsite.PortalSiteRequestContext;
27  
28  /***
29   * This class implements the portal-site menu option
30   * elements constructed and returned to decorators.
31   * 
32   * @author <a href="mailto:rwatler@apache.org">Randy Watler</a>
33   * @version $Id: MenuOptionImpl.java 537314 2007-05-11 23:08:36Z taylor $
34   */
35  public class MenuOptionImpl extends MenuElementImpl implements MenuOption, Cloneable
36  {
37      /***
38       * definition - menu option definition
39       */
40      private MenuOptionsDefinition definition;
41  
42      /***
43       * MenuOptionImpl - constructor
44       *
45       * @param parent containing menu implementation
46       * @param node menu option node proxy
47       * @param definition menu option definition
48       */
49      public MenuOptionImpl(MenuImpl parent, Node node, MenuOptionsDefinition definition)
50      {
51          super(parent, node);
52          this.definition = definition;
53      }
54  
55      /***
56       * getElementType - get type of menu element
57       *
58       * @return OPTION_ELEMENT_TYPE
59       */
60      public String getElementType()
61      {
62          return OPTION_ELEMENT_TYPE;
63      }
64  
65      /***
66       * getType - get type of menu option
67       *
68       * @return FOLDER_OPTION_TYPE, PAGE_OPTION_TYPE, or
69       *         LINK_OPTION_TYPE
70       */
71      public String getType()
72      {
73          // return type of menu option node proxy
74          Node node = getNode();
75          if (node instanceof Page)
76          {
77              return PAGE_OPTION_TYPE;
78          }
79          else if (node instanceof Link)
80          {
81              return LINK_OPTION_TYPE;
82          }
83          else if (node instanceof Folder)
84          {
85              return FOLDER_OPTION_TYPE;
86          }
87          return null;
88      }
89  
90      /***
91       * getSkin - get skin name for menu element
92       *
93       * @return skin name
94       */
95      public String getSkin()
96      {
97          // get skin from definition, from menu option
98          // node proxy, or inherit from parent menu
99          String skin = definition.getSkin();
100         if (skin == null)
101         {
102             Node node = getNode();
103             if (node instanceof Page)
104             {
105                 skin = ((Page)node).getSkin();
106             }
107             else if (node instanceof Link)
108             {
109                 skin = ((Link)node).getSkin();
110             }
111             else if (node instanceof Folder)
112             {
113                 skin = ((Folder)node).getSkin();
114             }
115         }
116         if (skin == null)
117         {
118             skin = super.getSkin();
119         }
120         return skin;
121     }
122 
123     /***
124      * getUrl - get url of menu option
125      *
126      * @return folder, page, or link url
127      */
128     public String getUrl()
129     {
130         return getNode().getUrl();
131     }
132 
133     /***
134      * getTarget - get target for url of menu option
135      *
136      * @return url target
137      */
138     public String getTarget()
139     {
140         // only link nodes support target
141         Node node = getNode();
142         if (node instanceof Link)
143         {
144             return ((Link)node).getTarget();
145         }
146         return null;
147     }
148 
149     /***
150      * getDefaultPage - get default page for a folder (if folder) of menu option
151      *
152      * @return url target
153      */
154     public String getDefaultPage()
155     {
156         // only link nodes support target
157         Node node = getNode();
158         if (node instanceof Folder)
159         {
160             return ((Folder)node).getDefaultPage();
161         }
162         return null;
163     }
164     
165     /***
166      * isHidden - get hidden state of menu option
167      *
168      * @return hidden state
169      */
170     public boolean isHidden()
171     {
172         return getNode().isHidden();
173     }
174 
175     /***
176      * isSelected - return true if menu option is selected by
177      *              the specified request context
178      *
179      * @param context request context
180      * @return selected state
181      */
182     public boolean isSelected(PortalSiteRequestContext context)
183     {
184         // compare the site view url of the page or
185         // folder menu option proxy with the url of
186         // the context request profiled page proxy
187         if (context != null)
188         {
189             // get request page
190             Page requestPage = null;
191             try
192             {
193                 requestPage = context.getPage();
194             }
195             catch (NodeNotFoundException nnfe)
196             {
197             }
198             catch (SecurityException se)
199             {
200             }
201             if (requestPage != null)
202             {
203                 // get selected status based or request page url
204                 Node node = getNode();
205                 if (node instanceof Page)
206                 {
207                     // page urls must match the request page
208                     // urls to be considered selected
209                     return requestPage.getUrl().equals(node.getUrl());
210                 }
211                 else if (node instanceof Folder)
212                 {
213                     // folder urls must be a prefix of the
214                     // request page urls to be considered
215                     // selected
216                     return requestPage.getUrl().startsWith(node.getUrl());
217                 }
218             }
219         }
220         return false;
221     }
222 }