Quellcode durchsuchen

Delete Element By ID

Teh-Hai Julian Zheng vor 8 Jahren
Ursprung
Commit
7a428ab1d1

+ 2 - 2
src/ui/controller/Control.java

@@ -88,8 +88,8 @@ public class Control {
 		objectController.addNewElementIntoCategoryObject(catName, objName, eleName, amount, energy);
 	}
 
-	public void deleteElementCanvas(String object, String element, int amount) {
-		objectController.deleteElementInCanvas(object, element, amount);
+	public void deleteElementCanvas(int id, String element, int amount) {
+		objectController.deleteElementInCanvas(id, element, amount);
 	}
 
 	/* Global Operations*/

+ 19 - 3
src/ui/controller/ObjectController.java

@@ -111,12 +111,12 @@ public class ObjectController {
 
 	/**
 	 * deletes a Element from a given Canvas Object
-	 * @param obj
+	 * @param ID
 	 * @param ele
 	 * @param amount
 	 */
-	public void deleteElementInCanvas(String obj, String ele, int amount) {
-		HolonObject object = searchHolonObject(obj, MODEL.getObjectsOnCanvas());
+	public void deleteElementInCanvas(int ID, String ele, int amount) {
+		HolonObject object = searchByID(ID, MODEL.getObjectsOnCanvas());
 		HolonElement element = searchHolonElement(object, ele);
 
 		element.setAmount(element.getAmount() - amount);
@@ -140,6 +140,20 @@ public class ObjectController {
 		}
 		return null;
 	}
+	
+	/**
+	 * Search for Object by ID
+	 * @param ID
+	 * @param list
+	 * @return
+	 */
+	public HolonObject searchByID(int ID, ArrayList<CpsObject> list){
+		for (CpsObject objects : list) {
+			if(objects.getID() == ID)
+				return (HolonObject) objects;
+		}
+		return null;
+	}
 
 	/**
 	 * Search for Element
@@ -155,6 +169,8 @@ public class ObjectController {
 		}
 		return null;
 	}
+	
+
 
 	/**
 	 * Returns the ID of the selected Object 0 = no Object is selected

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

@@ -172,7 +172,7 @@ class MyCanvas extends JPanel implements MouseListener, MouseMotionListener {
 				// If drawing an Edge (CTRL down)
 				if (e.isControlDown()){
 					drawEdge = true;
-					controller.deleteElementCanvas("Power Plant", "Rehab", 1);
+					controller.deleteElementCanvas(1, "Rehab", 1);
 					System.out.println(((HolonObject)model.getCategories().get(0).getObjects().get(0)).getElements().get(2).getAmount());
 				}
 				if (tempCps.getClass() == HolonObject.class) {