65 lines
1.6 KiB
Java
65 lines
1.6 KiB
Java
package org.hwo.interactiveobjects;
|
|
|
|
import java.util.Hashtable;
|
|
|
|
public class ObjectEditorUIHelper {
|
|
|
|
private static Hashtable<Class<?>, IObjectEditorUI> hashEditors = new Hashtable<Class<?>, IObjectEditorUI>();
|
|
|
|
private static ObjectEditorUI getObjectEditorUI(Class<?> clazz)
|
|
{
|
|
ObjectEditorUI editor = clazz.getAnnotation(ObjectEditorUI.class);
|
|
if (editor != null){
|
|
return editor;
|
|
}
|
|
|
|
if (clazz.getSuperclass() != null)
|
|
return getObjectEditorUI(clazz.getSuperclass());
|
|
|
|
return null;
|
|
}
|
|
|
|
private static ObjectEditorUI getObjectEditorUI(Object item)
|
|
{
|
|
Class<?> clazz = item.getClass();
|
|
return getObjectEditorUI(clazz);
|
|
}
|
|
|
|
public static IObjectEditorUI editor(Object item) throws NoClassDefFoundError{
|
|
IObjectEditorUI editor;
|
|
|
|
if (item == null){
|
|
throw new NullPointerException("editor(...) needs object");
|
|
}
|
|
|
|
if (hashEditors.containsKey(item.getClass())){
|
|
return hashEditors.get(item.getClass());
|
|
}
|
|
|
|
ObjectEditorUI editorUI = getObjectEditorUI(item.getClass());
|
|
if (editorUI!=null){
|
|
try {
|
|
editor = editorUI.editor().newInstance();
|
|
hashEditors.put( item.getClass(), editor);
|
|
return editor;
|
|
} catch (InstantiationException e) {
|
|
e.printStackTrace();
|
|
} catch (IllegalAccessException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
throw new NoClassDefFoundError(String.format("%s has no ObjectEditorUI", item));
|
|
}
|
|
|
|
public static boolean edit(Object item) throws NoClassDefFoundError {
|
|
IObjectEditorUI editor = editor(item);
|
|
return editor.editObject(item);
|
|
}
|
|
|
|
public static boolean isEditable(Class<?> clazz){
|
|
return getObjectEditorUI(clazz)!=null;
|
|
}
|
|
|
|
}
|