You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by ca...@apache.org on 2007/05/15 11:22:44 UTC

svn commit: r538102 - in /myfaces/tomahawk/trunk/sandbox/core/src: main/java/org/apache/myfaces/custom/util/ComponentUtils.java test/java/org/apache/myfaces/custom/util/ test/java/org/apache/myfaces/custom/util/ComponentUtilsTest.java

Author: cagatay
Date: Tue May 15 02:22:43 2007
New Revision: 538102

URL: http://svn.apache.org/viewvc?view=rev&rev=538102
Log:
Add helper method to manipulate dom event attributes easily and added tests

Added:
    myfaces/tomahawk/trunk/sandbox/core/src/test/java/org/apache/myfaces/custom/util/
    myfaces/tomahawk/trunk/sandbox/core/src/test/java/org/apache/myfaces/custom/util/ComponentUtilsTest.java   (with props)
Modified:
    myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/util/ComponentUtils.java

Modified: myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/util/ComponentUtils.java
URL: http://svn.apache.org/viewvc/myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/util/ComponentUtils.java?view=diff&rev=538102&r1=538101&r2=538102
==============================================================================
--- myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/util/ComponentUtils.java (original)
+++ myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/util/ComponentUtils.java Tue May 15 02:22:43 2007
@@ -118,6 +118,37 @@
 
         return null;
     }
+	
+	
+	private static boolean isDecorated(UIComponent component, String attribute, String value) {
+		String attributeValue = (String) component.getAttributes().get(attribute);
+		
+		if(attributeValue == null || attributeValue.indexOf(value) == -1)
+			return false;
+		else
+			return true;
+	}
+	
+	/**
+     * Changes the event attributes like onclick by appending the given value 
+     * 
+     * @param 	component 	UIComponent instance that the attribute belongs to
+     * @param 	attribute 	Attribute to be changed
+     * @param 	value 		Value to be appended
+     */
+	public static void decorateEventAttribute(UIComponent component, String attribute, String value) {
+		if(isDecorated(component, attribute, value))
+			return;
+			
+		String attributeValue = (String) component.getAttributes().get(attribute);
+		
+		if(attributeValue == null)
+			component.getAttributes().put(attribute, value);
+		else
+			if( attributeValue.endsWith(";"))
+				component.getAttributes().put(attribute, attributeValue + value);
+			else
+				component.getAttributes().put(attribute, attributeValue + ";" + value);
+	}
 
-
-}
+}
\ No newline at end of file

Added: myfaces/tomahawk/trunk/sandbox/core/src/test/java/org/apache/myfaces/custom/util/ComponentUtilsTest.java
URL: http://svn.apache.org/viewvc/myfaces/tomahawk/trunk/sandbox/core/src/test/java/org/apache/myfaces/custom/util/ComponentUtilsTest.java?view=auto&rev=538102
==============================================================================
--- myfaces/tomahawk/trunk/sandbox/core/src/test/java/org/apache/myfaces/custom/util/ComponentUtilsTest.java (added)
+++ myfaces/tomahawk/trunk/sandbox/core/src/test/java/org/apache/myfaces/custom/util/ComponentUtilsTest.java Tue May 15 02:22:43 2007
@@ -0,0 +1,45 @@
+/*
+ * 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.myfaces.custom.util;
+
+import javax.faces.component.html.HtmlCommandButton;
+
+import junit.framework.TestCase;
+
+/**
+ * @author cagatay (latest modification by $Author$)
+ * @version $Revision$ $Date$
+ */
+public class ComponentUtilsTest extends TestCase{
+
+	public void testDecorateEventAttribute() {
+		HtmlCommandButton button = new HtmlCommandButton();
+		button.setId("testButton");
+		
+		ComponentUtils.decorateEventAttribute(button, "onclick", "alert('Whad up?');");
+		assertEquals("alert('Whad up?');", (String) button.getAttributes().get("onclick"));
+		
+		//Second try to decorate with same value
+		ComponentUtils.decorateEventAttribute(button, "onclick", "alert('Whad up?');");
+		assertEquals("alert('Whad up?');", (String) button.getAttributes().get("onclick"));
+		
+		ComponentUtils.decorateEventAttribute(button, "onclick", "return false;");
+		assertEquals("alert('Whad up?');return false;", (String) button.getAttributes().get("onclick"));
+	}
+}
\ No newline at end of file

Propchange: myfaces/tomahawk/trunk/sandbox/core/src/test/java/org/apache/myfaces/custom/util/ComponentUtilsTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: myfaces/tomahawk/trunk/sandbox/core/src/test/java/org/apache/myfaces/custom/util/ComponentUtilsTest.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision