source: trunk/quest-core-events/src/main/java/de/ugoe/cs/quest/eventcore/guimodel/GUIElementFactory.java @ 744

Last change on this file since 744 was 744, checked in by pharms, 12 years ago
  • removed find bugs warning
File size: 10.5 KB
Line 
1package de.ugoe.cs.quest.eventcore.guimodel;
2
3import java.io.File;
4import java.io.FileInputStream;
5import java.io.FileNotFoundException;
6import java.io.IOException;
7import java.io.InputStream;
8import java.lang.reflect.Constructor;
9import java.lang.reflect.InvocationTargetException;
10import java.util.Properties;
11import java.util.logging.Level;
12
13import de.ugoe.cs.util.console.Console;
14
15/**
16 * TODO comment
17 *
18 * TODO rename class to GUIElementFactory
19 *
20 * @version $Revision: $ $Date: 13.05.2012$
21 * @author 2012, last modified by $Author: patrick$
22 */
23public class GUIElementFactory implements IGUIElementFactory {
24   
25    /** */
26    private static GUIElementFactory instance = new GUIElementFactory();
27
28    /**
29     * TODO: comment
30     *
31     */
32    private GUIElementFactory() {
33    }
34
35    /**
36     * TODO: comment
37     *
38     * @return
39     */
40    public static synchronized GUIElementFactory getInstance() {
41        return instance;
42    }
43
44   
45    /** */
46    private Properties mappingsFromConfiguration;
47
48    /**
49     * TODO: comment
50     *
51     * @param object1
52     * @param object2
53     * @return
54     */
55    protected boolean equals(Object object1, Object object2) {
56        if (object1 == object2) {
57            return true;
58        }
59        else if (object1 != null) {
60            return object1.equals(object2);
61        }
62        else {
63            // object 1 is null but object 2 not --> return false
64            return false;
65        }
66    }
67
68    /**
69     * TODO: comment
70     *
71     * @param parameterTypes
72     * @param parameters
73     * @return
74     * @throws GUIModelConfigurationException
75     */
76    @Override
77    public IGUIElement instantiateGUIElement(IGUIElementSpec specification, IGUIElement parent)
78        throws GUIModelConfigurationException
79    {
80        Properties mappings = getMappingsFromConfiguration();
81        IGUIElement guiElement = null;
82
83        String className = mappings.getProperty(specification.getType());
84        if (className != null) {
85            try {
86                Class<?> clazz = this.getClass().getClassLoader().loadClass(className);
87
88                if (!IGUIElement.class.isAssignableFrom(clazz)) {
89                    Console.traceln(Level.WARNING, "configured GUI element representing class " +
90                                    className + " is no valid GUIElement derivate.");
91
92                    return null;
93                }
94
95                Constructor<?> constructor = null;
96                Class<?> parentClass = (parent == null) ? null : parent.getClass();
97               
98                // search for a constructor, that perfectly matches the types
99                for (Constructor<?> candidate : clazz.getConstructors()) {
100                    if ((parentClass != null) &&
101                        (candidate.getParameterTypes().length == 2) &&
102                        (candidate.getParameterTypes()[0].equals(specification.getClass())) &&
103                        (candidate.getParameterTypes()[1].equals(parentClass)))
104                    {
105                        constructor = candidate;
106                        break;
107                    }
108                    else if (parentClass == null) {
109                        if ((candidate.getParameterTypes().length >= 1) &&
110                            (candidate.getParameterTypes()[0].equals(specification.getClass())))
111                        {
112                            constructor = candidate;
113                            break;
114                        }
115                    }
116                }
117               
118                if (constructor == null) {
119                    // search for an assignable constructor
120                    for (Constructor<?> candidate : clazz.getConstructors()) {
121                        if ((candidate.getParameterTypes().length == 2) &&
122                            (candidate.getParameterTypes()[0].isInstance(specification)) &&
123                            (candidate.getParameterTypes()[1].isInstance(parent)))
124                        {
125                            constructor = candidate;
126                            break;
127                        }
128                    }
129                   
130                }
131               
132                if (constructor != null) {
133                    guiElement = (IGUIElement) constructor.newInstance(specification, parent);
134                }
135                else {
136                    throw new NoSuchMethodException
137                        ("no constructor with two parameters and assignable parameter types for " +
138                         specification.getClass() + " and " +
139                         (parent != null ? parent.getClass() : "null") + " found in class " +
140                         clazz);
141                }
142
143            }
144            catch (ClassNotFoundException e) {
145                Console.traceln(Level.WARNING, "configured GUI element representing class " +
146                                className + " can not be loaded.");
147                throw new GUIModelConfigurationException
148                    ("configured GUI element representing class " + className +
149                     " can not be loaded.", e);
150            }
151            catch (SecurityException e) {
152                Console.traceln(Level.WARNING, "configured GUI element representing class " +
153                                className + " can not be instantiated due to security reasons.");
154                throw new GUIModelConfigurationException
155                    ("configured GUI element representing class " + className +
156                     " can not be instantiated due to security reasons.", e);
157            }
158            catch (NoSuchMethodException e) {
159                Console.traceln(Level.WARNING, "configured GUI element representing class " +
160                                className + " does not provide an appropriate constructor.");
161                throw new GUIModelConfigurationException
162                    ("configured GUI element representing class " + className +
163                     " does not provide an appropriate constructor.", e);
164            }
165            catch (IllegalArgumentException e) {
166                Console.traceln(Level.WARNING, "configured GUI element representing class " +
167                                className + " does not provide an appropriate constructor " +
168                                "accepting the provided parameters.");
169                throw new GUIModelConfigurationException
170                    ("configured GUI element representing class " + className + " does not " +
171                     "provide an appropriate constructor accepting the provided parameters.", e);
172            }
173            catch (InstantiationException e) {
174                Console.traceln(Level.WARNING, "configured GUI element representing class " +
175                                className + " can not be instantiated.");
176                throw new GUIModelConfigurationException
177                    ("configured GUI element representing class " + className +
178                     " can not be instantiated.", e);
179            }
180            catch (IllegalAccessException e) {
181                Console.traceln(Level.WARNING, "configured GUI element representing class " +
182                                className + " can not be instantiated.");
183                throw new GUIModelConfigurationException
184                    ("configured GUI element representing class " + className +
185                     " can not be instantiated.", e);
186            }
187            catch (InvocationTargetException e) {
188                Console.traceln(Level.WARNING, "configured GUI element representing class " +
189                                className + " can not be instantiated.");
190                throw new GUIModelConfigurationException
191                    ("configured GUI element representing class " + className +
192                     " can not be instantiated.", e);
193            }
194        }
195       
196        if (guiElement == null ) {
197            Console.traceln(Level.WARNING, "no class representing GUI elements of type " +
198                            specification.getType() + " found. Please extend GUI element " +
199                            "mapping files.");
200            throw new GUIModelConfigurationException
201                ("no class representing GUI elements of type " + specification.getType() +
202                 " found. Please extend GUI element mapping files");
203        }
204
205        return guiElement;
206    }
207
208    /**
209     * TODO: comment
210     *
211     * @return
212     */
213    private synchronized Properties getMappingsFromConfiguration()
214        throws GUIModelConfigurationException
215    {
216        if (mappingsFromConfiguration != null) {
217            return mappingsFromConfiguration;
218        }
219        else {
220            mappingsFromConfiguration = new Properties();
221           
222            File mappingsFolder = new File("data/guimappings");
223            File[] children = mappingsFolder.listFiles();
224           
225            if (children != null) {
226                for (File mappingsFile : children) {
227                    if (!mappingsFile.isDirectory() &&
228                        mappingsFile.getName().startsWith("guimapping") &&
229                        mappingsFile.getName().endsWith(".txt"))
230                    {
231                        InputStream inStream = null;
232                        try {
233                            inStream = new FileInputStream(mappingsFile);
234                            mappingsFromConfiguration.load(inStream);
235                        }
236                        catch (FileNotFoundException e) {
237                            throw new GUIModelConfigurationException
238                                ("could not read mapping configuration file " + mappingsFile, e);
239                        }
240                        catch (IOException e) {
241                            throw new GUIModelConfigurationException
242                                ("could not read mapping configuration file " + mappingsFile, e);
243                        }
244                        finally {
245                            if (inStream != null) {
246                                try {
247                                    inStream.close();
248                                }
249                                catch (IOException e) {
250                                    // ignore
251                                }
252                            }
253                        }
254                    }
255                }
256            }
257            else {
258                throw new GUIModelConfigurationException
259                    ("no GUI mappings file provided in folder " + mappingsFolder);
260            }
261
262            return mappingsFromConfiguration;
263        }
264    }
265}
Note: See TracBrowser for help on using the repository browser.