Browse Source

Fixes a lot of Save/Load/Group/Controller things ^^

Adds Fallback to Canvas if Edges weren't loaded into
Model.getCanvasEdges.
Fixes LoadController not being able to process HolonObjects/Edges of no
longer existing UpperNodes.
Adds a bunch of DebugCode / NullPointer handling if problems would still
occur.
Loading/Copying/Pasting e.g. should be fine now.
Andreas T. Meyer-Berg 6 years ago
parent
commit
f5b117069c

+ 7 - 0
src/classes/CpsEdge.java

@@ -274,5 +274,12 @@ public class CpsEdge {
             //maxCapacity = CAPACITY_TO_UPPER_NODE;
         }
     }
+    
+    @Override
+    public String toString(){
+    	String A = a == null ? "null" : a.getName();
+    	String B = b == null ? "null" : b.getName();
+    	return "CpsEdge: " + a + " to " + b;
+    }
 
 }

+ 3 - 0
src/classes/HolonBattery.java

@@ -122,9 +122,12 @@ public class HolonBattery extends AbstractCpsObject{
 	}
 	public String toString()
 	{
+		return "HolonBattery" + this.getId();
+		/*
 		return "HolonBattery ID:" + this.getId() + " State:" + getState().name() 
 				+ " InRatio:" + getInRatio() + " OutRatio:" + getOutRatio() 
 				+ " Akku: " + getStateOfChargeAtTimeStep(SingletonControl.getInstance().getControl().getModel().getCurIteration()) + "/" + getCapacity();
+	    */
 	}
 	public void setStateOfChargeAtTimeStep(float newStateOfCharge, int x) {
 		if(iterations != SingletonControl.getInstance().getControl().getModel().getIterations())

+ 12 - 1
src/ui/controller/ClipboardController.java

@@ -212,7 +212,18 @@ public class ClipboardController {
         } else {
             // else look up the table and put it into the right Uppernode
             CpsUpperNode temp2 = (CpsUpperNode) objDispatch.get(objIDMap.get(Integer.parseInt(temp.getSav())));
-            uppC.addObjectInUpperNode(temp, temp2, false);
+            if(temp2!=null)
+            	uppC.addObjectInUpperNode(temp, temp2, false);
+            else{
+            	/**
+            	 * if corresponding Uppernode doesn't exist:
+            	 * Add it to the current Canvas/UpperNode
+            	 */
+            	 if (upperNode == null)
+                     cvsC.addObject(temp);
+                 else
+                     uppC.addObjectInUpperNode(temp, upperNode, false);
+            }
         }
 
         objDispatch.put(temp.getId(), temp);

+ 21 - 3
src/ui/controller/LoadController.java

@@ -273,8 +273,16 @@ public class LoadController {
         } else {
             // else look up the table and put it into the right Uppernode
             CpsUpperNode temp2 = (CpsUpperNode) objDispatch.get(Integer.parseInt(temp.getSav()));
-            uppC.addObjectInUpperNode(temp, temp2, false);
-        }
+            if(temp2 != null)
+            	uppC.addObjectInUpperNode(temp, temp2, false);
+            else{
+            	/**
+            	 * if null try to load on canvas
+            	 */
+            	cvsC.addObject(temp);
+            }
+
+       }
 
         objDispatch.put(temp.getId(), temp);
 
