Browse Source

Merge branch 'BP_master' of git@git.tk.informatik.tu-darmstadt.de:carlos.garcia/praktikum-holons.git into BP_master

Tom Troppmann 6 years ago
parent
commit
9338ad0d22

+ 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())

+ 5 - 4
src/ui/controller/CanvasController.java

@@ -42,14 +42,15 @@ public class CanvasController {
 	 * 
 	 * @param object
 	 *            CpsObject to be added.
+	 * @param replace when true objects could be replaced
 	 */
-	public void addObject(AbstractCpsObject object) {
+	public void addObject(AbstractCpsObject object, boolean replace) {
 		model.getCvsObjIdx().put(object.getId(), model.getObjectsOnCanvas().size());
 		model.getObjectsOnCanvas().add(object);
 		/**
 		 * check if we should drag & drop replace
 		 */
-		if(!(object instanceof CpsNode)){
+		if(!(object instanceof CpsNode) && replace){
 			/** x of the dragged Object */
 			int x = object.getPosition().x;
 			
@@ -102,7 +103,7 @@ public class CanvasController {
 	public void addNewObject(AbstractCpsObject object) {
 		object.setSav("CVS");
 		// object.setConnections(new ArrayList<CpsEdge>());
-		addObject(object);
+		addObject(object, true);
 	}
 
 	/**
@@ -237,7 +238,7 @@ public class CanvasController {
 			tCps.setPosition(new Position(p.x + (cps.getPosition().x - x), p.y + (cps.getPosition().y - y)));
 			tCps.setSav(cps.getSav());
 			tempList.add(tCps);
-			addObject(tCps);
+			addObject(tCps, false);
 			// MODEL.getSelectedCpsObjects().add(tCps);
 		}
 

+ 13 - 2
src/ui/controller/ClipboardController.java

@@ -204,7 +204,7 @@ public class ClipboardController {
         // if its stored before on the canvas just put it there
         if (temp.getSav().equals(sav)) {
             if (upperNode == null)
-                cvsC.addObject(temp);
+                cvsC.addObject(temp, false);
             else
                 uppC.addObjectInUpperNode(temp, upperNode, false);
             // mark the Pasted Objects
@@ -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, false);
+                 else
+                     uppC.addObjectInUpperNode(temp, upperNode, false);
+            }
         }
 
         objDispatch.put(temp.getId(), temp);

+ 22 - 4
src/ui/controller/LoadController.java

@@ -268,13 +268,21 @@ public class LoadController {
         }
         // if its stored before on the canvas just put it there
         if (temp.getSav().equals("CVS")) {
-            cvsC.addObject(temp);
+            cvsC.addObject(temp, false);
 
         } 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, false);
+            }
+
+       }
 
         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) {

+ 29 - 0
src/ui/view/GUI.java

@@ -420,6 +420,21 @@ public class GUI implements CategoryListener {
 			public void actionPerformed(ActionEvent e) {
 				try {
 					controller.loadAutoSave(controller.getUndoSave());
+					/**
+					 * close bugged Tabs
+					 */
+					for (int i = 1; i < tabbedPaneInnerOriginal.getTabCount(); i++) {
+						JScrollPane scrollPaneOriginal = (JScrollPane) tabbedPaneInnerOriginal
+								.getComponentAt(i);
+						if (((UpperNodeCanvas) scrollPaneOriginal.getViewport()
+								.getComponent(0)).upperNode == null) {
+							((ButtonTabComponent) tabbedPaneInnerOriginal
+									.getTabComponentAt(i)).removeTabs();
+							break;
+						}
+					}
+					
+					
 					controller.calculateStateForCurrentTimeStep();
 					canvas.repaint();
 					unitGraph.update(model.getObjectsOnCanvas());
@@ -440,6 +455,20 @@ public class GUI implements CategoryListener {
 			public void actionPerformed(ActionEvent e) {
 				try {
 					controller.loadAutoSave(controller.getRedoSave());
+					/**
+					 * close bugged Tabs
+					 */
+					for (int i = 1; i < tabbedPaneInnerOriginal.getTabCount(); i++) {
+						JScrollPane scrollPaneOriginal = (JScrollPane) tabbedPaneInnerOriginal
+								.getComponentAt(i);
+						if (((UpperNodeCanvas) scrollPaneOriginal.getViewport()
+								.getComponent(0)).upperNode == null) {
+							((ButtonTabComponent) tabbedPaneInnerOriginal
+									.getTabComponentAt(i)).removeTabs();
+							break;
+						}
+					}
+					
 					controller.calculateStateForCurrentTimeStep();
 					canvas.repaint();
 					unitGraph.update(model.getObjectsOnCanvas());

+ 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);