Modified SaveLoadManager to use standard IDs
[Mograsim.git] / net.mograsim.logic.model / src / net / mograsim / logic / model / snippets / CodeSnippetSupplier.java
1 package net.mograsim.logic.model.snippets;
2
3 import java.util.Collections;
4 import java.util.HashMap;
5 import java.util.Map;
6
7 public class CodeSnippetSupplier<C, S>
8 {
9         private final Map<String, String> standardSnippetIDClassNames = new HashMap<>();
10         private final Map<String, String> standardSnippetIDClassNamesUnmodifiable = Collections.unmodifiableMap(standardSnippetIDClassNames);
11         private final Map<String, SnippetDefinintion<C, ?, S>> snippetSuppliersForClassNames = new HashMap<>();
12         private final SnippetDefinintion<C, ?, S> defaultSnippetSupplier;
13
14         public CodeSnippetSupplier(SnippetDefinintion<C, ?, S> defaultSnippetSupplier)
15         {
16                 this.defaultSnippetSupplier = defaultSnippetSupplier;
17         }
18
19         public void addStandardSnippetID(String standardSnippetID, String associatedSnippetClassName)
20         {
21                 standardSnippetIDClassNames.put(standardSnippetID, associatedSnippetClassName);
22         }
23
24         public Map<String, String> getStandardSnippetIDs()
25         {
26                 return standardSnippetIDClassNamesUnmodifiable;
27         }
28
29         public void setSnippetSupplier(String id, SnippetDefinintion<C, ?, S> snippetSupplier)
30         {
31                 snippetSuppliersForClassNames.put(id, snippetSupplier);
32         }
33
34         // TODO report errors
35         public SnippetDefinintion<C, ?, S> getSnippetSupplier(String id)
36         {
37                 if (id != null)
38                 {
39                         String snippetClassName;
40                         if (id.startsWith("class:"))
41                                 snippetClassName = id.substring(6);
42                         else
43                                 snippetClassName = standardSnippetIDClassNames.get(id);
44                         if (snippetClassName != null)
45                         {
46                                 tryLoadSnippetClass(snippetClassName);
47                                 SnippetDefinintion<C, ?, S> snippetSupplier = snippetSuppliersForClassNames.get(snippetClassName);
48                                 if (snippetSupplier != null)
49                                         return snippetSupplier;
50                         }
51                         System.err.println("Couldn't load snippet " + id + "; using default");
52                 }
53                 if (defaultSnippetSupplier == null)
54                         throw new IllegalArgumentException("No default snippet set");
55                 return defaultSnippetSupplier;
56         }
57
58         // static helpers
59
60         private static void tryLoadSnippetClass(String snippetClassName)
61         {
62                 tryInvokeStaticInitializer(snippetClassName, "Error getting snippet class: %s: %s\n");
63         }
64
65         public static void tryInvokeStaticInitializer(String className, String errorMessageFormat)
66         {
67                 try
68                 {
69                         Class.forName(className, true, CodeSnippetSupplier.class.getClassLoader());
70                 }
71                 catch (ClassNotFoundException e)
72                 {
73                         System.err.printf(errorMessageFormat, className, "ClassNotFoundException thrown: " + e.getMessage());
74                 }
75         }
76
77 }