You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wicket.apache.org by mg...@apache.org on 2011/04/18 19:22:30 UTC

svn commit: r1094664 - in /wicket/trunk/wicket-core/src: main/java/org/apache/wicket/behavior/AttributeAppender.java test/java/org/apache/wicket/behavior/AttributeAppenderTest.java

Author: mgrigorov
Date: Mon Apr 18 17:22:29 2011
New Revision: 1094664

URL: http://svn.apache.org/viewvc?rev=1094664&view=rev
Log:
WICKET-3552 Allow AttributeAppender to also prepend values


Modified:
    wicket/trunk/wicket-core/src/main/java/org/apache/wicket/behavior/AttributeAppender.java
    wicket/trunk/wicket-core/src/test/java/org/apache/wicket/behavior/AttributeAppenderTest.java

Modified: wicket/trunk/wicket-core/src/main/java/org/apache/wicket/behavior/AttributeAppender.java
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket-core/src/main/java/org/apache/wicket/behavior/AttributeAppender.java?rev=1094664&r1=1094663&r2=1094664&view=diff
==============================================================================
--- wicket/trunk/wicket-core/src/main/java/org/apache/wicket/behavior/AttributeAppender.java (original)
+++ wicket/trunk/wicket-core/src/main/java/org/apache/wicket/behavior/AttributeAppender.java Mon Apr 18 17:22:29 2011
@@ -43,6 +43,21 @@ import org.apache.wicket.util.string.Str
  *     <a href="#" wicket:id="foo" class="link hot" onmouseover="doSomething();foo();return false;">
  * </pre>
  * 
+ * AttributeAppenders can also be instructed to prepend the given value:
+ * 
+ * <pre>
+ * link.add(new AttributeAppender(&quot;class&quot;, new Model&lt;String&gt;(&quot;hot&quot;), &quot; &quot;, true));
+ * link.add(new AttributeAppender(&quot;onmouseover&quot;, new Model&lt;String&gt;(&quot;foo();return false;&quot;), &quot;;&quot;, true));
+ * </pre>
+ * 
+ * this will result in the following markup:
+ * 
+ * <pre>
+ *     &lt;a href=&quot;#&quot; wicket:id=&quot;foo&quot; class=&quot;hot link&quot; onmouseover=&quot;foo();return false;doSomething();&quot;&gt;
+ * </pre>
+ * 
+ * This is useful for instance to add a Javascript confirmation dialog before performing an action.
+ * 
  * @author Martijn Dashorst
  */
 public class AttributeAppender extends AttributeModifier
@@ -56,6 +71,11 @@ public class AttributeAppender extends A
 	private final String separator;
 
 	/**
+	 * A flag indicating whether the new attribute value will be prepended
+	 */
+	private final boolean prepend;
+
+	/**
 	 * Creates an AttributeModifier that appends the appendModel's value to the current value of the
 	 * attribute, and will add the attribute when addAttributeIfNotPresent is true.
 	 * 
@@ -71,8 +91,30 @@ public class AttributeAppender extends A
 	public AttributeAppender(String attribute, boolean addAttributeIfNotPresent,
 		IModel<?> appendModel, String separator)
 	{
+		this(attribute, addAttributeIfNotPresent, appendModel, separator, false);
+	}
+
+	/**
+	 * Creates an AttributeModifier that appends the appendModel's value to the current value of the
+	 * attribute, and will add the attribute when addAttributeIfNotPresent is true.
+	 * 
+	 * @param attribute
+	 *            the attribute to append the appendModels value to
+	 * @param addAttributeIfNotPresent
+	 *            when true, adds the attribute to the tag
+	 * @param appendModel
+	 *            the model supplying the value to append
+	 * @param separator
+	 *            the separator string, comes between the original value and the append value
+	 * @param prepend
+	 *            if true, the attribute modifier will prepend the attribute with the appendModel
+	 */
+	public AttributeAppender(String attribute, boolean addAttributeIfNotPresent,
+		IModel<?> appendModel, String separator, boolean prepend)
+	{
 		super(attribute, addAttributeIfNotPresent, appendModel);
 		this.separator = separator;
+		this.prepend = prepend;
 	}
 
 	/**
@@ -88,40 +130,58 @@ public class AttributeAppender extends A
 	 */
 	public AttributeAppender(String attribute, IModel<?> appendModel, String separator)
 	{
-		super(attribute, true, appendModel);
-		this.separator = separator;
+		this(attribute, true, appendModel, separator, false);
+	}
+
+	/**
+	 * Creates an AttributeModifier that appends the appendModel's value to the current value of the
+	 * attribute, and will add the attribute when it is not there already.
+	 * 
+	 * @param attribute
+	 *            the attribute to append the appendModels value to
+	 * @param appendModel
+	 *            the model supplying the value to append
+	 * @param separator
+	 *            the separator string, comes between the original value and the append value
+	 * @param prepend
+	 *            if true, the attribute modifier will prepend the attribute with the appendModel
+	 */
+	public AttributeAppender(String attribute, IModel<?> appendModel, String separator,
+		boolean prepend)
+	{
+		this(attribute, true, appendModel, separator, prepend);
 	}
 
