String widgetClass = widgetNode.getAttribute("class");
String widgetName = widgetNode.getAttribute("name");
String widgetPreset = widgetNode.getAttribute("preset");
boolean hasPreset = !widgetPreset.equals("");
Constructor widgetConstructor = (Constructor) constructorCache.get(widgetClass + (hasPreset ? "_P" : ""));
NodeList childNodes = widgetNode.getChildNodes();
try {
try {
if (widgetConstructor == null) {
String classAttribute = widgetNode.getAttribute("class");
String className = DEFAULT_PREFIX + classAttribute;
if (classAttribute.indexOf('.') != -1)
className = classAttribute;
Class wClass = Class.forName(className);
if (hasPreset)
widgetConstructor = wClass.getConstructor(new Class[] { Widget.class, String.class, String.class });
else
widgetConstructor = wClass.getConstructor(new Class[] { Widget.class, String.class });
constructorCache.put(widgetClass + (hasPreset ? "_P" : ""), widgetConstructor);
}
if (hasPreset)
widget =
(Widget) widgetConstructor.newInstance(
new Object[] { parentWidget, widgetName.equals("") ? null : widgetName, widgetPreset });
else
widget =
(Widget) widgetConstructor.newInstance(
new Object[] { parentWidget, widgetName.equals("") ? null : widgetName });
} catch (ClassNotFoundException e) {
throw new GUIException("Unknown widget class [" + widgetNode.getAttribute("class") + "]");
} catch (NoSuchMethodException e) {
throw new GUIException("Widget constructor not found", e);
} catch (IllegalAccessException e) {
throw new GUIException("Widget constructor could not be called", e);
} catch (InstantiationException e) {
throw new GUIException("Widget is abstract", e);
} catch (InvocationTargetException e) {
throw new GUIException("Widget constructor threw an exception", e);
}
for (int i = 0; i < childNodes.getLength(); i++) {
if (childNodes.item(i).getNodeName().equals("property")) {
Element propertyNode = (Element) childNodes.item(i);
widget.setProperty(propertyNode.getAttribute("name"), pf.constructProperty(propertyNode));
} else if (listener != null && childNodes.item(i).getNodeName().equals("emit")) {
Element emitNode = (Element) childNodes.item(i);
widget.addListener(emitNode.getAttribute("event"), emitNode.getAttribute("name"), listener);
} else if (childNodes.item(i).getNodeName().equals("layout")) {
Element layoutNode = (Element) childNodes.item(i);
widget.setProperty("layout", lf.constructLayout(widget, layoutNode));
}
}
} catch (Exception e) {
throw new GUIException("Error while creating a widget of class [" + widgetClass + "], name [" + widgetName + "] and preset [" + widgetPreset + "]", e);
}
/* Process child nodes after all properties are set */
for (int i = 0; i < childNodes.getLength(); i++) {
if (childNodes.item(i).getNodeName().equals("widget")) {
Element childNode = (Element) childNodes.item(i);
Widget childWidget = null;
Object anchor = null;
/* Get the child's anchor subnode */
Element anchorNode = XMLUtils.getChild(childNode, "anchor");