Pārlūkot izejas kodu

Object Controls for adding Elements

Teh-Hai Julian Zheng 8 gadi atpakaļ
vecāks
revīzija
b3235dc4b4

BIN
bin/classes/HolonElement.class


BIN
bin/classes/HolonObject.class


BIN
bin/ui/controller/ObjectControl.class


+ 2 - 21
src/classes/Category.java

@@ -11,8 +11,8 @@ public class Category implements ComparableObject{
 	private String name;
 	
 	public Category(String name){
-		this.setObjects(new ArrayList<CpsObject>());
-		this.setName(name);
+		setObjects(new ArrayList<CpsObject>());
+		setName(name);
 		id = -1;
 	}
 
@@ -54,23 +54,4 @@ public class Category implements ComparableObject{
 		return id;
 	}
 	
-//	public void addObject(HolonObject toAdd){
-//		objects.add(toAdd);
-//	}
-//	
-//	public void deleteObject(int idx){
-//		objects.remove(idx);
-//	}
-//	
-//	public ArrayList<HolonObject> getObjects(){
-//		return objects;
-//	}
-//	
-//	public void changeName(String name){
-//		this.name = name;
-//	}
-//	
-//	public String getName(){
-//		return name;
-//	}
 }

+ 55 - 28
src/classes/HolonElement.java

