1 package net.mograsim.logic.model.editor;
3 import java.io.IOException;
5 import java.util.Map.Entry;
7 import org.eclipse.swt.SWT;
8 import org.eclipse.swt.widgets.FileDialog;
9 import org.eclipse.swt.widgets.Shell;
11 import net.mograsim.logic.model.model.ViewModelModifiable;
12 import net.mograsim.logic.model.serializing.DeserializedSubmodelComponent;
13 import net.mograsim.logic.model.serializing.IdentifierGetter;
14 import net.mograsim.logic.model.serializing.IndirectGUIComponentCreator;
15 import net.mograsim.logic.model.serializing.SubmodelComponentSerializer;
16 import net.mograsim.logic.model.snippets.SubmodelComponentSnippetSuppliers;
17 import net.mograsim.logic.model.snippets.highlevelstatehandlers.standard.StandardHighLevelStateHandlerSnippetSuppliers;
19 public class SaveLoadManager
21 private String savePath = null;
22 private Editor editor;
24 public SaveLoadManager(Editor editor)
37 public void openSaveAsDialog()
39 Shell fdShell = new Shell();
40 FileDialog fd = new FileDialog(fdShell, SWT.SAVE);
41 fd.setText("Save as...");
42 fd.setFilterExtensions(new String[] { "*.json" });
43 String result = fd.open();
52 private void innerSave()
56 IdentifierGetter idGetter = new IdentifierGetter();
57 idGetter.componentIDs = c -> getStandardID(c, IndirectGUIComponentCreator.getStandardComponentIDs(), true);
58 idGetter.symbolRendererIDs = h -> getStandardID(h,
59 SubmodelComponentSnippetSuppliers.symbolRendererSupplier.getStandardSnippetIDs());
60 idGetter.outlineRendererIDs = h -> getStandardID(h,
61 SubmodelComponentSnippetSuppliers.outlineRendererSupplier.getStandardSnippetIDs());
62 idGetter.highLevelStateHandlerIDs = h -> getStandardID(h,
63 SubmodelComponentSnippetSuppliers.highLevelStateHandlerSupplier.getStandardSnippetIDs());
64 idGetter.atomicHighLevelStateHandlerIDs = h -> getStandardID(h,
65 StandardHighLevelStateHandlerSnippetSuppliers.atomicHandlerSupplier.getStandardSnippetIDs());
66 idGetter.subcomponentHighLevelStateHandlerIDs = h -> getStandardID(h,
67 StandardHighLevelStateHandlerSnippetSuppliers.subcomponentHandlerSupplier.getStandardSnippetIDs());
68 SubmodelComponentSerializer.serialize(editor.toBeEdited, idGetter, savePath);
73 System.err.println("Failed to save component!");
78 private static String getStandardID(Object o, Map<String, String> standardIDs)
80 return getStandardID(o, standardIDs, false);
83 private static String getStandardID(Object o, Map<String, String> standardIDs, boolean standardIDsHaveClassConcatenated)
85 String verboseID = (standardIDsHaveClassConcatenated ? "class:" : "") + o.getClass().getCanonicalName();
86 return standardIDs.entrySet().stream().filter(e -> e.getValue().equals(verboseID)).map(Entry::getKey).findAny()
87 .orElseGet(() -> (standardIDsHaveClassConcatenated ? "" : "class:") + verboseID);
90 public static void openLoadDialog() throws IOException
92 Shell fdShell = new Shell();
93 FileDialog fd = new FileDialog(fdShell, SWT.OPEN);
94 fd.setText("Load component...");
95 fd.setFilterExtensions(new String[] { "*.json" });
96 String result = fd.open();
100 new Editor((DeserializedSubmodelComponent) SubmodelComponentSerializer.deserialize(new ViewModelModifiable(), result));