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

Last change on this file since 449 was 449, checked in by pharms, 12 years ago

Initial import.

File size: 9.7 KB
Line 
1//-------------------------------------------------------------------------------------------------
2// Module    : $RCSfile: AbstractDefaultGUIElementFactory.java,v $
3// Version   : $Revision: 0.0 $  $Author: patrick $  $Date: 13.05.2012 $
4// Project   : GUIModel
5// Creation  : 2012 by patrick
6// Copyright : Patrick Harms, 2012
7//-------------------------------------------------------------------------------------------------
8package de.ugoe.cs.quest.eventcore.guimodel;
9
10import java.io.IOException;
11import java.io.InputStream;
12import java.lang.reflect.InvocationTargetException;
13import java.util.ArrayList;
14import java.util.HashMap;
15import java.util.List;
16import java.util.Map;
17import java.util.Properties;
18import java.util.logging.Level;
19import java.util.logging.Logger;
20
21//-------------------------------------------------------------------------------------------------
22/**
23 * TODO comment
24 *
25 * @version $Revision: $ $Date: 13.05.2012$
26 * @author 2012, last modified by $Author: patrick$
27 */
28//-------------------------------------------------------------------------------------------------
29public class AbstractDefaultGUIElementFactory
30{
31  /** */
32  private Properties mMappingsFromConfiguration;
33
34  /** */
35  private Map<String, List<GUIElement>> mGuiElementCache = new HashMap<String, List<GUIElement>>();
36 
37  //-----------------------------------------------------------------------------------------------
38  /**
39   * TODO: comment
40   *
41   * @param key
42   * @return
43   */
44  //-----------------------------------------------------------------------------------------------
45  protected List<GUIElement> getGUIElementCandidates(String key)
46  {
47    return mGuiElementCache.get(key);
48  }
49
50  //-----------------------------------------------------------------------------------------------
51  /**
52   * TODO: comment
53   *
54   * @param retVal
55   */
56  //-----------------------------------------------------------------------------------------------
57  protected void addGUIElementToCache(String key, GUIElement guiElement)
58  {
59    List<GUIElement> candidates = mGuiElementCache.get(key);
60   
61    if (candidates == null)
62    {
63      candidates = new ArrayList<GUIElement>();
64      mGuiElementCache.put(key, candidates);
65    }
66   
67    candidates.add(guiElement);
68  }
69
70  //-----------------------------------------------------------------------------------------------
71  /**
72   * TODO: comment
73   *
74   * @param icon
75   * @param icon2
76   * @return
77   */
78  //-----------------------------------------------------------------------------------------------
79  protected boolean equals(Object object1, Object object2)
80  {
81    if (object1 == object2)
82    {
83      return true;
84    }
85    else if (object1 != null)
86    {
87      return object1.equals(object2);
88    }
89    else
90    {
91      return object2 == null;
92    }
93  }
94
95  //-----------------------------------------------------------------------------------------------
96  /**
97   * TODO: comment
98   *
99   * @param name
100   * @return
101   */
102  //-----------------------------------------------------------------------------------------------
103  protected String getKey(String name)
104  {
105    if (name == null)
106    {
107      return "noName";
108    }
109    else
110    {
111      return name;
112    }
113  }
114
115  //-----------------------------------------------------------------------------------------------
116  /**
117   * TODO: comment
118   *
119   * @param parameterTypes
120   * @param parameters
121   * @return
122   * @throws GUIModelConfigurationException
123   */
124  //-----------------------------------------------------------------------------------------------
125  protected GUIElement instantiateGUIElementFromConfiguredMappings(String    type,
126                                                                   Object... parameters)
127    throws GUIModelConfigurationException
128  {
129    Properties mappings = getMappingsFromConfiguration();
130   
131    String className = mappings.getProperty(type);
132    if (className != null)
133    {
134      try
135      {
136        Class<?> clazz = this.getClass().getClassLoader().loadClass(className);
137       
138        if (!GUIElement.class.isAssignableFrom(clazz))
139        {
140          Logger.getLogger(this.getClass().getName()).warning
141            ("configured GUI element representing class " + className + " is no valid GUIElement " +
142             "derivate.");
143         
144          return null;
145        }
146       
147        Class<?>[] parameterTypes = new Class<?>[parameters.length];
148       
149        for (int i = 0; i < parameters.length; i++)
150        {
151          parameterTypes[i] = parameters[i].getClass();
152         
153          if (Boolean.class.equals(parameterTypes[i]))
154          {
155            parameterTypes[i] = boolean.class;
156          }
157          else if (Integer.class.equals(parameterTypes[i]))
158          {
159            parameterTypes[i] = int.class;
160          }
161          else if (Double.class.equals(parameterTypes[i]))
162          {
163            parameterTypes[i] = double.class;
164          }
165          else if (Character.class.equals(parameterTypes[i]))
166          {
167            parameterTypes[i] = char.class;
168          }
169          else if (Byte.class.equals(parameterTypes[i]))
170          {
171            parameterTypes[i] = byte.class;
172          }
173          else if (Float.class.equals(parameterTypes[i]))
174          {
175            parameterTypes[i] = float.class;
176          }
177          else if (Long.class.equals(parameterTypes[i]))
178          {
179            parameterTypes[i] = long.class;
180          }
181          else if (Short.class.equals(parameterTypes[i]))
182          {
183            parameterTypes[i] = short.class;
184          }
185        }
186       
187        GUIElement guiElement =
188          (GUIElement) clazz.getConstructor(parameterTypes).newInstance(parameters);
189       
190        if (guiElement instanceof AbstractDefaultGUIElement)
191        {
192          ((AbstractDefaultGUIElement) guiElement).setOriginalTypeInfo(type);
193        }
194       
195        return guiElement;
196      }
197      catch (ClassNotFoundException e)
198      {
199        Logger.getLogger(this.getClass().getName()).warning
200          ("configured GUI element representing class " + className + " can not be loaded.");
201        throw new GUIModelConfigurationException
202          ("configured GUI element representing class " + className + " can not be loaded.", e);
203      }
204      catch (SecurityException e)
205      {
206        Logger.getLogger(this.getClass().getName()).log
207          (Level.WARNING, "configured GUI element representing class " + className + " can not " +
208           "be instantiated due to security reasons.", e);
209        throw new GUIModelConfigurationException
210          ("configured GUI element representing class " + className + " can not " +
211           "be instantiated due to security reasons.", e);
212      }
213      catch (NoSuchMethodException e)
214      {
215        Logger.getLogger(this.getClass().getName()).warning
216          ("configured GUI element representing class " + className + " does not provide an " +
217           "appropriate constructur.");
218        throw new GUIModelConfigurationException
219          ("configured GUI element representing class " + className + " does not provide an " +
220           "appropriate constructur.", e);
221      }
222      catch (IllegalArgumentException e)
223      {
224        Logger.getLogger(this.getClass().getName()).warning
225          ("configured GUI element representing class " + className + " does not provide an " +
226           "appropriate constructur accepting the provided parameters.");
227        throw new GUIModelConfigurationException
228          ("configured GUI element representing class " + className + " does not provide an " +
229           "appropriate constructur accepting the provided parameters.", e);
230      }
231      catch (InstantiationException e)
232      {
233        Logger.getLogger(this.getClass().getName()).log
234          (Level.WARNING, "configured GUI element representing class " + className + " can not " +
235           "be instantiated.", e);
236        throw new GUIModelConfigurationException
237          ("configured GUI element representing class " + className + " can not " +
238           "be instantiated.", e);
239      }
240      catch (IllegalAccessException e)
241      {
242        Logger.getLogger(this.getClass().getName()).log
243          (Level.WARNING, "configured GUI element representing class " + className + " can not " +
244           "be instantiated.", e);
245        throw new GUIModelConfigurationException
246          ("configured GUI element representing class " + className + " can not " +
247           "be instantiated.", e);
248      }
249      catch (InvocationTargetException e)
250      {
251        Logger.getLogger(this.getClass().getName()).log
252          (Level.WARNING, "configured GUI element representing class " + className + " can not " +
253           "be instantiated.", e);
254        throw new GUIModelConfigurationException
255          ("configured GUI element representing class " + className + " can not " +
256           "be instantiated.", e);
257      }
258    }
259   
260    return null;
261  }
262
263  //-----------------------------------------------------------------------------------------------
264  /**
265   * TODO: comment
266   *
267   * @return
268   */
269  //-----------------------------------------------------------------------------------------------
270  private synchronized Properties getMappingsFromConfiguration()
271  {
272    if (mMappingsFromConfiguration != null)
273    {
274      return mMappingsFromConfiguration;
275    }
276    else
277    {
278      mMappingsFromConfiguration = new Properties();
279     
280      InputStream inStream =
281        this.getClass().getClassLoader().getResourceAsStream("GUIElementMapping.txt");
282     
283      if (inStream != null)
284      {
285        try
286        {
287          mMappingsFromConfiguration.load(inStream);
288        }
289        catch (IOException e)
290        {
291          Logger.getLogger(this.getClass().getName()).warning
292            ("could not load GUIElementMapping.txt from classpath, but this may be intended");
293        }
294      }
295     
296      return mMappingsFromConfiguration;
297    }
298  }
299
300}
Note: See TracBrowser for help on using the repository browser.