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

Last change on this file since 585 was 585, checked in by sherbold, 12 years ago
  • rewrote create of GUI elements by the IGUIElementFactory implementations. We now do not require platform-specific GUI element factories. Instead, mappings files located in the folder data/guimappings that start with guimapping- and and with .txt are used to determine which classes in the GUI structure belong to which GUI elements.
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 AbstractDefaultGUIElementFactory implements IGUIElementFactory {
30   
31    /** */
32    private static AbstractDefaultGUIElementFactory instance = new AbstractDefaultGUIElementFactory();
33
34    /**
35     * TODO: comment
36     *
37     */
38    private AbstractDefaultGUIElementFactory() {
39    }
40
41    /**
42     * TODO: comment
43     *
44     * @return
45     */
46    public static synchronized AbstractDefaultGUIElementFactory 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        Properties mappings = getMappingsFromConfiguration();
85        IGUIElement guiElement = null;
86
87        String className = mappings.getProperty(specification.getType());
88        if (className != null) {
89            try {
90                Class<?> clazz = this.getClass().getClassLoader().loadClass(className);
91
92                if (!IGUIElement.class.isAssignableFrom(clazz)) {
93                    Logger.getLogger(this.getClass().getName()).warning
94                        ("configured GUI element representing class " + className +
95                         " is no valid GUIElement " + "derivate.");
96
97                    return null;
98                }
99
100                Class<?>[] parameterTypes = new Class<?>[1];
101                parameterTypes[0] = specification.getClass();
102
103                guiElement =
104                    (IGUIElement) clazz.getConstructor(parameterTypes).newInstance(specification);
105
106                return guiElement;
107            }
108            catch (ClassNotFoundException e) {
109                Logger.getLogger(this.getClass().getName()).warning
110                    ("configured GUI element representing class " + className +
111                     " can not be loaded.");
112                throw new GUIModelConfigurationException
113                    ("configured GUI element representing class " + className +
114                     " can not be loaded.", e);
115            }
116            catch (SecurityException e) {
117                Logger.getLogger(this.getClass().getName()).log
118                    (Level.WARNING, "configured GUI element representing class " + className +
119                     " can not be instantiated due to security reasons.", e);
120                throw new GUIModelConfigurationException
121                    ("configured GUI element representing class " + className +
122                     " can not be instantiated due to security reasons.", e);
123            }
124            catch (NoSuchMethodException e) {
125                Logger.getLogger(this.getClass().getName()).warning
126                    ("configured GUI element representing class " + className +
127                     " does not provide an appropriate constructur.");
128                throw new GUIModelConfigurationException
129                    ("configured GUI element representing class " + className +
130                     " does not provide an appropriate constructur.", e);
131            }
132            catch (IllegalArgumentException e) {
133                Logger.getLogger(this.getClass().getName()).warning
134                    ("configured GUI element representing class " + className + " does not " +
135                     "provide an appropriate constructur accepting the provided parameters.");
136                throw new GUIModelConfigurationException
137                    ("configured GUI element representing class " + className + " does not " +
138                     "provide an appropriate constructur accepting the provided parameters.", e);
139            }
140            catch (InstantiationException e) {
141                Logger.getLogger(this.getClass().getName()).log
142                    (Level.WARNING, "configured GUI element representing class " + className +
143                     " can not be instantiated.", e);
144                throw new GUIModelConfigurationException
145                    ("configured GUI element representing class " + className +
146                     " can not be instantiated.", e);
147            }
148            catch (IllegalAccessException e) {
149                Logger.getLogger(this.getClass().getName()).log
150                    (Level.WARNING, "configured GUI element representing class " + className +
151                     " can not be instantiated.", e);
152                throw new GUIModelConfigurationException
153                    ("configured GUI element representing class " + className +
154                     " can not be instantiated.", e);
155            }
156            catch (InvocationTargetException e) {
157                Logger.getLogger(this.getClass().getName()).log
158                    (Level.WARNING, "configured GUI element representing class " + className +
159                     " can not be instantiated.", e);
160                throw new GUIModelConfigurationException
161                    ("configured GUI element representing class " + className +
162                     " can not be instantiated.", e);
163            }
164        }
165        if( guiElement==null ) {
166            Console.printerrln("could not find GUI element representing class " + specification.getType());
167        }
168
169        return guiElement;
170    }
171
172    /**
173     * TODO: comment
174     *
175     * @return
176     */
177    private synchronized Properties getMappingsFromConfiguration() {
178        if (mappingsFromConfiguration != null) {
179            return mappingsFromConfiguration;
180        }
181        else {
182            mappingsFromConfiguration = new Properties();
183           
184            File mappingsFolder = new File("data/guimappings");
185            for( File mappingsFile : mappingsFolder.listFiles()) {
186                if(!mappingsFile.isDirectory() && mappingsFile.getName().startsWith("guimapping") && mappingsFile.getName().endsWith(".txt")) {
187                    InputStream inStream;
188                    try {
189                        inStream = new FileInputStream(mappingsFile);
190                        mappingsFromConfiguration.load(inStream);
191                    }
192                    catch (FileNotFoundException e1) {
193                        // TODO Auto-generated catch block
194                        e1.printStackTrace();
195                    }
196                    catch (IOException e) {
197                        Logger.getLogger(this.getClass().getName()).warning
198                            ("invalid GUI mappings files " + mappingsFile.getName());
199                    }
200                }
201            }
202
203            return mappingsFromConfiguration;
204        }
205    }
206}
Note: See TracBrowser for help on using the repository browser.