Browse Source

Refactors connection painting to use the new system

Andreas T. Meyer-Berg 6 năm trước cách đây
mục cha
commit
6966212cbd

+ 23 - 102
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/view/VisualisationPanel.java

@@ -15,7 +15,7 @@ import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Connection;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Model;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Port;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.SmartDevice;
-import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.protocols.MQTT_protocol;
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.util.Pair;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.view.VisualisationInteractor;
 
 /**
@@ -179,7 +179,7 @@ public class VisualisationPanel extends JPanel implements Observer {
 							+ control.getDevice_visualization_radius() + 11);
 		}
 	}
-
+	
 	/**
 	 * Paints the Connections
 	 * 
@@ -194,6 +194,7 @@ public class VisualisationPanel extends JPanel implements Observer {
 			x_offset = interactor.dragged_x-interactor.dragged.getX();
 			y_offset = interactor.dragged_y-interactor.dragged.getY();
 		}
+		
 		// For all Connections
 		for (Connection c : model.getConnections()) {
 			Color connectionState;
@@ -215,118 +216,38 @@ public class VisualisationPanel extends JPanel implements Observer {
 				connectionState = Color.BLUE;
 				break;
 			}
-			//g.setColor(connectionState);
+			g.setColor(connectionState);
 			/**
 			 * All Devices that are part of the connection
 			 */
 			Collection<Port> d = c.getParticipants();
 			if(d.size()==0){
-				System.out.println("WARNING: Empty, undeleted Connection:"+c.toString());
+				System.out.println("WARNING: Empty, undeleted Connection: "+c.toString());
 				continue;
 			}
-			if(c.getProtocol() instanceof MQTT_protocol){
-				Port broker = c.getProtocol().getDevicesWithRole(0).iterator().next();
-				int broker_x = 0, broker_y = 0;
-				if(broker==null){
-					/**
-					 * if broker was removed, paint broker at the average position
-					 */
-					for(Port p: c.getParticipants()){
-						if(interactor.mode==VisualisationInteractor.SELECTED_DRAG && interactor.selectedDevices.contains(p.getOwner())){
-							broker_x+=p.getOwner().getX()+x_offset;
-							broker_y+=p.getOwner().getY()+y_offset;
-						}else{
-
-							broker_x+=p.getOwner().getX();
-							broker_y+=p.getOwner().getY();
-						}
-					}
-					broker_x/=c.getParticipants().size();
-					broker_y/=c.getParticipants().size();
-					g.fillOval(broker_x-control.getDevice_visualization_radius()/4, broker_y-control.getDevice_visualization_radius()/4, control.getDevice_visualization_radius()/2, control.getDevice_visualization_radius()/2);
-				}else{
-					if(interactor.mode==VisualisationInteractor.SELECTED_DRAG && interactor.selectedDevices.contains(broker.getOwner())){
-						broker_x=broker.getOwner().getX()+x_offset;
-						broker_y=broker.getOwner().getY()+y_offset;						
-					}else{
-						broker_x=broker.getOwner().getX();
-						broker_y=broker.getOwner().getY();
-					}
-				}
-				for(Port sd:d){
-					if(!model.getDevices().contains(sd.getOwner())){
-						SmartDevice removed = sd.getOwner();
-						g.setColor(Color.RED);
-						g.fillOval(removed.getX()-control.getDevice_visualization_radius()/4, removed.getY()-control.getDevice_visualization_radius()/4, control.getDevice_visualization_radius()/2, control.getDevice_visualization_radius()/2);
-					}else{
-						g.setColor(connectionState);
-					}
-					
-					if (broker!= sd) {
-						// Check if dragged object
-						int s_x, s_y, sd_x, sd_y;
-						if (interactor.mode==VisualisationInteractor.SELECTED_DRAG && broker != null && interactor.selectedDevices.contains(broker.getOwner())) {
-							s_x = broker.getOwner().getX()+x_offset;
-							s_y = broker.getOwner().getY()+y_offset;
-						} else if(broker==null){
-							/**
-							 * if broker was removed, paint edges to the previously calculated average position
-							 */
-							s_x = broker_x;
-							s_y = broker_y;
-						}else{
-							s_x = broker_x;
-							s_y = broker_y;
-						}
-						if (interactor.mode==VisualisationInteractor.SELECTED_DRAG && interactor.selectedDevices.contains(sd.getOwner())) {
-							sd_x = sd.getOwner().getX()+x_offset;
-							sd_y = sd.getOwner().getY()+y_offset;
-						} else {
-							sd_x = sd.getOwner().getX();
-							sd_y = sd.getOwner().getY();
-						}
-						g.drawLine(s_x, s_y, sd_x, sd_y);
-					}
-				}
+			if(c.getProtocol()==null){
+				System.out.println("WARNING: Protocol is null: "+c.toString());
+				continue;
 			}
-			else if (d.size() == 2) {
-				Port s = c.getSource();
-				for (Port sd : d) {
-					if(!model.getDevices().contains(sd.getOwner())){
-						SmartDevice removed = sd.getOwner();
-						g.setColor(Color.RED);
-						g.fillOval(removed.getX()-control.getDevice_visualization_radius()/4, removed.getY()-control.getDevice_visualization_radius()/4, control.getDevice_visualization_radius()/2, control.getDevice_visualization_radius()/2);
-					}else{
-						g.setColor(connectionState);
+			for(Pair<Port,Port> p: c.getProtocol().getTopology() ){
+				if(p.getLeft() != null && p.getLeft().getOwner() != null && p.getRight() != null && p.getRight().getOwner() != null){
+					SmartDevice from = p.getLeft().getOwner();
+					SmartDevice to = p.getRight().getOwner();
+					int xFrom = from.getX();
+					int yFrom = from.getY();
+					int xTo = to.getX();
+					int yTo = to.getY();
+					if(interactor.selectedDevices.contains(from)^interactor.selectedDevicesDrag.contains(from)){
+						xFrom+=x_offset;
+						yFrom+=y_offset;
 					}
-					if (s != sd) {
-						// Check if dragged object
-						int s_x, s_y, sd_x, sd_y;
-						if (interactor.mode==VisualisationInteractor.SELECTED_DRAG && interactor.selectedDevices.contains(s.getOwner())) {
-							s_x = s.getOwner().getX()+x_offset;
-							s_y = s.getOwner().getY()+y_offset;
-						} else {
-							s_x = s.getOwner().getX();
-							s_y = s.getOwner().getY();
-						}
-						if (interactor.mode==VisualisationInteractor.SELECTED_DRAG && interactor.selectedDevices.contains(sd.getOwner())) {
-							sd_x = sd.getOwner().getX()+x_offset;
-							sd_y = sd.getOwner().getY()+y_offset;
-						} else {
-							sd_x = sd.getOwner().getX();
-							sd_y = sd.getOwner().getY();
-						}
-						g.drawLine(s_x, s_y, sd_x, sd_y);
+					if(interactor.selectedDevices.contains(to)^interactor.selectedDevicesDrag.contains(to)){
+						xTo+=x_offset;
+						yTo+=y_offset;
 					}
+					g.drawLine(xFrom, yFrom, xTo, yTo);
 				}
-			} else if (d.size() == 1) {
-
-			} else if (d.size() <= 0) {
-				// Invalid Connection
-			} else {
-				// Draw MultiConnection
 			}
-
 		}
 		// paint new in progress connection, if a connection or link is in
 		// creation