Jelajahi Sumber

Changes in HolonElements, GUI and constructor of HolonObject/HolonSwitch/HolonTransformer

Edgardo Palza 8 tahun lalu
induk
melakukan
e117d07fda

TEMPAT SAMPAH
bin/classes/CpsObject.class


TEMPAT SAMPAH
bin/classes/HolonElement.class


TEMPAT SAMPAH
bin/classes/HolonObject.class


TEMPAT SAMPAH
bin/classes/HolonSwitch.class


TEMPAT SAMPAH
bin/classes/HolonTransformer.class


TEMPAT SAMPAH
bin/ui/controller/CategoryController.class


TEMPAT SAMPAH
bin/ui/controller/Control.class


TEMPAT SAMPAH
bin/ui/view/GUI$1.class


TEMPAT SAMPAH
bin/ui/view/GUI$2.class


TEMPAT SAMPAH
bin/ui/view/GUI$3.class


TEMPAT SAMPAH
bin/ui/view/GUI$4.class


TEMPAT SAMPAH
bin/ui/view/GUI$5.class


TEMPAT SAMPAH
bin/ui/view/GUI$6.class


TEMPAT SAMPAH
bin/ui/view/GUI.class


TEMPAT SAMPAH
bin/ui/view/MyCanvas.class


+ 10 - 8
src/classes/CpsObject.java

@@ -5,7 +5,7 @@ import java.util.ArrayList;
 import Interfaces.ComparableObject;
 import ui.model.IdCounter;
 
