Browse Source

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

Jessey Widhalm 7 years ago
parent
commit
f47018e26d

+ 2 - 0
src/classes/CpsEdge.java

@@ -48,6 +48,8 @@ public class CpsEdge {
 		this.flow = flow;
 		if(flow > maxCapacity){
 			isWorking = false;
+		}else{
+			isWorking = true;
 		}
 	}
 	

+ 11 - 3
src/classes/HolonSwitch.java

@@ -20,19 +20,22 @@ public class HolonSwitch extends CpsObject {
 
 	public HolonSwitch(String ObjName) {
 		super(ObjName);
-		setState(false);
+		setState(true);
+		setActiveAt(true);
 	}
 
 	public HolonSwitch(String ObjName, String obj) {
 		super(ObjName);
 		super.setName(obj);
-		setState(false);
+		setState(true);
 		setActiveAt(true);
 	}
 
 	public HolonSwitch(CpsObject obj) {
 		super(obj);
-		setState(((HolonSwitch) obj).getState());
+		super.setName(obj.getName());
+		setState(true);
+		setActiveAt(true);
 	}
 
 	public void switchState() {
@@ -50,6 +53,11 @@ public class HolonSwitch extends CpsObject {
 
 	public void setState(boolean state) {
 		this.active = state;
+		if (this.active == true) {
+			setImage("/Images/switch-on.png");
+		} else {
+			setImage("/Images/switch-off.png");
+		}
 	}
 
 	/**

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

@@ -33,7 +33,7 @@ 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-off.png");
+		addNewHolonSwitch(mpC.searchCategory("Component"), "Switch", "/Images/switch-on.png");
 
 	}
 

+ 5 - 0
src/ui/controller/Control.java

@@ -11,6 +11,7 @@ import classes.CpsObject;
 import classes.HolonElement;
 import classes.HolonObject;
 import ui.model.Model;
+import ui.view.MyCanvas;
 
 public class Control {
 
@@ -159,6 +160,10 @@ public class Control {
 		simulationManager.reset();
 		simulationManager.calculateStateForTimeStep(x);
 	}
+	
+	public void setCanvas(MyCanvas can){
+		simulationManager.setCanvas(can);
+	}
 
 	/**
 	 * Getter for Model

+ 15 - 12
src/ui/controller/SimulationManager.java

@@ -8,13 +8,16 @@ import classes.CpsObject;
 import classes.HolonObject;
 import classes.subNet;
 import ui.model.Model;
+import ui.view.MyCanvas;
 
 public class SimulationManager {
 	private Model model;
 	private ArrayList<CpsObject> objectsToHandle;
 	private ArrayList<subNet> subNets;
+	private MyCanvas canvas;
 	
 	public SimulationManager(Model m){
+		canvas = null;
 		model = m;
 		subNets = new ArrayList<subNet>();
 	}
@@ -33,27 +36,23 @@ public class SimulationManager {
 				e.setFlow(production);
 			}
 			for(HolonObject hl: singleSubNet.getObjects()){
-				boolean end = false;
-				int i = 0;
-				while(!end){
+				for(int i = 0; i < hl.getConnections().size(); i++){
 					CpsEdge edge = hl.getConnectedTo().get(i);
 					if(edge.getState()){
-						if(hl.getCurrentEnergyAtTimeStep(x) + edge.getFlow() >= 0 ){
+						System.out.println(hl.getCurrentEnergyAtTimeStep(x));
+						System.out.println(edge.getFlow());
+						if((hl.getCurrentEnergyAtTimeStep(x) + edge.getFlow()) >= 0 ){
 							hl.setSupplied(true);
 						}else{
 							hl.setSupplied(false);
 						}
-						end = true;
-					}else{
-						i++;
-						if(i == singleSubNet.getObjects().size()){
-							end = true;
-						}
+						break;
 					}
 				}
+				System.out.println(hl.getName() + "is supplied:" + hl.getSupplied());
 			}
 		}
-		
+		canvas.repaint();
 	}
 	
 	/**
@@ -100,7 +99,7 @@ public class SimulationManager {
 				}
 			}
 		}
-		printNet();
+		//printNet();
 	}
 	
 	/**
@@ -184,6 +183,10 @@ public class SimulationManager {
 		}
 	}
 	
+	public void setCanvas(MyCanvas can){
+		canvas = can;
+	}
+	
 	public void reset(){
 		copyObjects(model.getObjectsOnCanvas());
 	}

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

@@ -188,10 +188,10 @@ public class GUI<E> implements CategoryListener {
 		this.model = control.getModel();
 		this.canvas = new MyCanvas(model, control);
 		this.unitGraph = new UnitGraph(model, control);
-
 		control.initListener(this);
 		initialize();
 		updateCategories(model.getCategories());
+		controller.setCanvas(canvas);
 	}
 
 	/**
@@ -969,14 +969,7 @@ public class GUI<E> implements CategoryListener {
 		split_Graph_HolonEl.setBorder(null);
 		scrollPane_2.setBorder(null);
 		panel_HolonEl.setBorder(null);
-		btnTest.addActionListener(new ActionListener() {
-			public void actionPerformed(ActionEvent arg0) {
-				SimulationManager sm = new SimulationManager(model);
-				sm.calculateStateForTimeStep(1);
-			}
-		});
-		
-		panel_HolonEl.add(btnTest);
+
 		tableHolonElementScrollPane.setBorder(null);
 
 		frmCyberPhysical.getContentPane().add(timePanel, BorderLayout.SOUTH);
@@ -1157,6 +1150,7 @@ public class GUI<E> implements CategoryListener {
 			private void showMenu(MouseEvent e) {
 				popup.show(e.getComponent(), e.getX(), e.getY());
 			}
+			
 		});
 	}
 

+ 23 - 32
src/ui/view/MyCanvas.java

@@ -35,7 +35,7 @@ import ui.controller.Control;
 import ui.model.Model;
 import ui.model.idCounter;
 
-class MyCanvas extends JPanel implements MouseListener, MouseMotionListener {
+public class MyCanvas extends JPanel implements MouseListener, MouseMotionListener {
 	/**
 	 * 
 	 */
@@ -98,16 +98,6 @@ class MyCanvas extends JPanel implements MouseListener, MouseMotionListener {
 		RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
 		g2.setRenderingHints(rh);
 
-		// Supplied Highlighting
-
-		/*
-		 * // Selection if (selectRect != null &&
-		 * controller.searchByID(model.getSelectedObjectID()) != null &&
-		 * controller.searchByID(model.getSelectedObjectID()).getClass() !=
-		 * CpsNode.class) { g2.setColor(Color.BLUE); g2.fillRect((int)
-		 * selectRect.getX(), (int) selectRect.getY(), (int)
-		 * selectRect.getWidth(), (int) selectRect.getHeight()); }
-		 */
 		// drawEdges
 		// g2.setColor(Color.BLACK);
 		if (drawEdge) {
@@ -176,16 +166,29 @@ class MyCanvas extends JPanel implements MouseListener, MouseMotionListener {
 			if (cps.getID() == model.getSelectedObjectID() && controller.searchByID(model.getSelectedObjectID()) != null
 					&& controller.searchByID(model.getSelectedObjectID()) instanceof CpsNode) {
 				img = new ImageIcon(this.getClass().getResource("/Images/node_selected.png")).getImage();
-			} else {
+			}
+			else {
+				if (cps instanceof HolonSwitch) {
+					if (((HolonSwitch) cps).getActiveAt()[model.getCurIteration()]) {
+						((HolonSwitch) cps).setState(true);
+					} else {
+						((HolonSwitch) cps).setState(false);
+					}
+				} 
 				if (cps == tempCps) {
 					g2.setColor(Color.BLUE);
 					g2.fillRect((int) selectRect.getX(), (int) selectRect.getY(), (int) selectRect.getWidth(),
 							(int) selectRect.getHeight());
 				} else if (cps instanceof HolonObject) {
-					g2.setColor(Color.GRAY);
+					if (((HolonObject) cps).getSupplied()) {
+						g2.setColor(Color.GREEN);
+					} else {
+						g2.setColor(Color.RED);
+					}
 					g2.fillRect(cps.getPosition().x - (controller.getScale() / 20),
-							cps.getPosition().y - (controller.getScale() / 20), (int) selectRect.getWidth(),
-							(int) selectRect.getHeight());
+							cps.getPosition().y - (controller.getScale() / 20), 
+							controller.getScale() + ((controller.getScale() / 20) * 2),
+							controller.getScale() + ((controller.getScale() / 20) * 2));
 				}
 				File checkPath = new File(cps.getImage());
 				if (checkPath.exists()) {
@@ -202,11 +205,6 @@ class MyCanvas extends JPanel implements MouseListener, MouseMotionListener {
 
 	@Override
 	public void mouseClicked(MouseEvent e) {
-		// If double clicked on a Switch change the Image to on/off
-		if (doubleClick() && tempCps != null && tempCps.getClass() == HolonSwitch.class) {
-			((HolonSwitch) tempCps).switchState();
-		}
-		repaint();
 	}
 
 	@Override
@@ -393,13 +391,12 @@ class MyCanvas extends JPanel implements MouseListener, MouseMotionListener {
 				}
 				if (newEdge) {
 					e = new CpsEdge(cps, tempCps);
-					// cps.AddConnection(e);
-					// tempCps.AddConnection(e);
+					controller.calculateStateForTimeStep(model.getCurIteration());
 					controller.AddEdgeOnCanvas(e);
 				}
 			}
 		}
-		// Auf eine Edge gezoggen?
+		// Edge auf eine Edge gezogen?
 		if (onEdge) {
 			CpsEdge p = mousePositionOnEdge(x, y);
 			if (p != null) {
@@ -419,17 +416,11 @@ class MyCanvas extends JPanel implements MouseListener, MouseMotionListener {
 				k = p.getB();
 
 				e = new CpsEdge(n, tempCps);
-				// n.AddConnection(e);
-				// tempCps.AddConnection(e);
-
+				
 				e1 = new CpsEdge(n, r);
-				// n.AddConnection(e1);
-				// r.AddConnection(e1);
-
+				
 				e2 = new CpsEdge(n, k);
-				// n.AddConnection(e2);
-				// k.AddConnection(e2);
-
+				
 				p.getA().getConnections().remove(p);
 				p.getB().getConnections().remove(p);
 

+ 6 - 1
src/ui/view/UnitGraph.java

@@ -583,7 +583,12 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
 			MAXIMUM = he.getEnergy();
 			he.setGraphPoints((LinkedList<Point>) pointList.clone());
 			for (int i = 0; i < arrayOfFloats.length; i++) {
-				he.getEnergyAt()[i] = convertToValueY(getYValueAt_2((int) (i * width / (model.getIterations() - 1))));
+				if (he.getEnergy() >=0 ) {
+					he.getEnergyAt()[i] = convertToValueY(getYValueAt_2((int) (i * width / (model.getIterations() - 1))));	
+				} else {
+					he.getEnergyAt()[i] = -convertToValueY(getYValueAt_2((int) (i * width / (model.getIterations() - 1))));
+				}
+				
 			}
 			arrayOfFloats = he.getEnergyAt();
 		}