From 3cb713298a58151db28953cb4dc285509c58685e Mon Sep 17 00:00:00 2001 From: Harald Wolff Date: Fri, 1 Apr 2016 22:42:18 +0200 Subject: [PATCH] org.hwo.interactiveobjects --- .../interactiveobjects/IObjectEditorUI.java | 5 ++ .../InteractiveObjectHelper.java | 6 +- .../interactiveobjects/ObjectEditorUI.java | 19 ++++++ .../ObjectEditorUIHelper.java | 60 +++++++++++++++++++ 4 files changed, 88 insertions(+), 2 deletions(-) create mode 100644 src/org/hwo/interactiveobjects/IObjectEditorUI.java create mode 100644 src/org/hwo/interactiveobjects/ObjectEditorUI.java create mode 100644 src/org/hwo/interactiveobjects/ObjectEditorUIHelper.java diff --git a/src/org/hwo/interactiveobjects/IObjectEditorUI.java b/src/org/hwo/interactiveobjects/IObjectEditorUI.java new file mode 100644 index 0000000..dd15747 --- /dev/null +++ b/src/org/hwo/interactiveobjects/IObjectEditorUI.java @@ -0,0 +1,5 @@ +package org.hwo.interactiveobjects; + +public interface IObjectEditorUI { + public boolean editObject(Object o); +} diff --git a/src/org/hwo/interactiveobjects/InteractiveObjectHelper.java b/src/org/hwo/interactiveobjects/InteractiveObjectHelper.java index f43832a..cea62a9 100644 --- a/src/org/hwo/interactiveobjects/InteractiveObjectHelper.java +++ b/src/org/hwo/interactiveobjects/InteractiveObjectHelper.java @@ -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); + } } } diff --git a/src/org/hwo/interactiveobjects/ObjectEditorUI.java b/src/org/hwo/interactiveobjects/ObjectEditorUI.java new file mode 100644 index 0000000..f4349f5 --- /dev/null +++ b/src/org/hwo/interactiveobjects/ObjectEditorUI.java @@ -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 editor(); +} + +/* +@Retention(RetentionPolicy.RUNTIME) +public @interface InteractiveObject { + + public boolean keywordSearch() default false; + + public Class editor(); + +}*/ diff --git a/src/org/hwo/interactiveobjects/ObjectEditorUIHelper.java b/src/org/hwo/interactiveobjects/ObjectEditorUIHelper.java new file mode 100644 index 0000000..75e71cb --- /dev/null +++ b/src/org/hwo/interactiveobjects/ObjectEditorUIHelper.java @@ -0,0 +1,60 @@ +package org.hwo.interactiveobjects; + +import java.util.Hashtable; + +public class ObjectEditorUIHelper { + + private static Hashtable, IObjectEditorUI> hashEditors = new Hashtable, 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; + } + +}