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.portals.bridges.jsf;
    18
    19  import javax.faces.component.NamingContainer;
    20  import javax.faces.component.UIViewRoot;
    21  import javax.faces.context.FacesContext;
    22  import javax.portlet.RenderResponse;
    23
    24  /***
    25   * A portlet view root that implements a naming container to creates unique
    26   * client ids.
    27   * @author Matthew Bruzek
    28   */
    29  public class PortletUIViewRoot extends UIViewRoot implements NamingContainer
    30  {
    31      /*** A portlet view id constant to prepend to the namespace. */
    32      public static final String VIEW_PREFIX = "view";
    33
    34      private String _namespace;
    35
    36      /*** The default constructor calls the UIViewRoot default constructor.  */
    37      public PortletUIViewRoot()
    38      {
    39          super();
    40      }
    41
    42      /***
    43       * The convenience constructor creates a PortletUIViewRoot from a UIViewRoot.
    44       * @param viewRoot The UIViewRoot to use when creating this object.
    45       */
    46      public PortletUIViewRoot( UIViewRoot viewRoot )
    47      {
    48          setLocale( viewRoot.getLocale() );
    49          setRenderKitId( viewRoot.getRenderKitId() );
    50          setViewId( viewRoot.getViewId() );
    51      }
    52
    53      /***
    54       * Return a string which can be used as output to the response which uniquely
    55       * identifies a component within the current view.
    56       * @param context The FacesContext object for the current request.
    57       */
    58      public String getClientId( FacesContext context )
    59      {
    60          if ( context == null )
    61              throw new NullPointerException( "context can not be null." );
    62
    63          if (_namespace == null && context.getExternalContext().getResponse() instanceof RenderResponse) {
    64              String nameSpace = context.getExternalContext().encodeNamespace( "" );
    65              _namespace = VIEW_PREFIX + nameSpace;
    66          }
    67          return _namespace;
    68      }
    69
    70
    71      public Object saveState(FacesContext context) {
    72          Object values[] = new Object[2];
    73          values[0] = super.saveState(context);
    74          values[1] = _namespace;
    75          return values;
    76      }
    77
    78
    79      public void restoreState(FacesContext context, Object state) {
    80          Object values[] = (Object[])state;
    81          super.restoreState(context, values[0]);
    82          _namespace = (String) values[1];
    83      }
    84  }
    85