Browse Source

Adds basic notifyObserver relations to improve the MVC-pattern

Andreas T. Meyer-Berg 5 years ago
parent
commit
86ba369e63

+ 1 - 1
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/Main.java

@@ -53,7 +53,7 @@ public class Main {
 	    //initializeTest();
 	    initializeMQTTTest();
 	    for(int i=0; i<10; i++)
-	    sim.simulateTimeIntervall(0+50*i, 50);
+	    	sim.simulateTimeIntervall(0+50*i, 50);
 	}
 	
 	/**

+ 28 - 0
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/control/Controller.java

@@ -1,5 +1,7 @@
 package de.tu_darmstadt.tk.SmartHomeNetworkSim.control;
 
+import java.util.Observer;
+
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Connection;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Link;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Model;
@@ -227,4 +229,30 @@ public class Controller {
 	public void removeSmartDeviceFromLink(SmartDevice toRemove, Link link) {
 		link.removeDevice(toRemove);
 	}
+	
+	/**
+	 * Notifies Observer, which may update their visualization/state
+	 */
+	public void notifyObservers(){
+		model.setChanged();
+		model.notifyObservers();
+	}
+	
+	/**
+	 * Adds Observer to the model, which will be notified, when the model changes
+	 * 
+	 * @param observer observer to be to notified on changes
+	 */
+	public void addObserver(Observer observer){
+		model.addObserver(observer);
+	}
+	
+	/**
+	 * Removes Observer from the model. The Observer will no longer be notified on changes 
+	 * 
+	 * @param observer observer to be removed
+	 */
+	public void removeObserver(Observer observer){
+		model.deleteObserver(observer);
+	}
 }

+ 6 - 1
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/core/Model.java

@@ -3,13 +3,14 @@ package de.tu_darmstadt.tk.SmartHomeNetworkSim.core;
 import java.util.ArrayList;
 import java.util.LinkedList;
 import java.util.List;
+import java.util.Observable;
 
 /**
  * Model of the smart home, which contains all the important parts of the simulation, like {@link SmartDevice} and their {@link Link}.
  *
  * @author Andreas T. Meyer-Berg
  */
-public class Model {
+public class Model extends Observable{
 
 	private List<SmartDevice> devices;
 	private List<Link> connectionNetworks;
@@ -148,4 +149,8 @@ public class Model {
 	public void addConnection(Connection connection) {
 		this.connections.add(connection);
 	}	
+	
+	public void setChanged(){
+		super.setChanged();
+	}
 }

+ 1 - 0
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/view/MainFrame.java

@@ -61,6 +61,7 @@ public class MainFrame extends JFrame {
 		
 		panel = new VisualisationPanel(model, control);
 		getContentPane().add(panel, BorderLayout.CENTER);
+		c.addObserver(panel);
 		//Update model Dimensions to visualisation size
 		
 		try {

+ 6 - 5
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/view/VisualisationInteractor.java

@@ -130,7 +130,7 @@ public class VisualisationInteractor implements MouseInputListener,
 			// Delete the clicked object
 				controller.deleteSmartDevice(clicked);
 				clicked = null;
-				panel.repaint();
+				controller.notifyObservers();
 			});
 
 		rightClickMenu.add(itemDelete);
@@ -181,8 +181,9 @@ public class VisualisationInteractor implements MouseInputListener,
 							for (Packet p : c.getTerminationPackages(1000))
 								System.out.println(p.toString());
 						});
-				model.getConnections().removeAll(terminated);
-				panel.repaint();
+				if(model.getConnections().removeAll(terminated))
+					controller.notifyObservers();
+			
 			});
 		rightClickMenu.add(itemDebug);
 
@@ -327,8 +328,8 @@ public class VisualisationInteractor implements MouseInputListener,
 				&& (dragged.getX() != dragged_x || dragged.getY() != dragged_y)) {
 			control.moveSmartDevice(dragged, dragged_x, dragged_y,
 					dragged.getZ());
-			panel.repaint();
 			dragged = null;
+			model.notifyObservers();
 		}
 	}
 
@@ -366,7 +367,7 @@ public class VisualisationInteractor implements MouseInputListener,
 		} else {
 			connectionFrom = null;
 		}
-		panel.repaint();
+		control.notifyObservers();
 	}
 
 	/**

+ 8 - 1
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/view/VisualisationPanel.java

@@ -5,6 +5,8 @@ import java.awt.Graphics;
 import java.awt.event.ComponentEvent;
 import java.awt.event.ComponentListener;
 import java.util.Collection;
+import java.util.Observable;
+import java.util.Observer;
 
 import javax.swing.JPanel;
 
@@ -16,7 +18,7 @@ import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.SmartDevice;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.protocols.MQTT_protocol;
 
 @SuppressWarnings("serial")
-public class VisualisationPanel extends JPanel {
+public class VisualisationPanel extends JPanel implements Observer {
 
 	/**
 	 * Smart Home model which is visualized
@@ -275,4 +277,9 @@ public class VisualisationPanel extends JPanel {
 		this.visualisationRadius = visualisationRadius;
 	}
 
+	@Override
+	public void update(Observable o, Object arg) {
+		repaint();
+	}
+
 }