/*
 * Copyright 2004-2005 the original author or authors.
 * 
 * Licensed 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.codehaus.groovy.grails.support;


import java.beans.PropertyEditorSupport;
import org.springframework.util.ClassUtils;

/**
 * 
 * 
 * @author Steven Devijver
 * @since Aug 8, 2005
 */
public class ClassEditor extends PropertyEditorSupport {

	private ClassLoader classLoader = null;
	

	public ClassEditor() {
		super();
	}

	public ClassEditor(Object arg0) {
		super(arg0);
	}

	public void setClassLoader(ClassLoader classLoader) {
		this.classLoader = classLoader;
	}
	
	public String getAsText() {
		return ((Class)getValue()).getName();
	}
	
	public void setAsText(String className) throws IllegalArgumentException {
		try {

                        Class clazz = ClassUtils.resolvePrimitiveClassName(className);
 	                if (clazz != null) {
 	                        setValue(clazz);
 	                } 
                        else {
			    setValue(this.classLoader.loadClass(className));
                        }
		} catch (ClassNotFoundException e) {
			throw new IllegalArgumentException("Could not load class [" + className + "]!");
		}
	}

}

@@ -616,17 +616,16 @@
 nse.%0A */
- 
 %0Apackage
@@ -674,98 +674,145 @@
 ort 
-java.beans.PropertyEditorSupport;%0Aimport org.springframework.util.ClassUtils;%0A%0A/**%0A * 
+org.springframework.util.ClassUtils;%0A%0Aimport java.beans.PropertyEditorSupport;%0A%0A/**%0A * Converts Strings to Class references for Spring
 %0A *
- 
 %0A * 
@@ -835,16 +835,44 @@
 evijver%0A
+ * @author Graeme Rocher%0A *%0A
  * @sinc
@@ -947,17 +947,20 @@
 port %7B%0A%0A
-%09
+    
 private 
@@ -995,12 +995,14 @@
 ll;%0A
-%09
 %0A%0A
-%09
+    
 publ
@@ -1016,26 +1016,32 @@
 sEditor() %7B%0A
-%09%09
+        
 super();%0A%09%7D%0A
@@ -1037,21 +1037,27 @@
 uper();%0A
-%09%7D%0A%0A%09
+    %7D%0A%0A    
 public C
@@ -1082,18 +1082,24 @@
 arg0) %7B%0A
-%09%09
+        
 super(ar
@@ -1107,13 +1107,19 @@
 0);%0A
-%09%7D%0A%0A%09
+    %7D%0A%0A    
 publ
@@ -1168,18 +1168,58 @@
 ader) %7B%0A
-%09%09
+        if(classLoader!=null)%0A            
 this.cla
@@ -1242,22 +1242,27 @@
 Loader;%0A
-%09%7D%0A%09%0A%09
+    %7D%0A%0A    
 public S
@@ -1281,18 +1281,24 @@
 ext() %7B%0A
-%09%09
+        
 return (
@@ -1304,16 +1304,17 @@
 ((Class)
+ 
 getValue
@@ -1332,14 +1332,19 @@
 ();%0A
-%09%7D%0A%09%0A%09
+    %7D%0A%0A    
 publ
@@ -1417,26 +1417,16 @@
 n %7B%0A
-%09%09try %7B%0A%0A 
         
     
@@ -1413,35 +1413,39 @@
 ption %7B%0A        
-   
+try %7B%0A%0A
             Clas
@@ -1503,22 +1503,16 @@
 sName);%0A
- %09    
         
@@ -1536,25 +1536,16 @@
 null) %7B%0A
- %09       
         
@@ -1552,17 +1552,16 @@
         
- 
 setValue
@@ -1573,11 +1573,8 @@
 z);%0A
- %09 
     
@@ -1585,13 +1585,16 @@
     
-   %7D 
+%7D else %7B
 %0A   
@@ -1610,55 +1610,152 @@
     
-        else %7B%0A%09%09%09    setValue(this.classLoader
+final ClassLoader cl = this.classLoader != null ? this.classLoader : Thread.currentThread().getContextClassLoader();%0A                setValue(cl
 .loa
@@ -1778,36 +1778,34 @@
 ));%0A            
-    
+%7D%0A
         %7D%0A%09%09%7D ca
@@ -1797,20 +1797,16 @@
        %7D
-%0A%09%09%7D
  catch (
@@ -1837,11 +1837,20 @@
 ) %7B%0A
-%09%09%09
+            
 thro
@@ -1930,13 +1930,22 @@
 %22);%0A
-%09%09%7D%0A%09
+        %7D%0A    
 %7D%0A%0A%7D

fix so that it uses thread context loader if classloader property not set