Fixed Editor duplication of deserialized components
[Mograsim.git] / net.mograsim.logic.model.editor / src / net / mograsim / logic / model / editor / SaveLoadManager.java
1 package net.mograsim.logic.model.editor;
2
3 import java.io.IOException;
4 import java.util.Map;
5 import java.util.Map.Entry;
6
7 import org.eclipse.swt.SWT;
8 import org.eclipse.swt.widgets.FileDialog;
9 import org.eclipse.swt.widgets.Shell;
10
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;
18
19 public class SaveLoadManager
20 {
21         private String savePath = null;
22         private Editor editor;
23
24         public SaveLoadManager(Editor editor)
25         {
26                 this.editor = editor;
27         }
28
29         public void save()
30         {
31                 if (savePath == null)
32                         openSaveAsDialog();
33                 else
34                         innerSave();
35         }
36
37         public void openSaveAsDialog()
38         {
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();
44                 fdShell.dispose();
45                 if (result != null)
46                 {
47                         savePath = result;
48                         innerSave();
49                 }
50         }
51
52         private void innerSave()
53         {
54                 try
55                 {
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);
69                 }
70                 catch (IOException e)
71                 {
72                         savePath = null;
73                         System.err.println("Failed to save component!");
74                         e.printStackTrace();
75                 }
76         }
77
78         private static String getStandardID(Object o, Map<String, String> standardIDs)
79         {
80                 return getStandardID(o, standardIDs, false);
81         }
82
83         private static String getStandardID(Object o, Map<String, String> standardIDs, boolean standardIDsHaveClassConcatenated)
84         {
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);
88         }
89
90         public static void openLoadDialog() throws IOException
91         {
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();
97                 fdShell.dispose();
98                 if (result != null)
99                 {
100                         new Editor((DeserializedSubmodelComponent) SubmodelComponentSerializer.deserialize(new ViewModelModifiable(), result));
101                 }
102         }
103 }