Serializing now serializes everything; among many other things:
[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
5 import org.eclipse.swt.SWT;
6 import org.eclipse.swt.widgets.FileDialog;
7 import org.eclipse.swt.widgets.Shell;
8
9 import net.mograsim.logic.model.model.ViewModelModifiable;
10 import net.mograsim.logic.model.serializing.DeserializedSubmodelComponent;
11 import net.mograsim.logic.model.serializing.IdentifierGetter;
12 import net.mograsim.logic.model.serializing.SubmodelComponentSerializer;
13
14 public class SaveLoadManager
15 {
16         private String savePath = null;
17         private Editor editor;
18
19         public SaveLoadManager(Editor editor)
20         {
21                 this.editor = editor;
22         }
23
24         public void save()
25         {
26                 if (savePath == null)
27                         openSaveAsDialog();
28                 else
29                         innerSave();
30         }
31
32         public void openSaveAsDialog()
33         {
34                 Shell fdShell = new Shell();
35                 FileDialog fd = new FileDialog(fdShell, SWT.SAVE);
36                 fd.setText("Save as...");
37                 fd.setFilterExtensions(new String[] { "*.json" });
38                 String result = fd.open();
39                 fdShell.dispose();
40                 if (result != null)
41                 {
42                         savePath = result;
43                         innerSave();
44                 }
45         }
46
47         private void innerSave()
48         {
49                 try
50                 {
51                         IdentifierGetter idGetter = new IdentifierGetter();
52                         idGetter.componentIDs = c ->
53                         {
54                                 if (Editor.identifierPerComponent.containsKey(c))
55                                         return Editor.identifierPerComponent.get(c);
56                                 return "class:" + c.getClass().getCanonicalName();
57                         };
58                         SubmodelComponentSerializer.serialize(editor.toBeEdited, idGetter, savePath);
59                 }
60                 catch (IOException e)
61                 {
62                         savePath = null;
63                         System.err.println("Failed to save component!");
64                         e.printStackTrace();
65                 }
66         }
67
68         public static void openLoadDialog() throws IOException
69         {
70                 Shell fdShell = new Shell();
71                 FileDialog fd = new FileDialog(fdShell, SWT.OPEN);
72                 fd.setText("Load component...");
73                 fd.setFilterExtensions(new String[] { "*.json" });
74                 String result = fd.open();
75                 fdShell.dispose();
76                 if (result != null)
77                 {
78                         new Editor((DeserializedSubmodelComponent) SubmodelComponentSerializer.deserialize(new ViewModelModifiable(), result));
79                 }
80         }
81 }