Преглед на файлове

Fix for save-Files with old type names.

Tom Troppmann преди 3 години
родител
ревизия
2250d9951c
променени са 1 файла, в които са добавени 38 реда и са изтрити 18 реда
  1. 38 18
      src/ui/controller/LoadController.java

+ 38 - 18
src/ui/controller/LoadController.java

@@ -38,7 +38,13 @@ public class LoadController {
     private NodeController uppC;
     private MultiPurposeController mpC;
     private JsonParser parser;
-
+    private static HashMap<String,String> OldTypeNameReplacement = new HashMap<String, String>();
+    
+    static {
+    	OldTypeNameReplacement.put("CpsNode", "Node");
+    	OldTypeNameReplacement.put("CpsUpperNode", "GroupNode");
+    	
+    }
     /**
      * Constructor.
      *
@@ -50,6 +56,7 @@ public class LoadController {
      */
     LoadController(Model model, CategoryController cg, CanvasController cvs, ObjectController obj,
                    NodeController uppC, MultiPurposeController mp) {
+    	
         this.model = model;
         this.cgC = cg;
         this.cvsC = cvs;
@@ -159,11 +166,11 @@ public class LoadController {
             if (key.contains("CATEGORY"))
                 loadCategory(json.get(key));
             if (key.contains("CGOBJECT"))
-                loadCategoryObject(json.get(key));
+                loadCategoryObject(json.get(key).getAsJsonObject());
             if (key.contains("CGELEMENT"))
                 loadCategoryElements(json.get(key), eleDispatch);//TODO
             if (key.contains("CVSOBJECT"))
-                loadCanvasObject(json.get(key), objDispatch);
+                loadCanvasObject(json.get(key).getAsJsonObject(), objDispatch);
             if (key.contains("CVSELEMENT"))
                 loadCanvasElements(json.get(key), objDispatch, eleDispatch);
             if (key.contains("SWUNITGRAPH"))
@@ -225,8 +232,16 @@ public class LoadController {
     /**
      * Load a given Object in Category by Deserialization
      */
-    private void loadCategoryObject(JsonElement jsonElement) {
-        AbstractCanvasObject temp = model.getGson().fromJson(jsonElement.getAsJsonObject(), AbstractCanvasObject.class);
+    private void loadCategoryObject(JsonObject jsonObject) {
+    	AbstractCanvasObject temp;
+    	replaceOldClassNames(jsonObject);
+    	try {
+    		temp = model.getGson().fromJson(jsonObject, AbstractCanvasObject.class);
+    	}catch (com.google.gson.JsonParseException e){
+    		System.err.println(jsonObject.get("type").getAsString());
+    		return;
+    	}
+        
         temp.setImage(checkOS(temp.getImage()));
         initObjects(temp);
         if (mpC.searchCatObj(mpC.searchCat(temp.getSav()), temp.getObjName()) != null)
@@ -250,24 +265,20 @@ public class LoadController {
     /**
      * Load a given Object in Canvas by Deserialization
      */
-    private void loadCanvasObject(JsonElement jsonElement, HashMap<Integer, AbstractCanvasObject> objDispatch) {
+    private void loadCanvasObject(JsonObject jsonObject, HashMap<Integer, AbstractCanvasObject> objDispatch) {
     	AbstractCanvasObject temp = null;
-    	if(jsonElement.getAsJsonObject().get("type").getAsString().equals("CpsUpperNode")) {
-    		//Change Old Name
-    		jsonElement.getAsJsonObject().addProperty("type", "GroupNode");
+    	replaceOldClassNames(jsonObject);
+    	try {
+    		temp = model.getGson().fromJson(jsonObject, AbstractCanvasObject.class);
+    	}catch (com.google.gson.JsonParseException e){
+    		System.err.println(jsonObject.get("type").getAsString());
+    		return;
     	}
-    	try{
-        	temp = model.getGson().fromJson(jsonElement.getAsJsonObject(), AbstractCanvasObject.class);
-        }catch(java.lang.ClassCastException e) {
-        	System.err.println("ClassRenameError");
-        	e.printStackTrace();
-        	return;
-        }
         initObjects(temp);
         temp.setImage(checkOS(temp.getImage()));
         if (temp instanceof GroupNode) {
-            model.getHashcodeMap().put(jsonElement.getAsJsonObject().get("hash").getAsInt(), (GroupNode) temp);
-            ((GroupNode) temp).setLeftBorder(jsonElement.getAsJsonObject().get("properties").getAsJsonObject().get("leftBorder").getAsInt());
+            model.getHashcodeMap().put(jsonObject.get("hash").getAsInt(), (GroupNode) temp);
+            ((GroupNode) temp).setLeftBorder(jsonObject.get("properties").getAsJsonObject().get("leftBorder").getAsInt());
         }
         // if its stored before on the canvas just put it there
         if (temp.getSav().equals("CVS")) {
@@ -291,6 +302,15 @@ public class LoadController {
 
     }
 
+    
+    
+	private void replaceOldClassNames(JsonObject abstractCanvasClassJsonObject) {
+		String type = abstractCanvasClassJsonObject.get("type").getAsString();
+		if(OldTypeNameReplacement.containsKey(type)) {
+			abstractCanvasClassJsonObject.addProperty("type", OldTypeNameReplacement.get(type));
+		}
+	}
+
     /**
      * Load a given Element in Canvas by Deserialization
      */