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

Last change on this file since 545 was 545, checked in by pharms, 12 years ago
  • first version of event targets for GUI events including the GUI model management and the support for the instantiation of default GUI elements
File size: 8.1 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.IOException;
10import java.io.InputStream;
11import java.lang.reflect.InvocationTargetException;
12import java.util.Properties;
13import java.util.logging.Level;
14import java.util.logging.Logger;
15
16/**
17 * TODO comment
18 *
19 * @version $Revision: $ $Date: 13.05.2012$
20 * @author 2012, last modified by $Author: patrick$
21 */
22public abstract class AbstractDefaultGUIElementFactory {
23   
24    /** */
25    private Properties mMappingsFromConfiguration;
26
27    /**
28     * TODO: comment
29     *
30     * @param object1
31     * @param object2
32     * @return
33     */
34    protected boolean equals(Object object1, Object object2) {
35        if (object1 == object2) {
36            return true;
37        }
38        else if (object1 != null) {
39            return object1.equals(object2);
40        }
41        else {
42            return object2 == null;
43        }
44    }
45
46    /**
47     * TODO: comment
48     *
49     * @param parameterTypes
50     * @param parameters
51     * @return
52     * @throws GUIModelConfigurationException
53     */
54    protected IGUIElement instantiateGUIElementFromConfiguredMappings(String type,
55                                                                      Object... parameters)
56        throws GUIModelConfigurationException
57    {
58        Properties mappings = getMappingsFromConfiguration();
59
60        String className = mappings.getProperty(type);
61        if (className != null) {
62            try {
63                Class<?> clazz = this.getClass().getClassLoader().loadClass(className);
64
65                if (!IGUIElement.class.isAssignableFrom(clazz)) {
66                    Logger.getLogger(this.getClass().getName()).warning
67                        ("configured GUI element representing class " + className +
68                         " is no valid GUIElement " + "derivate.");
69
70                    return null;
71                }
72
73                Class<?>[] parameterTypes = new Class<?>[parameters.length];
74
75                for (int i = 0; i < parameters.length; i++) {
76                    parameterTypes[i] = parameters[i].getClass();
77
78                    if (Boolean.class.equals(parameterTypes[i])) {
79                        parameterTypes[i] = boolean.class;
80                    }
81                    else if (Integer.class.equals(parameterTypes[i])) {
82                        parameterTypes[i] = int.class;
83                    }
84                    else if (Double.class.equals(parameterTypes[i])) {
85                        parameterTypes[i] = double.class;
86                    }
87                    else if (Character.class.equals(parameterTypes[i])) {
88                        parameterTypes[i] = char.class;
89                    }
90                    else if (Byte.class.equals(parameterTypes[i])) {
91                        parameterTypes[i] = byte.class;
92                    }
93                    else if (Float.class.equals(parameterTypes[i])) {
94                        parameterTypes[i] = float.class;
95                    }
96                    else if (Long.class.equals(parameterTypes[i])) {
97                        parameterTypes[i] = long.class;
98                    }
99                    else if (Short.class.equals(parameterTypes[i])) {
100                        parameterTypes[i] = short.class;
101                    }
102                }
103
104                IGUIElement guiElement =
105                    (IGUIElement) clazz.getConstructor(parameterTypes).newInstance(parameters);
106
107                if (guiElement instanceof AbstractDefaultGUIElement) {
108                    ((AbstractDefaultGUIElement) guiElement).setOriginalTypeInfo(type);
109                }
110
111                return guiElement;
112            }
113            catch (ClassNotFoundException e) {
114                Logger.getLogger(this.getClass().getName()).warning
115                    ("configured GUI element representing class " + className +
116                     " can not be loaded.");
117                throw new GUIModelConfigurationException
118                    ("configured GUI element representing class " + className +
119                     " can not be loaded.", e);
120            }
121            catch (SecurityException e) {
122                Logger.getLogger(this.getClass().getName()).log
123                    (Level.WARNING, "configured GUI element representing class " + className +
124                     " can not be instantiated due to security reasons.", e);
125                throw new GUIModelConfigurationException
126                    ("configured GUI element representing class " + className +
127                     " can not be instantiated due to security reasons.", e);
128            }
129            catch (NoSuchMethodException e) {
130                Logger.getLogger(this.getClass().getName()).warning
131                    ("configured GUI element representing class " + className +
132                     " does not provide an appropriate constructur.");
133                throw new GUIModelConfigurationException
134                    ("configured GUI element representing class " + className +
135                     " does not provide an appropriate constructur.", e);
136            }
137            catch (IllegalArgumentException e) {
138                Logger.getLogger(this.getClass().getName()).warning
139                    ("configured GUI element representing class " + className + " does not " +
140                     "provide an appropriate constructur accepting the provided parameters.");
141                throw new GUIModelConfigurationException
142                    ("configured GUI element representing class " + className + " does not " +
143                     "provide an appropriate constructur accepting the provided parameters.", e);
144            }
145            catch (InstantiationException e) {
146                Logger.getLogger(this.getClass().getName()).log
147                    (Level.WARNING, "configured GUI element representing class " + className +
148                     " can not be instantiated.", e);
149                throw new GUIModelConfigurationException
150                    ("configured GUI element representing class " + className +
151                     " can not be instantiated.", e);
152            }
153            catch (IllegalAccessException e) {
154                Logger.getLogger(this.getClass().getName()).log
155                    (Level.WARNING, "configured GUI element representing class " + className +
156                     " can not be instantiated.", e);
157                throw new GUIModelConfigurationException
158                    ("configured GUI element representing class " + className +
159                     " can not be instantiated.", e);
160            }
161            catch (InvocationTargetException e) {
162                Logger.getLogger(this.getClass().getName()).log
163                    (Level.WARNING, "configured GUI element representing class " + className +
164                     " can not be instantiated.", e);
165                throw new GUIModelConfigurationException
166                    ("configured GUI element representing class " + className +
167                     " can not be instantiated.", e);
168            }
169        }
170
171        return null;
172    }
173
174    /**
175     * TODO: comment
176     *
177     * @return
178     */
179    private synchronized Properties getMappingsFromConfiguration() {
180        if (mMappingsFromConfiguration != null) {
181            return mMappingsFromConfiguration;
182        }
183        else {
184            mMappingsFromConfiguration = new Properties();
185
186            InputStream inStream =
187                this.getClass().getClassLoader().getResourceAsStream("GUIElementMapping.txt");
188
189            if (inStream != null) {
190                try {
191                    mMappingsFromConfiguration.load(inStream);
192                }
193                catch (IOException e) {
194                    Logger.getLogger(this.getClass().getName()).warning
195                        ("could not load GUIElementMapping.txt from classpath, but this may be " +
196                         "intended");
197                }
198            }
199
200            return mMappingsFromConfiguration;
201        }
202    }
203}
Note: See TracBrowser for help on using the repository browser.