Author: janne
Date: Wed May  9 11:13:58 2007
New Revision: 536609

URL: http://svn.apache.org/viewvc?view=rev&rev=536609
Log:
modified OnChangeAjaxBehavior so it can be used to track changes on all 
FormComponents.

plus added the tests.

Added:
    
incubator/wicket/trunk/jdk-1.4/wicket/src/test/java/org/apache/wicket/ajax/form/
    
incubator/wicket/trunk/jdk-1.4/wicket/src/test/java/org/apache/wicket/ajax/form/OnChangeAjaxBehaviorTest.java
    
incubator/wicket/trunk/jdk-1.4/wicket/src/test/java/org/apache/wicket/ajax/form/OnChangeAjaxBehaviorTestPage.html
    
incubator/wicket/trunk/jdk-1.4/wicket/src/test/java/org/apache/wicket/ajax/form/OnChangeAjaxBehaviorTestPage.java
    
incubator/wicket/trunk/jdk-1.4/wicket/src/test/java/org/apache/wicket/ajax/form/OnChangeAjaxBehaviorTestPage_expected.html
Modified:
    
incubator/wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/ajax/form/OnChangeAjaxBehavior.java

Modified: 
incubator/wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/ajax/form/OnChangeAjaxBehavior.java
URL: 
http://svn.apache.org/viewvc/incubator/wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/ajax/form/OnChangeAjaxBehavior.java?view=diff&rev=536609&r1=536608&r2=536609
==============================================================================
--- 
incubator/wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/ajax/form/OnChangeAjaxBehavior.java
 (original)
+++ 
incubator/wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/ajax/form/OnChangeAjaxBehavior.java
 Wed May  9 11:13:58 2007
@@ -16,25 +16,18 @@
  */
 package org.apache.wicket.ajax.form;
 
+import org.apache.wicket.Component;
 import org.apache.wicket.Response;
-import org.apache.wicket.WicketRuntimeException;
+import org.apache.wicket.markup.ComponentTag;
 import org.apache.wicket.markup.html.form.AbstractTextComponent;
 import org.apache.wicket.util.string.JavascriptUtils;
 
 /**
- * A behavior that updates the hosting [EMAIL PROTECTED] 
AbstractTextComponent} (typically
- * [EMAIL PROTECTED] TextField} or [EMAIL PROTECTED] TextArea}) via ajax when 
an onkeyup javascript
- * event is triggered. 
+ * A behavior that updates the hosting [EMAIL PROTECTED] FormComponent} via 
ajax when 
+ * value of the component is changed.
  * 
- * Opposed to [EMAIL PROTECTED] AjaxFormComponentUpdatingBehavior} with 
onkeyup event 
- * this behavior ignores events which do not really change the content of the 
- * component (like esc, cursor keys, tabs etc.), which reduces the the amount 
- * of extra requests that will be sent to the server. Also, opposed to 
- * [EMAIL PROTECTED] AjaxFormComponentUpdatingBehavior} with onchange/onblur 
event
- * this behavior sends the request immediately after the keypress.
- * 
- * If you need similiar behavior to other (non-text) form components, use 
- * [EMAIL PROTECTED] AjaxFormComponentUpdatingBehavior} with onchange event.
+ * This behavior uses best available method to track changes on different 
+ * types of form components. 
  * 
  * @author Janne Hietamäki (janne)
  * 
@@ -51,27 +44,36 @@
                super("onkeyup");
        }
 
-       /**
-        * 
-        * @see org.apache.wicket.behavior.AbstractAjaxBehavior#onBind()
-        */
-       protected void onBind()
+       protected final void onComponentTag(final ComponentTag tag)
        {
-               super.onBind();
+               super.onComponentTag(tag);
+               if (this.getComponent().isEnabled())
+               {
+                       tag.put(getEventForComponent(getComponent()), 
getEventHandler());
+               }
+       }
 
