backport to 2.0.2. fixed remove bug in AutoCompleteTextField. CLK-546

git-svn-id: https://svn.apache.org/repos/asf/incubator/click/branches/click-2.0.x@770719 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/documentation/docs/roadmap-changes.html b/documentation/docs/roadmap-changes.html
index 84ee812..21af95f 100644
--- a/documentation/docs/roadmap-changes.html
+++ b/documentation/docs/roadmap-changes.html
@@ -103,6 +103,12 @@
           [<a target='_blank' href="https://issues.apache.org/click/browse/CLK-499">499</a>].

       </li>

       <li class="change">

+          Fixed a bug in AutoCompleteTextField where if the field was removed

+          from a Form, its JavaScript resources would still be rendered.

+          This issue was raised by Paul Zammit

+          [<a target='_blank' href="https://issues.apache.org/jira/browse/CLK-546">546</a>].

+      </li>

+      <li class="change">

           Fixed a bug in control.js which caused the addLoadEvent event queue to

           be reset if multiple control.js scripts are imported in the same page.

           This issue was raised by Adrian

diff --git a/extras/src/org/apache/click/extras/control/AutoCompleteTextField.java b/extras/src/org/apache/click/extras/control/AutoCompleteTextField.java
index 29f13be..ab06107 100644
--- a/extras/src/org/apache/click/extras/control/AutoCompleteTextField.java
+++ b/extras/src/org/apache/click/extras/control/AutoCompleteTextField.java
@@ -221,6 +221,27 @@
     }

 

     /**

+     * @see org.apache.click.control.Field#setParent(Object)

+     *

+     * @param parent the parent of the Control

+     * @throws IllegalStateException if {@link #name} is not defined

+     * @throws IllegalArgumentException if the given parent instance is

+     * referencing <tt>this</tt> object: <tt>if (parent == this)</tt>

+     */

+    public void setParent(Object parent) {

+        if (parent == null) {

+            // If the field parent control is set to null (indicating the field

+            // is being removed), also remove the field from its parent page

+            Page page = getPage();

+            if (page != null) {

+                page.getControls().remove(this);

+                page.getModel().remove(getName());

+            }

+        }

+        super.setParent(parent);

+    }

+

+    /**

      * Return the HTML CSS and JavaScript includes.

      *

      * @see org.apache.click.Control#getHtmlImports()

@@ -266,12 +287,22 @@
      */

     public void onInit() {

         super.onInit();

+

+        Page page = getPage();

+        if (page == null) {

+            // If parent page is not reachable, exit early

+            return;

+        }

+

         // See whether control has been registered at Page level.

-        Object control = getPage().getModel().get(getName());

+        Object control = page.getModel().get(getName());

 

         // If not registered, then register control

         if (control == null) {

-            getPage().addControl(this);

+            // Ensure current parent control does not change

+            Object parent = getParent();

+            page.addControl(this);

+            setParent(parent);

 

         } else if (!(control instanceof AutoCompleteTextField)) {

             String message =