/** * Gets the ObjectLoader for the specified class. * * @param classtype the class * @return the ObjectLoader */ public static XML.ObjectLoader getLoader(Class classtype) { // look for registered loader first ObjectLoader loader = (ObjectLoader) loaders.get(classtype); // if no registered loader, look for static getLoader() method in class if (loader == null) { try { Method method = classtype.getMethod("getLoader", (Class[]) null); // $NON-NLS-1$ if (method != null && Modifier.isStatic(method.getModifiers())) { loader = (ObjectLoader) method.invoke(null, (Object[]) null); if (loader != null) { // register loader for future calls setLoader(classtype, loader); } } } catch (Exception ex) { /** empty block */ } } // if still no loader found, use the default loader if (loader == null) { if (defaultLoader == null) { defaultLoader = new XMLLoader(); } loader = defaultLoader; } return loader; }
// static initializer defines loaders for commonly used classes static { setLoader( Color.class, new XML.ObjectLoader() { public void saveObject(XMLControl control, Object obj) { java.awt.Color color = (java.awt.Color) obj; control.setValue("red", color.getRed()); // $NON-NLS-1$ control.setValue("green", color.getGreen()); // $NON-NLS-1$ control.setValue("blue", color.getBlue()); // $NON-NLS-1$ control.setValue("alpha", color.getAlpha()); // $NON-NLS-1$ } public Object createObject(XMLControl control) { int r = control.getInt("red"); // $NON-NLS-1$ int g = control.getInt("green"); // $NON-NLS-1$ int b = control.getInt("blue"); // $NON-NLS-1$ int a = control.getInt("alpha"); // $NON-NLS-1$ return new java.awt.Color(r, g, b, a); } public Object loadObject(XMLControl control, Object obj) { int r = control.getInt("red"); // $NON-NLS-1$ int g = control.getInt("green"); // $NON-NLS-1$ int b = control.getInt("blue"); // $NON-NLS-1$ int a = control.getInt("alpha"); // $NON-NLS-1$ return new java.awt.Color(r, g, b, a); } }); setLoader( Double.class, new XML.ObjectLoader() { public void saveObject(XMLControl control, Object obj) { Double dbl = (Double) obj; control.setValue("value", dbl.doubleValue()); // $NON-NLS-1$ } public Object createObject(XMLControl control) { double val = control.getDouble("value"); // $NON-NLS-1$ return new Double(val); } public Object loadObject(XMLControl control, Object obj) { Double dbl = (Double) obj; double val = control.getDouble("value"); // $NON-NLS-1$ if (dbl.doubleValue() == val) { return dbl; } return new Double(val); } }); setLoader( Integer.class, new XML.ObjectLoader() { public void saveObject(XMLControl control, Object obj) { Integer i = (Integer) obj; control.setValue("value", i.intValue()); // $NON-NLS-1$ } public Object createObject(XMLControl control) { int val = control.getInt("value"); // $NON-NLS-1$ return new Integer(val); } public Object loadObject(XMLControl control, Object obj) { Integer i = (Integer) obj; int val = control.getInt("value"); // $NON-NLS-1$ if (i.intValue() == val) { return i; } return new Integer(val); } }); setLoader( Boolean.class, new XML.ObjectLoader() { public void saveObject(XMLControl control, Object obj) { Boolean bool = (Boolean) obj; control.setValue("value", bool.booleanValue()); // $NON-NLS-1$ } public Object createObject(XMLControl control) { boolean val = control.getBoolean("value"); // $NON-NLS-1$ return new Boolean(val); } public Object loadObject(XMLControl control, Object obj) { Boolean bool = (Boolean) obj; boolean val = control.getBoolean("value"); // $NON-NLS-1$ if (bool.booleanValue() == val) { return bool; } return new Boolean(val); } }); }