You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by mm...@apache.org on 2004/12/17 14:19:11 UTC

cvs commit: incubator-myfaces/webapps/examples/web jslistener.jsp

mmarinschek    2004/12/17 05:19:11

  Modified:    src/components/org/apache/myfaces/component/html/util
                        AddResource.java
               src/components/org/apache/myfaces/custom/popup
                        HtmlPopupRenderer.java
               tlds     myfaces_ext.tld
               webapps/examples/src/org/apache/myfaces/examples/resource
                        example_messages.properties
               webapps/examples/web/WEB-INF examples-config.xml
               webapps/examples/web/inc navigation.jsp page_header.jsp
  Added:       src/components/org/apache/myfaces/custom/jslistener/resource
                        JSListener.js
               src/components/org/apache/myfaces/custom/jslistener
                        JsValueChangeListener.java
                        JsValueChangeListenerTag.java
               webapps/examples/web jslistener.jsp
  Log:
  new component jsValueChangeListener
  
  Revision  Changes    Path
  1.15      +6 -2      incubator-myfaces/src/components/org/apache/myfaces/component/html/util/AddResource.java
  
  Index: AddResource.java
  ===================================================================
  RCS file: /home/cvs/incubator-myfaces/src/components/org/apache/myfaces/component/html/util/AddResource.java,v
  retrieving revision 1.14
  retrieving revision 1.15
  diff -u -r1.14 -r1.15
  --- AddResource.java	6 Dec 2004 01:02:02 -0000	1.14
  +++ AddResource.java	17 Dec 2004 13:19:10 -0000	1.15
  @@ -41,6 +41,9 @@
    * @author Sylvain Vieujot (latest modification by $Author$)
    * @version $Revision$ $Date$
    * $Log$
  + * Revision 1.15  2004/12/17 13:19:10  mmarinschek
  + * new component jsValueChangeListener
  + *
    * Revision 1.14  2004/12/06 01:02:02  svieujot
    * Write the response in the log messages (mainly to debug problems due to filters order).
    *
  @@ -259,7 +262,8 @@
           
           InputStream is = getResource(componentName, resourceFileName);
           if( is == null ){
  -            throw new IOException("Unable to find resource "+resourceFileName+" for component "+componentName);
  +            throw new IOException("Unable to find resource "+resourceFileName+" for component "+componentName+
  +                    ". Check that this file is available in the classpath in sub-directory /resource of the component-directory.");
           }
           OutputStream os = response.getOutputStream();
           int c;
  
  
  
  1.1                  incubator-myfaces/src/components/org/apache/myfaces/custom/jslistener/resource/JSListener.js
  
  Index: JSListener.js
  ===================================================================
  function orgApacheMyfacesJsListenerSetExpressionProperty(srcId, destId, property, expression)
  {
      var srcElem = document.getElementById(srcId);
      var destElem = document.getElementById(destId);
  
      expression = expression.replace(/\$srcElem/g,"srcElem");
      expression = expression.replace(/\$destElem/g,"destElem");
  
      var value = eval(expression);
  
      if(property)
      {
          var propertySetStr = "destElem."+property+"='"+value+"';";
  
          eval(propertySetStr);
      }
  }
  
  
  1.1                  incubator-myfaces/src/components/org/apache/myfaces/custom/jslistener/JsValueChangeListener.java
  
  Index: JsValueChangeListener.java
  ===================================================================
  /*
   * Copyright (c) 2004 Your Corporation. All Rights Reserved.
   */
  package org.apache.myfaces.custom.jslistener;
  
  import javax.faces.FacesException;
  import javax.faces.component.StateHolder;
  import javax.faces.component.UIComponentBase;
  import javax.faces.component.ValueHolder;
  import javax.faces.context.FacesContext;
  import javax.faces.convert.Converter;
  import javax.faces.el.ValueBinding;
  import javax.faces.event.AbortProcessingException;
  import javax.faces.event.ActionEvent;
  import javax.faces.event.ActionListener;
  
  /**
   * @author Martin Marinschek (latest modification by $Author: mmarinschek $)
   * @version $Revision: 1.1 $ $Date: 2004/12/17 13:19:10 $
   * $Log: JsValueChangeListener.java,v $
   * Revision 1.1  2004/12/17 13:19:10  mmarinschek
   * new component jsValueChangeListener
   *
   */
  public class JsValueChangeListener
  {
  }
  
  
  
  1.1                  incubator-myfaces/src/components/org/apache/myfaces/custom/jslistener/JsValueChangeListenerTag.java
  
  Index: JsValueChangeListenerTag.java
  ===================================================================
  /*
   * Copyright (c) 2004 Your Corporation. All Rights Reserved.
   */
  package org.apache.myfaces.custom.jslistener;
  
  import org.apache.myfaces.custom.updateactionlistener.UpdateActionListener;
  import org.apache.myfaces.renderkit.html.HTML;
  import org.apache.myfaces.renderkit.JSFAttr;
  import org.apache.myfaces.component.html.util.AddResource;
  import org.apache.commons.logging.impl.Jdk14Logger;
  import org.apache.commons.logging.Log;
  import org.apache.commons.logging.LogFactory;
  
  import javax.faces.application.Application;
  import javax.faces.component.ActionSource;
  import javax.faces.component.UIComponent;
  import javax.faces.component.UINamingContainer;
  import javax.faces.context.FacesContext;
  import javax.faces.convert.Converter;
  import javax.faces.webapp.UIComponentTag;
  import javax.servlet.jsp.JspException;
  import javax.servlet.jsp.tagext.Tag;
  import javax.servlet.jsp.tagext.TagSupport;
  
  /**
   * @author Martin Marinschek (latest modification by $Author: mmarinschek $)
   * @version $Revision: 1.1 $ $Date: 2004/12/17 13:19:10 $
   * $Log: JsValueChangeListenerTag.java,v $
   * Revision 1.1  2004/12/17 13:19:10  mmarinschek
   * new component jsValueChangeListener
   *
   *
   */
  public class JsValueChangeListenerTag
          extends TagSupport
  {
      //private static final Log log = LogFactory.getLog(UpdateActionListenerTag.class);
  
      private String _for;
      private String _property;
      private String _expressionValue;
      private static Log log = LogFactory.getLog(JsValueChangeListenerTag.class);
  
      public JsValueChangeListenerTag()
      {
      }
  
      public void setFor(String aFor)
      {
          _for = aFor;
      }
  
      public void setExpressionValue(String expressionValue)
      {
          _expressionValue = expressionValue;
      }
  
      public void setProperty(String property)
      {
          _property = property;
      }
  
  
      public int doStartTag() throws JspException
      {
          if (_for == null) throw new JspException("for attribute not set");
  
          //Find parent UIComponentTag
          UIComponentTag componentTag = UIComponentTag.getParentUIComponentTag(pageContext);
          if (componentTag == null)
          {
              throw new JspException("ValueChangeListenerTag has no UIComponentTag ancestor");
          }
  
          if (componentTag.getCreated())
          {
  
              AddResource.addJavaScriptToHeader(
                      JsValueChangeListenerTag.class, "JSListener.js", true, getFacesContext());
  
              //Component was just created, so we add the Listener
              UIComponent component = componentTag.getComponentInstance();
  
              if(_for!=null)
              {
                  UIComponent forComponent = component.findComponent(_for);
  
                  String forComponentId = null;
  
                  if (forComponent == null)
                  {
                      if (log.isInfoEnabled())
                      {
                          log.info("Unable to find component '" + _for + "' (calling findComponent on component '" + component.getClientId(getFacesContext()) + "') - will try to render component id based on the parent-id (on same level)");
                      }
                      if (_for.length() > 0 && _for.charAt(0) == UINamingContainer.SEPARATOR_CHAR)
                      {
                          //absolute id path
                          forComponentId = _for.substring(1);
                      }
                      else
                      {
                          //relative id path, we assume a component on the same level as the label component
                          String labelClientId = component.getClientId(getFacesContext());
                          int colon = labelClientId.lastIndexOf(UINamingContainer.SEPARATOR_CHAR);
                          if (colon == -1)
                          {
                              forComponentId = _for;
                          }
                          else
                          {
                              forComponentId = labelClientId.substring(0, colon + 1) + _for;
                          }
                      }
                  }
                  else
                  {
                      forComponentId = forComponent.getClientId(getFacesContext());
                  }
  
                  String expressionValue = _expressionValue.replaceAll("\\'","\\\\'");
                  expressionValue = expressionValue.replaceAll("\"","\\\"");
  
  
                  String methodCall = "orgApacheMyfacesJsListenerSetExpressionProperty('"+
                          component.getClientId(getFacesContext())+"','"+
                          forComponentId+"',"+
                          (_property==null?"null":"'"+_property+"'")+
                          ",'"+expressionValue+"');";
  
  
                  callMethod(component, "onchange",methodCall);
  
              }
          }
  
          return Tag.SKIP_BODY;
      }
  
      private void callMethod(UIComponent uiComponent, String propName, String value)
      {
          Object oldValue = uiComponent.getAttributes().get(propName);
  
          if(oldValue != null)
          {
              String oldValueStr = oldValue.toString().trim();
  
              //check if method call has already been added...
              if(oldValueStr.indexOf(value)!=-1)
                  return;
  
              if(oldValueStr.length()>0 && !oldValueStr.endsWith(";"))
                  oldValueStr +=";";
  
              value = oldValueStr + value;
  
          }
  
          uiComponent.getAttributes().put(propName, value);
      }
  
      protected FacesContext getFacesContext()
      {
          return FacesContext.getCurrentInstance();
      }
  }
  
  
  
  
  
  1.8       +19 -7     incubator-myfaces/src/components/org/apache/myfaces/custom/popup/HtmlPopupRenderer.java
  
  Index: HtmlPopupRenderer.java
  ===================================================================
  RCS file: /home/cvs/incubator-myfaces/src/components/org/apache/myfaces/custom/popup/HtmlPopupRenderer.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- HtmlPopupRenderer.java	3 Dec 2004 20:52:11 -0000	1.7
  +++ HtmlPopupRenderer.java	17 Dec 2004 13:19:10 -0000	1.8
  @@ -31,6 +31,9 @@
    * @author Martin Marinschek (latest modification by $Author$)
    * @version $Revision$ $Date$
    * $Log$
  + * Revision 1.8  2004/12/17 13:19:10  mmarinschek
  + * new component jsValueChangeListener
  + *
    * Revision 1.7  2004/12/03 20:52:11  svieujot
    * Defer script loading for additional performance.
    *
  @@ -167,14 +170,23 @@
   
       private void callMethod(UIComponent uiComponent, String propName, String value)
       {
  -        uiComponent.getAttributes().put(propName, value);
  +        Object oldValue = uiComponent.getAttributes().get(propName);
   
  -        try
  -        {
  -            BeanUtils.setProperty(uiComponent,propName,value);
  -        }
  -        catch (Throwable th)
  +        if(oldValue != null)
           {
  +            String oldValueStr = oldValue.toString().trim();
  +
  +            //check if method call has already been added...
  +            if(oldValueStr.indexOf(value)!=-1)
  +                return;
  +
  +            if(oldValueStr.length()>0 && !oldValueStr.endsWith(";"))
  +                oldValueStr +=";";
  +
  +            value = oldValueStr + value;
  +
           }
  +
  +        uiComponent.getAttributes().put(propName, value);
       }
   }
  
  
  
  1.143     +23 -0     incubator-myfaces/tlds/myfaces_ext.tld
  
  Index: myfaces_ext.tld
  ===================================================================
  RCS file: /home/cvs/incubator-myfaces/tlds/myfaces_ext.tld,v
  retrieving revision 1.142
  retrieving revision 1.143
  diff -u -r1.142 -r1.143
  --- myfaces_ext.tld	13 Dec 2004 22:45:07 -0000	1.142
  +++ myfaces_ext.tld	17 Dec 2004 13:19:11 -0000	1.143
  @@ -488,6 +488,29 @@
           </attribute>
       </tag>
   
  +    <!-- jsValueChangeListener -->
  +    <tag>
  +        <name>jsValueChangeListener</name>
  +        <tag-class>org.apache.myfaces.custom.jslistener.JsValueChangeListenerTag</tag-class>
  +        <body-content>empty</body-content>
  +        <description>Value change listener on client side.</description>
  +        <attribute>
  +          <name>for</name>
  +          <required>false</required>
  +          <rtexprvalue>false</rtexprvalue>
  +        </attribute>
  +        <attribute>
  +          <name>property</name>
  +          <required>false</required>
  +          <rtexprvalue>false</rtexprvalue>
  +        </attribute>
  +        <attribute>
  +          <name>expressionValue</name>
  +          <required>false</required>
  +          <rtexprvalue>false</rtexprvalue>
  +        </attribute>
  +    </tag>
  +
       <!-- checkbox -->
       <tag>
           <name>checkbox</name>
  
  
  
  1.11      +1 -0      incubator-myfaces/webapps/examples/src/org/apache/myfaces/examples/resource/example_messages.properties
  
  Index: example_messages.properties
  ===================================================================
  RCS file: /home/cvs/incubator-myfaces/webapps/examples/src/org/apache/myfaces/examples/resource/example_messages.properties,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- example_messages.properties	2 Dec 2004 22:28:30 -0000	1.10
  +++ example_messages.properties	17 Dec 2004 13:19:11 -0000	1.11
  @@ -21,6 +21,7 @@
   nav_TabbedPane      = Tabbed Pane
   nav_Calendar        = Calendar
   nav_Popup           = Popup
  +nav_JsListener      = JavaScript Listener
   nav_dataList        = Dynamic Lists
   nav_tree            = Tree
   nav_treeTable       = Tree Table
  
  
  
  1.38      +5 -0      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.37
  retrieving revision 1.38
  diff -u -r1.37 -r1.38
  --- examples-config.xml	2 Dec 2004 22:28:31 -0000	1.37
  +++ examples-config.xml	17 Dec 2004 13:19:11 -0000	1.38
  @@ -268,6 +268,11 @@
           </navigation-case>
   
           <navigation-case>
  +            <from-outcome>go_jslistener</from-outcome>
  +            <to-view-id>/jslistener.jsp</to-view-id>
  +        </navigation-case>
  +
  +        <navigation-case>
               <from-outcome>go_date</from-outcome>
               <to-view-id>/date.jsp</to-view-id>
           </navigation-case>
  
  
  
  1.45      +1 -0      incubator-myfaces/webapps/examples/web/inc/navigation.jsp
  
  Index: navigation.jsp
  ===================================================================
  RCS file: /home/cvs/incubator-myfaces/webapps/examples/web/inc/navigation.jsp,v
  retrieving revision 1.44
  retrieving revision 1.45
  diff -u -r1.44 -r1.45
  --- navigation.jsp	2 Dec 2004 22:28:28 -0000	1.44
  +++ navigation.jsp	17 Dec 2004 13:19:11 -0000	1.45
  @@ -21,6 +21,7 @@
               <x:commandNavigation id="nav_2_4_6" value="#{example_messages['nav_TabbedPane']}" action="go_tabbedPane" />
               <x:commandNavigation id="nav_2_4_7" value="#{example_messages['nav_Calendar']}" action="go_calendar" />
               <x:commandNavigation id="nav_2_4_71" value="#{example_messages['nav_Popup']}" action="go_popup" />
  +            <x:commandNavigation id="nav_2_4_72" value="#{example_messages['nav_JsListener']}" action="go_jslistener" />            
               <x:commandNavigation id="nav_2_4_8" value="#{example_messages['nav_Date']}" action="go_date" />
               <x:commandNavigation id="nav_2_4_81" value="#{example_messages['nav_HtmlEditor']}" action="go_htmlEditor" />
               <x:commandNavigation id="nav_2_4_9" value="#{example_messages['nav_dataList']}" action="go_dataList" />
  
  
  
  1.41      +1 -0      incubator-myfaces/webapps/examples/web/inc/page_header.jsp
  
  Index: page_header.jsp
  ===================================================================
  RCS file: /home/cvs/incubator-myfaces/webapps/examples/web/inc/page_header.jsp,v
  retrieving revision 1.40
  retrieving revision 1.41
  diff -u -r1.40 -r1.41
  --- page_header.jsp	30 Nov 2004 08:58:30 -0000	1.40
  +++ page_header.jsp	17 Dec 2004 13:19:11 -0000	1.41
  @@ -33,6 +33,7 @@
                   <x:navigationMenuItem id="nav_2_4_5" itemLabel="#{example_messages['nav_TabbedPane']}" action="go_tabbedPane" icon="images/myfaces.gif" />
                   <x:navigationMenuItem id="nav_2_4_6" itemLabel="#{example_messages['nav_Calendar']}" action="go_calendar" icon="images/myfaces.gif" split="true" />
                   <x:navigationMenuItem id="nav_2_4_6_1" itemLabel="#{example_messages['nav_Popup']}" action="go_popup" icon="images/myfaces.gif" split="true" />
  +                <x:navigationMenuItem id="nav_2_4_6_2" itemLabel="#{example_messages['nav_JsListener']}" action="go_jslistener" icon="images/myfaces.gif" split="true" />                
                   <x:navigationMenuItem id="nav_2_4_7" itemLabel="#{example_messages['nav_dataList']}" action="go_dataList" icon="images/myfaces.gif" />
                   <x:navigationMenuItem id="nav_2_4_8" itemLabel="#{example_messages['nav_tree']}" action="go_tree" icon="images/myfaces.gif" />
                   <x:navigationMenuItem id="nav_2_4_9" itemLabel="#{example_messages['nav_treeTable']}" action="go_treeTable" icon="images/myfaces.gif" />
  
  
  
  1.1                  incubator-myfaces/webapps/examples/web/jslistener.jsp
  
  Index: jslistener.jsp
  ===================================================================
  <%@ page import="java.math.BigDecimal,
                   java.util.Date"%>
  <%@ page session="false" contentType="text/html;charset=utf-8"%>
  <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
  <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
  <%@ taglib uri="http://myfaces.apache.org/extensions" prefix="x"%>
  <html>
  
  <%@include file="inc/head.inc" %>
  
  <!--
  /*
   * 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.
   */
  //-->
  
  <body>
  
  <f:view>
  
      <f:loadBundle basename="org.apache.myfaces.examples.resource.example_messages" var="example_messages"/>
  
      <x:panelLayout id="page" layout="#{globalOptions.pageLayout}"
              styleClass="pageLayout"
              headerClass="pageHeader"
              navigationClass="pageNavigation"
              bodyClass="pageBody"
              footerClass="pageFooter" >
  
          <f:facet name="header">
              <f:subview id="header">
                  <jsp:include page="inc/page_header.jsp" />
              </f:subview>
          </f:facet>
  
          <f:facet name="navigation">
              <f:subview id="menu" >
                  <jsp:include page="inc/navigation.jsp" />
              </f:subview>
          </f:facet>
  
          <f:facet name="body">
  
              <h:panelGroup id="body">
  
                  <h:panelGrid columns="2">
                      <h:outputLabel for="text1" value="Text:"/>
                      <h:inputText id="text1">
                          <x:jsValueChangeListener for="text2" property="value" expressionValue="($srcElem.value=='yes')?'true':'false'" />
                          <x:jsValueChangeListener for="text3" property="value" expressionValue="$srcElem.value" />
                          <x:jsValueChangeListener for="text4" expressionValue="$destElem.innerHTML = $srcElem.value" />
                          <x:jsValueChangeListener for="text5" expressionValue="($srcElem.value=='hide')?$destElem.style.display='none':$destElem.style.display='inline'" />
                      </h:inputText>
                      <h:inputText id="text2"/>
                      <h:inputText id="text3"/>
                      <h:panelGroup id="text4" />
                      <h:panelGroup id="text5" >
                          <h:outputText value="Hide me - enter hide in first input-field."/>
                      </h:panelGroup>
                  </h:panelGrid>
              </h:panelGroup>
          </f:facet>
  
          <%@include file="inc/page_footer.jsp" %>
  
      </x:panelLayout>
  
  </f:view>
  
  </body>
  
  </html>