myfaces-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mat...@apache.org
Subject cvs commit: incubator-myfaces/src/components/org/apache/myfaces/component/html/util HtmlComponentUtils.java
Date Thu, 13 Jan 2005 09:24:53 GMT
matzew      2005/01/13 01:24:53

  Modified:    src/components/org/apache/myfaces/component/html/ext
                        HtmlInputText.java
               webapps/examples/web/WEB-INF examples-config.xml
               webapps/examples/web forceId.jsp
               src/components/org/apache/myfaces/component/html/util
                        HtmlComponentUtils.java
  Added:       webapps/examples/src/org/apache/myfaces/examples/forceid
                        ForceIdBean.java User.java
  Removed:     webapps/examples/src/org/apache/myfaces/examples/misc
                        ForceIdBean.java
  Log:
  added patch form Sean Schofield. forceId for reuse of "legacy JavaScript" (MyFaces-70)
  
  Revision  Changes    Path
  1.1                  incubator-myfaces/webapps/examples/src/org/apache/myfaces/examples/forceid/ForceIdBean.java
  
  Index: ForceIdBean.java
  ===================================================================
  /*
   * Copyright 2004 The Apache Software Foundation.
   * 
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   * 
   *      http://www.apache.org/licenses/LICENSE-2.0
   * 
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  package org.apache.myfaces.examples.forceid;
  
  /**
   * @author <a href="mailto:matzew@apache.org">Matthias Weßendorf</a> 
   */
  public class ForceIdBean {
      
      private String valueOne, valueTwo;
      private User users[];
      
      public ForceIdBean(){
          users = new User[2];
          users[0] = new User("MyFaces","secrect");
          users[1] = new User("Tomcat","secrect");
      }
  
      public String getValueOne() {
          return valueOne;
      }
      public void setValueOne(String valueOne) {
          this.valueOne = valueOne;
      }
      public String getValueTwo() {
          return valueTwo;
      }
      public void setValueTwo(String valueTwo) {
          this.valueTwo = valueTwo;
      }
      public User[] getUsers() {
          return users;
      }
      public void setUsers(User[] users) {
          this.users = users;
      }
  }
  
  
  
  1.1                  incubator-myfaces/webapps/examples/src/org/apache/myfaces/examples/forceid/User.java
  
  Index: User.java
  ===================================================================
  /*
   * Copyright 2004 The Apache Software Foundation.
   * 
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   * 
   *      http://www.apache.org/licenses/LICENSE-2.0
   * 
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  package org.apache.myfaces.examples.forceid;
  
  import org.apache.commons.logging.Log;
  import org.apache.commons.logging.LogFactory;
  import org.apache.myfaces.application.jsp.JspStateManagerImpl;
  
  /**
   * @author <a href="mailto:matzew@apache.org">Matthias Weßendorf</a> 
   */
  public class User {
      
      public String username,password;
      private static final Log log = LogFactory.getLog(User.class);
      
      public User(){}
      
      public User(String username,String password){
          this.username = username;
          this.password = password;
      }
  
      public String getPassword() {
          return password;
      }
      public void setPassword(String password) {
          this.password = password;
      }
      public String getUsername() {
          return username;
      }
      public void setUsername(String username) {
          this.username = username;
      }
      
      public String update(){
          if(log.isDebugEnabled()){
              log.debug("USERNAME: "+this.username);
              log.debug("PASSWORD: "+this.password);
          }
          return ("go_forceId");
      }
  }
  
  
  
  1.6       +6 -7      incubator-myfaces/src/components/org/apache/myfaces/component/html/ext/HtmlInputText.java
  
  Index: HtmlInputText.java
  ===================================================================
  RCS file: /home/cvs/incubator-myfaces/src/components/org/apache/myfaces/component/html/ext/HtmlInputText.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- HtmlInputText.java	10 Jan 2005 08:08:14 -0000	1.5
  +++ HtmlInputText.java	13 Jan 2005 09:24:53 -0000	1.6
  @@ -26,6 +26,9 @@
    * @author Manfred Geiler (latest modification by $Author$)
    * @version $Revision$ $Date$
    * $Log$
  + * Revision 1.6  2005/01/13 09:24:53  matzew
  + * added patch form Sean Schofield. forceId for reuse of "legacy JavaScript" (MyFaces-70)
  + *
    * Revision 1.5  2005/01/10 08:08:14  matzew
    * added patch form sean schofield. forceId for reuse of "legacy JavaScript" (MyFaces-70)
    *
  @@ -51,11 +54,7 @@
       
       public String getClientId(FacesContext context)
       {
  -        if (clientId == null) 
  -        {
  -            clientId = HtmlComponentUtils.getClientId(this, getRenderer(context), context);
  -        }
  -        return clientId;
  +        return HtmlComponentUtils.getClientId(this, getRenderer(context), context);
       }
       
       //------------------ GENERATED CODE BEGIN (do not modify!) --------------------
  
  
  
  1.43      +1 -1      incubator-myfaces/webapps/examples/web/WEB-INF/examples-config.xml
  
  Index: examples-config.xml
  ===================================================================
  RCS file: /home/cvs/incubator-myfaces/webapps/examples/web/WEB-INF/examples-config.xml,v
  retrieving revision 1.42
  retrieving revision 1.43
  diff -u -r1.42 -r1.43
  --- examples-config.xml	10 Jan 2005 08:08:15 -0000	1.42
  +++ examples-config.xml	13 Jan 2005 09:24:53 -0000	1.43
  @@ -86,7 +86,7 @@
   
       <managed-bean>
           <managed-bean-name>forceIdBean</managed-bean-name>
  -        <managed-bean-class>org.apache.myfaces.examples.misc.ForceIdBean</managed-bean-class>
  +        <managed-bean-class>org.apache.myfaces.examples.forceid.ForceIdBean</managed-bean-class>
           <managed-bean-scope>session</managed-bean-scope>
       </managed-bean>
   
  
  
  
  1.2       +12 -1     incubator-myfaces/webapps/examples/web/forceId.jsp
  
  Index: forceId.jsp
  ===================================================================
  RCS file: /home/cvs/incubator-myfaces/webapps/examples/web/forceId.jsp,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- forceId.jsp	10 Jan 2005 08:08:18 -0000	1.1
  +++ forceId.jsp	13 Jan 2005 09:24:53 -0000	1.2
  @@ -69,13 +69,24 @@
   
                       <h:outputText id="forceTwoOutput" value="#{example_messages['forceTwo']}"/>
                       <x:inputText id="forceTwo" value="#{forceIdBean.valueTwo}" forceId="true"/>
  -                    <h:message id="forceTwoMessage" for="forceTwo" styleClass="error"
