Use SWT's FileDialog instead of self-written one
[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.SubmodelComponentSerializer;
12
13 public class SaveLoadManager
14 {
15         private String savePath = null;
16         private Editor editor;
17
18         public SaveLoadManager(Editor editor)
19         {
20                 this.editor = editor;
21         }
22
23         public void save()
24         {
25                 if (savePath == null)
26                         openSaveAsDialog();
27                 else
28                         innerSave();
29         }
30
31         public void openSaveAsDialog()
32         {
33                 Shell fdShell = new Shell();
34                 FileDialog fd = new FileDialog(fdShell, SWT.SAVE);
35                 fd.setText("Save as...");
36                 fd.setFilterExtensions(new String[] { "*.json" });
37                 String result = fd.open();
38                 fdShell.dispose();
39                 if (result != null)
40                 {
41                         savePath = result;
42                         innerSave();
43                 }
44         }
45
46         private void innerSave()
47         {
48                 try
49                 {
50                         SubmodelComponentSerializer.serialize(editor.toBeEdited, c ->
51                         {
52                                 if (Editor.identifierPerComponent.containsKey(c))
53                                         return Editor.identifierPerComponent.get(c);
54                                 return "class:" + c.getClass().getCanonicalName();
55                         }, savePath);
56                 }
57                 catch (IOException e)
58                 {
59                         savePath = null;
60                         System.err.println("Failed to save component!");
61                         e.printStackTrace();
62                 }
63         }
64
65         public static void openLoadDialog() throws IOException
66         {
67                 Shell fdShell = new Shell();
68                 FileDialog fd = new FileDialog(fdShell, SWT.OPEN);
69                 fd.setText("Load component...");
70                 fd.setFilterExtensions(new String[] { "*.json" });
71                 String result = fd.open();
72                 fdShell.dispose();
73                 if (result != null)
74                 {
75                         new Editor((DeserializedSubmodelComponent) SubmodelComponentSerializer.deserialize(new ViewModelModifiable(), result));
76                 }
77         }
78 }