1 package net.mograsim.logic.ui.serializing;
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.util.HashMap;
8 import net.mograsim.logic.ui.serializing.snippets.Renderer;
9 import net.mograsim.logic.ui.serializing.snippets.SnippetSupplier;
10 import net.mograsim.logic.ui.serializing.snippets.outlinerenderers.DefaultOutlineRenderer;
11 import net.mograsim.logic.ui.serializing.snippets.symbolrenderers.DefaultSymbolRenderer;
12 import net.mograsim.logic.ui.util.JsonHandler;
14 public class CodeSnippetSupplier<S>
16 // public static members
17 public static final CodeSnippetSupplier<Renderer> symbolRendererSupplier;
18 public static final CodeSnippetSupplier<Renderer> outlineRendererSupplier;
22 symbolRendererSupplier = new CodeSnippetSupplier<>(SnippetSupplier.create(Void.class, DefaultSymbolRenderer::new));
23 outlineRendererSupplier = new CodeSnippetSupplier<>(SnippetSupplier.create(Void.class, DefaultOutlineRenderer::new));
26 // per-instance members
28 private final Map<String, String> standardSnippetIDClassNames = new HashMap<>();
29 private final Map<String, SnippetSupplier<?, S>> snippetSuppliersForClassNames = new HashMap<>();
30 private final SnippetSupplier<?, S> defaultSnippetSupplier;
32 private CodeSnippetSupplier(SnippetSupplier<?, S> defaultSnippetSupplier)
34 this.defaultSnippetSupplier = defaultSnippetSupplier;
37 public void addStandardSnippetID(String standardSnippetID, String associatedSnippetClassName)
39 standardSnippetIDClassNames.put(standardSnippetID, associatedSnippetClassName);
42 public void setSnippetSupplier(String id, SnippetSupplier<?, S> snippetSupplier)
44 snippetSuppliersForClassNames.put(id, snippetSupplier);
48 public SnippetSupplier<?, S> getSnippetSupplier(String id)
52 String snippetClassName;
53 if (id.startsWith("class:"))
54 snippetClassName = id.substring(6);
56 snippetClassName = standardSnippetIDClassNames.get(id);
57 if (snippetClassName != null)
59 tryLoadSnippetClass(snippetClassName);
60 SnippetSupplier<?, S> snippetSupplier = snippetSuppliersForClassNames.get(snippetClassName);
61 if (snippetSupplier != null)
62 return snippetSupplier;
64 System.err.println("Couldn't load snippet " + id + "; using default");
66 return defaultSnippetSupplier;
73 try (InputStream s = IndirectGUIComponentCreator.class.getResourceAsStream("./standardSnippetIDMapping.json"))
76 throw new IOException("Resource not found");
77 SnippetIDClassNames tmp = JsonHandler.readJson(s, SnippetIDClassNames.class);
78 tmp.standardOutlineRendererSuppliers.forEach(outlineRendererSupplier::addStandardSnippetID);
79 tmp.standardSymbolRendererSuppliers.forEach(symbolRendererSupplier::addStandardSnippetID);
83 System.err.println("Failed to initialize standard snippet ID mapping: ");
88 private static class SnippetIDClassNames
90 public Map<String, String> standardOutlineRendererSuppliers;
91 public Map<String, String> standardSymbolRendererSuppliers;
94 private static void tryLoadSnippetClass(String snippetClassName)
96 tryInvokeStaticInitializer(snippetClassName, "Error getting snippet class: %s: %s\n");
99 public static void tryInvokeStaticInitializer(String className, String errorMessageFormat)
103 Class.forName(className, true, CodeSnippetSupplier.class.getClassLoader());
105 catch (ClassNotFoundException e)
107 System.err.printf(errorMessageFormat, className, "ClassNotFoundException thrown: " + e.getMessage());