-               if (!(getComponent() instanceof AbstractTextComponent))
+       private String getEventForComponent(Component component)
+       {
+               if (component instanceof AbstractTextComponent)
+               {
+                       return "onkeyup";
+               }
+               else
                {
-                       throw new WicketRuntimeException("Behavior " + 
getClass().getName()
-                                       + " can only be added to an instance of 
a AbstractTextComponent");
+                       return "onchange";
                }
        }
 
-       protected void onComponentRendered()
+       protected final void onComponentRendered()
        {
-               Response response = getComponent().getResponse();
-               final String id = getComponent().getMarkupId();
-               response.write(JavascriptUtils.SCRIPT_OPEN_TAG);
-               response.write("new Wicket.ChangeHandler('" + id + "');");
-               response.write(JavascriptUtils.SCRIPT_CLOSE_TAG);
+               if (getComponent() instanceof AbstractTextComponent)
+               {
+                       Response response = getComponent().getResponse();
+                       final String id = getComponent().getMarkupId();
+                       response.write(JavascriptUtils.SCRIPT_OPEN_TAG);
+                       response.write("new Wicket.ChangeHandler('" + id + 
"');");
+                       response.write(JavascriptUtils.SCRIPT_CLOSE_TAG);
+               }
        }
 }

Added: 
incubator/wicket/trunk/jdk-1.4/wicket/src/test/java/org/apache/wicket/ajax/form/OnChangeAjaxBehaviorTest.java
URL: 
http://svn.apache.org/viewvc/incubator/wicket/trunk/jdk-1.4/wicket/src/test/java/org/apache/wicket/ajax/form/OnChangeAjaxBehaviorTest.java?view=auto&rev=536609
==============================================================================
--- 
incubator/wicket/trunk/jdk-1.4/wicket/src/test/java/org/apache/wicket/ajax/form/OnChangeAjaxBehaviorTest.java
 (added)
+++ 
incubator/wicket/trunk/jdk-1.4/wicket/src/test/java/org/apache/wicket/ajax/form/OnChangeAjaxBehaviorTest.java
 Wed May  9 11:13:58 2007
@@ -0,0 +1,35 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.wicket.ajax.form;
+
+import org.apache.wicket.WicketTestCase;
+
+/**
+ * @author Janne Hietamäki (janne)
+ */
+public class OnChangeAjaxBehaviorTest extends WicketTestCase
+{
+       /**
+        * @throws Exception
+        */
+       public void testRendering() throws Exception
+       {
+               executeTest(OnChangeAjaxBehaviorTestPage.class,
+                               "OnChangeAjaxBehaviorTestPage_expected.html");
+       }
+
+}

Added: 
incubator/wicket/trunk/jdk-1.4/wicket/src/test/java/org/apache/wicket/ajax/form/OnChangeAjaxBehaviorTestPage.html
URL: 
http://svn.apache.org/viewvc/incubator/wicket/trunk/jdk-1.4/wicket/src/test/java/org/apache/wicket/ajax/form/OnChangeAjaxBehaviorTestPage.html?view=auto&rev=536609
==============================================================================
--- 
incubator/wicket/trunk/jdk-1.4/wicket/src/test/java/org/apache/wicket/ajax/form/OnChangeAjaxBehaviorTestPage.html
 (added)
+++ 
incubator/wicket/trunk/jdk-1.4/wicket/src/test/java/org/apache/wicket/ajax/form/OnChangeAjaxBehaviorTestPage.html
 Wed May  9 11:13:58 2007
@@ -0,0 +1,10 @@
+<html>
+       <body>
+               <form wicket:id="form">
+                       <input type="text" wicket:id="field"/>
+       
+                       <select wicket:id="dropDown">
+                       </select>
+               </form>                 
+       </body>
+</html>
\ No newline at end of file

Added: 
incubator/wicket/trunk/jdk-1.4/wicket/src/test/java/org/apache/wicket/ajax/form/OnChangeAjaxBehaviorTestPage.java
URL: 
http://svn.apache.org/viewvc/incubator/wicket/trunk/jdk-1.4/wicket/src/test/java/org/apache/wicket/ajax/form/OnChangeAjaxBehaviorTestPage.java?view=auto&rev=536609
==============================================================================
--- 
incubator/wicket/trunk/jdk-1.4/wicket/src/test/java/org/apache/wicket/ajax/form/OnChangeAjaxBehaviorTestPage.java
 (added)
+++ 
incubator/wicket/trunk/jdk-1.4/wicket/src/test/java/org/apache/wicket/ajax/form/OnChangeAjaxBehaviorTestPage.java
 Wed May  9 11:13:58 2007
