1 | // Module : $RCSfile: AbstractDefaultGUIElementFactory.java,v $ |
---|
2 | // Version : $Revision: 0.0 $ $Author: patrick $ $Date: 13.05.2012 $ |
---|
3 | // Project : GUIModel |
---|
4 | // Creation : 2012 by patrick |
---|
5 | // Copyright : Patrick Harms, 2012 |
---|
6 | |
---|
7 | package de.ugoe.cs.quest.eventcore.guimodel; |
---|
8 | |
---|
9 | import java.io.IOException; |
---|
10 | import java.io.InputStream; |
---|
11 | import java.lang.reflect.InvocationTargetException; |
---|
12 | import java.util.Properties; |
---|
13 | import java.util.logging.Level; |
---|
14 | import java.util.logging.Logger; |
---|
15 | |
---|
16 | /** |
---|
17 | * TODO comment |
---|
18 | * |
---|
19 | * @version $Revision: $ $Date: 13.05.2012$ |
---|
20 | * @author 2012, last modified by $Author: patrick$ |
---|
21 | */ |
---|
22 | public abstract class AbstractDefaultGUIElementFactory implements IGUIElementFactory { |
---|
23 | |
---|
24 | /** */ |
---|
25 | private Properties mappingsFromConfiguration; |
---|
26 | |
---|
27 | /** |
---|
28 | * TODO: comment |
---|
29 | * |
---|
30 | * @param object1 |
---|
31 | * @param object2 |
---|
32 | * @return |
---|
33 | */ |
---|
34 | protected boolean equals(Object object1, Object object2) { |
---|
35 | if (object1 == object2) { |
---|
36 | return true; |
---|
37 | } |
---|
38 | else if (object1 != null) { |
---|
39 | return object1.equals(object2); |
---|
40 | } |
---|
41 | else { |
---|
42 | // object 1 is null but object 2 not --> return false |
---|
43 | return false; |
---|
44 | } |
---|
45 | } |
---|
46 | |
---|
47 | /** |
---|
48 | * TODO: comment |
---|
49 | * |
---|
50 | * @param parameterTypes |
---|
51 | * @param parameters |
---|
52 | * @return |
---|
53 | * @throws GUIModelConfigurationException |
---|
54 | */ |
---|
55 | protected IGUIElement instantiateGUIElementFromConfiguredMappings(IGUIElementSpec specification) |
---|
56 | throws GUIModelConfigurationException |
---|
57 | { |
---|
58 | Properties mappings = getMappingsFromConfiguration(); |
---|
59 | |
---|
60 | String className = mappings.getProperty(specification.getType()); |
---|
61 | if (className != null) { |
---|
62 | try { |
---|
63 | Class<?> clazz = this.getClass().getClassLoader().loadClass(className); |
---|
64 | |
---|
65 | if (!IGUIElement.class.isAssignableFrom(clazz)) { |
---|
66 | Logger.getLogger(this.getClass().getName()).warning |
---|
67 | ("configured GUI element representing class " + className + |
---|
68 | " is no valid GUIElement " + "derivate."); |
---|
69 | |
---|
70 | return null; |
---|
71 | } |
---|
72 | |
---|
73 | Class<?>[] parameterTypes = new Class<?>[1]; |
---|
74 | parameterTypes[0] = specification.getClass(); |
---|
75 | |
---|
76 | IGUIElement guiElement = |
---|
77 | (IGUIElement) clazz.getConstructor(parameterTypes).newInstance(specification); |
---|
78 | |
---|
79 | return guiElement; |
---|
80 | } |
---|
81 | catch (ClassNotFoundException e) { |
---|
82 | Logger.getLogger(this.getClass().getName()).warning |
---|
83 | ("configured GUI element representing class " + className + |
---|
84 | " can not be loaded."); |
---|
85 | throw new GUIModelConfigurationException |
---|
86 | ("configured GUI element representing class " + className + |
---|
87 | " can not be loaded.", e); |
---|
88 | } |
---|
89 | catch (SecurityException e) { |
---|
90 | Logger.getLogger(this.getClass().getName()).log |
---|
91 | (Level.WARNING, "configured GUI element representing class " + className + |
---|
92 | " can not be instantiated due to security reasons.", e); |
---|
93 | throw new GUIModelConfigurationException |
---|
94 | ("configured GUI element representing class " + className + |
---|
95 | " can not be instantiated due to security reasons.", e); |
---|
96 | } |
---|
97 | catch (NoSuchMethodException e) { |
---|
98 | Logger.getLogger(this.getClass().getName()).warning |
---|
99 | ("configured GUI element representing class " + className + |
---|
100 | " does not provide an appropriate constructur."); |
---|
101 | throw new GUIModelConfigurationException |
---|
102 | ("configured GUI element representing class " + className + |
---|
103 | " does not provide an appropriate constructur.", e); |
---|
104 | } |
---|
105 | catch (IllegalArgumentException e) { |
---|
106 | Logger.getLogger(this.getClass().getName()).warning |
---|
107 | ("configured GUI element representing class " + className + " does not " + |
---|
108 | "provide an appropriate constructur accepting the provided parameters."); |
---|
109 | throw new GUIModelConfigurationException |
---|
110 | ("configured GUI element representing class " + className + " does not " + |
---|
111 | "provide an appropriate constructur accepting the provided parameters.", e); |
---|
112 | } |
---|
113 | catch (InstantiationException e) { |
---|
114 | Logger.getLogger(this.getClass().getName()).log |
---|
115 | (Level.WARNING, "configured GUI element representing class " + className + |
---|
116 | " can not be instantiated.", e); |
---|
117 | throw new GUIModelConfigurationException |
---|
118 | ("configured GUI element representing class " + className + |
---|
119 | " can not be instantiated.", e); |
---|
120 | } |
---|
121 | catch (IllegalAccessException e) { |
---|
122 | Logger.getLogger(this.getClass().getName()).log |
---|
123 | (Level.WARNING, "configured GUI element representing class " + className + |
---|
124 | " can not be instantiated.", e); |
---|
125 | throw new GUIModelConfigurationException |
---|
126 | ("configured GUI element representing class " + className + |
---|
127 | " can not be instantiated.", e); |
---|
128 | } |
---|
129 | catch (InvocationTargetException e) { |
---|
130 | Logger.getLogger(this.getClass().getName()).log |
---|
131 | (Level.WARNING, "configured GUI element representing class " + className + |
---|
132 | " can not be instantiated.", e); |
---|
133 | throw new GUIModelConfigurationException |
---|
134 | ("configured GUI element representing class " + className + |
---|
135 | " can not be instantiated.", e); |
---|
136 | } |
---|
137 | } |
---|
138 | |
---|
139 | return null; |
---|
140 | } |
---|
141 | |
---|
142 | /** |
---|
143 | * TODO: comment |
---|
144 | * |
---|
145 | * @return |
---|
146 | */ |
---|
147 | private synchronized Properties getMappingsFromConfiguration() { |
---|
148 | if (mappingsFromConfiguration != null) { |
---|
149 | return mappingsFromConfiguration; |
---|
150 | } |
---|
151 | else { |
---|
152 | mappingsFromConfiguration = new Properties(); |
---|
153 | |
---|
154 | InputStream inStream = |
---|
155 | this.getClass().getClassLoader().getResourceAsStream("GUIElementMapping.txt"); |
---|
156 | |
---|
157 | if (inStream != null) { |
---|
158 | try { |
---|
159 | mappingsFromConfiguration.load(inStream); |
---|
160 | } |
---|
161 | catch (IOException e) { |
---|
162 | Logger.getLogger(this.getClass().getName()).warning |
---|
163 | ("could not load GUIElementMapping.txt from classpath, but this may be " + |
---|
164 | "intended"); |
---|
165 | } |
---|
166 | } |
---|
167 | |
---|
168 | return mappingsFromConfiguration; |
---|
169 | } |
---|
170 | } |
---|
171 | } |
---|