1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package ch.elca.el4j.services.gui.swing.cookswing;
18
19 import java.lang.reflect.Method;
20
21 import javax.swing.JPanel;
22
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25 import org.w3c.dom.Element;
26
27 import cookxml.core.DecodeEngine;
28 import cookxml.core.exception.CookXmlException;
29 import cookxml.core.exception.CreatorException;
30 import cookxml.core.interfaces.Creator;
31
32
33
34
35
36
37
38
39
40
41 public class CreateComponentCreator implements Creator {
42
43
44
45
46 protected static final String CREATE_METHOD = "create-method";
47
48
49
50
51 protected static final String FINISH_METHOD = "finish-method";
52
53
54
55
56 private static final Logger s_logger = LoggerFactory
57 .getLogger(CreateComponentCreator.class);
58
59
60 public Object create(String parentNS, String parentTag, Element elm,
61 Object parentObj, DecodeEngine decodeEngine) throws CreatorException {
62
63 Object form = decodeEngine.getVariable("this");
64 String methodName = elm.getAttribute(CREATE_METHOD);
65
66 if (!methodName.equals("")) {
67 return invokeMethod(form, methodName);
68 } else {
69 return new JPanel();
70 }
71 }
72
73
74 public Object editFinished(String parentNS, String parentTag, Element elm,
75 Object parentObj, Object obj, DecodeEngine decodeEngine) throws CookXmlException {
76
77 Object form = decodeEngine.getVariable("this");
78 String methodName = elm.getAttribute(FINISH_METHOD);
79
80 if (!methodName.equals("")) {
81 invokeMethod(form, methodName, obj);
82 }
83 return obj;
84 }
85
86
87
88
89
90
91
92
93
94 protected Object invokeMethod(Object form, String methodName, Object... parameters) {
95 Class<?> formClass = form.getClass();
96 while (formClass != Object.class) {
97
98 final Method[] publicMethods = formClass.getDeclaredMethods();
99 for (int i = 0; i < publicMethods.length; ++i) {
100 if (methodName.equals(publicMethods[i].getName())) {
101 try {
102 publicMethods[i].setAccessible(true);
103 return publicMethods[i].invoke(form, parameters);
104 } catch (Exception e) {
105
106 continue;
107 }
108 }
109 }
110 formClass = formClass.getSuperclass();
111 }
112 s_logger.error("Error processing <create-component>. Could not find method '" + methodName + "'.");
113 return null;
114 }
115 }