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 2009/03/26 07:17:37 UTC
svn commit: r758530 - in /incubator/click/trunk/click/framework:
src/org/apache/click/util/ContainerUtils.java
test/org/apache/click/util/ContainerUtilsTest.java
Author: sabob
Date: Thu Mar 26 06:17:35 2009
New Revision: 758530
URL: http://svn.apache.org/viewvc?rev=758530&view=rev
Log:
added method to find control by type
Modified:
incubator/click/trunk/click/framework/src/org/apache/click/util/ContainerUtils.java
incubator/click/trunk/click/framework/test/org/apache/click/util/ContainerUtilsTest.java
Modified: incubator/click/trunk/click/framework/src/org/apache/click/util/ContainerUtils.java
URL: http://svn.apache.org/viewvc/incubator/click/trunk/click/framework/src/org/apache/click/util/ContainerUtils.java?rev=758530&r1=758529&r2=758530&view=diff
==============================================================================
--- incubator/click/trunk/click/framework/src/org/apache/click/util/ContainerUtils.java (original)
+++ incubator/click/trunk/click/framework/src/org/apache/click/util/ContainerUtils.java Thu Mar 26 06:17:35 2009
@@ -297,10 +297,10 @@
* Find and return the first control with a matching name in the specified
* container.
*
- * @param container the container that is checked for controls for matching
- * names
+ * @param container the container that is searched for a control with a
+ * matching name
* @param name the name of the control to find
- * @return the control with a matching name
+ * @return the control which name matched the given name
*/
public static Control findControlByName(Container container, String name) {
Control control = (Control) container.getControl(name);
@@ -326,6 +326,34 @@
}
/**
+ * Find and return the first control of a given type in the specified
+ * container.
+ * <p/>
+ * Please note that this method does exact matching. If the given type is a
+ * superclass of a control, it won't count as a match.
+ *
+ * @param container the container that is searched for a control of a
+ * given type
+ * @param type the type of the control to find
+ * @return the control which type matched the given type
+ */
+ public static Control findControlByType(Container container, Class type) {
+ for (int i = 0; i < container.getControls().size(); i++) {
+ Control childControl = (Control) container.getControls().get(i);
+
+ if (childControl.getClass().equals(type)) {
+ return childControl;
+ } else {
+ if (childControl instanceof Container) {
+ Container childContainer = (Container) childControl;
+ return findControlByType(childContainer, type);
+ }
+ }
+ }
+ return null;
+ }
+
+ /**
* Find and return the specified controls parent Form or null
* if no Form is present.
*
Modified: incubator/click/trunk/click/framework/test/org/apache/click/util/ContainerUtilsTest.java
URL: http://svn.apache.org/viewvc/incubator/click/trunk/click/framework/test/org/apache/click/util/ContainerUtilsTest.java?rev=758530&r1=758529&r2=758530&view=diff
==============================================================================
--- incubator/click/trunk/click/framework/test/org/apache/click/util/ContainerUtilsTest.java (original)
+++ incubator/click/trunk/click/framework/test/org/apache/click/util/ContainerUtilsTest.java Thu Mar 26 06:17:35 2009
@@ -22,10 +22,12 @@
import junit.framework.TestCase;
import org.apache.click.MockContext;
import org.apache.click.control.Button;
+import org.apache.click.control.Field;
import org.apache.click.control.FieldSet;
import org.apache.click.control.Form;
import org.apache.click.control.HiddenField;
import org.apache.click.control.Label;
+import org.apache.click.control.Panel;
import org.apache.click.control.TextField;
/**
@@ -69,6 +71,34 @@
}
/**
+ * Check that the first control of specified type if found in container.
+ */
+ public void testFindControlByType() {
+ MockContext.initContext();
+ // set up the form
+ Form form = new Form("sample");
+
+ Panel panel = new Panel("panel");
+
+ form.add(panel);
+
+ panel.add(new Label("label1"));
+
+ // TextField to find
+ Field field1 = new TextField("field1");
+ panel.add(field1);
+
+ panel.add(new Label("label2"));
+ panel.add(new TextField("field2"));
+
+ Field result = (Field) ContainerUtils.findControlByType(form,
+ TextField.class);
+
+ // Check that field1 was found
+ assertEquals(field1, result);
+ }
+
+ /**
* Check that domain class with duplicate getter methods resolves to the
* no-argument version.
*