+
 	/**
 	 * @see org.apache.wicket.AttributeModifier#newValue(java.lang.String, java.lang.String)
 	 */
 	@Override
 	protected String newValue(String currentValue, String appendValue)
 	{
-		final int appendValueLen = (appendValue == null) ? 0 : appendValue.length();
-
-		final AppendingStringBuffer sb;
-		if (currentValue == null)
+		// Shortcut for empty values
+		if (Strings.isEmpty(currentValue))
 		{
-			sb = new AppendingStringBuffer(appendValueLen + separator.length());
+			return appendValue != null ? appendValue : "";
 		}
-		else
+		else if (Strings.isEmpty(appendValue))
 		{
-			sb = new AppendingStringBuffer(currentValue.length() + appendValueLen +
-				separator.length());
-			sb.append(currentValue);
+			return currentValue;
 		}
 
-		// if the current value or the append value is empty, the separator is
-		// not needed.
-		if (!Strings.isEmpty(currentValue) && !Strings.isEmpty(appendValue))
+		final AppendingStringBuffer sb = new AppendingStringBuffer(currentValue.length() +
+			appendValue.length() + separator.length());
+
+		if (prepend)
 		{
+			sb.append(appendValue);
 			sb.append(separator);
+			sb.append(currentValue);
 		}
-
-		// only append the value when it is not empty.
-		if (!Strings.isEmpty(appendValue))
+		else
 		{
+			sb.append(currentValue);
+			sb.append(separator);
 			sb.append(appendValue);
 		}
 		return sb.toString();

Modified: wicket/trunk/wicket-core/src/test/java/org/apache/wicket/behavior/AttributeAppenderTest.java
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket-core/src/test/java/org/apache/wicket/behavior/AttributeAppenderTest.java?rev=1094664&r1=1094663&r2=1094664&view=diff
==============================================================================
--- wicket/trunk/wicket-core/src/test/java/org/apache/wicket/behavior/AttributeAppenderTest.java (original)
+++ wicket/trunk/wicket-core/src/test/java/org/apache/wicket/behavior/AttributeAppenderTest.java Mon Apr 18 17:22:29 2011
@@ -34,7 +34,24 @@ public class AttributeAppenderTest exten
 		AttributeAppender appender = new AttributeAppender("", null, " ");
 		assertEquals("oldvalue newvalue", appender.newValue("oldvalue", "newvalue"));
 		assertEquals("newvalue", appender.newValue("", "newvalue"));
+		assertEquals("newvalue", appender.newValue(null, "newvalue"));
 		assertEquals("oldvalue", appender.newValue("oldvalue", ""));
+		assertEquals("oldvalue", appender.newValue("oldvalue", null));
+		assertEquals("", appender.newValue(null, null));
+	}
+
+	/**
+	 * Test
+	 */
+	public void testNewValue1Prepend()
+	{
+		AttributeAppender appender = new AttributeAppender("", null, " ", true);
+		assertEquals("newvalue oldvalue", appender.newValue("oldvalue", "newvalue"));
+		assertEquals("newvalue", appender.newValue("", "newvalue"));
+		assertEquals("newvalue", appender.newValue(null, "newvalue"));
+		assertEquals("oldvalue", appender.newValue("oldvalue", ""));
+		assertEquals("oldvalue", appender.newValue("oldvalue", null));
+		assertEquals("", appender.newValue(null, null));
 	}
 
 	/**
@@ -45,6 +62,23 @@ public class AttributeAppenderTest exten
 		AttributeAppender appender = new AttributeAppender("", null, ";");
 		assertEquals("oldvalue;newvalue", appender.newValue("oldvalue", "newvalue"));
 		assertEquals("newvalue", appender.newValue("", "newvalue"));
+		assertEquals("newvalue", appender.newValue(null, "newvalue"));
+		assertEquals("oldvalue", appender.newValue("oldvalue", ""));
+		assertEquals("oldvalue", appender.newValue("oldvalue", null));
+		assertEquals("", appender.newValue(null, null));
+	}
+
+	/**
+	 * Test
+	 */
+	public void testNewValue2Prepend()
+	{
+		AttributeAppender appender = new AttributeAppender("", null, ";", true);
+		assertEquals("newvalue;oldvalue", appender.newValue("oldvalue", "newvalue"));
+		assertEquals("newvalue", appender.newValue("", "newvalue"));
+		assertEquals("newvalue", appender.newValue(null, "newvalue"));
 		assertEquals("oldvalue", appender.newValue("oldvalue", ""));
+		assertEquals("oldvalue", appender.newValue("oldvalue", null));
+		assertEquals("", appender.newValue(null, null));
 	}
 }