Browse Source

Merge branches 'Ohne_Drag_and_Drop' and 'Ohne_Drag_and_Drop' of https://git.tk.informatik.tu-darmstadt.de/carlos.garcia/praktikum-holons into Ohne_Drag_and_Drop

Teh-Hai Julian Zheng 7 years ago
parent
commit
23baaa7f35

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

@@ -32,7 +32,6 @@ public class CategoryController {
 
 		addNewHolonObject(mpC.searchCategory("Energy"), "Power Plant", new ArrayList<HolonElement>(), "/Images/power-plant.png");
 		addNewHolonObject(mpC.searchCategory("Building"), "House", new ArrayList<HolonElement>(), "/Images/home-2.png");
-		addNewHolonTransformer(mpC.searchCategory("Component"), "Transformer", "/Images/transformer-1.png");
 		addNewHolonSwitch(mpC.searchCategory("Component"), "Switch", "/Images/switch-on.png");
 
 	}

+ 27 - 40
src/ui/controller/Control.java

@@ -8,6 +8,7 @@ import java.util.ArrayList;
 import Interfaces.CategoryListener;
 import classes.Category;
 import classes.CpsEdge;
+import classes.CpsNode;
 import classes.CpsObject;
 import classes.HolonElement;
 import classes.HolonObject;
@@ -30,7 +31,7 @@ public class Control {
 	private final AutoSaveController autoSaveController;
 	private SimulationManager simulationManager;
 	private String autoPath = "";
-	
+
 	public Control(Model model) {
 		this.MODEL = model;
 
@@ -72,12 +73,6 @@ public class Control {
 
 	public void addObject(Category cat, String obj, ArrayList<HolonElement> ele, String img) {
 		categoryController.addNewHolonObject(cat, obj, ele, img);
-		try {
-			autoSave();
-		} catch (IOException e) {
-			// TODO Auto-generated catch block
-			e.printStackTrace();
-		}
 	}
 
 	public void addTransformer(Category cat, String obj) {
@@ -99,19 +94,18 @@ public class Control {
 	public void delObjectCategory(String cat, String obj) {
 		categoryController.deleteObject(cat, obj);
 	}
-	
 
 	public void deleteSelectedObject(CpsObject obj) {
 		objectController.deleteSelectedObject(obj);
 	}
-	
+
 	public void addSelectedObject(CpsObject obj) {
 		objectController.addSelectedObject(obj);
 	}
 
 	/* Operations for Canvas */
 
-	public void AddEdgeOnCanvas(CpsEdge edge){
+	public void AddEdgeOnCanvas(CpsEdge edge) {
 		canvasController.addEdgeOnCanvas(edge);
 		try {
 			autoSave();
@@ -137,11 +131,13 @@ public class Control {
 
 	public void addObjectCanvas(CpsObject object) {
 		canvasController.addNewObject(object);
-		try {
-			autoSave();
-		} catch (IOException e) {
-			// TODO Auto-generated catch block
-			e.printStackTrace();
+		if (!(object instanceof CpsNode)) {
+			try {
+				autoSave();
+			} catch (IOException e) {
+				// TODO Auto-generated catch block
+				e.printStackTrace();
+			}
 		}
 	}
 
@@ -168,11 +164,11 @@ public class Control {
 		objectController.addNewElementIntoCategoryObject(catName, objName, eleName, amount, energy);
 	}
 
-	public void deleteElementCanvas(int id, String element){
+	public void deleteElementCanvas(int id, String element) {
 		objectController.deleteElementInCanvas(id, element);
 	}
 
-	public void deleteElementCanvas(HolonObject obj, HolonElement ele){
+	public void deleteElementCanvas(HolonObject obj, HolonElement ele) {
 		objectController.deleteElement(obj, ele);
 	}
 
@@ -206,45 +202,36 @@ public class Control {
 	public void initListener(CategoryListener catLis) {
 		categoryController.addCategoryListener(catLis);
 	}
-	
-	public void calculateStateForTimeStep(int x){
+
+	public void calculateStateForTimeStep(int x) {
 		simulationManager.reset();
 		simulationManager.calculateStateForTimeStep(x);
 	}
-	
-	public void setCanvas(MyCanvas can){
+
+	public void setCanvas(MyCanvas can) {
 		simulationManager.setCanvas(can);
 	}
-	
 
 	private void autoSave() throws IOException {
 		autoSaveController.increaseAutoSaveNr();
-		storeController.writeSaveFile(autoPath+autoSaveController.getAutoSaveNr());
+		storeController.writeSaveFile(autoPath + autoSaveController.getAutoSaveNr());
 	}
-	
-	public String getUndoSave(){
+
+	public String getUndoSave() {
 		autoSaveController.decreaseAutoSaveNr();
-		if(!new File(autoPath + (autoSaveController.getAutoSaveNr())).exists()){
+		if (!new File(autoPath + (autoSaveController.getAutoSaveNr())).exists()) {
 			autoSaveController.increaseAutoSaveNr();
 		}
-		return autoPath+(autoSaveController.getAutoSaveNr());
+		return autoPath + (autoSaveController.getAutoSaveNr());
 	}
-	
-	public String getRedoSave(){
+
+	public String getRedoSave() {
 		autoSaveController.increaseAutoSaveNr();
-		if(!new File(autoPath+(autoSaveController.getAutoSaveNr())).exists()){
+		if (!new File(autoPath + (autoSaveController.getAutoSaveNr())).exists()) {
 			autoSaveController.decreaseAutoSaveNr();
 		}
-		return autoPath+(autoSaveController.getAutoSaveNr());
-	}
-	
-//	public void increaseAutoSaveNr(){
-//		autoSaveController.increaseAutoSaveNr();
-//	}
-//	
-//	public void decreaseAutoSaveNr(){
-//		autoSaveController.decreaseAutoSaveNr();
-//	}
+		return autoPath + (autoSaveController.getAutoSaveNr());
+	}
 
 	/**
 	 * Getter for Model

+ 1 - 1
src/ui/controller/LoadController.java

@@ -72,7 +72,7 @@ public class LoadController {
 		if (mode.equals(MODE.ALL) || mode.equals(MODE.CATEGORY)) {
 			MODEL.setCgIdx(new HashMap<String, Integer>());
 			MODEL.setCategories(new ArrayList<Category>());
-			MODEL.setEdgesOnCanvas(new ArrayList<>());
+			MODEL.setEdgesOnCanvas(new ArrayList<CpsEdge>());
 		}
 		if (mode.equals(MODE.ALL) || mode.equals(MODE.CATEGORY)) {
 			MODEL.setCvsObjIdx(new HashMap<Integer, Integer>());

+ 27 - 1
src/ui/controller/SimulationManager.java

@@ -5,6 +5,7 @@ import java.util.ArrayList;
 import classes.CpsEdge;
 import classes.CpsNode;
 import classes.CpsObject;
+import classes.HolonElement;
 import classes.HolonObject;
 import classes.HolonSwitch;
 import classes.subNet;
@@ -35,6 +36,7 @@ public class SimulationManager {
 		for(subNet singleSubNet: subNets){
 			float production = calculateEnergy("prod", singleSubNet, x);
 			float consumption = calculateEnergy("cons", singleSubNet, x);
+			float minConsumption = calculateMinimumEnergy( singleSubNet, x);
 			for(CpsEdge e: singleSubNet.getEdges()){
 				e.setFlow(production);
 			}
@@ -48,7 +50,13 @@ public class SimulationManager {
 								hl.setState(2);
 							}
 							if((production + consumption) < 0){
-								hl.setState(1);
+								if((production + minConsumption) >= 0){
+									hl.setState(4);
+									System.out.println("yellow");
+								}else{
+									hl.setState(1);
+									System.out.println("orange");
+								}
 							}
 							break;
 						}
@@ -89,6 +97,24 @@ public class SimulationManager {
 		return energy;
 	}
 	
+	public float calculateMinimumEnergy(subNet sN, int x){
+		float min = 0;
+		float minElement = 0;
+		for(HolonObject hl: sN.getObjects()){
+			if(hl.getElements().size() > 0 && hl.getElements().get(0).getTotalEnergyAtTimeStep(x) < 0 ){
+				minElement = hl.getElements().get(0).getTotalEnergyAtTimeStep(x);
+			}
+			for(HolonElement he: hl.getElements()){
+				if(minElement < he.getTotalEnergyAtTimeStep(x) && he.getTotalEnergyAtTimeStep(x) < 0){
+					minElement = he.getTotalEnergyAtTimeStep(x);
+				}
+			}
+			System.out.println(minElement);
+			min = min + minElement;
+		}
+		return min;
+	}
+	
 	/**
 	 * generates all subNets from all objectsToHandle
 	 */

+ 28 - 15
src/ui/view/GUI.java

@@ -330,7 +330,7 @@ public class GUI<E> implements CategoryListener {
 		 * RIGHT CONTAINER (INFORMATION)
 		 **********************/
 		// Set up of the HolonElements section
-		Object[] columnNames = { "Device", "Energy", "Quantity", "Activated" };
+		Object[] columnNames = { "Object", "Device", "Energy", "Quantity", "Activated" };
 		tableModelHolonElement.setColumnIdentifiers(columnNames);
 		tableHolonElement.setBorder(null);
 		tableHolonElement.setModel(tableModelHolonElement);
@@ -476,12 +476,12 @@ public class GUI<E> implements CategoryListener {
 				try {
 					int yMouse = yTHIS;
 					int yBMouse = yBTHIS;
-					int selectedValueX = (int) Math.floor(xTHIS / (tableHolonElement.getWidth() / 4));
+					int selectedValueX = (int) Math.floor(xTHIS / (tableHolonElement.getWidth() / 5));
 					int selectedValueY = (int) Math.floor(yMouse / 16);
-					int selectedValueBX = (int) Math.floor(xBTHIS / (tableHolonElement.getWidth() / 4));
+					int selectedValueBX = (int) Math.floor(xBTHIS / (tableHolonElement.getWidth() / 5));
 					int selectedValueBY = (int) Math.floor(yBMouse / 16);
 					if (getActualCps() != null && getActualCps().getClass() == HolonObject.class) {
-						if (selectedValueBX == 3) {
+						if (selectedValueBX == 4) {
 							HolonElement eleBTemp = getActualHolonElement((HolonObject) getActualCps(), yBMouse);
 							String newBStuff = tableModelHolonElement.getValueAt(selectedValueBY, selectedValueBX)
 									.toString();
@@ -491,12 +491,12 @@ public class GUI<E> implements CategoryListener {
 							HolonElement eleTemp = getActualHolonElement((HolonObject) getActualCps(), yMouse);
 							String newStuff = tableModelHolonElement.getValueAt(selectedValueY, selectedValueX)
 									.toString();
-							if (selectedValueX == 0) {
+							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);
 							}
@@ -775,6 +775,7 @@ public class GUI<E> implements CategoryListener {
 		 * the clicked HolonObject
 		 */
 		canvas.addMouseListener(new MouseAdapter() {
+
 			@Override
 			public void mousePressed(MouseEvent e) {
 				if (temp == null || temp.getID() != model.getSelectedObjectID()) {
@@ -789,6 +790,7 @@ public class GUI<E> implements CategoryListener {
 				// Update of the Information about the Properties - only for
 				// CpsObjects
 				// Erase old data
+				System.out.println(model.getSelectedCpsObjects());
 				if (tableModelProperties.getRowCount() > 0) {
 					for (int i = tableModelProperties.getRowCount() - 1; i > -1; i--) {
 						tableModelProperties.removeRow(i);
@@ -808,10 +810,10 @@ public class GUI<E> implements CategoryListener {
 				boolean nothingImportant = true;
 				for (CpsObject c : model.getSelectedCpsObjects()) {
 					if (nothingImportant) {
-						System.out.println("Elements: " + c.getName());
+						System.out.println("Elements: " + c.getName() + " and ID: " + c.getID());
 						nothingImportant = false;
 					} else {
-						System.out.println(c.getName());
+						System.out.println(c.getName() + " and ID: " + c.getID());
 					}
 				}
 				// Write new data
@@ -881,6 +883,12 @@ public class GUI<E> implements CategoryListener {
 					deleteRows();
 				}
 			}
+
+			@Override
+			public void mouseReleased(MouseEvent e) {
+				refreshTableHolonElement();
+				refreshTableProperties();
+			}
 		});
 
 		toolBar.add(btnAdd);
@@ -1236,8 +1244,9 @@ public class GUI<E> implements CategoryListener {
 		// HolonObjects
 
 		deleteRows();
-		if (getActualCps() != null) {
-			fillElementTable(((HolonObject) getActualCps()).getElements());
+		System.out.println(model.getSelectedCpsObjects());
+		if (model.getSelectedCpsObjects() != null) {
+			fillElementTable(model.getSelectedCpsObjects());
 		}
 		tableModelHolonElement.fireTableDataChanged();
 	}
@@ -1260,10 +1269,14 @@ public class GUI<E> implements CategoryListener {
 	 * @param elements
 	 *            ArrayList to be displayed
 	 */
-	public void fillElementTable(ArrayList<HolonElement> elements) {
-		for (HolonElement he : elements) {
-			Object[] temp = { he.getEleName(), he.getEnergy(), he.getAmount(), he.getActive() };
-			tableModelHolonElement.addRow(temp);
+	public void fillElementTable(ArrayList<CpsObject> objects) {
+		for (CpsObject o : objects) {
+			if (o instanceof HolonObject) {
+				for (HolonElement he : ((HolonObject) o).getElements()) {
+					Object[] temp = { o.getName(), he.getEleName(), he.getEnergy(), he.getAmount(), he.getActive() };
+					tableModelHolonElement.addRow(temp);
+				}
+			}
 		}
 	}
 

+ 36 - 5
src/ui/view/MyCanvas.java

@@ -191,6 +191,9 @@ public class MyCanvas extends JPanel implements MouseListener, MouseMotionListen
 							controller.getScale() + ((controller.getScale() / 20) * 2),
 							controller.getScale() + ((controller.getScale() / 20) * 2));
 				} else if (cps instanceof HolonObject) {
+					if (((HolonObject) cps).getState() == 4) {
+						g2.setColor(Color.YELLOW);
+					}
 					if (((HolonObject) cps).getState() == 3) {
 						g2.setColor(Color.lightGray);
 					}
@@ -242,6 +245,9 @@ public class MyCanvas extends JPanel implements MouseListener, MouseMotionListen
 		if (!e.isControlDown() && dragging == false) {
 			model.getSelectedCpsObjects().clear();
 			TempSelected.clear();
+			if (tempCps != null) {
+				controller.addSelectedObject(tempCps);
+			}
 		}
 	}
 
@@ -341,13 +347,19 @@ public class MyCanvas extends JPanel implements MouseListener, MouseMotionListen
 		if (!model.getSelectedCpsObjects().contains(tempCps)) {
 			model.getSelectedCpsObjects().clear();
 			TempSelected.clear();
+			if (tempCps != null) {
+				controller.addSelectedObject(tempCps);
+			}
 		}
 
 		if (dragging) {
 			try {
+				float xDist, yDist; // Distance
+
 				x = e.getX() - controller.getScaleDiv2();
 				y = e.getY() - controller.getScaleDiv2();
 
+				// Make sure its in bounds
 				if (e.getX() < controller.getScaleDiv2())
 					x = 0;
 				else if (e.getX() > this.getWidth() - controller.getScaleDiv2())
@@ -356,16 +368,35 @@ public class MyCanvas extends JPanel implements MouseListener, MouseMotionListen
 					y = 0;
 				else if (e.getY() > this.getHeight() - controller.getScaleDiv2())
 					y = this.getHeight() - controller.getScale();
-				// Drag Position
-				tempCps.setPosition(x, y);
-				// Highlighting Position
-				selectRect.setLocation(x - (controller.getScale() / 20), y - (controller.getScale() / 20));
+
+				// Distance
+				xDist = x - tempCps.getPosition().x;
+				yDist = y - tempCps.getPosition().y;
+
+				tempCps.setPosition(x, y); // Drag Position
+				selectRect.setLocation(x - (controller.getScale() / 20), y - (controller.getScale() / 20)); // Highlighting-Position
+
 				// TipText Position and name
 				objectTT.setTipText(tempCps.getName());
 				objectTT.setLocation(x, y + controller.getScale());
+
+				// All Selected Objects
 				for (CpsObject cps : model.getSelectedCpsObjects()) {
 					if (cps != tempCps) {
-
+						x = (int) (cps.getPosition().x + xDist);
+						y = (int) (cps.getPosition().y + yDist);
+
+						// Make sure its in bounds
+						if (x < 0 - controller.getScaleDiv2())
+							x = 0;
+						else if (x > this.getWidth() - controller.getScale())
+							x = this.getWidth() - controller.getScale();
+						if (y < controller.getScaleDiv2())
+							y = 0;
+						else if (y > this.getHeight() - controller.getScale())
+							y = this.getHeight() - controller.getScale();
+
+						cps.setPosition(x, y);
 					}
 				}
 				repaint();

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

@@ -10,7 +10,7 @@ public class PropertyTable extends DefaultTableModel {
 	public Class<?> getColumnClass(int columnIndex) {
 		Class clazz = String.class;
 			switch (columnIndex) {
-			case 3:
+			case 4:
 				clazz = Boolean.class;
 				break;
 			}