KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > myfaces > custom > navmenu > NavigationMenuUtils


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.myfaces.custom.navmenu;
17
18 import org.apache.commons.logging.LogFactory;
19 import org.apache.commons.logging.Log;
20 import org.apache.myfaces.component.UserRoleUtils;
21
22 import javax.faces.component.UIComponent;
23 import javax.faces.component.UISelectItems;
24 import javax.faces.context.FacesContext;
25 import java.util.*;
26
27 /**
28  * @author Thomas Spiegl (latest modification by $Author: oros $)
29  * @version $Revision: 1.5 $ $Date: 2004/12/13 23:14:38 $
30  * $Log: NavigationMenuUtils.java,v $
31  * Revision 1.5 2004/12/13 23:14:38 oros
32  * fix #1044663: handle enabledOnUserRole/visibleOnUserRole, disabled menu items are rendered with null actions
33  *
34  * Revision 1.4 2004/10/13 11:50:57 matze
35  * renamed packages to org.apache
36  *
37  * Revision 1.3 2004/07/05 08:28:25 royalts
38  * added example for <x:navigationMenuItems>
39  *
40  * Revision 1.2 2004/07/01 21:53:07 mwessendorf
41  * ASF switch
42  *
43  * Revision 1.1 2004/06/23 13:44:31 royalts
44  * no message
45  *
46  */

47 public class NavigationMenuUtils
48 {
49     private static final Log log = LogFactory.getLog(NavigationMenuUtils.class);
50
51     public static List getNavigationMenuItemList(UIComponent uiComponent)
52     {
53         List list = new ArrayList(uiComponent.getChildCount());
54         for (Iterator children = uiComponent.getChildren().iterator(); children.hasNext(); )
55         {
56             UIComponent child = (UIComponent)children.next();
57             if (child instanceof UINavigationMenuItem)
58             {
59                 NavigationMenuItem item;
60                 Object JavaDoc value = ((UINavigationMenuItem)child).getValue();
61                 if (value != null)
62                 {
63                     //get NavigationMenuItem from model via value binding
64
if (!(value instanceof NavigationMenuItem))
65                     {
66                         FacesContext facesContext = FacesContext.getCurrentInstance();
67                         throw new IllegalArgumentException JavaDoc("Value binding of UINavigationMenuItem with id " + child.getClientId(facesContext) + " does not reference an Object of type NavigationMenuItem");
68                     }
69                     item = (NavigationMenuItem)value;
70                 }
71                 else
72                 {
73                     UINavigationMenuItem uiItem = (UINavigationMenuItem)child;
74                     String JavaDoc label = uiItem.getItemLabel();
75                     if (label == null && uiItem.getItemValue() != null)
76                     {
77                         label = uiItem.getItemValue().toString();
78                     }
79                     item = new NavigationMenuItem(uiItem.getItemValue(),
80                                                   label,
81                                                   uiItem.getItemDescription(),
82                                                   uiItem.isItemDisabled() || ! UserRoleUtils.isEnabledOnUserRole(uiItem),
83                                                   uiItem.isRendered(),
84                                                   uiItem.getAction(),
85                                                   uiItem.getIcon(),
86                                                   uiItem.isSplit());
87                 }
88                 list.add(item);
89                 if (child.getChildCount() > 0)
90                 {
91                     List l = getNavigationMenuItemList(child);
92                     item.setNavigationMenuItems((NavigationMenuItem[]) l.toArray(new NavigationMenuItem[l.size()]));
93                 }
94             }
95             else if (child instanceof UISelectItems)
96             {
97                 Object JavaDoc value = ((UISelectItems)child).getValue();
98                 if (value instanceof NavigationMenuItem)
99                 {
100                     list.add(value);
101                 }
102                 else if (value instanceof NavigationMenuItem[])
103                 {
104                     for (int i = 0; i < ((NavigationMenuItem[])value).length; i++)
105                     {
106                         list.add(((NavigationMenuItem[])value)[i]);
107                     }
108                 }
109                 else if (value instanceof Collection)
110                 {
111                     for (Iterator it = ((Collection)value).iterator(); it.hasNext();)
112                     {
113                         Object JavaDoc item = it.next();
114                         if (!(item instanceof NavigationMenuItem))
115                         {
116                             FacesContext facesContext = FacesContext.getCurrentInstance();
117                             throw new IllegalArgumentException JavaDoc("Collection referenced by UINavigationMenuItems with id " + child.getClientId(facesContext) + " does not contain Objects of type NavigationMenuItem");
118                         }
119                         list.add(item);
120                     }
121                 }
122                 else
123                 {
124                     FacesContext facesContext = FacesContext.getCurrentInstance();
125                     throw new IllegalArgumentException JavaDoc("Value binding of UINavigationMenuItems with id " + child.getClientId(facesContext) + " does not reference an Object of type NavigationMenuItem, NavigationMenuItem[], Collection or Map");
126                 }
127             }
128             else
129             {
130                 FacesContext facesContext = FacesContext.getCurrentInstance();
131                 log.error("Invalid child with id " + child.getClientId(facesContext) + "of component with id : "+uiComponent.getClientId(facesContext)
132                         +" : must be UINavigationMenuItem or UINavigationMenuItems, is of type : "+((child==null)?"null":child.getClass().getName()));
133             }
134         }
135
136         return list;
137     }
138
139 }
140
Popular Tags