View Javadoc

1   /*
2    * Copyright 2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5    * use this file except in compliance with the License. You may obtain a copy of
6    * 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, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations under
14   * the License.
15   */
16  package org.apache.portals.bridges.jsf;
17  
18  import javax.faces.component.NamingContainer;
19  import javax.faces.component.UIViewRoot;
20  import javax.faces.context.FacesContext;
21  
22  /***
23   * A portlet view root that implements a naming container to creates unique 
24   * client ids.
25   * @author Matthew Bruzek
26   */
27  public class PortletUIViewRoot extends UIViewRoot implements NamingContainer
28  {
29    /*** A portlet view id constant to prepend to the namespace. */
30    public static final String VIEW_PREFIX = "view";
31    
32    /*** The default constructor calls the UIViewRoot default constructor.  */
33    public PortletUIViewRoot()
34    {
35      super();
36    }
37  
38    /***
39     * The convenience constructor creates a PortletUIViewRoot from a UIViewRoot.
40     * @param viewRoot The UIViewRoot to use when creating this object.
41     */
42    public PortletUIViewRoot( UIViewRoot viewRoot )
43    {
44      setLocale( viewRoot.getLocale() );
45      setRenderKitId( viewRoot.getRenderKitId() );
46      setViewId( viewRoot.getViewId() );
47    }
48  
49    /***
50     * Return a string which can be used as output to the response which uniquely 
51     * identifies a component within the current view.
52     * @param context The FacesContext object for the current request.
53     */
54    public String getClientId( FacesContext context )
55    {
56      if ( context == null )
57        throw new NullPointerException( "context can not be null." );
58      
59      String nameSpace = context.getExternalContext().encodeNamespace( "" );
60      return VIEW_PREFIX + nameSpace;
61    }
62  }