Jelajahi Sumber

IdCounter works properly, only HolonObjects in Canvas have ID's

Edgardo Palza 8 tahun lalu
induk
melakukan
9272bdb6ba

TEMPAT SAMPAH
bin/classes/CpsObject.class


TEMPAT SAMPAH
bin/classes/HolonObject.class


TEMPAT SAMPAH
bin/ui/controller/CategoryController.class


TEMPAT SAMPAH
bin/ui/controller/Control$command.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/Main$1.class


TEMPAT SAMPAH
bin/ui/view/MyCanvas.class


+ 4 - 4
src/classes/CpsObject.java

@@ -3,7 +3,7 @@ package classes;
 import java.util.ArrayList;
 
 import Interfaces.ComparableObject;
-import ui.model.iDCounter;
+import ui.model.idCounter;
 
 public abstract class CpsObject implements ComparableObject {
 	/* Type of the Object */
@@ -30,16 +30,16 @@ public abstract class CpsObject implements ComparableObject {
 		this.name = objName;
 		connectedTo = new ArrayList<CpsObject>();
 		position = new Position();
-		id = iDCounter.nextId();
+		// id = idCounter.nextId();
 		image = "/Images/Dummy_House.png";
-//		System.out.println("ID: " + id + " of " + objName);
+		System.out.println("ID: " + id + " of " + objName);
 	}
 
 	public CpsObject(CpsObject obj) {
 		this.objName = obj.getObjName();
 		connectedTo = new ArrayList<CpsObject>();
 		position = new Position();
-		id = iDCounter.nextId();
+		id = idCounter.nextId();
 		this.name = obj.getName();
 		this.energyIn = obj.getEnergyIn();
 		this.energyOut = obj.getEnergyOut();

+ 8 - 10
src/classes/HolonObject.java

@@ -32,13 +32,10 @@ public class HolonObject extends CpsObject {
 	public HolonObject(CpsObject obj) {
 		super(obj);
 		elements = new ArrayList<HolonElement>();
-		this.elements = ((HolonObject)obj).getElements();
+		this.elements = ((HolonObject) obj).getElements();
 
 	}
 
-
-
-	
 	/**
 	 * @return the elements
 	 */
@@ -47,7 +44,8 @@ public class HolonObject extends CpsObject {
 	}
 
 	/**
-	 * @param elements the elements to set
+	 * @param elements
+	 *            the elements to set
 	 */
 	public void setElements(ArrayList<HolonElement> elements) {
 		this.elements = elements;
@@ -61,7 +59,8 @@ public class HolonObject extends CpsObject {
 	}
 
 	/**
-	 * @param currentEnergy the currentEnergy to set
+	 * @param currentEnergy
+	 *            the currentEnergy to set
 	 */
 	public void setCurrentEnergy(float currentEnergy) {
 		this.currentEnergy = currentEnergy;
@@ -69,6 +68,7 @@ public class HolonObject extends CpsObject {
 
 	/**
 	 * deletes Element
+	 * 
 	 * @param idx
 	 */
 	public void deleteElement(int idx) {
@@ -79,17 +79,15 @@ public class HolonObject extends CpsObject {
 		return currentEnergy;
 	}
 
-	
-
 	/**
 	 * String of all consumers in this HolonObject
 	 * 
 	 * @return all the names of this HolonObject separated by "," each object
 	 */
 	public String toStringElements() {
-		String objString = "";
+		String objString = "Empty";
 		for (HolonElement e : elements) {
-			if (objString == "") {
+			if (objString == "Empty") {
 				objString = e.getEleName();
 			} else {
 				objString = objString + ", " + e.getEleName();

+ 0 - 3
src/ui/controller/CanvasController.java

@@ -1,17 +1,14 @@
 package ui.controller;
 
-import ui.model.iDCounter;
 import Interfaces.ObjectListener;
 import classes.CpsObject;
 import ui.model.Model;
 
 public class CanvasController {
 
-//	private iDCounter ID;
 	private Model MODEL;
 
 	public CanvasController(Model model) {
-//		this.ID = id;
 		this.MODEL = model;
 	}
 

+ 0 - 3
src/ui/controller/CategoryController.java

@@ -17,12 +17,9 @@ import ui.model.*;
 import ui.view.*;
 
 public class CategoryController {
-
-//	private iDCounter ID;
 	private Model MODEL;
 
 	public CategoryController(Model model) {
-//		this.ID = id;
 		this.MODEL = model;
 		initCategories();
 	}

+ 8 - 10
src/ui/controller/Control.java

@@ -1,7 +1,5 @@
 package ui.controller;
 
-import ui.model.iDCounter;
-
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
 import java.util.LinkedList;
@@ -19,18 +17,18 @@ public class Control {
 		CATEGORY, OBJECT
 	}
 
-//	private iDCounter ID;
+	// private iDCounter ID;
 	private Model MODEL;
-	
+
 	private ActionListener actionListener;
-	
+
 	private final CategoryController categoryController;
 	private final ObjectController objectController;
 	private final CanvasController canvasController;
 
 	public Control(Model model) {
 		this.MODEL = model;
-//		this.ID = id;
+		// this.ID = id;
 		this.categoryController = new CategoryController(model);
 		this.objectController = new ObjectController(model);
 		this.canvasController = new CanvasController(model);
@@ -44,7 +42,7 @@ public class Control {
 	public void addNewObject(Category cat, String name, String objType) {
 		categoryController.addNewHolonObject(cat, name, objType);
 	}
-	
+
 	public void addNewObject(Category cat, HolonObject object) {
 		categoryController.addObject(cat, object);
 	}
@@ -92,9 +90,9 @@ public class Control {
 			float energy) {
 		objectController.addNewElementIntoCategoryObject(catName, objName, eleName, amount, energy);
 	}
-	
-	public void deleteElement(){
-		
+
+	public void deleteElement() {
+
 	}
 
 	////////// etc

+ 8 - 13
src/ui/controller/ObjectController.java

@@ -4,7 +4,6 @@ import classes.Category;
 import classes.CpsObject;
 import classes.HolonElement;
 import classes.HolonObject;
-import ui.model.iDCounter;
 import ui.model.Model;
 
 import java.util.ArrayList;
@@ -18,19 +17,16 @@ public class ObjectController {
 	}
 
 	private Model MODEL;
-//	private int ID;
 
 	public ObjectController(Model model) {
 		this.MODEL = model;
-//		this.ID = iDCounter.nextId();
 		initHolonElements();
 	}
-	
-	
+
 	/**
 	 * init default Power supply of the Power Plant
 	 */
-	public void initHolonElements(){
+	public void initHolonElements() {
 		addNewElementIntoCategoryObject("Energy", "Power Plant", "Power", 1, 100);
 	}
 
@@ -98,21 +94,20 @@ public class ObjectController {
 			float energy) {
 		addElementIntoCategoryObject(category, object, new HolonElement(eleName, energy, amount));
 	}
-	
+
 	/**
 	 * deletes a Element from a given Object
+	 * 
 	 * @param obj
 	 * @param ele
 	 */
-	public void deleteElement(HolonObject obj, HolonElement ele){
+	public void deleteElement(HolonObject obj, HolonElement ele) {
 		obj.getElements().remove(ele);
 	}
-	
-	public void deleteElementInCategory(Category cat, HolonObject obj, HolonElement ele){
-		
+
+	public void deleteElementInCategory(Category cat, HolonObject obj, HolonElement ele) {
+
 	}
-	
-	
 
 	/**
 	 * Search for Object

+ 1 - 1
src/ui/model/iDCounter.java → src/ui/model/idCounter.java

@@ -1,6 +1,6 @@
 package ui.model;
 
-public class iDCounter {
+public class idCounter {
 	private static int counter = 0;
 
 	public static synchronized int nextId() {

+ 3 - 2
src/ui/view/GUI.java

@@ -256,8 +256,9 @@ public class GUI implements CategoryListener {
 						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());
+							CpsObject temp = model.getObjectsOnCanvas().get(i);
+							String output = "Element: " + temp.getObjName() + " with ID: " + temp.getID();
+							System.out.println(output);
 						}
 						canvas.repaint();
 						dragging = false;

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

@@ -25,7 +25,7 @@ public class Main {
 		EventQueue.invokeLater(new Runnable() {
 			public void run() {
 				try {
-					iDCounter ID = new iDCounter();
+					idCounter ID = new idCounter();
 					Model MODEL = new Model();
 					Control CONTROL = new Control(MODEL);
 					GUI VIEW = new GUI(CONTROL);

+ 70 - 58
src/ui/view/MyCanvas.java

@@ -40,13 +40,13 @@ class MyCanvas extends JPanel implements MouseListener, MouseMotionListener {
 	private Image img = null; // Contains the image to draw on MyCanvas
 	private int x = 0;
 	private int y = 0;
-	//edge Object Start Point
+	// edge Object Start Point
 	private Model model;
 	private final Control controller;
-	Graphics2D g2; //For Painting
+	Graphics2D g2; // For Painting
 	private int cx;
 	private int cy;
-	
+
 	boolean dragging = false;
 	boolean drawEdge = false;
 	boolean dropDelete = false;
@@ -57,7 +57,7 @@ class MyCanvas extends JPanel implements MouseListener, MouseMotionListener {
 	private JPopupMenu popmenu = new JPopupMenu();
 	private JMenuItem itemDelete = new JMenuItem("Delete Object");
 	private JToolTip objectTT = new JToolTip();
-	
+
 	public MyCanvas(final Model model, Control control) {
 		this.add(objectTT);
 		this.controller = control;
@@ -85,43 +85,50 @@ class MyCanvas extends JPanel implements MouseListener, MouseMotionListener {
 	 * Paints all Components on the Canvas
 	 * 
 	 * @param Graphics
-	 *            
+	 * 
 	 */
 	public void paintComponent(Graphics g) {
 		super.paintComponent(g);
 		g2 = (Graphics2D) g;
-		RenderingHints rh = new RenderingHints(
-	             RenderingHints.KEY_ANTIALIASING,
-	             RenderingHints.VALUE_ANTIALIAS_ON);
-	    g2.setRenderingHints(rh);
-	    
-		//Selection
-		if(selectRect != null){
+		RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
+		g2.setRenderingHints(rh);
+
+		// Selection
+		if (selectRect != null) {
 			g2.setColor(Color.GREEN);
-		    g2.fillRect((int)selectRect.getX(), (int)selectRect.getY(), (int)selectRect.getWidth(), (int)selectRect.getHeight());
+			g2.fillRect((int) selectRect.getX(), (int) selectRect.getY(), (int) selectRect.getWidth(),
+					(int) selectRect.getHeight());
 		}
-		
-		//drawEdges
+
+		// drawEdges
 		g2.setColor(Color.BLACK);
-		if(drawEdge)g2.drawLine(tempCps.getPos().x+GlobalVariables.SCALE_DIVIDED2, tempCps.getPos().y+GlobalVariables.SCALE_DIVIDED2, x, y);
-		
+		if (drawEdge)
+			g2.drawLine(tempCps.getPos().x + GlobalVariables.SCALE_DIVIDED2,
+					tempCps.getPos().y + GlobalVariables.SCALE_DIVIDED2, x, y);
+
 		for (CpsObject cps : model.getObjectsOnCanvas()) {
 			for (CpsObject con : cps.getConnectedTo()) {
-				if(con.getID() != model.getSelectedObjectID() && cps.getID() != model.getSelectedObjectID())
-				g2.drawLine(cps.getPos().x+GlobalVariables.SCALE_DIVIDED2, cps.getPos().y+GlobalVariables.SCALE_DIVIDED2, con.getPos().x+GlobalVariables.SCALE_DIVIDED2, con.getPos().y+GlobalVariables.SCALE_DIVIDED2);
+				if (con.getID() != model.getSelectedObjectID() && cps.getID() != model.getSelectedObjectID())
+					g2.drawLine(cps.getPos().x + GlobalVariables.SCALE_DIVIDED2,
+							cps.getPos().y + GlobalVariables.SCALE_DIVIDED2,
+							con.getPos().x + GlobalVariables.SCALE_DIVIDED2,
+							con.getPos().y + GlobalVariables.SCALE_DIVIDED2);
 			}
 		}
-		
-		//Highlighted Edge
-		g2.setColor(Color.GREEN);		
+
+		// Highlighted Edge
+		g2.setColor(Color.GREEN);
 		for (CpsObject cps : model.getObjectsOnCanvas()) {
 			for (CpsObject con : cps.getConnectedTo()) {
-				if(con.getID() == model.getSelectedObjectID())
-				g2.drawLine(cps.getPos().x+GlobalVariables.SCALE_DIVIDED2, cps.getPos().y+GlobalVariables.SCALE_DIVIDED2, con.getPos().x+GlobalVariables.SCALE_DIVIDED2, con.getPos().y+GlobalVariables.SCALE_DIVIDED2);
+				if (con.getID() == model.getSelectedObjectID())
+					g2.drawLine(cps.getPos().x + GlobalVariables.SCALE_DIVIDED2,
+							cps.getPos().y + GlobalVariables.SCALE_DIVIDED2,
+							con.getPos().x + GlobalVariables.SCALE_DIVIDED2,
+							con.getPos().y + GlobalVariables.SCALE_DIVIDED2);
 			}
 		}
-		
-		//Objects
+
+		// Objects
 		for (CpsObject cps : model.getObjectsOnCanvas()) {
 			img = new ImageIcon(this.getClass().getResource(cps.getImage())).getImage();
 			g2.drawImage(img, cps.getPos().x, cps.getPos().y, GlobalVariables.SCALE, GlobalVariables.SCALE, null);
@@ -150,79 +157,81 @@ class MyCanvas extends JPanel implements MouseListener, MouseMotionListener {
 		// TODO Auto-generated method stub
 		x = e.getX();
 		y = e.getY();
-		
+
 		tempCps = null;
-		//Object Selection
+		// Object Selection
 		for (CpsObject cps : model.getObjectsOnCanvas()) {
 			cx = cps.getPos().x;
 			cy = cps.getPos().y;
 			if (x - GlobalVariables.SCALE <= cx && y - GlobalVariables.SCALE <= cy && x >= cx && y >= cy) {
 				tempCps = cps;
-				if(e.isControlDown())drawEdge = true;
+				if (e.isControlDown())
+					drawEdge = true;
 			}
 		}
-		
-		//Object Selection Highlighting (selectRect)
+
+		// Object Selection Highlighting (selectRect)
 		objectSelectionHighlighting();
-		
-		repaint();		
+
+		repaint();
 	}
 
 	@Override
 	public void mouseReleased(MouseEvent e) {
-		if(drawEdge){
+		if (drawEdge) {
 			drawEdge = false;
 			drawDeleteEdge();
 		}
-		
+
 		if (dragging) {
 			x = e.getX();
 			y = e.getY();
-			
+
 			dragging = false;
 			tempCps.setPos(e.getX() - GlobalVariables.SCALE_DIVIDED2, e.getY() - GlobalVariables.SCALE_DIVIDED2);
 			tempCps = null;
 		}
-		
+
 		// Rechtsklick Liste
 		if (e.getButton() == e.BUTTON3) {
 			if (e.getButton() == e.BUTTON3 && tempCps != null) {
 				itemDelete.setEnabled(true);
-			}else {
+			} else {
 				itemDelete.setEnabled(false);
 			}
 			popmenu.show(e.getComponent(), e.getX(), e.getY());
 		}
-		
+
 		repaint();
 	}
 
 	@Override
 	public void mouseDragged(MouseEvent e) {
 		// TODO Auto-generated method stub
-			if(drawEdge){
+		if (drawEdge) {
 			x = e.getX();
 			y = e.getY();
 			repaint();
-			}else{
-				try {
-					tempCps.setPos(e.getX() - GlobalVariables.SCALE_DIVIDED2, e.getY() - GlobalVariables.SCALE_DIVIDED2);
-					dragging = true;
-					selectRect.setLocation(tempCps.getPos().x-(GlobalVariables.SCALE/20), tempCps.getPos().y-(GlobalVariables.SCALE/20));
-					objectTT.setTipText(tempCps.getName());
-					objectTT.setLocation(tempCps.getPos().x, tempCps.getPos().y);
-					repaint();
-				} catch (Exception e2) {
-					// TODO: handle exception
-				}
+		} else {
+			try {
+				tempCps.setPos(e.getX() - GlobalVariables.SCALE_DIVIDED2, e.getY() - GlobalVariables.SCALE_DIVIDED2);
+				dragging = true;
+				selectRect.setLocation(tempCps.getPos().x - (GlobalVariables.SCALE / 20),
+						tempCps.getPos().y - (GlobalVariables.SCALE / 20));
+				objectTT.setTipText(tempCps.getName());
+				objectTT.setLocation(tempCps.getPos().x, tempCps.getPos().y);
+				repaint();
+			} catch (Exception e2) {
+				// TODO: handle exception
 			}
+		}
 	}
 
 	@Override
 	public void mouseMoved(MouseEvent e) {
 		x = e.getX();
 		y = e.getY();
-		
+
 		boolean on = false;
 		for (CpsObject cps : model.getObjectsOnCanvas()) {
 			cx = cps.getPos().x;
@@ -233,25 +242,28 @@ class MyCanvas extends JPanel implements MouseListener, MouseMotionListener {
 				on = true;
 			}
 		}
-		if(!on){
+		if (!on) {
 			objectTT.setLocation(-200, -200);
 			objectTT.setTipText("");
 		}
 	}
-	
+
 	/**
 	 * Sets the Highlighting of the Selected Object
 	 */
 	private void objectSelectionHighlighting() {
-		if(tempCps != null){
-			selectRect.setBounds(tempCps.getPos().x-(GlobalVariables.SCALE/20), tempCps.getPos().y-(GlobalVariables.SCALE/20), GlobalVariables.SCALE+GlobalVariables.SCALE/10, GlobalVariables.SCALE+GlobalVariables.SCALE/10);
+		if (tempCps != null) {
+			selectRect.setBounds(tempCps.getPos().x - (GlobalVariables.SCALE / 20),
+					tempCps.getPos().y - (GlobalVariables.SCALE / 20),
+					GlobalVariables.SCALE + GlobalVariables.SCALE / 10,
+					GlobalVariables.SCALE + GlobalVariables.SCALE / 10);
 			controller.setSelectedObjectID(tempCps.getID());
-		}else {
+		} else {
 			controller.setSelectedObjectID(0);
 			selectRect.setRect(0, 0, 0, 0);
 		}
 	}
-	
+
 	/**
 	 * Draws or Deletes an Edge
 	 */