01: package org.osbl.client.wings.form;
02:
03: import org.osbl.client.wings.form.ObjectEditor;
04: import org.wings.util.SessionLocal;
05: import org.apache.commons.logging.LogFactory;
06:
07: import java.util.Map;
08: import java.util.HashMap;
09:
10: public class Editors {
11: private static org.apache.commons.logging.Log LOG = LogFactory
12: .getLog(Editors.class);
13:
14: private static final Map<Class, Class<? extends ObjectEditor>> editors = new HashMap<Class, Class<? extends ObjectEditor>>();
15: private static final Instances instances = new Instances();
16:
17: private Editors() {
18: }
19:
20: public static void registerEditor(Class type,
21: Class<? extends ObjectEditor> editorClass) {
22: LOG.info("Register ObjectEditor: "
23: + editorClass.getSimpleName() + " for "
24: + type.getSimpleName());
25: editors.put(type, editorClass);
26: }
27:
28: public static ObjectEditor get(Class type) {
29: Map<Class, ObjectEditor> map = instances.get();
30: ObjectEditor editor = map.get(type);
31: if (editor == null) {
32: try {
33: Class<? extends ObjectEditor> editorClass = editors
34: .get(type);
35: if (editorClass == null)
36: throw new RuntimeException("No editor for " + type);
37:
38: editor = editorClass.newInstance();
39: } catch (InstantiationException e) {
40: throw new RuntimeException(e);
41: } catch (IllegalAccessException e) {
42: throw new RuntimeException(e);
43: }
44: }
45: return editor;
46: }
47:
48: private static class Instances extends SessionLocal {
49: protected Map<Class, ObjectEditor> initialValue() {
50: return new HashMap<Class, ObjectEditor>();
51: }
52:
53: public Map<Class, ObjectEditor> get() {
54: return (Map<Class, ObjectEditor>) super.get();
55: }
56: }
57: }
|