X-Git-Url: https://mograsim.net/gitweb/?a=blobdiff_plain;f=net.mograsim.logic.ui%2Fsrc%2Fnet%2Fmograsim%2Flogic%2Fui%2Fserializing%2FCodeSnippetSupplier.java;h=a6b9dae7868f4ece349bdf969cbe92dceaf21966;hb=69cb6725ef670328959d55649257ded6ac924b33;hp=62014ca0168b6ad36d223a21bf7f545954471aef;hpb=c17518ca27f37f56b0da13aa144787a39e1f5454;p=Mograsim.git diff --git a/net.mograsim.logic.ui/src/net/mograsim/logic/ui/serializing/CodeSnippetSupplier.java b/net.mograsim.logic.ui/src/net/mograsim/logic/ui/serializing/CodeSnippetSupplier.java index 62014ca0..a6b9dae7 100644 --- a/net.mograsim.logic.ui/src/net/mograsim/logic/ui/serializing/CodeSnippetSupplier.java +++ b/net.mograsim.logic.ui/src/net/mograsim/logic/ui/serializing/CodeSnippetSupplier.java @@ -5,90 +5,51 @@ import java.io.InputStream; import java.util.HashMap; import java.util.Map; -import com.google.gson.JsonElement; - -import net.haspamelodica.swt.helper.swtobjectwrappers.Point; -import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle; +import net.mograsim.logic.ui.serializing.snippets.HighLevelStateHandler; import net.mograsim.logic.ui.serializing.snippets.Renderer; -import net.mograsim.logic.ui.serializing.snippets.RendererProvider; +import net.mograsim.logic.ui.serializing.snippets.SnippetSupplier; +import net.mograsim.logic.ui.serializing.snippets.highlevelstatehandlers.DefaultHighLevelStateHandler; +import net.mograsim.logic.ui.serializing.snippets.outlinerenderers.DefaultOutlineRenderer; +import net.mograsim.logic.ui.serializing.snippets.symbolrenderers.DefaultSymbolRenderer; import net.mograsim.logic.ui.util.JsonHandler; -import net.mograsim.preferences.ColorDefinition; -import net.mograsim.preferences.ColorManager; -import net.mograsim.preferences.Preferences; -public class CodeSnippetSupplier +public class CodeSnippetSupplier { - private static final Map standardSnippetIDClassNames = new HashMap<>(); - - private static final Map outlineRendererProvidersForComponentClassNames = new HashMap<>(); - private static final Map symbolRendererProvidersForComponentClassNames = new HashMap<>(); + // public static members + public static final CodeSnippetSupplier symbolRendererSupplier; + public static final CodeSnippetSupplier outlineRendererSupplier; + public static final CodeSnippetSupplier highLevelStateHandlerSupplier; - private static final RendererProvider defaultOutlineRendererProvider; - private static final RendererProvider defaultSymbolRendererProvider; static { - // TODO this code does not belong here - defaultOutlineRendererProvider = (comp, params) -> (gc, visReg) -> - { - ColorDefinition fg = Preferences.current().getColorDefinition("net.mograsim.logic.ui.color.foreground"); - if (fg != null) - gc.setForeground(ColorManager.current().toColor(fg)); - gc.drawRectangle(comp.getBounds()); - }; - defaultSymbolRendererProvider = (comp, params) -> (gc, visReg) -> - { - ColorDefinition fg = Preferences.current().getColorDefinition("net.mograsim.logic.ui.color.text"); - if (fg != null) - gc.setForeground(ColorManager.current().toColor(fg)); - String id = "TODO";// TODO add an ID of sorts to DeserializedSubmodelComponent - Point idSize = gc.textExtent(id); - Rectangle bounds = comp.getBounds(); - gc.drawText(id, bounds.x + (bounds.width - idSize.x) / 2, bounds.y + (bounds.height - idSize.y) / 2, true); - }; + symbolRendererSupplier = new CodeSnippetSupplier<>(SnippetSupplier.create(Void.class, DefaultSymbolRenderer::new)); + outlineRendererSupplier = new CodeSnippetSupplier<>(SnippetSupplier.create(Void.class, DefaultOutlineRenderer::new)); + highLevelStateHandlerSupplier = new CodeSnippetSupplier<>(SnippetSupplier.create(Void.class, DefaultHighLevelStateHandler::new)); } - static - { - try (InputStream s = IndirectGUIComponentCreator.class.getResourceAsStream("./standardSnippetIDMapping.json")) - { - if (s == null) - throw new IOException("Resource not found"); - Map tmp = JsonHandler.readJson(s, Map.class); - standardSnippetIDClassNames.putAll(tmp); - } - catch (IOException e) - { - System.err.println("Failed to initialize standard snippet ID mapping: " + e.getMessage()); - } - } + // per-instance members - public static void addStandardSnippetID(String standardSnippetID, String associatedSnippetClassName) - { - standardSnippetIDClassNames.put(standardSnippetID, associatedSnippetClassName); - } + private final Map standardSnippetIDClassNames = new HashMap<>(); + private final Map> snippetSuppliersForClassNames = new HashMap<>(); + private final SnippetSupplier defaultSnippetSupplier; - public static void setOutlineRendererProvider(String id, RendererProvider outlineRendererProvider) + private CodeSnippetSupplier(SnippetSupplier defaultSnippetSupplier) { - outlineRendererProvidersForComponentClassNames.put(id, outlineRendererProvider); + this.defaultSnippetSupplier = defaultSnippetSupplier; } - public static void setSymbolRendererProvider(String id, RendererProvider symbolRendererProvider) + public void addStandardSnippetID(String standardSnippetID, String associatedSnippetClassName) { - symbolRendererProvidersForComponentClassNames.put(id, symbolRendererProvider); - } - - public static Renderer createOutlineRenderer(String id, DeserializedSubmodelComponent component, JsonElement params) - { - return getSnippet(id, outlineRendererProvidersForComponentClassNames, defaultOutlineRendererProvider).create(component, params); + standardSnippetIDClassNames.put(standardSnippetID, associatedSnippetClassName); } - public static Renderer createSymbolRenderer(String id, DeserializedSubmodelComponent component, JsonElement params) + public void setSnippetSupplier(String id, SnippetSupplier snippetSupplier) { - return getSnippet(id, symbolRendererProvidersForComponentClassNames, defaultSymbolRendererProvider).create(component, params); + snippetSuppliersForClassNames.put(id, snippetSupplier); } // TODO report errors - private static C getSnippet(String id, Map specializedCodeMap, C defaultSnippet) + public SnippetSupplier getSnippetSupplier(String id) { if (id != null) { @@ -100,17 +61,45 @@ public class CodeSnippetSupplier if (snippetClassName != null) { tryLoadSnippetClass(snippetClassName); - C specializedCode = specializedCodeMap.get(snippetClassName); - if (specializedCode != null) - return specializedCode; + SnippetSupplier snippetSupplier = snippetSuppliersForClassNames.get(snippetClassName); + if (snippetSupplier != null) + return snippetSupplier; } + System.err.println("Couldn't load snippet " + id + "; using default"); + } + return defaultSnippetSupplier; + } + + // static helpers + + static + { + try (InputStream s = IndirectGUIComponentCreator.class.getResourceAsStream("./standardSnippetIDMapping.json")) + { + if (s == null) + throw new IOException("Resource not found"); + SnippetIDClassNames tmp = JsonHandler.readJson(s, SnippetIDClassNames.class); + tmp.standardOutlineRendererSuppliers.forEach(outlineRendererSupplier::addStandardSnippetID); + tmp.standardSymbolRendererSuppliers.forEach(symbolRendererSupplier::addStandardSnippetID); + tmp.standardHighLevelStateHandlerSuppliers.forEach(highLevelStateHandlerSupplier::addStandardSnippetID); + } + catch (Exception e) + { + System.err.println("Failed to initialize standard snippet ID mapping: "); + e.printStackTrace(); } - return defaultSnippet; + } + + private static class SnippetIDClassNames + { + public Map standardOutlineRendererSuppliers; + public Map standardSymbolRendererSuppliers; + public Map standardHighLevelStateHandlerSuppliers; } private static void tryLoadSnippetClass(String snippetClassName) { - tryInvokeStaticInitializer(snippetClassName, "Error getting snippet code for component class: %s: %s\n"); + tryInvokeStaticInitializer(snippetClassName, "Error getting snippet class: %s: %s\n"); } public static void tryInvokeStaticInitializer(String className, String errorMessageFormat) @@ -124,4 +113,5 @@ public class CodeSnippetSupplier System.err.printf(errorMessageFormat, className, "ClassNotFoundException thrown: " + e.getMessage()); } } + } \ No newline at end of file