1 14 package org.wings.template; 15 16 import bsh.Interpreter; 17 import org.apache.commons.logging.Log; 18 import org.apache.commons.logging.LogFactory; 19 import org.wings.SComponent; 20 import org.wings.session.SessionManager; 21 22 import java.lang.reflect.Method ; 23 import java.util.HashMap ; 24 25 35 public class DefaultPropertyManager implements PropertyManager { 36 37 private final transient static Log log = LogFactory.getLog(DefaultPropertyManager.class); 38 static final Class [] classes = {SComponent.class}; 39 40 public final HashMap propertyValueConverters = new HashMap (); 41 42 public static final DefaultPropertyValueConverter 43 DEFAULT_PROPERTY_VALUE_CONVERTER = DefaultPropertyValueConverter.INSTANCE; 44 45 private boolean scriptEnabled = false; 46 47 48 public DefaultPropertyManager() { 49 50 } 51 52 protected Interpreter createInterpreter() { 53 return new Interpreter(); 54 } 55 56 public void setProperty(SComponent comp, String name, String value) { 57 if (scriptEnabled && "SCRIPT".equals(name)) { 58 Interpreter interpreter = createInterpreter(); 59 60 try { 61 log.debug("eval script " + value); 62 63 interpreter.set("component", comp); 64 interpreter.set("session", SessionManager.getSession()); 65 66 interpreter.eval(value); 67 } catch (Exception ex) { 68 ex.printStackTrace(); 69 } 72 74 } 76 77 Method [] methods = comp.getClass().getMethods(); 78 79 for (int i = 0; i < methods.length; i++) { 80 Method method = methods[i]; 81 82 if (method.getName().startsWith("set") && 83 name.equals(method.getName().substring(3).toUpperCase()) && 84 method.getParameterTypes().length == 1) { 85 86 Class paramType = method.getParameterTypes()[0]; 87 88 PropertyValueConverter valueConverter = getValueConverter(paramType); 89 90 if (valueConverter != null) { 91 try { 92 method.setAccessible(true); 94 method.invoke(comp, 95 new Object []{valueConverter.convertPropertyValue(value, paramType)}); 96 return; 97 } catch (Exception ex) { 98 } 101 } } } } 105 106 public void addPropertyValueConverter(PropertyValueConverter valueConverter, 107 Class clazz) { 108 109 propertyValueConverters.put(clazz, valueConverter); 110 } 111 112 protected PropertyValueConverter getValueConverter(Class clazz) { 113 if (clazz == null) { 114 return DEFAULT_PROPERTY_VALUE_CONVERTER; 115 } 117 if (propertyValueConverters.containsKey(clazz)) { 118 return (PropertyValueConverter) propertyValueConverters.get(clazz); 119 } 121 return getValueConverter(clazz.getSuperclass()); 122 } 123 124 public Class [] getSupportedClasses() { 125 return classes; 126 } 127 } 128 | Popular Tags |