KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > wings > template > DefaultPropertyManager


1 /*
2  * $Id: DefaultPropertyManager.java,v 1.5 2004/12/01 07:54:28 hengels Exp $
3  * Copyright 2000,2005 wingS development team.
4  *
5  * This file is part of wingS (http://www.j-wings.org).
6  *
7  * wingS is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU Lesser General Public License
9  * as published by the Free Software Foundation; either version 2.1
10  * of the License, or (at your option) any later version.
11  *
12  * Please see COPYING for the complete licence.
13  */

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 JavaDoc;
23 import java.util.HashMap JavaDoc;
24
25 /**
26  * DefaultPropertyManager.java
27  * <p/>
28  * <p/>
29  * Created: Tue Aug 6 16:43:03 2002
30  *
31  * @author (c) mercatis information systems gmbh, 1999-2002
32  * @author <a HREF="mailto:armin.haaf@mercatis.de">Armin Haaf</a>
33  * @version $Revision: 1.5 $
34  */

35 public class DefaultPropertyManager implements PropertyManager {
36
37     private final transient static Log log = LogFactory.getLog(DefaultPropertyManager.class);
38     static final Class JavaDoc[] classes = {SComponent.class};
39
40     public final HashMap JavaDoc propertyValueConverters = new HashMap JavaDoc();
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 JavaDoc name, String JavaDoc 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 JavaDoc ex) {
68                 ex.printStackTrace();
69                 // ignore it, maybe log it
70
} // end of try-catch
71

72 // reset interpreter
73

74         } // end of if ()
75

76
77         Method JavaDoc[] methods = comp.getClass().getMethods();
78
79         for (int i = 0; i < methods.length; i++) {
80             Method JavaDoc 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 JavaDoc paramType = method.getParameterTypes()[0];
87
88                 PropertyValueConverter valueConverter = getValueConverter(paramType);
89
90                 if (valueConverter != null) {
91                     try {
92                         //System.out.println("invoke " + method);
93
method.setAccessible(true);
94                         method.invoke(comp,
95                                 new Object JavaDoc[]{valueConverter.convertPropertyValue(value, paramType)});
96                         return;
97                     } catch (Exception JavaDoc ex) {
98                         // ignore it, maybe log it...
99
} // end of try-catch
100

101                 } // end of if ()
102
} // end of if ()
103
} // end of for (int i=0; i<; i++)
104
}
105
106     public void addPropertyValueConverter(PropertyValueConverter valueConverter,
107                                           Class JavaDoc clazz) {
108
109         propertyValueConverters.put(clazz, valueConverter);
110     }
111
112     protected PropertyValueConverter getValueConverter(Class JavaDoc clazz) {
113         if (clazz == null) {
114             return DEFAULT_PROPERTY_VALUE_CONVERTER;
115         } // end of if ()
116

117         if (propertyValueConverters.containsKey(clazz)) {
118             return (PropertyValueConverter) propertyValueConverters.get(clazz);
119         } // end of if ()
120

121         return getValueConverter(clazz.getSuperclass());
122     }
123
124     public Class JavaDoc[] getSupportedClasses() {
125         return classes;
126     }
127 }
128
Popular Tags