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

Last change on this file since 655 was 655, checked in by pharms, 12 years ago
  • removed old copyright file header
File size: 10.4 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;
12import java.util.logging.Logger;
13
14/**
15 * TODO comment
16 *
17 * TODO rename class to GUIElementFactory
18 *
19 * @version $Revision: $ $Date: 13.05.2012$
20 * @author 2012, last modified by $Author: patrick$
21 */
22public class GUIElementFactory implements IGUIElementFactory {
23   
24    /** */
25    private static GUIElementFactory instance = new GUIElementFactory();
26
27    /**
28     * TODO: comment
29     *
30     */
31    private GUIElementFactory() {
32    }
33
34    /**
35     * TODO: comment
36     *
37     * @return
38     */
39    public static synchronized GUIElementFactory getInstance() {
40        return instance;
41    }
42
43   
44    /** */
45    private Properties mappingsFromConfiguration;
46
47    /**
48     * TODO: comment
49     *
50     * @param object1
51     * @param object2
52     * @return
53     */
54    protected boolean equals(Object object1, Object object2) {
55        if (object1 == object2) {
56            return true;
57        }
58        else if (object1 != null) {
59            return object1.equals(object2);
60        }
61        else {
62            // object 1 is null but object 2 not --> return false
63            return false;
64        }
65    }
66
67    /**
68     * TODO: comment
69     *
70     * @param parameterTypes
71     * @param parameters
72     * @return
73     * @throws GUIModelConfigurationException
74     */
75    @Override
76    public IGUIElement instantiateGUIElement(IGUIElementSpec specification, IGUIElement parent)
77        throws GUIModelConfigurationException
78    {
79        Properties mappings = getMappingsFromConfiguration();
80        IGUIElement guiElement = null;
81
82        String className = mappings.getProperty(specification.getType());
83        if (className != null) {
84            try {
85                Class<?> clazz = this.getClass().getClassLoader().loadClass(className);
86
87                if (!IGUIElement.class.isAssignableFrom(clazz)) {
88                    Logger.getLogger(this.getClass().getName()).warning
89                        ("configured GUI element representing class " + className +
90                         " 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                Logger.getLogger(this.getClass().getName()).warning
146                    ("configured GUI element representing class " + className +
147                     " can not be loaded.");
148                throw new GUIModelConfigurationException
149                    ("configured GUI element representing class " + className +
150                     " can not be loaded.", e);
151            }
152            catch (SecurityException e) {
153                Logger.getLogger(this.getClass().getName()).log
154                    (Level.WARNING, "configured GUI element representing class " + className +
155                     " can not be instantiated due to security reasons.", e);
156                throw new GUIModelConfigurationException
157                    ("configured GUI element representing class " + className +
158                     " can not be instantiated due to security reasons.", e);
159            }
160            catch (NoSuchMethodException e) {
161                Logger.getLogger(this.getClass().getName()).warning
162                    ("configured GUI element representing class " + className +
163                     " does not provide an appropriate constructur.");
164                throw new GUIModelConfigurationException
165                    ("configured GUI element representing class " + className +
166                     " does not provide an appropriate constructur.", e);
167            }
168            catch (IllegalArgumentException e) {
169                Logger.getLogger(this.getClass().getName()).warning
170                    ("configured GUI element representing class " + className + " does not " +
171                     "provide an appropriate constructur accepting the provided parameters.");
172                throw new GUIModelConfigurationException
173                    ("configured GUI element representing class " + className + " does not " +
174                     "provide an appropriate constructur accepting the provided parameters.", e);
175            }
176            catch (InstantiationException e) {
177                Logger.getLogger(this.getClass().getName()).log
178                    (Level.WARNING, "configured GUI element representing class " + className +
179                     " can not be instantiated.", e);
180                throw new GUIModelConfigurationException
181                    ("configured GUI element representing class " + className +
182                     " can not be instantiated.", e);
183            }
184            catch (IllegalAccessException e) {
185                Logger.getLogger(this.getClass().getName()).log
186                    (Level.WARNING, "configured GUI element representing class " + className +
187                     " can not be instantiated.", e);
188                throw new GUIModelConfigurationException
189                    ("configured GUI element representing class " + className +
190                     " can not be instantiated.", e);
191            }
192            catch (InvocationTargetException e) {
193                Logger.getLogger(this.getClass().getName()).log
194                    (Level.WARNING, "configured GUI element representing class " + className +
195                     " can not be instantiated.", e);
196                throw new GUIModelConfigurationException
197                    ("configured GUI element representing class " + className +
198                     " can not be instantiated.", e);
199            }
200        }
201       
202        if (guiElement == null ) {
203            Logger.getLogger(this.getClass().getName()).log
204                (Level.WARNING, "no class representing GUI elements of type " +
205                 specification.getType() + " found. Please extends GUI element mapping files.");
206            throw new GUIModelConfigurationException
207                ("no class representing GUI elements of type " + specification.getType() +
208                 " found. Please extends GUI element mapping files");
209        }
210
211        return guiElement;
212    }
213
214    /**
215     * TODO: comment
216     *
217     * @return
218     */
219    private synchronized Properties getMappingsFromConfiguration()
220        throws GUIModelConfigurationException
221    {
222        if (mappingsFromConfiguration != null) {
223            return mappingsFromConfiguration;
224        }
225        else {
226            mappingsFromConfiguration = new Properties();
227           
228            File mappingsFolder = new File("data/guimappings");
229            File[] children = mappingsFolder.listFiles();
230           
231            if (children != null) {
232                for (File mappingsFile : children) {
233                    if (!mappingsFile.isDirectory() &&
234                        mappingsFile.getName().startsWith("guimapping") &&
235                        mappingsFile.getName().endsWith(".txt"))
236                    {
237                        InputStream inStream;
238                        try {
239                            inStream = new FileInputStream(mappingsFile);
240                            mappingsFromConfiguration.load(inStream);
241                            inStream.close();
242                        }
243                        catch (FileNotFoundException e) {
244                            throw new GUIModelConfigurationException
245                                ("could not read mapping configuration file " + mappingsFile, e);
246                        }
247                        catch (IOException e) {
248                            throw new GUIModelConfigurationException
249                                ("could not read mapping configuration file " + mappingsFile, e);
250                        }
251                    }
252                }
253            }
254            else {
255                throw new GUIModelConfigurationException
256                    ("no GUI mappings file provided in folder " + mappingsFolder);
257            }
258
259            return mappingsFromConfiguration;
260        }
261    }
262}
Note: See TracBrowser for help on using the repository browser.