@@ -319,6 +327,11 @@ public class LoadController {
                 model.getEdgesOnCanvas().add(temp);
                 break;
             case CONNECTION:
+            	if(temp.getA() == null || temp.getB() == null){
+            		System.err.println(temp.toString());
+            		new Error().getStackTrace();
+            		break;
+            	}
                 // if no duplicates in connection store them into the given A and B
                 if (!uppC.lookforDuplicates(temp.getA(), temp.getB(), temp.getA().getConnections()))
                     temp.getA().getConnections().add(temp);
@@ -340,7 +353,12 @@ public class LoadController {
         }
 
         if (object.get("connection").getAsBoolean() && !type.equals(EDGETYPE.CONNECTION)) {
-            temp.getA().getConnections().add(temp);
+            if(temp.getA() == null || temp.getB() == null){
+            	System.err.println("invaldid Connection loaded: " + temp.toString());
+            	new Error().printStackTrace();
+            	return;
+            }
+        	temp.getA().getConnections().add(temp);
             temp.getB().getConnections().add(temp);
         }
 

+ 1 - 1
src/ui/controller/NodeController.java

@@ -451,7 +451,7 @@ class NodeController {
         		return;
         	}
         if(upperNode == null){
-    		new Error("object == null while adding "+object.toString()).printStackTrace();
+    		new Error("upperNode == null while adding "+object.toString()).printStackTrace();
     		return;
     	}
     	object.setSav("" + upperNode.getId());

+ 1 - 1
src/ui/view/AbstractCanvas.java

@@ -93,7 +93,7 @@ public abstract class AbstractCanvas extends JPanel {
 	// ------------------------------------------ METHODS
 	// ------------------------------------------
 	String paintEdge(CpsEdge con, String maxCap) {
-		if (con!=null && con.getA().getId() != model.getSelectedObjectID() && con.getB().getId() != model.getSelectedObjectID()
+		if (con!=null && con.getA() != null && con.getB() != null && con.getA().getId() != model.getSelectedObjectID() && con.getB().getId() != model.getSelectedObjectID()
 				&& con != edgeHighlight) {
 			if (con.getConnected() == CpsEdge.CON_UPPER_NODE
 					|| con.getConnected() == CpsEdge.CON_UPPER_NODE_AND_INSIDE) {

+ 11 - 1
src/ui/view/MyCanvas.java

@@ -430,6 +430,16 @@ public class MyCanvas extends AbstractCanvas implements MouseListener,
 			g2.drawLine(tempCps.getPosition().x, tempCps.getPosition().y, x, y);
 		}
 
+		if(model.getEdgesOnCanvas().isEmpty() && !model.getObjectsOnCanvas().isEmpty()
+				&& !model.getObjectsOnCanvas().get(0).getConnections().isEmpty()){
+			System.out.println("Edges were not added to Canvas: Fixing");
+			for(AbstractCpsObject cps :model.getObjectsOnCanvas())
+				for(CpsEdge e: cps.getConnections()){
+					if(!model.getEdgesOnCanvas().contains(e))
+						model.addEdgeOnCanvas(e);
+				}
+		}
+		
 		for (CpsEdge con : model.getEdgesOnCanvas()) {
 			maxCap = paintEdge(con, maxCap);
 		}
@@ -658,8 +668,8 @@ public class MyCanvas extends AbstractCanvas implements MouseListener,
 		if (doubleClick() && tempCps != null && tempCps instanceof HolonSwitch
 				&& MouseEvent.BUTTON3 != e.getButton()) {
 			((HolonSwitch) tempCps).switchState();
-		}
 
+		}
 		controller.calculateStateForTimeStep(model.getCurIteration());
 
 		triggerUpdateController();

+ 2 - 1
src/ui/view/UpperNodeCanvas.java

@@ -34,7 +34,7 @@ public class UpperNodeCanvas extends AbstractCanvas implements MouseListener, Mo
     protected String path;
     int code;
     private JLabel breadCrumb;
-
+    private int upperNodeID;
 
     /**
      * Constructor.
@@ -49,6 +49,7 @@ public class UpperNodeCanvas extends AbstractCanvas implements MouseListener, Mo
         this.controller = control;
         this.model = mod;
         this.upperNode = UpperNode;
+        upperNodeID = UpperNode.getId();
         this.code = UpperNode.hashCode();
         this.path = parentPath + upperNode.getName();
         this.breadCrumb = new JLabel(path);