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

Last change on this file since 592 was 592, checked in by pharms, 12 years ago
  • renamed former abstract factory so that the class names shows, that it is no more abstract and that is does not need to be overwritten
File size: 7.7 KB
Line 
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
7package de.ugoe.cs.quest.eventcore.guimodel;
8
9import java.io.File;
10import java.io.FileInputStream;
11import java.io.FileNotFoundException;
12import java.io.IOException;
13import java.io.InputStream;
14import java.lang.reflect.InvocationTargetException;
15import java.util.Properties;
16import java.util.logging.Level;
17import java.util.logging.Logger;
18
19import 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 */
29public 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)
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<?>[1];
103                parameterTypes[0] = specification.getClass();
104
105                guiElement =
106                    (IGUIElement) clazz.getConstructor(parameterTypes).newInstance(specification);
107
108                return guiElement;
109            }
110            catch (ClassNotFoundException e) {
111                Logger.getLogger(this.getClass().getName()).warning
112                    ("configured GUI element representing class " + className +
113                     " can not be loaded.");
114                throw new GUIModelConfigurationException
115                    ("configured GUI element representing class " + className +
116                     " can not be loaded.", e);
117            }
118            catch (SecurityException e) {
119                Logger.getLogger(this.getClass().getName()).log
120                    (Level.WARNING, "configured GUI element representing class " + className +
121                     " can not be instantiated due to security reasons.", e);
122                throw new GUIModelConfigurationException
123                    ("configured GUI element representing class " + className +
124                     " can not be instantiated due to security reasons.", e);
125            }
126            catch (NoSuchMethodException e) {
127                Logger.getLogger(this.getClass().getName()).warning
128                    ("configured GUI element representing class " + className +
129                     " does not provide an appropriate constructur.");
130                throw new GUIModelConfigurationException
131                    ("configured GUI element representing class " + className +
132                     " does not provide an appropriate constructur.", e);
133            }
134            catch (IllegalArgumentException e) {
135                Logger.getLogger(this.getClass().getName()).warning
136                    ("configured GUI element representing class " + className + " does not " +
137                     "provide an appropriate constructur accepting the provided parameters.");
138                throw new GUIModelConfigurationException
139                    ("configured GUI element representing class " + className + " does not " +
140                     "provide an appropriate constructur accepting the provided parameters.", e);
141            }
142            catch (InstantiationException e) {
143                Logger.getLogger(this.getClass().getName()).log
144                    (Level.WARNING, "configured GUI element representing class " + className +
145                     " can not be instantiated.", e);
146                throw new GUIModelConfigurationException
147                    ("configured GUI element representing class " + className +
148                     " can not be instantiated.", e);
149            }
150            catch (IllegalAccessException e) {
151                Logger.getLogger(this.getClass().getName()).log
152                    (Level.WARNING, "configured GUI element representing class " + className +
153                     " can not be instantiated.", e);
154                throw new GUIModelConfigurationException
155                    ("configured GUI element representing class " + className +
156                     " can not be instantiated.", e);
157            }
158            catch (InvocationTargetException e) {
159                Logger.getLogger(this.getClass().getName()).log
160                    (Level.WARNING, "configured GUI element representing class " + className +
161                     " can not be instantiated.", e);
162                throw new GUIModelConfigurationException
163                    ("configured GUI element representing class " + className +
164                     " can not be instantiated.", e);
165            }
166        }
167        if( guiElement==null ) {
168            Console.printerrln("could not find GUI element representing class " + specification.getType());
169        }
170
171        return guiElement;
172    }
173
174    /**
175     * TODO: comment
176     *
177     * @return
178     */
179    private synchronized Properties getMappingsFromConfiguration() {
180        if (mappingsFromConfiguration != null) {
181            return mappingsFromConfiguration;
182        }
183        else {
184            mappingsFromConfiguration = new Properties();
185           
186            File mappingsFolder = new File("data/guimappings");
187            for( File mappingsFile : mappingsFolder.listFiles()) {
188                if(!mappingsFile.isDirectory() && mappingsFile.getName().startsWith("guimapping") && mappingsFile.getName().endsWith(".txt")) {
189                    InputStream inStream;
190                    try {
191                        inStream = new FileInputStream(mappingsFile);
192                        mappingsFromConfiguration.load(inStream);
193                        inStream.close();
194                    }
195                    catch (FileNotFoundException e1) {
196                        // TODO Auto-generated catch block
197                        e1.printStackTrace();
198                    }
199                    catch (IOException e) {
200                        Logger.getLogger(this.getClass().getName()).warning
201                            ("invalid GUI mappings files " + mappingsFile.getName());
202                    }
203                }
204            }
205
206            return mappingsFromConfiguration;
207        }
208    }
209}
Note: See TracBrowser for help on using the repository browser.