You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@click.apache.org by sa...@apache.org on 2010/05/01 16:44:37 UTC

svn commit: r940043 - in /click/trunk/click/extras/src/org/apache/click/extras: control/AbstractContainerField.java panel/TabbedPanel.java

Author: sabob
Date: Sat May  1 14:44:36 2010
New Revision: 940043

URL: http://svn.apache.org/viewvc?rev=940043&view=rev
Log:
Added support for replacing a control if another control already exists with matching name. CLK-666

Modified:
    click/trunk/click/extras/src/org/apache/click/extras/control/AbstractContainerField.java
    click/trunk/click/extras/src/org/apache/click/extras/panel/TabbedPanel.java

Modified: click/trunk/click/extras/src/org/apache/click/extras/control/AbstractContainerField.java
URL: http://svn.apache.org/viewvc/click/trunk/click/extras/src/org/apache/click/extras/control/AbstractContainerField.java?rev=940043&r1=940042&r2=940043&view=diff
==============================================================================
--- click/trunk/click/extras/src/org/apache/click/extras/control/AbstractContainerField.java (original)
+++ click/trunk/click/extras/src/org/apache/click/extras/control/AbstractContainerField.java Sat May  1 14:44:36 2010
@@ -54,7 +54,7 @@ import org.apache.click.util.HtmlStringB
  *     }
  * } </pre>
  */
-public abstract class AbstractContainerField extends Field implements Container {
+public class AbstractContainerField extends Field implements Container {
 
     // Constants --------------------------------------------------------------
 
@@ -100,21 +100,59 @@ public abstract class AbstractContainerF
     /**
      * @see org.apache.click.control.Container#add(org.apache.click.Control).
      *
+     * <b>Please note</b>: if the container contains a control with the same name
+     * as the given control, that control will be
+     * {@link #replace(org.apache.click.Control, org.apache.click.Control) replaced}
+     * by the given control. If a control has no name defined it cannot be replaced.
+     *
      * @param control the control to add to the container and return
      * @return the control that was added to the container
+     * @throws IllegalArgumentException if the control is null
      */
     public Control add(Control control) {
         return insert(control, getControls().size());
     }
 
     /**
+     * Add the control to the container at the specified index, and return the
+     * added instance.
+     * <p/>
+     * <b>Please note</b>: if the container contains a control with the same name
+     * as the given control, that control will be
+     * {@link #replace(org.apache.click.Control, org.apache.click.Control) replaced}
+     * by the given control. If a control has no name defined it cannot be replaced.
+     *
      * @see org.apache.click.control.Container#insert(org.apache.click.Control, int).
      *
      * @param control the control to add to the container and return
      * @param index the index at which the control is to be inserted
      * @return the control that was added to the container
+     *
+     * @throws IllegalArgumentException if the control is null or if the control
+     * and container is the same instance
      */
     public Control insert(Control control, int index) {
+        // Check if panel already contains the control
+        String controlName = control.getName();
+        if (controlName != null) {
+            // Check if container already contains the control
+            Control currentControl = getControlMap().get(control.getName());
+
+            // If container already contains the control do a replace
+            if (currentControl != null) {
+
+                // Current control and new control are referencing the same object
+                // so we exit early
+                if (currentControl == control) {
+                    return control;
+                }
+
+                // If the two controls are different objects, we remove the current
+                // control and add the given control
+                return replace(currentControl, control);
+            }
+        }
+
         return ContainerUtils.insert(this, control, index, getControlMap());
     }
 
@@ -129,6 +167,28 @@ public abstract class AbstractContainerF
     }
 
     /**
+     * Replace the control in the container at the specified index, and return
+     * the newly added control.
+     *
+     * @see org.apache.click.control.Container#replace(org.apache.click.Control, org.apache.click.Control)
+     *
+     * @param currentControl the control currently contained in the container
+     * @param newControl the control to replace the current control contained in
+     * the container
+     * @return the new control that replaced the current control
+     *
+     * @throws IllegalArgumentException if the currentControl or newControl is
+     * null
+     * @throws IllegalStateException if the currentControl is not contained in
+     * the container
+     */
+    public Control replace(Control currentControl, Control newControl) {
+        int controlIndex = getControls().indexOf(currentControl);
+        return ContainerUtils.replace(this, currentControl, newControl,
+            controlIndex, getControlMap());
+    }
+
+    /**
      * Return the internal container instance.
      *
      * @deprecated the internal container instance was removed,

Modified: click/trunk/click/extras/src/org/apache/click/extras/panel/TabbedPanel.java
URL: http://svn.apache.org/viewvc/click/trunk/click/extras/src/org/apache/click/extras/panel/TabbedPanel.java?rev=940043&r1=940042&r2=940043&view=diff
==============================================================================
--- click/trunk/click/extras/src/org/apache/click/extras/panel/TabbedPanel.java (original)
+++ click/trunk/click/extras/src/org/apache/click/extras/panel/TabbedPanel.java Sat May  1 14:44:36 2010
@@ -208,8 +208,8 @@ public class TabbedPanel extends Panel {
      *     control with the same name
      */
     @Override
-    public Control add(Control control) {
-        super.add(control);
+    public Control insert(Control control, int index) {
+        super.insert(control, index);
 
         if (control instanceof Panel && getPanels().size() == 1) {
             setActivePanel((Panel) control);
@@ -219,6 +219,28 @@ public class TabbedPanel extends Panel {
     }
 
     /**
+     * Replace the current control with the new control.
+     *
+     * @param currentControl the current control container in the panel
+     * @param newControl the control to replace the current control
+     * @return the new control that replaced the current control
+     *
+     * @throws IllegalArgumentException if the currentControl or newControl is
+     * null
+     * @throws IllegalStateException if the currentControl is not contained in
+     * the panel
+     */
+    @Override
+    public Control replace(Control currentControl, Control newControl) {
+        super.replace(currentControl, newControl);
+
+        if (newControl instanceof Panel && getPanels().size() == 1) {
+            setActivePanel((Panel) newControl);
+        }
+        return newControl;
+    }
+
+    /**
      * Return the currently active panel.
      *
      * @return the currently active panel