@@ -0,0 +1,62 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.wicket.ajax.form;
+
+import org.apache.wicket.ajax.AjaxRequestTarget;
+import org.apache.wicket.markup.html.WebPage;
+import org.apache.wicket.markup.html.form.DropDownChoice;
+import org.apache.wicket.markup.html.form.Form;
+import org.apache.wicket.markup.html.form.TextField;
+
+/**
+ * @author Janne Hietam&auml;ki
+ */
+public class OnChangeAjaxBehaviorTestPage extends WebPage
+{
+
+       /**
+        * Construct.
+        */
+       public OnChangeAjaxBehaviorTestPage()
+       {
+               Form form = new Form("form");
+               add(form);
+
+               TextField field = new TextField("field");
+               field.add(new OnChangeAjaxBehavior(){
+                       private static final long serialVersionUID = 1L;
+
+                       protected void onUpdate(AjaxRequestTarget target)
+                       {
+                       }
+                       
+               });
+               form.add(field);
+
+               DropDownChoice choice = new DropDownChoice("dropDown");
+               choice.add(new OnChangeAjaxBehavior(){
+                       private static final long serialVersionUID = 1L;
+
+                       protected void onUpdate(AjaxRequestTarget target)
+                       {
+                       }
+                       
+               });
+               form.add(choice);
+       }
+
+}

Added: 
incubator/wicket/trunk/jdk-1.4/wicket/src/test/java/org/apache/wicket/ajax/form/OnChangeAjaxBehaviorTestPage_expected.html
URL: 
http://svn.apache.org/viewvc/incubator/wicket/trunk/jdk-1.4/wicket/src/test/java/org/apache/wicket/ajax/form/OnChangeAjaxBehaviorTestPage_expected.html?view=auto&rev=536609
==============================================================================
--- 
incubator/wicket/trunk/jdk-1.4/wicket/src/test/java/org/apache/wicket/ajax/form/OnChangeAjaxBehaviorTestPage_expected.html
 (added)
+++ 
incubator/wicket/trunk/jdk-1.4/wicket/src/test/java/org/apache/wicket/ajax/form/OnChangeAjaxBehaviorTestPage_expected.html
 Wed May  9 11:13:58 2007
@@ -0,0 +1,21 @@
+<html>
+       <head><script type="text/javascript" 
src="resources/org.apache.wicket.markup.html.WicketEventReference/wicket-event.js"></script>
+<script type="text/javascript" 
src="resources/org.apache.wicket.ajax.AbstractDefaultAjaxBehavior/wicket-ajax.js"></script>
+<script type="text/javascript" 
id="wicket-ajax-debug-enable"><!--/*--><![CDATA[/*><!--*/
+wicketAjaxDebugEnable=true;
+/*-->]]>*/</script>
+
+<script type="text/javascript" 
src="resources/org.apache.wicket.ajax.AbstractDefaultAjaxBehavior/wicket-ajax-debug.js"></script>
+</head><body>
+               <form id="form0" 
action="?wicket:interface=:0:form::IFormSubmitListener:" method="post" 
wicket:id="form"><div style="display:none"><input type="hidden" 
name="form0_hf_0" id="form0_hf_0" /></div>
+                       <input id="field1" name="field" value="" onkeyup="var 
wcall=wicketAjaxPost('?wicket:interface=:0:form:field::IBehaviorListener:0', 
wicketSerialize(Wicket.$('field1')), function() { }.bind(this), function() { 
}.bind(this));" type="text" wicket:id="field"/><script 
type="text/javascript"><!--/*--><![CDATA[/*><!--*/
+new Wicket.ChangeHandler('field1');
+/*-->]]>*/</script>
+
+       
+                       <select id="dropDown2" name="dropDown" onkeyup="var 
wcall=wicketAjaxPost('?wicket:interface=:0:form:dropDown::IBehaviorListener:0', 
wicketSerialize(Wicket.$('dropDown2')), function() { }.bind(this), function() { 
}.bind(this));" onchange="var 
wcall=wicketAjaxPost('?wicket:interface=:0:form:dropDown::IBehaviorListener:0', 
wicketSerialize(Wicket.$('dropDown2')), function() { }.bind(this), function() { 
}.bind(this));" wicket:id="dropDown">
+<option selected="selected" value="">Choose One</option>
+</select>
+               </form>                 
+       </body>
+</html>
\ No newline at end of file


Reply via email to