forked from LupusNobilis/java-org.hwo
org.hwo.interactiveobjects
parent
28920e2286
commit
3cb713298a
|
@ -0,0 +1,5 @@
|
||||||
|
package org.hwo.interactiveobjects;
|
||||||
|
|
||||||
|
public interface IObjectEditorUI {
|
||||||
|
public boolean editObject(Object o);
|
||||||
|
}
|
|
@ -56,8 +56,10 @@ public class InteractiveObjectHelper {
|
||||||
{
|
{
|
||||||
if (o != null){
|
if (o != null){
|
||||||
IInteractiveObjectEditor editor = getEditor(o);
|
IInteractiveObjectEditor editor = getEditor(o);
|
||||||
editor.setInteractiveObject(o);
|
if (editor != null){
|
||||||
editor.setVisible(true);
|
editor.setInteractiveObject(o);
|
||||||
|
editor.setVisible(true);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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();
|
||||||
|
|
||||||
|
}*/
|
|
@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue