Browse Source

Bug concerning the edition of the HolenElements is fixed. The Holonelements have a unique ID.

Edgardo Palza 7 years ago
parent
commit
8baafe1488

+ 11 - 0
src/classes/HolonElement.java

@@ -2,6 +2,7 @@ package classes;
 
 import java.awt.Point;
 import java.util.LinkedList;
+import ui.model.idCounterElem;
 
 public class HolonElement {
 
@@ -23,6 +24,7 @@ public class HolonElement {
 	String sav;
 	/* Object where the Element is Stored */
 	String obj;
+	int id;
 	/*
 	 * Energy at each point of the graph with 100 predefined points. At the
 	 * beginning, it starts with all values at energy
@@ -38,6 +40,7 @@ public class HolonElement {
 		setActive(true);
 		setSign(energy);
 		setEnergyAt(energy);
+		setId(idCounterElem.nextId());
 	}
 
 	public HolonElement(HolonElement element) {
@@ -49,6 +52,7 @@ public class HolonElement {
 		setEnergyAt(element.getEnergy());
 		setSav("CVS");
 		setObj(element.getObj());
+		setId(idCounterElem.nextId());
 	}
 
 	/**
@@ -227,4 +231,11 @@ public class HolonElement {
 		this.graphPoints = points;
 	}
 
+	private void setId(int id) {
+		this.id = id;
+	}
+
+	public int getId() {
+		return id;
+	}
 }

+ 19 - 9
src/classes/HolonObject.java

@@ -191,29 +191,39 @@ public class HolonObject extends CpsObject {
 		}
 		return ele;
 	}
-	
-	public boolean checkIfPartiallySupplied(int x){
-		if(getElements().size() == 0){
+
+	public HolonElement searchElementById(int id) {
+		HolonElement ele = null;
+		for (HolonElement e : getElements()) {
+			if (e.getId() == id) {
+				ele = e;
+			}
+		}
+		return ele;
+	}
+
+	public boolean checkIfPartiallySupplied(int x) {
+		if (getElements().size() == 0) {
 			return false;
 		}
 		float minConsum = getElements().get(0).getTotalEnergyAtTimeStep(x);
 		float prod = 0;
 		for (HolonElement e : getElements()) {
 			if (e.getActive()) {
-				if(e.getTotalEnergyAtTimeStep(x) > 0){
+				if (e.getTotalEnergyAtTimeStep(x) > 0) {
 					prod = prod + e.getTotalEnergyAtTimeStep(x);
 				}
-				if(minConsum < 0 && (e.getTotalEnergyAtTimeStep(x) > minConsum && e.getTotalEnergyAtTimeStep(x) < 0)){
+				if (minConsum < 0 && (e.getTotalEnergyAtTimeStep(x) > minConsum && e.getTotalEnergyAtTimeStep(x) < 0)) {
 					minConsum = e.getTotalEnergyAtTimeStep(x);
-				}else if(minConsum >= 0 && e.getTotalEnergyAtTimeStep(x) < minConsum){
+				} else if (minConsum >= 0 && e.getTotalEnergyAtTimeStep(x) < minConsum) {
 					minConsum = e.getTotalEnergyAtTimeStep(x);
 				}
 			}
 		}
-		System.out.println("minCons: " + minConsum + " prod: " + prod);
-		if(minConsum < 0 && prod >= -minConsum){
+//		System.out.println("minCons: " + minConsum + " prod: " + prod);
+		if (minConsum < 0 && prod >= -minConsum) {
 			return true;
-		}else{
+		} else {
 			return false;
 		}
 	}

+ 26 - 0
src/ui/model/idCounterElem.java

@@ -0,0 +1,26 @@
+package ui.model;
+
+public class idCounterElem {
+	private static int counter = 1;
+
+
+	public static synchronized int nextId() {
+		return counter++;
+
+	}
+	
+	/**
+	 * @return the counter
+	 */
+	public static int getCounter() {
+		return counter;
+	}
+
+	/**
+	 * @param counter the counter to set
+	 */
+	public static void setCounter(int counter) {
+		idCounterElem.counter = counter;
+	}
+
+}

+ 45 - 44
src/ui/view/GUI.java

@@ -446,9 +446,9 @@ public class GUI<E> implements CategoryListener {
 		mnNewMenu_2.add(mntmEditEdges);
 
 		mnNewMenu_2.add(mntmResetCategory);
-		
+
 		mnNewMenu_2.add(mnSimulationSpeed);
-		
+
 		mnSimulationSpeed.add(simulationSpeedField);
 		mntmResetCategory.addActionListener(new ActionListener() {
 
@@ -533,9 +533,9 @@ public class GUI<E> implements CategoryListener {
 		 * RIGHT CONTAINER (INFORMATION)
 		 **********************/
 		// Set up of the HolonElements section
-		Object[] columnNamesMulti = { "Object", "Device", "Energy", "Quantity", "Activated" };
+		Object[] columnNamesMulti = { "Object", "ID", "Device", "Energy", "Quantity", "Activated" };
 		tableModelHolonElementMulti.setColumnIdentifiers(columnNamesMulti);
-		Object[] columnNamesSingle = { "Device", "Energy", "Quantity", "Activated" };
+		Object[] columnNamesSingle = { "ID", "Device", "Energy", "Quantity", "Activated" };
 		tableModelHolonElementSingle.setColumnIdentifiers(columnNamesSingle);
 		tableHolonElement.setBorder(null);
 
@@ -630,6 +630,7 @@ public class GUI<E> implements CategoryListener {
 					refreshTableHolonElement();
 					refreshTableProperties();
 				}
+				holonEleNamesDisplayed = "None ";
 			}
 		});
 		/*
@@ -705,10 +706,10 @@ public class GUI<E> implements CategoryListener {
 					int selectedValueBY = (int) Math.floor(yBMouse / 16);
 					if (model.getSelectedCpsObjects().size() > 1) {
 						// Multiple Selection
-						int selectedValueX = (int) Math.floor(xTHIS / (tableHolonElement.getWidth() / 5));
-						int selectedValueBX = (int) Math.floor(xBTHIS / (tableHolonElement.getWidth() / 5));
+						int selectedValueX = (int) Math.floor(xTHIS / (tableHolonElement.getWidth() / 6));
+						int selectedValueBX = (int) Math.floor(xBTHIS / (tableHolonElement.getWidth() / 6));
 						if (getHolonObj(yMouse) != null) {
-							if (selectedValueBX == 4) {
+							if (selectedValueBX == 5) {
 								HolonElement eleBTemp = getActualHolonElement(null, yBMouse, 0);
 								String newBStuff = tableModelHolonElementMulti
 										.getValueAt(selectedValueBY, selectedValueBX).toString();
@@ -718,14 +719,12 @@ public class GUI<E> implements CategoryListener {
 								HolonElement eleTemp = getActualHolonElement(null, yMouse, 0);
 								String newStuff = tableModelHolonElementMulti.getValueAt(selectedValueY, selectedValueX)
 										.toString();
-								// Small bug, when it comes to edit the name of
-								// the HolonElement
-								if (selectedValueX == 1) {
+								if (selectedValueX == 2) {
 									eleTemp.setEleName(newStuff);
-								} else if (selectedValueX == 2) {
+								} else if (selectedValueX == 3) {
 									Float ftemp = Float.parseFloat(newStuff);
 									eleTemp.setEnergy(ftemp);
-								} else if (selectedValueX == 3) {
+								} else if (selectedValueX == 4) {
 									Integer iTemp = Integer.parseInt(newStuff);
 									eleTemp.setAmount(iTemp);
 								}
@@ -733,10 +732,10 @@ public class GUI<E> implements CategoryListener {
 						}
 					} else if (model.getSelectedCpsObjects().size() == 1) {
 						// Single Selection
-						int selectedValueX = (int) Math.floor(xTHIS / (tableHolonElement.getWidth() / 4));
-						int selectedValueBX = (int) Math.floor(xBTHIS / (tableHolonElement.getWidth() / 4));
+						int selectedValueX = (int) Math.floor(xTHIS / (tableHolonElement.getWidth() / 5));
+						int selectedValueBX = (int) Math.floor(xBTHIS / (tableHolonElement.getWidth() / 5));
 						if (getActualCps() != null && getActualCps().getClass() == HolonObject.class) {
-							if (selectedValueBX == 3) {
+							if (selectedValueBX == 4) {
 								HolonElement eleBTemp = getActualHolonElement((HolonObject) getActualCps(), yBMouse, 0);
 								String newBStuff = tableModelHolonElementSingle
 										.getValueAt(selectedValueBY, selectedValueBX).toString();
@@ -746,15 +745,12 @@ public class GUI<E> implements CategoryListener {
 								HolonElement eleTemp = getActualHolonElement((HolonObject) getActualCps(), yMouse, 0);
 								String newStuff = tableModelHolonElementSingle
 										.getValueAt(selectedValueY, selectedValueX).toString();
-								// Small bug, when it comes to edit the name of
-								// the HolonElement
-								if (selectedValueX == 0) {
-									System.out.println(eleTemp.getEleName());
+								if (selectedValueX == 1) {
 									eleTemp.setEleName(newStuff);
-								} else if (selectedValueX == 1) {
+								} else if (selectedValueX == 2) {
 									Float ftemp = Float.parseFloat(newStuff);
 									eleTemp.setEnergy(ftemp);
-								} else if (selectedValueX == 2) {
+								} else if (selectedValueX == 3) {
 									Integer iTemp = Integer.parseInt(newStuff);
 									eleTemp.setAmount(iTemp);
 								}
@@ -1423,7 +1419,7 @@ public class GUI<E> implements CategoryListener {
 		tableHolonElementScrollPane.setBorder(null);
 
 		frmCyberPhysical.getContentPane().add(timePanel, BorderLayout.SOUTH);
-		
+
 		simulationSpeedField.setText(Integer.toString(timePanel.getTimerSpeed()));
 		btnApply.addActionListener(new ActionListener() {
 			public void actionPerformed(ActionEvent e) {
@@ -1432,7 +1428,7 @@ public class GUI<E> implements CategoryListener {
 				mnSimulationSpeed.setPopupMenuVisible(false);
 			}
 		});
-		
+
 		mnSimulationSpeed.add(btnApply);
 	}
 
@@ -1533,41 +1529,46 @@ public class GUI<E> implements CategoryListener {
 	 */
 	public HolonElement getActualHolonElement(HolonObject obj, int yValue, int toMultiHash) {
 		final int yTemp = (int) Math.floor(yValue / 16);
-		int rowsTotal = tableModelHolonElementSingle.getRowCount();
+		int rowsTotal = 0;
 		if (obj == null) {
 			rowsTotal = tableModelHolonElementMulti.getRowCount();
+		} else {
+			rowsTotal = tableModelHolonElementSingle.getRowCount();
 		}
 		HolonObject obtTemp = null;
-		String eleTempName = "";
+		HolonElement toReturnEle = null;
+		int id = 0;
 		if (rowsTotal != 0 && rowsTotal > yTemp) {
 			if (obj == null) {
-				String temp = tableModelHolonElementMulti.getValueAt(yTemp, 0).toString();
-				int idTemp = Integer.parseInt(temp.split(", ")[1]);
-				obtTemp = (HolonObject) controller.searchByID(idTemp);
-				eleTempName = tableModelHolonElementMulti.getValueAt(yTemp, 1).toString();
+				String tempStringObj = tableModelHolonElementMulti.getValueAt(yTemp, 0).toString();
+				int idTempObj = Integer.parseInt(tempStringObj.split(", ")[1]);
+				obtTemp = (HolonObject) controller.searchByID(idTempObj);
+				id = Integer.parseInt(tableModelHolonElementMulti.getValueAt(yTemp, 1).toString());
 				ArrayList<HolonElement> eleTemp = new ArrayList<HolonElement>();
-				if (eleToDelete.containsKey(idTemp) && toMultiHash == 2) {
-					eleTemp = eleToDelete.get(idTemp);
-					if (!eleTemp.contains(obtTemp.searchElement(eleTempName))) {
-						eleTemp.add(obtTemp.searchElement(eleTempName));
-						eleToDelete.replace(idTemp, eleTemp);
+				if (eleToDelete.containsKey(idTempObj) && toMultiHash == 2) {
+					eleTemp = eleToDelete.get(idTempObj);
+					if (!eleTemp.contains(obtTemp.searchElementById(id))) {
+						eleTemp.add(obtTemp.searchElementById(id));
+						eleToDelete.replace(idTempObj, eleTemp);
 
 					}
 				} else if (toMultiHash == 2) {
-					eleTemp.add(obtTemp.searchElement(eleTempName));
-					eleToDelete.put(idTemp, eleTemp);
+					eleTemp.add(obtTemp.searchElementById(id));
+					eleToDelete.put(idTempObj, eleTemp);
 				} else if (toMultiHash == 1) {
 					eleToDelete = new HashMap<Integer, ArrayList<HolonElement>>();
-					eleTemp.add(obtTemp.searchElement(eleTempName));
-					eleToDelete.put(idTemp, eleTemp);
+					eleTemp.add(obtTemp.searchElementById(id));
+					eleToDelete.put(idTempObj, eleTemp);
+				} else if (toMultiHash == 0) {
+					toReturnEle = obtTemp.searchElementById(id);
 				}
 			} else {
 				eleToDelete = new HashMap<Integer, ArrayList<HolonElement>>();
-				obtTemp = obj;
-				eleTempName = tableModelHolonElementSingle.getValueAt(yTemp, 0).toString();
+				id = Integer.parseInt(tableModelHolonElementSingle.getValueAt(yTemp, 0).toString());
+				toReturnEle = obj.searchElementById(id);
 			}
-			model.setSelectedHolonElement(obtTemp.searchElement(eleTempName));
-			return obtTemp.searchElement(eleTempName);
+			model.setSelectedHolonElement(toReturnEle);
+			return toReturnEle;
 		} else {
 			eleToDelete = new HashMap<Integer, ArrayList<HolonElement>>();
 			model.setSelectedHolonElement(null);
@@ -1626,7 +1627,7 @@ public class GUI<E> implements CategoryListener {
 			for (CpsObject o : objects) {
 				if (o instanceof HolonObject) {
 					for (HolonElement he : ((HolonObject) o).getElements()) {
-						Object[] temp = { o.getName() + ", " + o.getID(), he.getEleName(), he.getEnergy(),
+						Object[] temp = { o.getName() + ", " + o.getID(), he.getId(), he.getEleName(), he.getEnergy(),
 								he.getAmount(), he.getActive() };
 						tableModelHolonElementMulti.addRow(temp);
 					}
@@ -1636,7 +1637,7 @@ public class GUI<E> implements CategoryListener {
 			CpsObject o = objects.get(0);
 			if (o instanceof HolonObject) {
 				for (HolonElement he : ((HolonObject) o).getElements()) {
-					Object[] temp = { he.getEleName(), he.getEnergy(), he.getAmount(), he.getActive() };
+					Object[] temp = { he.getId(), he.getEleName(), he.getEnergy(), he.getAmount(), he.getActive() };
 					tableModelHolonElementSingle.addRow(temp);
 				}
 			}

+ 4 - 4
src/ui/view/PropertyTable.java

@@ -9,15 +9,15 @@ public class PropertyTable extends DefaultTableModel {
 	@Override
 	public Class<?> getColumnClass(int columnIndex) {
 		Class clazz = String.class;
-		if (getColumnCount() == 5) {
+		if (getColumnCount() == 6) {
 			switch (columnIndex) {
-			case 4:
+			case 5:
 				clazz = Boolean.class;
 				break;
 			}
-		} else if (getColumnCount() == 4) {
+		} else if (getColumnCount() == 5) {
 			switch (columnIndex) {
-			case 3:
+			case 4:
 				clazz = Boolean.class;
 				break;
 			}