Fixed a bug in Am2900; created dlatch8/80; relayouted some components
[Mograsim.git] / net.mograsim.logic.model / src / net / mograsim / logic / model / snippets / CodeSnippetSupplier.java
index 0016cc7..22ebc09 100644 (file)
@@ -4,6 +4,8 @@ import java.util.Collections;
 import java.util.HashMap;
 import java.util.Map;
 
+import net.mograsim.logic.model.serializing.ReflectionHelper;
+
 public class CodeSnippetSupplier<C, S>
 {
        private final Map<String, String> standardSnippetIDClassNames = new HashMap<>();
@@ -16,9 +18,11 @@ public class CodeSnippetSupplier<C, S>
                this.defaultSnippetSupplier = defaultSnippetSupplier;
        }
 
-       public void addStandardSnippetID(String standardSnippetID, String associatedSnippetClassName)
+       public void addStandardSnippetID(String standardSnippetID, String associatedSnippetID)
        {
-               standardSnippetIDClassNames.put(standardSnippetID, associatedSnippetClassName);
+               if (!associatedSnippetID.startsWith("class:"))
+                       throw new IllegalArgumentException("Unrecognized snippet ID format: " + associatedSnippetID);
+               standardSnippetIDClassNames.put(standardSnippetID, associatedSnippetID);
        }
 
        public Map<String, String> getStandardSnippetIDs()
@@ -36,13 +40,10 @@ public class CodeSnippetSupplier<C, S>
        {
                if (id != null)
                {
-                       String snippetClassName;
-                       if (id.startsWith("class:"))
-                               snippetClassName = id.substring(6);
-                       else
-                               snippetClassName = standardSnippetIDClassNames.get(id);
-                       if (snippetClassName != null)
+                       String resolvedID = resolveID(id);
+                       if (resolvedID != null)
                        {
+                               String snippetClassName = resolvedID.substring(6);
                                tryLoadSnippetClass(snippetClassName);
                                SnippetDefinintion<C, ?, S> snippetSupplier = snippetSuppliersForClassNames.get(snippetClassName);
                                if (snippetSupplier != null)
@@ -55,23 +56,17 @@ public class CodeSnippetSupplier<C, S>
                return defaultSnippetSupplier;
        }
 
-       // static helpers
-
-       private static void tryLoadSnippetClass(String snippetClassName)
+       public String resolveID(String id)
        {
-               tryInvokeStaticInitializer(snippetClassName, "Error getting snippet class: %s: %s\n");
+               if (id.startsWith("class:"))
+                       return id;
+               return standardSnippetIDClassNames.get(id);
        }
 
-       public static void tryInvokeStaticInitializer(String className, String errorMessageFormat)
+       // static helpers
+
+       private static void tryLoadSnippetClass(String snippetClassName)
        {
-               try
-               {
-                       Class.forName(className, true, CodeSnippetSupplier.class.getClassLoader());
-               }
-               catch (ClassNotFoundException e)
-               {
-                       System.err.printf(errorMessageFormat, className, "ClassNotFoundException thrown: " + e.getMessage());
-               }
+               ReflectionHelper.tryInvokeStaticInitializer(snippetClassName, "Error getting snippet class: %s: %s\n");
        }
-
 }
\ No newline at end of file