/>
   
   				<h:panelGroup/>
   			  	<h:commandButton id="button" value="#{example_messages['button_submit']}" action="go_home"/>
                       <h:panelGroup/>
   
   			    </h:panelGrid>
  +			</h:form>
  +			
  +			<h:form id="dataTable">
  +			<h:dataTable value="#{forceIdBean.users}" var="user">
  +		    <h:column>
  +			<h:outputText value="Username"/>
  +      		<x:inputText id="username" value="#{user.username}" forceId="true"/>
  +			<h:outputText value="Password"/>
  +      		<x:inputText id="passwd" value="#{user.password}" forceId="true"/>
  +			<h:commandButton value="update" action="#{user.update}"/>
  +   			</h:column>
  +			</h:dataTable>
   			</h:form>
   
               </h:panelGroup>
  
  
  
  1.2       +85 -18    incubator-myfaces/src/components/org/apache/myfaces/component/html/util/HtmlComponentUtils.java
  
  Index: HtmlComponentUtils.java
  ===================================================================
  RCS file: /home/cvs/incubator-myfaces/src/components/org/apache/myfaces/component/html/util/HtmlComponentUtils.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- HtmlComponentUtils.java	10 Jan 2005 08:08:19 -0000	1.1
  +++ HtmlComponentUtils.java	13 Jan 2005 09:24:53 -0000	1.2
  @@ -17,6 +17,8 @@
   
   import org.apache.myfaces.renderkit.JSFAttr;
   import javax.faces.component.UIComponent;
  +import javax.faces.component.NamingContainer;
  +import javax.faces.component.UIData;
   import javax.faces.context.FacesContext;
   import javax.faces.render.Renderer;
   import java.util.HashMap;
  @@ -25,8 +27,8 @@
    * <p>Utility class for providing basic functionality to the HTML faces 
    * extended components.<p>
    * 
  - * @author not attributable
  - * @version 
  + * @author Sean Schofield
  + * @version $Revision$ $Date$
    */
   public class HtmlComponentUtils 
   {
  @@ -39,9 +41,20 @@
       private HtmlComponentUtils() 
       {}
       
  -    public static String getClientId(UIComponent component,
  -                                     Renderer renderer,
  -                                     FacesContext context)
  +    /**
  +     * Gets the client id associated with the component.  Checks the forceId 
  +     * attribute of the component (if present) and uses the orginally supplied 
  +     * id value if that attribute is true.  Also performs the required call 
  +     * to <code>convertClientId</code> on the {@link Renderer} argument.
  +     * 
  +     * @param component The component for which the client id is needed.
  +     * @param renderer The renderer associated with the component.
  +     * @param context Additional context information to help in the request.
  +     * @return The clientId to use with the specified component.
  +     */
  +    public static String getClientId(UIComponent component, 
  +                                        Renderer renderer, 
  +                                        FacesContext context)
       {
           // see if the originally supplied id should be used 
           Boolean forceValue = (Boolean)component.getAttributes().get(JSFAttr.FORCE_ID_ATTR);
  @@ -49,25 +62,30 @@
           
           if (forceId && component.getId() != null)
           {
  -            /** @todo handle "indexed" data values */            
               String clientId = component.getId();
               
  +            /**
  +             * See if there is a parent naming container.  If there is ...
  +             */
  +            UIComponent parentContainer = HtmlComponentUtils.findParentNamingContainer(component,
false);
  +            if (parentContainer != null)
  +            {
  +                if (parentContainer instanceof UIData)
  +                {
  +                    int rowIndex = ((UIData)parentContainer).getRowIndex();
  +                    if (rowIndex != -1)
  +                    {
  +                        clientId = clientId + "[" + rowIndex + "]";
  +                    }
  +                }
  +            }
  +            
               // JSF spec requires that renderer get a chance to convert the id
               if (renderer != null)
               {
                   clientId = renderer.convertClientId(context, clientId);
               }
               
  -            // avoid having duplicate id's 
  -            HashMap idMap = (HashMap)context.getViewRoot().getAttributes().get(KEY_COMPONENT_ID_MAP);
  -            
  -            if (idMap == null)
  -            {
  -                idMap = new HashMap();
  -                context.getViewRoot().getAttributes().
  -                    put(KEY_COMPONENT_ID_MAP, idMap);
  -            }
  -
               /**
                * Since components that use this utility are intended to be 
                * rendered in HTML they should conform to the XHTML standard 
  @@ -75,15 +93,27 @@
                * Right now this is pretty much impossible to enforce in the 
                * appropriate Renderer class so its being done here.
                */
  +            HashMap idMap = (HashMap)context.getViewRoot().getAttributes().get(KEY_COMPONENT_ID_MAP);
  +
  +            if (idMap == null)
  +            {
  +                idMap = new HashMap();
  +                context.getViewRoot().getAttributes().put(KEY_COMPONENT_ID_MAP, idMap);
  +            }
  +
               if (idMap.containsKey(clientId))
               {
                   UIComponent mappedComponent = (UIComponent)idMap.get(clientId);
                   if (component.equals(mappedComponent))
                   {
  -                    throw new IllegalArgumentException("Duplicate cliientId not allowed.
 View already contains the id: " + 
  -                        clientId);
  +                    throw new IllegalArgumentException(
  +                        "Duplicate cliientId not allowed.  View already contains the id:
" + clientId);
                   }
               }
  +            else
  +            {
  +                idMap.put(clientId, component);
  +            }
               
               return clientId;
           }
  @@ -92,4 +122,41 @@
               return component.getClientId(context);
           }
       }
  +    
  +    /**
  +     * Locates the {@link NamingContainer} associated with the givem 
  +     * {@link UIComponent}.
  +     * 
  +     * @param component The component whose naming locator needs to be found.
  +     * @param returnRootIfNotFound Whether or not the root should be returned 
  +     *    if no naming container is found.
  +     * @return The parent naming container (or root if applicable).
  +     */
  +    public static UIComponent findParentNamingContainer(UIComponent component, 
  +        boolean returnRootIfNotFound)
  +    {
  +        UIComponent parent = component.getParent();
  +        if (returnRootIfNotFound && parent == null)
  +        {
  +            return component;
  +        }
  +        while (parent != null)
  +        {
  +            if (parent instanceof NamingContainer) return parent;
  +            if (returnRootIfNotFound)
  +            {
  +                UIComponent nextParent = parent.getParent();
  +                if (nextParent == null)
  +                {
  +                    return parent;  //Root
  +                }
  +                parent = nextParent;
  +            }
  +            else
  +            {
  +                parent = parent.getParent();
  +            }
  +        }
  +        return null;
  +    }    
   }
  
  
  

Mime
View raw message