org.hwo.interactiveobjects

thobaben_serialize
Harald Wolff 2016-04-01 22:42:18 +02:00
parent 28920e2286
commit 3cb713298a
4 changed files with 88 additions and 2 deletions

View File

@ -0,0 +1,5 @@
package org.hwo.interactiveobjects;
public interface IObjectEditorUI {
public boolean editObject(Object o);
}

View File

@ -56,8 +56,10 @@ public class InteractiveObjectHelper {
{
if (o != null){
IInteractiveObjectEditor editor = getEditor(o);
editor.setInteractiveObject(o);
editor.setVisible(true);
if (editor != null){
editor.setInteractiveObject(o);
editor.setVisible(true);
}
}
}

View File

@ -0,0 +1,19 @@
package org.hwo.interactiveobjects;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
public @interface ObjectEditorUI {
public Class<? extends IObjectEditorUI> editor();
}
/*
@Retention(RetentionPolicy.RUNTIME)
public @interface InteractiveObject {
public boolean keywordSearch() default false;
public Class<? extends IInteractiveObjectEditor> editor();
}*/

View File

@ -0,0 +1,60 @@
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 (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;
}
}