Merge branch 'development' of https://gitlab.lrz.de/lrr-tum/students/eragp-misim...
[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 ->
58                         {
59                                 if (Editor.identifierPerComponent.containsKey(c))
60                                         return Editor.identifierPerComponent.get(c);
61                                 return getStandardID(c, IndirectGUIComponentCreator.getStandardComponentIDs(), true);
62                         };
63                         idGetter.symbolRendererIDs = h -> getStandardID(h,
64                                         SubmodelComponentSnippetSuppliers.symbolRendererSupplier.getStandardSnippetIDs());
65                         idGetter.outlineRendererIDs = h -> getStandardID(h,
66                                         SubmodelComponentSnippetSuppliers.outlineRendererSupplier.getStandardSnippetIDs());
67                         idGetter.highLevelStateHandlerIDs = h -> getStandardID(h,
68                                         SubmodelComponentSnippetSuppliers.highLevelStateHandlerSupplier.getStandardSnippetIDs());
69                         idGetter.atomicHighLevelStateHandlerIDs = h -> getStandardID(h,
70                                         StandardHighLevelStateHandlerSnippetSuppliers.atomicHandlerSupplier.getStandardSnippetIDs());
71                         idGetter.subcomponentHighLevelStateHandlerIDs = h -> getStandardID(h,
72                                         StandardHighLevelStateHandlerSnippetSuppliers.subcomponentHandlerSupplier.getStandardSnippetIDs());
73                         SubmodelComponentSerializer.serialize(editor.toBeEdited, idGetter, savePath);
74                 }
75                 catch (IOException e)
76                 {
77                         savePath = null;
78                         System.err.println("Failed to save component!");
79                         e.printStackTrace();
80                 }
81         }
82
83         private static String getStandardID(Object o, Map<String, String> standardIDs)
84         {
85                 return getStandardID(o, standardIDs, false);
86         }
87
88         private static String getStandardID(Object o, Map<String, String> standardIDs, boolean standardIDsHaveClassConcatenated)
89         {
90                 String verboseID = (standardIDsHaveClassConcatenated ? "class:" : "") + o.getClass().getCanonicalName();
91                 return standardIDs.entrySet().stream().filter(e -> e.getValue().equals(verboseID)).map(Entry::getKey).findAny()
92                                 .orElseGet(() -> (standardIDsHaveClassConcatenated ? "" : "class:") + verboseID);
93         }
94
95         public static void openLoadDialog() throws IOException
96         {
97                 Shell fdShell = new Shell();
98                 FileDialog fd = new FileDialog(fdShell, SWT.OPEN);
99                 fd.setText("Load component...");
100                 fd.setFilterExtensions(new String[] { "*.json" });
101                 String result = fd.open();
102                 fdShell.dispose();
103                 if (result != null)
104                 {
105                         new Editor((DeserializedSubmodelComponent) SubmodelComponentSerializer.deserialize(new ViewModelModifiable(), result));
106                 }
107         }
108 }