View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.custom.navigation;
20  
21  import java.io.IOException;
22  import java.util.Iterator;
23  import java.util.List;
24  
25  import javax.faces.component.UIComponent;
26  import javax.faces.component.UIViewRoot;
27  import javax.faces.context.FacesContext;
28  
29  import org.apache.commons.logging.Log;
30  import org.apache.commons.logging.LogFactory;
31  import org.apache.myfaces.component.AlignProperty;
32  import org.apache.myfaces.component.DataProperties;
33  import org.apache.myfaces.component.EventAware;
34  import org.apache.myfaces.component.PanelProperties;
35  import org.apache.myfaces.component.UniversalProperties;
36  import org.apache.myfaces.component.html.ext.HtmlPanelGroup;
37  
38  /**
39   * 
40   * Renders a vertical menu structure with support for nested menu 
41   * items. Unless otherwise specified, all attributes accept 
42   * static values or EL expressions.
43   * 
44   * Panel, that includes navigation items ({@link HtmlCommandNavigation}) and other
45   * components (separators).
46   * 
47   * @JSFComponent
48   *   name = "t:panelNavigation"
49   *   class = "org.apache.myfaces.custom.navigation.HtmlPanelNavigation"
50   *   tagClass = "org.apache.myfaces.custom.navigation.HtmlPanelNavigationTag"
51   * @since 1.1.7
52   * @author Manfred Geiler (latest modification by $Author: lu4242 $)
53   * @version $Revision: 691856 $ $Date: 2008-09-03 21:40:30 -0500 (Wed, 03 Sep 2008) $
54   */
55  public abstract class AbstractHtmlPanelNavigation
56          extends HtmlPanelGroup implements AlignProperty,
57          UniversalProperties, EventAware, DataProperties, PanelProperties
58  {
59      private static final Log log = LogFactory.getLog(AbstractHtmlPanelNavigation.class);
60  
61      public static final String COMPONENT_TYPE = "org.apache.myfaces.HtmlPanelNavigation";
62      public static final String COMPONENT_FAMILY = "javax.faces.Panel";
63      private static final String DEFAULT_RENDERER_TYPE = "org.apache.myfaces.Navigation";
64  
65      private static final int DEFAULT_BORDER = Integer.MIN_VALUE;
66  
67      private static final String PREVIOUS_VIEW_ROOT = AbstractHtmlPanelNavigation.class.getName() + ".PREVIOUS_VIEW_ROOT";
68      private boolean _itemOpenActiveStatesRestored = false;
69  
70      public void decode(FacesContext context)
71      {
72          super.decode(context);    //To change body of overridden methods use File | Settings | File Templates.
73          
74          //Save the current view root for later reference...
75          context.getExternalContext().getRequestMap().put(PREVIOUS_VIEW_ROOT, context.getViewRoot());
76          //...and remember that this instance needs NO special treatment on rendering:
77          _itemOpenActiveStatesRestored = true;
78      }
79  
80      public void encodeBegin(FacesContext context) throws IOException
81      {
82          if (!_itemOpenActiveStatesRestored && getChildCount() > 0)
83          {
84              UIViewRoot previousRoot = (UIViewRoot)context.getExternalContext().getRequestMap().get(PREVIOUS_VIEW_ROOT);
85              if (previousRoot != null)
86              {
87                  restoreOpenActiveStates(context, previousRoot, getChildren());
88              }
89              else
90              {
91                  //no previous root, means no decode was done
92                  //--> a new request
93              }
94          }
95          
96          super.encodeBegin(context);    //To change body of overridden methods use File | Settings | File Templates.
97      }
98      
99      public void restoreOpenActiveStates(FacesContext facesContext,
100                                         UIViewRoot previousRoot,
101                                         List children)
102     {
103         for (Iterator it = children.iterator(); it.hasNext(); )
104         {
105             UIComponent child = (UIComponent)it.next();
106             if (child instanceof HtmlCommandNavigation)
107             {
108                 HtmlCommandNavigation previousItem = (HtmlCommandNavigation)previousRoot.findComponent(child.getClientId(facesContext));
109                 if (previousItem != null)
110                 {
111 
112                     HtmlCommandNavigation childItem = (HtmlCommandNavigation)child;
113                     if(previousItem.getOpenDirectly()!=null)
114                     {
115                         childItem.setOpen(previousItem.isOpen());
116                     }
117                     else if(previousItem.getValueBinding("open")!=null)
118                     {
119                         childItem.setValueBinding("open",previousItem.getValueBinding("open"));
120                     }
121 
122                     if(previousItem.getActiveDirectly()!=null)
123                     {
124                         childItem.setActive(previousItem.isActive());
125                     }
126                     else if(previousItem.getValueBinding("active")!=null)
127                     {
128                         childItem.setValueBinding("active",previousItem.getValueBinding("active"));
129                     }
130                 }
131                 else
132                 {
133                     log.error("Navigation item " + child.getClientId(facesContext) + " not found in previous view.");
134                 }
135                 if (child.getChildCount() > 0)
136                 {
137                     restoreOpenActiveStates(facesContext, previousRoot, child.getChildren());
138                 }
139             }
140         }
141     }
142             
143     /**
144      * The CSS class of closed navigation items.
145      * 
146      * @JSFProperty
147      */
148     public abstract String getItemClass();
149 
150     /**
151      * The CSS class of open navigation items.
152      * 
153      * @JSFProperty
154      */
155     public abstract String getOpenItemClass();
156 
157     /**
158      * The CSS class of the active navigation item.
159      * 
160      * @JSFProperty
161      */
162     public abstract String getActiveItemClass();
163 
164     /**
165      * The CSS class for the td element of a separator.
166      * 
167      * @JSFProperty
168      */
169     public abstract String getSeparatorClass();
170 
171     /**
172      * The CSS Style of closed navigation items.
173      * 
174      * @JSFProperty
175      */
176     public abstract String getItemStyle();
177 
178     /**
179      * The CSS Style of open navigation items.
180      * 
181      * @JSFProperty
182      */
183     public abstract String getOpenItemStyle();
184 
185     /**
186      * The CSS Style of the active navigation item.
187      * 
188      * @JSFProperty
189      */
190     public abstract String getActiveItemStyle();
191 
192     /**
193      * The CSS Style for the td element of a separator.
194      * 
195      * @JSFProperty
196      */
197     public abstract String getSeparatorStyle();
198 
199 }