The final restructured version for automatic build using maven tycho
[Mograsim.git] / plugins / net.mograsim.logic.model.editor / src / net / mograsim / logic / model / editor / SaveLoadManager.java
diff --git a/plugins/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/SaveLoadManager.java b/plugins/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/SaveLoadManager.java
new file mode 100644 (file)
index 0000000..bbe202b
--- /dev/null
@@ -0,0 +1,75 @@
+package net.mograsim.logic.model.editor;
+
+import java.io.IOException;
+
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.widgets.FileDialog;
+import org.eclipse.swt.widgets.Shell;
+
+import net.mograsim.logic.model.model.LogicModelModifiable;
+import net.mograsim.logic.model.serializing.DeserializedSubmodelComponent;
+import net.mograsim.logic.model.serializing.IndirectModelComponentCreator;
+import net.mograsim.logic.model.serializing.SubmodelComponentSerializer;
+
+public class SaveLoadManager
+{
+       private String savePath = null;
+       private Editor editor;
+
+       public SaveLoadManager(Editor editor)
+       {
+               this.editor = editor;
+       }
+
+       public void save()
+       {
+               if (savePath == null)
+                       openSaveAsDialog();
+               else
+                       innerSave();
+       }
+
+       public void openSaveAsDialog()
+       {
+               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;
+                       innerSave();
+               }
+       }
+
+       private void innerSave()
+       {
+               try
+               {
+                       SubmodelComponentSerializer.serialize(editor.toBeEdited, savePath);
+               }
+               catch (IOException e)
+               {
+                       savePath = null;
+                       System.err.println("Failed to save component!");
+                       e.printStackTrace();
+               }
+       }
+
+       public static void openLoadDialog() throws IOException
+       {
+               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) IndirectModelComponentCreator.createComponent(new LogicModelModifiable(),
+                                       "jsonfile:" + result));
+               }
+       }
+}