package net.mograsim.logic.model.editor;
import java.io.IOException;
-import java.util.Map;
-import java.util.Map.Entry;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.FileDialog;
import net.mograsim.logic.model.model.ViewModelModifiable;
import net.mograsim.logic.model.serializing.DeserializedSubmodelComponent;
-import net.mograsim.logic.model.serializing.IdentifierGetter;
import net.mograsim.logic.model.serializing.IndirectGUIComponentCreator;
import net.mograsim.logic.model.serializing.SubmodelComponentSerializer;
-import net.mograsim.logic.model.snippets.SubmodelComponentSnippetSuppliers;
-import net.mograsim.logic.model.snippets.highlevelstatehandlers.standard.StandardHighLevelStateHandlerSnippetSuppliers;
public class SaveLoadManager
{
{
try
{
- IdentifierGetter idGetter = new IdentifierGetter();
- idGetter.componentIDs = c -> getStandardID(c, IndirectGUIComponentCreator.getStandardComponentIDs(), true);
- idGetter.symbolRendererIDs = h -> getStandardID(h,
- SubmodelComponentSnippetSuppliers.symbolRendererSupplier.getStandardSnippetIDs());
- idGetter.outlineRendererIDs = h -> getStandardID(h,
- SubmodelComponentSnippetSuppliers.outlineRendererSupplier.getStandardSnippetIDs());
- idGetter.highLevelStateHandlerIDs = h -> getStandardID(h,
- SubmodelComponentSnippetSuppliers.highLevelStateHandlerSupplier.getStandardSnippetIDs());
- idGetter.atomicHighLevelStateHandlerIDs = h -> getStandardID(h,
- StandardHighLevelStateHandlerSnippetSuppliers.atomicHandlerSupplier.getStandardSnippetIDs());
- idGetter.subcomponentHighLevelStateHandlerIDs = h -> getStandardID(h,
- StandardHighLevelStateHandlerSnippetSuppliers.subcomponentHandlerSupplier.getStandardSnippetIDs());
- SubmodelComponentSerializer.serialize(editor.toBeEdited, idGetter, savePath);
+ SubmodelComponentSerializer.serialize(editor.toBeEdited, savePath);
}
catch (IOException e)
{
}
}
- private static String getStandardID(Object o, Map<String, String> standardIDs)
- {
- return getStandardID(o, standardIDs, false);
- }
-
- private static String getStandardID(Object o, Map<String, String> standardIDs, boolean standardIDsHaveClassConcatenated)
- {
- String verboseID = (standardIDsHaveClassConcatenated ? "class:" : "") + o.getClass().getCanonicalName();
- return standardIDs.entrySet().stream().filter(e -> e.getValue().equals(verboseID)).map(Entry::getKey).findAny()
- .orElseGet(() -> (standardIDsHaveClassConcatenated ? "" : "class:") + verboseID);
- }
-
public static void openLoadDialog() throws IOException
{
Shell fdShell = new Shell();
package net.mograsim.logic.model.serializing;
+import static net.mograsim.logic.model.snippets.SubmodelComponentSnippetSuppliers.highLevelStateHandlerSupplier;
+import static net.mograsim.logic.model.snippets.SubmodelComponentSnippetSuppliers.outlineRendererSupplier;
+import static net.mograsim.logic.model.snippets.SubmodelComponentSnippetSuppliers.symbolRendererSupplier;
+import static net.mograsim.logic.model.snippets.highlevelstatehandlers.standard.StandardHighLevelStateHandlerSnippetSuppliers.atomicHandlerSupplier;
+import static net.mograsim.logic.model.snippets.highlevelstatehandlers.standard.StandardHighLevelStateHandlerSnippetSuppliers.subcomponentHandlerSupplier;
+
+import java.util.Map;
+import java.util.Map.Entry;
import java.util.function.Function;
import net.mograsim.logic.model.model.components.GUIComponent;
+import net.mograsim.logic.model.snippets.CodeSnippetSupplier;
import net.mograsim.logic.model.snippets.HighLevelStateHandler;
import net.mograsim.logic.model.snippets.Renderer;
import net.mograsim.logic.model.snippets.highlevelstatehandlers.standard.atomic.AtomicHighLevelStateHandler;
public Function<AtomicHighLevelStateHandler, String> atomicHighLevelStateHandlerIDs;
/**
- * Creates a new IdentifierGetter using "class:" concatenated with a component's / snippet's complete (canonical) class name as the
- * default for all ID getter functions.
+ * Creates a new IdentifierGetter using the following as the default for all ID getter functions: <br>
+ * Define the verbose ID as <code>"class:" + canonicalClassName</code>.<br>
+ * If there is a standard ID mapping to this verbose ID recorded in the matching {@link CodeSnippetSupplier}, use this ID; if not, use
+ * the verbose ID.
*/
public IdentifierGetter()
{
- Function<Object, String> defaultSnippetIDGetter = c -> "class:" + c.getClass().getCanonicalName();
- this.componentIDs = defaultSnippetIDGetter::apply;
- this.symbolRendererIDs = defaultSnippetIDGetter::apply;
- this.outlineRendererIDs = defaultSnippetIDGetter::apply;
- this.highLevelStateHandlerIDs = defaultSnippetIDGetter::apply;
- this.subcomponentHighLevelStateHandlerIDs = defaultSnippetIDGetter::apply;
- this.atomicHighLevelStateHandlerIDs = defaultSnippetIDGetter::apply;
+ componentIDs = generateStandardIDFunction(IndirectGUIComponentCreator.getStandardComponentIDs());
+ symbolRendererIDs = generateStandardIDFunction(symbolRendererSupplier);
+ outlineRendererIDs = generateStandardIDFunction(outlineRendererSupplier);
+ highLevelStateHandlerIDs = generateStandardIDFunction(highLevelStateHandlerSupplier);
+ atomicHighLevelStateHandlerIDs = generateStandardIDFunction(atomicHandlerSupplier);
+ subcomponentHighLevelStateHandlerIDs = generateStandardIDFunction(subcomponentHandlerSupplier);
+ }
+
+ private static <T> Function<T, String> generateStandardIDFunction(CodeSnippetSupplier<?, T> snippetSupplier)
+ {
+ return generateStandardIDFunction(snippetSupplier.getStandardSnippetIDs());
+ }
+
+ private static <T> Function<T, String> generateStandardIDFunction(Map<String, String> standardComponentIDs)
+ {
+ return t ->
+ {
+ String verboseID = "class:" + t.getClass().getCanonicalName();
+ return standardComponentIDs.entrySet().stream().filter(e -> e.getValue().equals(verboseID)).map(Entry::getKey).findAny()
+ .orElse(verboseID);
+ };
}
}
\ No newline at end of file
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()
{
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)
return defaultSnippetSupplier;
}
+ public String resolveID(String id)
+ {
+ if (id.startsWith("class:"))
+ return id;
+ return standardSnippetIDClassNames.get(id);
+ }
+
// static helpers
private static void tryLoadSnippetClass(String snippetClassName)
mograsim version: 0.1.3
{
"standardSubcomponentHandlerSuppliers": {
- "delegating": "net.mograsim.logic.model.snippets.highlevelstatehandlers.standard.subcomponent.DelegatingSubcomponentHighLevelStateHandler"
+ "delegating": "class:net.mograsim.logic.model.snippets.highlevelstatehandlers.standard.subcomponent.DelegatingSubcomponentHighLevelStateHandler"
},
"standardAtomicHandlerSuppliers": {
- "delegating": "net.mograsim.logic.model.snippets.highlevelstatehandlers.standard.atomic.DelegatingAtomicHighLevelStateHandler",
- "wireForcing": "net.mograsim.logic.model.snippets.highlevelstatehandlers.standard.atomic.WireForcingAtomicHighLevelStateHandler",
- "bitVectorSplitting": "net.mograsim.logic.model.snippets.highlevelstatehandlers.standard.atomic.BitVectorSplittingAtomicHighLevelStateHandler"
+ "delegating": "class:net.mograsim.logic.model.snippets.highlevelstatehandlers.standard.atomic.DelegatingAtomicHighLevelStateHandler",
+ "wireForcing": "class:net.mograsim.logic.model.snippets.highlevelstatehandlers.standard.atomic.WireForcingAtomicHighLevelStateHandler",
+ "bitVectorSplitting": "class:net.mograsim.logic.model.snippets.highlevelstatehandlers.standard.atomic.BitVectorSplittingAtomicHighLevelStateHandler"
}
}
\ No newline at end of file
mograsim version: 0.1.3
{
"standardOutlineRendererSuppliers": {
- "default": "net.mograsim.logic.model.snippets.outlinerenderers.DefaultOutlineRenderer"
+ "default": "class:net.mograsim.logic.model.snippets.outlinerenderers.DefaultOutlineRenderer"
},
"standardSymbolRendererSuppliers": {
- "default": "net.mograsim.logic.model.snippets.symbolrenderers.DefaultSymbolRenderer",
- "centeredText": "net.mograsim.logic.model.snippets.symbolrenderers.CenteredTextSymbolRenderer",
- "simpleRectangularLike": "net.mograsim.logic.model.snippets.symbolrenderers.SimpleRectangularLikeSymbolRenderer",
- "pinNames": "net.mograsim.logic.model.snippets.symbolrenderers.PinNamesSymbolRenderer"
+ "default": "class:net.mograsim.logic.model.snippets.symbolrenderers.DefaultSymbolRenderer",
+ "centeredText": "class:net.mograsim.logic.model.snippets.symbolrenderers.CenteredTextSymbolRenderer",
+ "simpleRectangularLike": "class:net.mograsim.logic.model.snippets.symbolrenderers.SimpleRectangularLikeSymbolRenderer",
+ "pinNames": "class:net.mograsim.logic.model.snippets.symbolrenderers.PinNamesSymbolRenderer"
},
"standardHighLevelStateHandlerSuppliers": {
- "default": "net.mograsim.logic.model.snippets.highlevelstatehandlers.DefaultHighLevelStateHandler",
- "standard": "net.mograsim.logic.model.snippets.highlevelstatehandlers.standard.StandardHighLevelStateHandler"
+ "default": "class:net.mograsim.logic.model.snippets.highlevelstatehandlers.DefaultHighLevelStateHandler",
+ "standard": "class:net.mograsim.logic.model.snippets.highlevelstatehandlers.standard.StandardHighLevelStateHandler"
}
}
\ No newline at end of file