@@ -3,79 +3,106 @@ package classes;
 public class HolonElement {
 
 	/* Name of the gadget */
-	String name;
+	String eleName;
 	/* Quantity */
 	int amount;
 	/* Energy per gadget */
 	float energy;
 	/* If the gadget is working xor not (true xor false) */
-	boolean isWorking;
+	boolean active;
 	/* Total Energy */
 	float totalEnergy;
 	/* Path of the image for the Obj. */
 	String image;
 
-	public HolonElement(String name, int amount, float energy) {
-		this.name = name;
-		this.amount = amount;
-		this.energy = energy;
-		this.isWorking = true;
-		// System.out.println("You create some " + name + "'s. The amount is:" +
-		// amount);
-		// System.out.println("It's actual status is: " + isWorking);
+	
+	
+	public HolonElement(String eleName, int amount, float energy) {
+		setEleName(eleName);
+		setAmount(amount);
+		setEnergy(energy);
+		setActive(true);
 	}
 
-	public HolonElement(HolonElement hol) {
-		this.name = hol.getName();
-		this.amount = hol.getAmount();
-		this.energy = hol.getEnergy();
-		this.isWorking = true;
-		// System.out.println("You create some " + name + "'s. The amount is:" +
-		// amount);
-		// System.out.println("It's actual status is: " + isWorking);
-	}
 
-	public String getName() {
-		return name;
+	/**
+	 * @return the name
+	 */
+	public String getEleName() {
+		return eleName;
 	}
 
-	public void setName(String name) {
-		this.name = name;
+	/**
+	 * @param name the name to set
+	 */
+	public void setEleName(String name) {
+		this.eleName = name;
 	}
 
+	/**
+	 * @return the amount
+	 */
 	public int getAmount() {
 		return amount;
 	}
 
+	/**
+	 * @param amount the amount to set
+	 */
 	public void setAmount(int amount) {
 		this.amount = amount;
 	}
 
+	/**
+	 * @return the energy
+	 */
 	public float getEnergy() {
 		return energy;
 	}
 
+	/**
+	 * @param energy the energy to set
+	 */
 	public void setEnergy(float energy) {
 		this.energy = energy;
 	}
 
-	public void switchGadget(boolean bool) {
-		this.isWorking = bool;
+	/**
+	 * @return the active
+	 */
+	public boolean setActive() {
+		return active;
 	}
 
-	public boolean getState() {
-		return isWorking;
+	/**
+	 * @param active the active to set
+	 */
+	public void setActive(boolean active) {
+		this.active = active;
 	}
 
-	/* Image path */
+	/**
+	 * @return the image
+	 */
 	public String getImage() {
 		return image;
 	}
 
+
+
+
+
+	/**
+	 * @param image the image to set
+	 */
 	public void setImage(String image) {
 		this.image = image;
 	}
 
+
+
+
+
 	/**
 	 * Multiply the amount of gadgets, given by the user, and the
 	 * consumption/production. If the switch isWorking is turned off for on

+ 4 - 4
src/classes/HolonObject.java

@@ -85,9 +85,9 @@ public class HolonObject extends CpsObject {
 		String objString = "";
 		for (HolonElement e : consumers) {
 			if (objString == "") {
-				objString = e.getName();
+				objString = e.getEleName();
 			} else {
-				objString = objString + ", " + e.getName();
+				objString = objString + ", " + e.getEleName();
 			}
 
 		}
@@ -103,9 +103,9 @@ public class HolonObject extends CpsObject {
 		String objString = "";
 		for (HolonElement e : producers) {
 			if (objString == "") {
-				objString = e.getName();
+				objString = e.getEleName();
 			} else {
-				objString = objString + ", " + e.getName();
+				objString = objString + ", " + e.getEleName();
 			}
 
 		}

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

@@ -92,9 +92,9 @@ public class CategoryController {
 		if (position != -1) {
 			deleteCategoryAt(position);
 			return true;
-		} else {
+		} else
 			return false;
-		}
+
 	}
 
 	/**

+ 100 - 0
src/ui/controller/ObjectControl.java

@@ -1,12 +1,22 @@
 package ui.controller;
 
+import classes.Category;
 import classes.CpsObject;
+import classes.HolonElement;
+import classes.HolonObject;
 import ui.model.IdCounter;
 import ui.model.Model;
+
+import java.util.ArrayList;
+
 import Interfaces.ObjectListener;
 
 public class ObjectControl {
 
+	public enum Type {
+		CONSUMER, PRODUCER
+	}
+
 	private Model M;
 	private int ID;
 
@@ -37,4 +47,94 @@ public class ObjectControl {
 		}
 	}
 
+	/**
+	 * Adds Element into a Object
+	 */
+	public void addElement(HolonObject object, HolonElement element, Type type) {
+
+		if (type.equals(Type.CONSUMER))
+			object.getConsumers().add(element);
+		else
+			object.getProducers().add(element);
+	}
+
+	/**
+	 * Adds Element into a Object on the Canvas
+	 * 
+	 * @param object
+	 * @param element
+	 * @param type
+	 */
+	public void addElementIntoCanvasObject(HolonObject object, HolonElement element, Type type) {
+		addElement(object, element, type);
+	}
+
+	/**
+	 * Add a new Element into a Object on the Canvas
+	 * 
+	 * @param object
+	 * @param eleName
+	 * @param amount
+	 * @param energy
+	 * @param type
+	 */
+	public void addNewElementIntoCanvasObject(String object, String eleName, int amount, float energy, Type type) {
+		addElementIntoCanvasObject(searchHolonObject(object, M.getObjectsOnCanvas()),
+				new HolonElement(eleName, amount, energy), type);
+	}
+
+	/**
+	 * Add Element into a Object in Category
+	 * 
+	 * @param object
+	 * @param element
+	 * @param type
+	 */
+	public void addElementIntoCategoryObject(HolonObject object, HolonElement element, Type type) {
+		addElement(object, element, type);
+	}
+
+	/**
+	 * Add a new Element into a Object in Category
+	 * 
+	 * @param category
+	 * @param object
+	 * @param eleName
+	 * @param amount
+	 * @param energy
+	 * @param type
+	 */
+	public void addNewElementIntoCategoryObject(String category, String object, String eleName, int amount,
+			float energy, Type type) {
+		Category cat = null;
+
+		for (Category cats : M.getCategories()) {
+			if (cats.getName().equals(category)) {
+				cat = cats;
+				break;
+			}
+		}
+		addElementIntoCategoryObject(searchHolonObject(object, cat.getObjects()),
+				new HolonElement(eleName, amount, energy), type);
+	}
+
+	/**
+	 * Search for Object
+	 * 
+	 * @param object
+	 * @param list
+	 * @return
+	 */
+	public HolonObject searchHolonObject(String object, ArrayList<CpsObject> list) {
+		HolonObject obj = null;
+
+		for (CpsObject objects : list) {
+			if (objects.getObjName().equals(object)) {
+				obj = (HolonObject) objects;
+				break;
+			}
+		}
+
+		return obj;
+	}
 }