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.File; |
---|
10 | import java.io.FileInputStream; |
---|
11 | import java.io.FileNotFoundException; |
---|
12 | import java.io.IOException; |
---|
13 | import java.io.InputStream; |
---|
14 | import java.lang.reflect.InvocationTargetException; |
---|
15 | import java.util.Properties; |
---|
16 | import java.util.logging.Level; |
---|
17 | import java.util.logging.Logger; |
---|
18 | |
---|
19 | import de.ugoe.cs.util.console.Console; |
---|
20 | |
---|
21 | /** |
---|
22 | * TODO comment |
---|
23 | * |
---|
24 | * TODO rename class to GUIElementFactory |
---|
25 | * |
---|
26 | * @version $Revision: $ $Date: 13.05.2012$ |
---|
27 | * @author 2012, last modified by $Author: patrick$ |
---|
28 | */ |
---|
29 | public class GUIElementFactory implements IGUIElementFactory { |
---|
30 | |
---|
31 | /** */ |
---|
32 | private static GUIElementFactory instance = new GUIElementFactory(); |
---|
33 | |
---|
34 | /** |
---|
35 | * TODO: comment |
---|
36 | * |
---|
37 | */ |
---|
38 | private GUIElementFactory() { |
---|
39 | } |
---|
40 | |
---|
41 | /** |
---|
42 | * TODO: comment |
---|
43 | * |
---|
44 | * @return |
---|
45 | */ |
---|
46 | public static synchronized GUIElementFactory getInstance() { |
---|
47 | return instance; |
---|
48 | } |
---|
49 | |
---|
50 | |
---|
51 | /** */ |
---|
52 | private Properties mappingsFromConfiguration; |
---|
53 | |
---|
54 | /** |
---|
55 | * TODO: comment |
---|
56 | * |
---|
57 | * @param object1 |
---|
58 | * @param object2 |
---|
59 | * @return |
---|
60 | */ |
---|
61 | protected boolean equals(Object object1, Object object2) { |
---|
62 | if (object1 == object2) { |
---|
63 | return true; |
---|
64 | } |
---|
65 | else if (object1 != null) { |
---|
66 | return object1.equals(object2); |
---|
67 | } |
---|
68 | else { |
---|
69 | // object 1 is null but object 2 not --> return false |
---|
70 | return false; |
---|
71 | } |
---|
72 | } |
---|
73 | |
---|
74 | /** |
---|
75 | * TODO: comment |
---|
76 | * |
---|
77 | * @param parameterTypes |
---|
78 | * @param parameters |
---|
79 | * @return |
---|
80 | * @throws GUIModelConfigurationException |
---|
81 | */ |
---|
82 | @Override |
---|
83 | public IGUIElement instantiateGUIElement(IGUIElementSpec specification, IGUIElement parent) |
---|
84 | throws GUIModelConfigurationException |
---|
85 | { |
---|
86 | Properties mappings = getMappingsFromConfiguration(); |
---|
87 | IGUIElement guiElement = null; |
---|
88 | |
---|
89 | String className = mappings.getProperty(specification.getType()); |
---|
90 | if (className != null) { |
---|
91 | try { |
---|
92 | Class<?> clazz = this.getClass().getClassLoader().loadClass(className); |
---|
93 | |
---|
94 | if (!IGUIElement.class.isAssignableFrom(clazz)) { |
---|
95 | Logger.getLogger(this.getClass().getName()).warning |
---|
96 | ("configured GUI element representing class " + className + |
---|
97 | " is no valid GUIElement " + "derivate."); |
---|
98 | |
---|
99 | return null; |
---|
100 | } |
---|
101 | |
---|
102 | Class<?>[] parameterTypes = new Class<?>[2]; |
---|
103 | parameterTypes[0] = specification.getClass(); |
---|
104 | parameterTypes[1] = parent.getClass(); |
---|
105 | |
---|
106 | guiElement = (IGUIElement) clazz.getConstructor(parameterTypes).newInstance |
---|
107 | (specification, parent); |
---|
108 | |
---|
109 | return guiElement; |
---|
110 | } |
---|
111 | catch (ClassNotFoundException e) { |
---|
112 | Logger.getLogger(this.getClass().getName()).warning |
---|
113 | ("configured GUI element representing class " + className + |
---|
114 | " can not be loaded."); |
---|
115 | throw new GUIModelConfigurationException |
---|
116 | ("configured GUI element representing class " + className + |
---|
117 | " can not be loaded.", e); |
---|
118 | } |
---|
119 | catch (SecurityException e) { |
---|
120 | Logger.getLogger(this.getClass().getName()).log |
---|
121 | (Level.WARNING, "configured GUI element representing class " + className + |
---|
122 | " can not be instantiated due to security reasons.", e); |
---|
123 | throw new GUIModelConfigurationException |
---|
124 | ("configured GUI element representing class " + className + |
---|
125 | " can not be instantiated due to security reasons.", e); |
---|
126 | } |
---|
127 | catch (NoSuchMethodException e) { |
---|
128 | Logger.getLogger(this.getClass().getName()).warning |
---|
129 | ("configured GUI element representing class " + className + |
---|
130 | " does not provide an appropriate constructur."); |
---|
131 | throw new GUIModelConfigurationException |
---|
132 | ("configured GUI element representing class " + className + |
---|
133 | " does not provide an appropriate constructur.", e); |
---|
134 | } |
---|
135 | catch (IllegalArgumentException e) { |
---|
136 | Logger.getLogger(this.getClass().getName()).warning |
---|
137 | ("configured GUI element representing class " + className + " does not " + |
---|
138 | "provide an appropriate constructur accepting the provided parameters."); |
---|
139 | throw new GUIModelConfigurationException |
---|
140 | ("configured GUI element representing class " + className + " does not " + |
---|
141 | "provide an appropriate constructur accepting the provided parameters.", e); |
---|
142 | } |
---|
143 | catch (InstantiationException e) { |
---|
144 | Logger.getLogger(this.getClass().getName()).log |
---|
145 | (Level.WARNING, "configured GUI element representing class " + className + |
---|
146 | " can not be instantiated.", e); |
---|
147 | throw new GUIModelConfigurationException |
---|
148 | ("configured GUI element representing class " + className + |
---|
149 | " can not be instantiated.", e); |
---|
150 | } |
---|
151 | catch (IllegalAccessException e) { |
---|
152 | Logger.getLogger(this.getClass().getName()).log |
---|
153 | (Level.WARNING, "configured GUI element representing class " + className + |
---|
154 | " can not be instantiated.", e); |
---|
155 | throw new GUIModelConfigurationException |
---|
156 | ("configured GUI element representing class " + className + |
---|
157 | " can not be instantiated.", e); |
---|
158 | } |
---|
159 | catch (InvocationTargetException e) { |
---|
160 | Logger.getLogger(this.getClass().getName()).log |
---|
161 | (Level.WARNING, "configured GUI element representing class " + className + |
---|
162 | " can not be instantiated.", e); |
---|
163 | throw new GUIModelConfigurationException |
---|
164 | ("configured GUI element representing class " + className + |
---|
165 | " can not be instantiated.", e); |
---|
166 | } |
---|
167 | } |
---|
168 | if( guiElement==null ) { |
---|
169 | Console.printerrln("could not find GUI element representing class " + specification.getType()); |
---|
170 | } |
---|
171 | |
---|
172 | return guiElement; |
---|
173 | } |
---|
174 | |
---|
175 | /** |
---|
176 | * TODO: comment |
---|
177 | * |
---|
178 | * @return |
---|
179 | */ |
---|
180 | private synchronized Properties getMappingsFromConfiguration() |
---|
181 | throws GUIModelConfigurationException |
---|
182 | { |
---|
183 | if (mappingsFromConfiguration != null) { |
---|
184 | return mappingsFromConfiguration; |
---|
185 | } |
---|
186 | else { |
---|
187 | mappingsFromConfiguration = new Properties(); |
---|
188 | |
---|
189 | File mappingsFolder = new File("data/guimappings"); |
---|
190 | File[] children = mappingsFolder.listFiles(); |
---|
191 | |
---|
192 | if (children != null) { |
---|
193 | for (File mappingsFile : children) { |
---|
194 | if (!mappingsFile.isDirectory() && |
---|
195 | mappingsFile.getName().startsWith("guimapping") && |
---|
196 | mappingsFile.getName().endsWith(".txt")) |
---|
197 | { |
---|
198 | InputStream inStream; |
---|
199 | try { |
---|
200 | inStream = new FileInputStream(mappingsFile); |
---|
201 | mappingsFromConfiguration.load(inStream); |
---|
202 | inStream.close(); |
---|
203 | } |
---|
204 | catch (FileNotFoundException e) { |
---|
205 | throw new GUIModelConfigurationException |
---|
206 | ("could not read mapping configuration file " + mappingsFile, e); |
---|
207 | } |
---|
208 | catch (IOException e) { |
---|
209 | throw new GUIModelConfigurationException |
---|
210 | ("could not read mapping configuration file " + mappingsFile, e); |
---|
211 | } |
---|
212 | } |
---|
213 | } |
---|
214 | } |
---|
215 | else { |
---|
216 | throw new GUIModelConfigurationException |
---|
217 | ("no GUI mappings file provided in folder " + mappingsFolder); |
---|
218 | } |
---|
219 | |
---|
220 | return mappingsFromConfiguration; |
---|
221 | } |
---|
222 | } |
---|
223 | } |
---|