Moved Am2904/Am2910Testbenches to test source folder
[Mograsim.git] / net.mograsim.logic.model.editor / src / net / mograsim / logic / model / editor / SaveLoadManager.java
index f4a2eca..1e3d271 100644 (file)
@@ -1,12 +1,20 @@
 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 org.eclipse.swt.widgets.Shell;
 
-import net.mograsim.logic.model.editor.ui.DialogManager;
 import net.mograsim.logic.model.model.ViewModelModifiable;
 import net.mograsim.logic.model.serializing.DeserializedSubmodelComponent;
-import net.mograsim.logic.model.serializing.SubmodelComponentDeserializer;
-import net.mograsim.logic.model.util.JsonHandler;
+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
 {
@@ -28,10 +36,15 @@ public class SaveLoadManager
 
        public void openSaveAsDialog()
        {
-               String result[] = DialogManager.openMultiTextDialog("Save as...", "Save", "Cancel", "Path");
-               if(result != null)
+               Shell fdShell = new Shell();
+               FileDialog fd = new FileDialog(fdShell, SWT.SAVE);
+               fd.setText("Save as...");
+               fd.setFilterExtensions(new String[] { "*.json" });
+               String result = fd.open();
+               fdShell.dispose();
+               if (result != null)
                {
-                       savePath = result[0];
+                       savePath = result;
                        innerSave();
                }
        }
@@ -40,13 +53,21 @@ public class SaveLoadManager
        {
                try
                {
-                       JsonHandler.writeJson(editor.toBeEdited.calculateParams(c ->
-                       {
-                               if (Editor.identifierPerComponent.containsKey(c))
-                                       return Editor.identifierPerComponent.get(c);
-                               return "class:" + c.getClass().getCanonicalName();
-                       }), savePath);
-               } catch (IOException e)
+                       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);
+               }
+               catch (IOException e)
                {
                        savePath = null;
                        System.err.println("Failed to save component!");
@@ -54,13 +75,30 @@ public class SaveLoadManager
                }
        }
 
-       public static void openLoadDialog()
+       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
        {
-               String[] result = DialogManager.openMultiTextDialog("Load Component...", "Load", "Cancel", "Path");
-               if(result != null)
+               Shell fdShell = new Shell();
+               FileDialog fd = new FileDialog(fdShell, SWT.OPEN);
+               fd.setText("Load component...");
+               fd.setFilterExtensions(new String[] { "*.json" });
+               String result = fd.open();
+               fdShell.dispose();
+               if (result != null)
                {
-                       new Editor((DeserializedSubmodelComponent) SubmodelComponentDeserializer
-                                       .create(new ViewModelModifiable(), result[0]));
+                       new Editor((DeserializedSubmodelComponent) IndirectGUIComponentCreator.createComponent(new ViewModelModifiable(),
+                                       "file:" + result));
                }
        }
 }