-public class CpsObject implements ComparableObject {
+public abstract class CpsObject implements ComparableObject {
 	/* Type of the Object */
 	String objName;
 	/* Name given by the user */
@@ -18,20 +18,22 @@ public class CpsObject implements ComparableObject {
 	ArrayList<CpsObject> connectedTo;
 	/* Position with a X and Y value */
 	Position position;
-	/*Energy input and output of each object in the grid*/
+	/* Energy input and output of each object in the grid */
 	float energyIn;
 	float energyOut;
-	
+
 	/**
 	 * Constructor for an CpsObejct with an unique ID
 	 */
 	public CpsObject(String objName) {
 		this.objName = objName;
+		this.name = objName;
 		connectedTo = new ArrayList<CpsObject>();
 		position = new Position();
 		id = IdCounter.nextId();
 		image = "/Images/Dummy_House.png";
 	}
+
 	public CpsObject(CpsObject obj) {
 		this.objName = obj.getObjName();
 		connectedTo = new ArrayList<CpsObject>();
@@ -42,7 +44,7 @@ public class CpsObject implements ComparableObject {
 		this.energyOut = obj.getEnergyOut();
 		this.image = obj.getImage();
 	}
-	
+
 	/* Obj type */
 	public String getObjName() {
 		return objName;
@@ -85,16 +87,16 @@ public class CpsObject implements ComparableObject {
 	}
 
 	/* Position (x and Y) */
-	public void setPos(int x, int y){
+	public void setPos(int x, int y) {
 		position.x = x;
 		position.y = y;
 	}
 
-	public Position getPos(){
+	public Position getPos() {
 		return position;
 	}
-	
-	/*Getter and Setters for the energy input and output*/
+
+	/* Getter and Setters for the energy input and output */
 	public float getEnergyIn() {
 		return energyIn;
 	}

+ 10 - 6
src/classes/HolonElement.java

@@ -20,17 +20,19 @@ public class HolonElement {
 		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);
+		// System.out.println("You create some " + name + "'s. The amount is:" +
+		// amount);
+		// System.out.println("It's actual status is: " + isWorking);
 	}
-	
+
 	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);
+		// System.out.println("You create some " + name + "'s. The amount is:" +
+		// amount);
+		// System.out.println("It's actual status is: " + isWorking);
 	}
 
 	public String getName() {
@@ -64,6 +66,7 @@ public class HolonElement {
 	public boolean getState() {
 		return isWorking;
 	}
+
 	/* Image path */
 	public String getImage() {
 		return image;
@@ -81,7 +84,8 @@ public class HolonElement {
 	 * @return totalEnergy (actual)
 	 */
 	public float getTotalEnergy() {
-		totalEnergy = ((float)amount)*energy;
+		totalEnergy = ((float) amount) * energy;
 		return totalEnergy;
 	}
+
 }

+ 84 - 26
src/classes/HolonObject.java

@@ -3,55 +3,113 @@ package classes;
 import java.util.ArrayList;
 
 public class HolonObject extends CpsObject {
-	
-	/*Array of all consumers*/
+
+	/* Array of all consumers */
 	private ArrayList<HolonElement> consumers;
-	/*Array of all producers*/
+	/* Array of all producers */
 	private ArrayList<HolonElement> producers;
-	/*Total of consumption*/
+	/* Total of consumption */
 	private float currentEnergy;
 	/**
-	 * State of the building: 
-	 * 0 = fully supplied (currentEnergy == 0)
-	 * 1 = not enough energy (currentEnergy > 0)
-	 * 2 = oversupplied (currentEnergy < 0)
+	 * State of the building: 0 = fully supplied (currentEnergy == 0) 1 = not
+	 * enough energy (currentEnergy > 0) 2 = oversupplied (currentEnergy < 0)
 	 */
 	int state;
-	
+
 	/**
-	 * Constructor
-	 * Set by default the name of the object equals to the category name, until the user changes it.
+	 * Constructor Set by default the name of the object equals to the category
+	 * name, until the user changes it.
 	 */
 	public HolonObject(String ObjName) {
 		super(ObjName);
+		consumers = new ArrayList<HolonElement>();
+		producers = new ArrayList<HolonElement>();
+	}
+
+	public HolonObject(String ObjName, String obj) {
+		super(ObjName);
+		super.name = obj;
+		consumers = new ArrayList<HolonElement>();
+		producers = new ArrayList<HolonElement>();
 	}
-	
+
 	public HolonObject(CpsObject obj) {
 		super(obj.objName);
 		/*
-		this.consumers = obj.consumers;
-		this.producers = obj.producers;
-		*/
+		 * this.consumers = obj.consumers; this.producers = obj.producers;
+		 */
 	}
-	
-	public void addConsumer(HolonElement consumer){
+
+	public void addConsumer(HolonElement consumer) {
 		consumers.add(consumer);
 	}
-	
-	public void addProducer(HolonElement producer){
+
+	public void addProducer(HolonElement producer) {
 		producers.add(producer);
 	}
-	
-	public void deleteConsumer(int idx){
+
+	public void deleteConsumer(int idx) {
 		consumers.remove(idx);
 	}
-	
-	public void deleteProducer(int idx){
+
+	public void deleteProducer(int idx) {
 		producers.remove(idx);
 	}
-	
-	public float calculateCurrentEnergy(){
+
+	public float calculateCurrentEnergy() {
 		return currentEnergy;
 	}
-	
+
+	public ArrayList<HolonElement> getConsumers() {
+		return consumers;
+	}
+
+	public void setConsumers(ArrayList<HolonElement> consumers) {
+		this.consumers = consumers;
+	}
+
+	public ArrayList<HolonElement> getProducers() {
+		return producers;
+	}
+
+	public void setProducers(ArrayList<HolonElement> producers) {
+		this.producers = producers;
+	}
+
+	/**
+	 * String of all consumers in this HolonObject
+	 * 
+	 * @return all the names of this HolonObject separated by "," each object
+	 */
+	public String toStringConsumers() {
+		String objString = "";
+		for (HolonElement e : consumers) {
+			if (objString == "") {
+				objString = e.getName();
+			} else {
+				objString = objString + ", " + e.getName();
+			}
+
+		}
+		return objString;
+	}
+
+	/**
+	 * String of all producers in this HolonObject
+	 * 
+	 * @return all the names of this HolonObject separated by "," each object
+	 */
+	public String toStringProducers() {
+		String objString = "";
+		for (HolonElement e : producers) {
+			if (objString == "") {
+				objString = e.getName();
+			} else {
+				objString = objString + ", " + e.getName();
+			}
+
+		}
+		return objString;
+	}
+
 }

+ 13 - 6
src/classes/HolonSwitch.java

@@ -1,25 +1,32 @@
 package classes;
 
 public class HolonSwitch extends CpsObject {
-	/*True, if this wire is working (capable of carrying electricity), else false*/
+	/*
+	 * True, if this wire is working (capable of carrying electricity), else
+	 * false
+	 */
 	boolean isWorking;
 
 	public HolonSwitch(String ObjName) {
 		super(ObjName);
 		isWorking = false;
 	}
-	
+
+	public HolonSwitch(String ObjName, String obj) {
+		super(ObjName);
+		super.name = obj;
+		isWorking = false;
+	}
+
 	public HolonSwitch(HolonObject obj) {
 		super(obj.objName);
 		isWorking = false;
 	}
-	
-	
+
 	public void switchState() {
 		this.isWorking = !isWorking;
 	}
-	
-	
+
 	public boolean getStates() {
 		return this.isWorking;
 	}

+ 6 - 2
src/classes/HolonTransformer.java

@@ -12,11 +12,15 @@ public class HolonTransformer extends CpsObject {
 	public HolonTransformer(String ObjName) {
 		super(ObjName);
 	}
-	
+
+	public HolonTransformer(String ObjName, String obj) {
+		super(ObjName);
+		super.name = obj;
+	}
+
 	public HolonTransformer(HolonObject obj) {
 		super(obj.objName);
 	}
-	
 
 	/**
 	 * Set transform ratio

+ 36 - 35
src/ui/controller/CategoryController.java

@@ -48,12 +48,11 @@ public class CategoryController {
 
 		addCategory(energy);
 		addCategory(building);
-		addCategory(component);		
+		addCategory(component);
 	}
 
 	/**
-	 * Adds Category into Model
-	 * if a Category with the same name already exists
+	 * Adds Category into Model if a Category with the same name already exists
 	 * Add Category_+1
 	 * 
 	 * @param toAdd
@@ -62,14 +61,15 @@ public class CategoryController {
 	public void addCategory(Category toAdd) {
 		int number = 0;
 		String name = toAdd.getName();
-		while(containsInList( M.getCategories(),toAdd)){
+		while (containsInList(M.getCategories(), toAdd)) {
 			number++;
 			toAdd.setName(name + "_" + number);
-		};
+		}
+		;
 		M.getCategories().add(toAdd);
 		notifyCatListeners();
 	}
-	
+
 	/**
 	 * Adds New Category into Model
 	 * 
@@ -80,23 +80,23 @@ public class CategoryController {
 
 		addCategory(new Category(name));
 	}
-	
+
 	/**
 	 * deletes category with given name
+	 * 
 	 * @param catName
 	 * @return true if successfull, otherwise false
 	 */
-	public boolean deleteCategory(String catName){
+	public boolean deleteCategory(String catName) {
 		int position = getPositionInList(M.getCategories(), searchCatNode(catName));
-		if(position != -1){
+		if (position != -1) {
 			deleteCategoryAt(position);
 			return true;
-		}
-		else{
+		} else {
 			return false;
 		}
 	}
-	
+
 	/**
 	 * deletes a Category at given position
 	 */
@@ -126,8 +126,8 @@ public class CategoryController {
 	 * @param obj
 	 *            New Object Name
 	 */
-	public void addNewHolonObject(Category cat, String obj) {
-		addObject(cat, new HolonObject(obj));
+	public void addNewHolonObject(Category cat, String obj, String objType) {
+		addObject(cat, new HolonObject(objType, obj));
 	}
 
 	/**
@@ -138,8 +138,8 @@ public class CategoryController {
 	 * @param obj
 	 *            New Object Name
 	 */
-	public void addNewHolonTransformer(Category cat, String obj) {
-		addObject(cat, new HolonTransformer(obj));
+	public void addNewHolonTransformer(Category cat, String obj, String objType) {
+		addObject(cat, new HolonTransformer(objType, obj));
 	}
 
 	/**
@@ -150,34 +150,34 @@ public class CategoryController {
 	 * @param obj
 	 *            New Object Name
 	 */
-	public void addNewHolonSwitch(Category cat, String obj) {
-		addObject(cat, new HolonSwitch(obj));
+	public void addNewHolonSwitch(Category cat, String obj, String objType) {
+		addObject(cat, new HolonSwitch(objType, obj));
 	}
-	
+
 	/**
 	 * deletes given Object in given Category
+	 * 
 	 * @param toDelete
 	 * @param deleteIn
 	 */
 	public void deleteObjectInCat(String toDelete, String deleteIn) {
 		Category cat = searchCatNode(deleteIn);
 		print(cat.getObjects());
-		for(int i = 0; i < cat.getObjects().size(); i++){
-			if(cat.getObjects().get(i).getCompareName().equals(toDelete)){
+		for (int i = 0; i < cat.getObjects().size(); i++) {
+			if (cat.getObjects().get(i).getCompareName().equals(toDelete)) {
 				cat.getObjects().remove(i);
 				cat.getObjects();
 				notifyCatListeners();
 			}
 		}
-		
+
 	}
-	
-	public void print(ArrayList<CpsObject> iterate){
-		for(CpsObject cps : iterate){
+
+	public void print(ArrayList<CpsObject> iterate) {
+		for (CpsObject cps : iterate) {
 			System.out.println(cps.getCompareName());
 		}
 	}
-	
 
 	/**
 	 * 
@@ -186,7 +186,7 @@ public class CategoryController {
 	public void addCatListener(CategoryListener catLis) {
 		M.getCategoryListeners().add(catLis);
 	}
-	
+
 	/**
 	 * notifies all listeners about changes in the Categories
 	 */
@@ -198,6 +198,7 @@ public class CategoryController {
 
 	/**
 	 * search for category
+	 * 
 	 * @param name
 	 * @return
 	 */
@@ -212,30 +213,30 @@ public class CategoryController {
 		}
 		return query;
 	}
-	
+
 	/**
 	 * gets the position of an object in the array
+	 * 
 	 * @param arrayList
 	 * @param toSearch
 	 * @return -1 if object is not in the array, otherwise the index
 	 */
-	public int getPositionInList(ArrayList<? extends ComparableObject> arrayList, ComparableObject toSearch){
-		for(int i = 0;i < arrayList.size(); i++){
-			if(arrayList.get(i).getCompareName() == toSearch.getCompareName()){
+	public int getPositionInList(ArrayList<? extends ComparableObject> arrayList, ComparableObject toSearch) {
+		for (int i = 0; i < arrayList.size(); i++) {
+			if (arrayList.get(i).getCompareName() == toSearch.getCompareName()) {
 				return i;
 			}
 		}
 		return -1;
 	}
 
-	public boolean containsInList(ArrayList<? extends ComparableObject> arrayList, ComparableObject toSearch){
-		for(ComparableObject obj : arrayList){
-			if(obj.getCompareName().equals(toSearch.getCompareName())){
+	public boolean containsInList(ArrayList<? extends ComparableObject> arrayList, ComparableObject toSearch) {
+		for (ComparableObject obj : arrayList) {
+			if (obj.getCompareName().equals(toSearch.getCompareName())) {
 				return true;
 			}
 		}
 		return false;
 	}
 
-	
 }

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

@@ -40,16 +40,16 @@ public class Control {
 		categoryController.addNewCategory(catName);
 	}
 
-	public void addNewObject(Category cat, String name) {
-		categoryController.addNewHolonObject(cat, name);
+	public void addNewObject(Category cat, String name, String objType) {
+		categoryController.addNewHolonObject(cat, name, objType);
 	}
 
-	public void addNewTransformer(Category cat, String name) {
-		categoryController.addNewHolonTransformer(cat, name);
+	public void addNewTransformer(Category cat, String name, String objType) {
+		categoryController.addNewHolonTransformer(cat, name, objType);
 	}
 
-	public void addNewSwitch(Category cat, String name) {
-		categoryController.addNewHolonSwitch(cat, name);
+	public void addNewSwitch(Category cat, String name, String objType) {
+		categoryController.addNewHolonSwitch(cat, name, objType);
 	}
 
 	public Category searchCategory(String name) {

+ 57 - 43
src/ui/view/GUI.java

@@ -56,7 +56,10 @@ import javax.swing.ImageIcon;
 
 import classes.Category;
 import classes.CpsObject;
+import classes.HolonElement;
 import classes.HolonObject;
+import classes.HolonSwitch;
+import classes.HolonTransformer;
 import Interfaces.CategoryListener;
 
 import java.awt.event.ActionListener;
@@ -110,7 +113,7 @@ public class GUI implements CategoryListener {
 	private String actualObjectClicked;
 	private Image img = null;
 	private CpsObject tempCps = null;
-	
+
 	private final MyCanvas canvas;
 
 	/**
@@ -127,7 +130,7 @@ public class GUI implements CategoryListener {
 	/**
 	 * Initialize the contents of the frame.
 	 */
-	@SuppressWarnings("serial")
+	@SuppressWarnings({ "serial", "unchecked" })
 	private void initialize() {
 		frmCyberPhysical = new JFrame();
 		frmCyberPhysical.setTitle("Cyber Physical Systems Model");
@@ -197,62 +200,68 @@ public class GUI implements CategoryListener {
 		splitPane_2.setDividerLocation(200);
 
 		splitPane.setLeftComponent(scrollPane_1);
-		img = new ImageIcon(this.getClass().getResource("/Images/Dummy_House.png")).getImage()
-				.getScaledInstance(30, 30, java.awt.Image.SCALE_SMOOTH);
+		img = new ImageIcon(this.getClass().getResource("/Images/Dummy_House.png")).getImage().getScaledInstance(30, 30,
+				java.awt.Image.SCALE_SMOOTH);
 		Icon icon = new ImageIcon(img);
-		
-		
+
 		TreeCellRenderer customRenderer = new TreeCellRenderer() {
-			
+
 			@Override
 			public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded,
 					boolean leaf, int row, boolean hasFocus) {
 				JLabel label = new JLabel();
 				Image imgR = null;
 				CpsObject tempCPS = null;
-				
+
 				for (Category cat : model.getCategories()) {
 					for (CpsObject cps : cat.getObjects()) {
 						if (value.toString().compareTo(cps.getCompareName()) == 0) {
 							tempCPS = cps;
-							System.out.println(tempCPS.getImage());
+							// System.out.println(tempCPS.getImage());
 							imgR = new ImageIcon(this.getClass().getResource(cps.getImage())).getImage()
 									.getScaledInstance(30, 30, java.awt.Image.SCALE_SMOOTH);
 							if (imgR != null) {
-					            label.setIcon(new ImageIcon (imgR));
-					        }
+								label.setIcon(new ImageIcon(imgR));
+							}
 						}
 					}
 				}
-		        label.setText(value.toString());
-		        return label;
-				
+				label.setText(value.toString());
+				return label;
+
 			}
 		};
-		
-		
-		/*DefaultTreeCellRenderer renderer = (DefaultTreeCellRenderer) tree.getCellRenderer();
-		renderer.setLeafIcon(icon);
-		// renderer.setClosedIcon(icon);
-		// renderer.setOpen*/
+
+		/*
+		 * DefaultTreeCellRenderer renderer = (DefaultTreeCellRenderer)
+		 * tree.getCellRenderer(); renderer.setLeafIcon(icon); //
+		 * renderer.setClosedIcon(icon); // renderer.setOpen
+		 */
 		tree.setRowHeight(icon.getIconHeight());
-		
+
 		tree.setCellRenderer(customRenderer);
-		
+
 		tree.addMouseListener(new MouseAdapter() {
 			public void mouseReleased(MouseEvent e) {
 				try {
 					if (dragging) {
-<<<<<<< HEAD
-						CpsObject h = new CpsObject(actualObjectClicked);
-=======
-						HolonObject h = new HolonObject(tempCps);
->>>>>>> be22b224d998a4747c8839f1c312f403a8726dda
+
+						// if(){
+						// HolonSwitch h = new HolonSwitch(actualObjectClicked);
+						// }else if(){
+						// HolonTransformer h = new
+						// HolonTransformer(actualObjectClicked);
+						// }else{
+						HolonObject h = new HolonObject(actualObjectClicked);
 						h.setPos((int) canvas.getMousePosition().getX(), (int) canvas.getMousePosition().getY());
 						controller.addObject(h);
 						for (int i = 0; i < model.getObjectsOnCanvas().size(); i++) {
 							System.out.println("Element: " + model.getObjectsOnCanvas().get(i).getObjName()
-									+ "with ID: " + model.getObjectsOnCanvas().get(i).getID());
+									+ " with ID: " + model.getObjectsOnCanvas().get(i).getID()
+									+ " and the following gadgets: Consumers: "
+									+ ((HolonObject) model.getObjectsOnCanvas().get(i)).toStringConsumers()
+									+ " and Producers: "
+									+ ((HolonObject) model.getObjectsOnCanvas().get(i)).toStringProducers());
 						}
 						canvas.repaint();
 						dragging = false;
@@ -266,14 +275,17 @@ public class GUI implements CategoryListener {
 		tree.addMouseListener(new MouseAdapter() {
 			public void mousePressed(MouseEvent e) {
 				try {
+					HolonObject h = new HolonObject("test");
 					actualObjectClicked = tree.getPathForLocation(e.getX(), e.getY()).getLastPathComponent().toString();
-					/*//System.out.println(actualObjectClicked);
-					if (actualObjectClicked.compareTo("Power Plant") == 0 || actualObjectClicked.compareTo("House") == 0
-							|| actualObjectClicked.compareTo("Switch") == 0
-							|| actualObjectClicked.compareTo("Transformer") == 0) {
-						
-					}
-					*/
+					/*
+					 * //System.out.println(actualObjectClicked); if
+					 * (actualObjectClicked.compareTo("Power Plant") == 0 ||
+					 * actualObjectClicked.compareTo("House") == 0 ||
+					 * actualObjectClicked.compareTo("Switch") == 0 ||
+					 * actualObjectClicked.compareTo("Transformer") == 0) {
+					 * 
+					 * }
+					 */
 					for (Category cat : model.getCategories()) {
 						for (CpsObject cps : cat.getObjects()) {
 							if (actualObjectClicked.compareTo(cps.getCompareName()) == 0) {
@@ -281,7 +293,8 @@ public class GUI implements CategoryListener {
 										.getScaledInstance(30, 30, java.awt.Image.SCALE_SMOOTH);
 								tempCps = cps;
 								dragging = true;
-								Cursor cursor = Toolkit.getDefaultToolkit().createCustomCursor(img, new Point(0, 0), "Image");
+								Cursor cursor = Toolkit.getDefaultToolkit().createCustomCursor(img, new Point(0, 0),
+										"Image");
 								frmCyberPhysical.setCursor(cursor);
 							}
 						}
@@ -381,7 +394,7 @@ public class GUI implements CategoryListener {
 				}
 			}
 		});
-		
+
 	}
 
 	/*
@@ -398,23 +411,24 @@ public class GUI implements CategoryListener {
 			if (selectedNode.getLevel() == 1) {
 				String objname = JOptionPane.showInputDialog("Please enter a Name for the " + objType);
 				Category cat = controller.searchCategory(selectedNode.getUserObject().toString());
-				if(objname.equals("show")){
+				if (objname.equals("show")) {
 					addObjectPopUP = new AddObjectPopUp();
 					addObjectPopUP.setVisible(true);
 				}
-				
-				if(objname.length() != 0){
+
+				if (objname.length() != 0) {
 					switch (objType) {
 
 					case "Object":
-						controller.addNewObject(cat, objname);
+						controller.addNewObject(cat, objname, objType);
+
 						break;
 
 					case "Switch":
-						controller.addNewSwitch(cat, objname);
+						controller.addNewSwitch(cat, objname, objType);
 
 					case "Transformer":
-						controller.addNewTransformer(cat, objname);
+						controller.addNewTransformer(cat, objname, objType);
 					}
 				}
 			} else {

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

@@ -60,7 +60,7 @@ class MyCanvas extends JPanel implements MouseListener, MouseMotionListener {
 		
 		for (CpsObject cps : model.getObjectsOnCanvas()) {
 			
-			System.out.println(cps.getImage());
+		//	System.out.println(cps.getImage());
 			g.drawImage(img, cps.getPos().x, cps.getPos().y, null);
 		}
 	}