Author: jdonnerstag
Date: Thu Jun 23 10:19:39 2011
New Revision: 1138813

URL: http://svn.apache.org/viewvc?rev=1138813&view=rev
Log:
applied: Allow for dynamic prefix and suffixes in CheckBoxMultipleChoice
Issue: WICKET-3478

Added:
    
wicket/trunk/wicket-core/src/test/java/org/apache/wicket/markup/html/form/CheckBoxMultipleChoiceTest.java
Modified:
    
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/markup/html/form/CheckBoxMultipleChoice.java

Modified: 
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/markup/html/form/CheckBoxMultipleChoice.java
URL: 
http://svn.apache.org/viewvc/wicket/trunk/wicket-core/src/main/java/org/apache/wicket/markup/html/form/CheckBoxMultipleChoice.java?rev=1138813&r1=1138812&r2=1138813&view=diff
==============================================================================
--- 
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/markup/html/form/CheckBoxMultipleChoice.java
 (original)
+++ 
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/markup/html/form/CheckBoxMultipleChoice.java
 Thu Jun 23 10:19:39 2011
@@ -236,6 +236,32 @@ public class CheckBoxMultipleChoice<T> e
        }
 
        /**
+        * @param index
+        *            index of the choice
+        * @param choice
+        *            the choice itself
+        * @return Prefix to use before choice. The default implementation just 
returns
+        *         {@link #getPrefix()}. Override to have a prefix dependent on 
the choice item.
+        */
+       protected String getPrefix(int index, T choice)
+       {
+               return getPrefix();
+       }
+
+       /**
+        * @param index
+        *            index of the choice
+        * @param choice
+        *            the choice itself
+        * @return Separator to use between radio options. The default 
implementation just returns
+        *         {@link #getSuffix()}. Override to have a prefix dependent on 
the choice item.
+        */
+       protected String getSuffix(int index, T choice)
+       {
+               return getSuffix();
+       }
+
+       /**
         * @param prefix
         *            Prefix to use before choice
         * @return this
@@ -360,7 +386,7 @@ public class CheckBoxMultipleChoice<T> e
                if (label != null)
                {
                        // Append option suffix
-                       buffer.append(getPrefix());
+                       buffer.append(getPrefix(index, choice));
 
                        String id = getChoiceRenderer().getIdValue(choice, 
index);
                        final String idAttr = getCheckBoxMarkupId(id);
@@ -399,7 +425,7 @@ public class CheckBoxMultipleChoice<T> e
                        buffer.append("\">").append(escaped).append("</label>");
 
                        // Append option suffix
-                       buffer.append(getSuffix());
+                       buffer.append(getSuffix(index, choice));
                }
        }
 

Added: 
wicket/trunk/wicket-core/src/test/java/org/apache/wicket/markup/html/form/CheckBoxMultipleChoiceTest.java
URL: 
http://svn.apache.org/viewvc/wicket/trunk/wicket-core/src/test/java/org/apache/wicket/markup/html/form/CheckBoxMultipleChoiceTest.java?rev=1138813&view=auto
==============================================================================
--- 
wicket/trunk/wicket-core/src/test/java/org/apache/wicket/markup/html/form/CheckBoxMultipleChoiceTest.java
 (added)
+++ 
wicket/trunk/wicket-core/src/test/java/org/apache/wicket/markup/html/form/CheckBoxMultipleChoiceTest.java
 Thu Jun 23 10:19:39 2011
@@ -0,0 +1,133 @@
+/*
+ * 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.markup.html.form;
+
+import java.util.Arrays;
+import java.util.List;
+
+import org.apache.wicket.WicketTestCase;
+import org.apache.wicket.markup.IMarkupFragment;
+import org.apache.wicket.markup.Markup;
+import org.apache.wicket.markup.html.WebPage;
+import org.junit.Test;
+
+/**
+ * Test the dynamic prefix/suffix feature introduced with
+ * https://issues.apache.org/jira/browse/WICKET-3478
+ * 
+ * @author Carl-Eric Menzel <[email protected]> 
<[email protected]>
+ */
+public class CheckBoxMultipleChoiceTest extends WicketTestCase
+{
+       /** */
+       public static class TestPage extends WebPage
+       {
+               private static final long serialVersionUID = 1L;
+
+               /**
+                * Construct.
+                * 
+                * @param show1
+                * @param show2
+                * @param show3
+                */
+               public TestPage(final boolean show1, final boolean show2, final 
boolean show3)
+               {
+                       List<? extends String> choices = Arrays.asList("a", 
"b", "c");
+                       add(new 
CheckBoxMultipleChoice<String>("checkWithoutPrefix", choices)
+                       {
+                               private static final long serialVersionUID = 1L;
+
+                               @Override
+                               public boolean isVisible()
+                               {
+                                       return show1;
+                               }
+                       });
+                       add(new 
CheckBoxMultipleChoice<String>("checkWithFixedPrefix", choices)
+                       {
+                               private static final long serialVersionUID = 1L;
+
+                               @Override
+                               public boolean isVisible()
+                               {
+                                       return show2;
+                               }
+                       }.setPrefix("pre").setSuffix("suf"));
+                       add(new 
CheckBoxMultipleChoice<String>("checkWithDynamicPrefix", choices)
+                       {
+                               private static final long serialVersionUID = 1L;
+
+                               @Override
+                               public boolean isVisible()
+                               {
+                                       return show3;
+                               }
+
+                               @Override
+                               protected String getPrefix(int index, String 
choice)
+                               {
+                                       return "pre" + index + choice;
+                               }
+
+                               @Override
+                               protected String getSuffix(int index, String 
choice)
+                               {
+                                       return "suf" + index + choice;
+                               }
+                       });
+               }
+
+               @Override
+               public IMarkupFragment getMarkup()
+               {
+                       return Markup.of("<html><body>" //
+                               + "<div wicket:id='checkWithoutPrefix'></div>" 
//
+                               + "<div 
wicket:id='checkWithFixedPrefix'></div>" //
+                               + "<div 
wicket:id='checkWithDynamicPrefix'></div>" //
+                               + "</body></html>");
+               }
+       }
+
+       /** */
+       @Test
+       public void noPrefix()
+       {
+               tester.startPage(new TestPage(true, false, false));
+               tester.assertContains("<div 
wicket:id=\"checkWithoutPrefix\"><input name=\"checkWithoutPrefix\"");
+       }
+
+       /** */
+       @Test
+       public void fixedPrefix()
+       {
+               tester.startPage(new TestPage(false, true, false));
+               tester.assertContains("<div 
wicket:id=\"checkWithFixedPrefix\">pre<input name=\"checkWithFixedPrefix\"");
+               tester.assertContains("</label>sufpre<input 
name=\"checkWithFixedPrefix\"");
+               tester.assertContains("</label>suf</div>");
+       }
+
+       /** */
+       @Test
+       public void dynamicPrefix()
+       {
+               tester.startPage(new TestPage(false, false, true));
+               tester.assertContains("<div 
wicket:id=\"checkWithDynamicPrefix\">pre0a<input 
name=\"checkWithDynamicPrefix\"");
+               tester.assertContains("</label>suf0apre1b<input 
name=\"checkWithDynamicPrefix\"");
+               tester.assertContains("</label>suf2c</div>");
+       }
+}
\ No newline at end of file


Reply via email to