Adapt Editor to new system, removed json duplication
[Mograsim.git] / net.mograsim.logic.model.editor / src / net / mograsim / logic / model / editor / Editor.java
index 37efbae..c0a504a 100644 (file)
@@ -1,5 +1,6 @@
 package net.mograsim.logic.model.editor;
 
+import java.io.IOException;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Map;
@@ -12,6 +13,7 @@ import com.google.gson.JsonParser;
 import com.google.gson.JsonSyntaxException;
 
 import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
+import net.mograsim.logic.model.am2900.Am2900Loader;
 import net.mograsim.logic.model.editor.handles.ComponentHandle;
 import net.mograsim.logic.model.editor.handles.Handle;
 import net.mograsim.logic.model.editor.handles.HandleManager;
@@ -23,6 +25,7 @@ import net.mograsim.logic.model.model.ViewModelModifiable;
 import net.mograsim.logic.model.model.components.GUIComponent;
 import net.mograsim.logic.model.model.wires.GUIWire;
 import net.mograsim.logic.model.serializing.DeserializedSubmodelComponent;
+import net.mograsim.logic.model.serializing.IdentifierGetter;
 import net.mograsim.logic.model.serializing.IndirectGUIComponentCreator;
 
 public final class Editor
@@ -52,10 +55,10 @@ public final class Editor
                dialogManager = new DialogManager(gui.shell);
 
                toBeEdited.submodel.addComponentRemovedListener(c -> identifierPerComponent.remove(c));
-               
+
                gui.open();
        }
-       
+
        public ViewModelModifiable getSubmodel()
        {
                return toBeEdited.getSubmodelModifiable();
@@ -66,8 +69,8 @@ public final class Editor
                return selection;
        }
 
-       //TODO: Remove this error prone method: Relative offset may change between multiple moves,
-       //because Handles have different ways of responding to reqMove(...), causing strange behaviour
+       // TODO: Remove this error prone method: Relative offset may change between multiple moves,
+       // because Handles have different ways of responding to reqMove(...), causing strange behaviour
        @Deprecated
        public void moveSelection(double x, double y)
        {
@@ -83,7 +86,7 @@ public final class Editor
                        c.reqMove(newX, newY);
                }
        }
-       
+
        public void moveHandles(double x, double y, Map<Handle, Point> handleOffsetMap)
        {
                Point snapped = new Point(x, y);
@@ -112,7 +115,7 @@ public final class Editor
                for (Handle h : selection)
                {
                        Optional<ComponentInfo> cInfo = h.reqCopy(refPoint);
-                       if(cInfo.isPresent())
+                       if (cInfo.isPresent())
                                copyBuffer.add(cInfo.get());
                }
        }
@@ -129,18 +132,22 @@ public final class Editor
                }
                moveSelection(x, y);
        }
-       
+
        public void save()
        {
                saveManager.save();
        }
 
+       public void saveAs()
+       {
+               saveManager.openSaveAsDialog();
+       }
+
        public void addComponent(double x, double y)
        {
                boolean successful = false;
                JsonElement params = JsonNull.INSTANCE;
-               outer:
-               while(!successful)
+               outer: while (!successful)
                {
                        String selected = gui.getAddListSelected();
                        try
@@ -151,27 +158,28 @@ public final class Editor
                                moveSelection(x, y);
                                successful = true;
                        }
-                       catch(UnsupportedOperationException | JsonSyntaxException | NumberFormatException e)
+                       catch (@SuppressWarnings("unused") UnsupportedOperationException | JsonSyntaxException | NumberFormatException e)
                        {
                                String result = DialogManager.openMultiLineTextDialog("Add component", "Create", "Cancel", "Parameters:");
-                               if(result == null)
+                               if (result == null)
                                        break outer;
                                params = new JsonParser().parse(result);
                        }
                }
        }
-       
+
        private GUIComponent addComponent(String identifier, JsonElement params)
        {
-               GUIComponent comp = IndirectGUIComponentCreator.createComponent(toBeEdited.getSubmodelModifiable(), identifier,
-                               params);
+               GUIComponent comp = IndirectGUIComponentCreator.createComponent(toBeEdited.getSubmodelModifiable(), identifier, params);
                identifierPerComponent.put(comp, identifier);
                return comp;
        }
-       
+
        public static String getIdentifier(GUIComponent c)
        {
-               return identifierPerComponent.get(c);
+               if (identifierPerComponent.containsKey(c))
+                       return identifierPerComponent.get(c);
+               return new IdentifierGetter().componentIDs.apply(c);
        }
 
        public void duplicate()
@@ -188,8 +196,10 @@ public final class Editor
                case OFF:
                        break;
                case ABSOLUTE:
-                       newP.x -= newP.x % snapX;
-                       newP.y -= newP.y % snapY;
+                       newP.x = (int) (newP.x / snapX + .5) * snapX;
+                       newP.y = (int) (newP.y / snapY + .5) * snapY;
+                       break;
+               default:
                        break;
                }
        }
@@ -199,7 +209,7 @@ public final class Editor
                public final double relX, relY;
                public final String identifier;
                public final JsonElement params;
-               
+
                public ComponentInfo(double relX, double relY, String identifier, JsonElement params)
                {
                        this.relX = relX;
@@ -209,6 +219,7 @@ public final class Editor
                }
        }
 
+       @SuppressWarnings("unused")
        public void addWire(PinHandle a, PinHandle b)
        {
                new GUIWire(toBeEdited.getSubmodelModifiable(), a.getPin(), b.getPin(), new Point[0]);
@@ -217,16 +228,17 @@ public final class Editor
        public static enum Snapping
        {
                OFF, ABSOLUTE;
-               
+
                @Override
                public String toString()
                {
                        return super.toString().toLowerCase();
                }
        }
-       
-       public static void main(String[] args)
+
+       public static void main(String[] args) throws IOException
        {
+               Am2900Loader.setup();
                SaveLoadManager.openLoadDialog();
        }