Jelajahi Sumber

Refactors interaction states on the visualisation panel

Andreas T. Meyer-Berg 6 tahun lalu
induk
melakukan
98b0544617

+ 78 - 53
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/view/VisualisationInteractor.java

@@ -215,12 +215,6 @@ public class VisualisationInteractor implements MouseInputListener,
 
 	@Override
 	public void mouseClicked(MouseEvent e) {
-		// Finish operations
-		if (dragged != null) {
-			finishDrag();
-		}
-		connectionFrom = null;
-
 		/*
 		 * FindSmartDevice that was clicked
 		 */
@@ -232,6 +226,19 @@ public class VisualisationInteractor implements MouseInputListener,
 				clicked = d;
 			}
 		}
+		
+		switch (mode) {
+		case DRAG_DEVICE:
+			// Finish Drag Device operations
+			finishDrag();
+			break;
+		case DRAG_CONNECTION:
+			finishConnectionCreation();			
+			break;
+		default:
+			break;
+		}
+		
 		// If Rightclick
 		if (e.getButton() == MouseEvent.BUTTON3) {
 			// Show the RightClickMenu
@@ -279,42 +286,57 @@ public class VisualisationInteractor implements MouseInputListener,
 		dragged_x = e.getX();
 		dragged_y = e.getY();
 		// Check if SmartDevice was clicked
-		SmartDevice pressedOn = getSmartDeviceAtPosition(e.getX(), e.getY());
-		if (pressedOn == null){
-			//Click drag - multi select
-			dragged_x_start = e.getX();
-			dragged_y_start = e.getY();
-			mode = DRAG_SELECT;
-			selectedDevices.clear();
-			return;
-		}
-
-		// Recognize possible drag operation
-		if (e.getButton() == MouseEvent.BUTTON1 && connectionFrom == null) {
-			dragged = pressedOn;
-			dragged_x = pressedOn.getX();
-			dragged_y = pressedOn.getY();
-			mode = DRAG_DEVICE;
-		} else if (e.getButton() == MouseEvent.BUTTON3 && dragged == null) {
-			connectionFrom = pressedOn;
-			mode = DRAG_CONNECTION;
+		SmartDevice pressedOn = getSmartDeviceAtPosition(dragged_x, dragged_y);
+		switch (mode) {
+		case SELECTED:
+		case RIGHTCLICK_MENU:
+			mode = NOTHING;
+		case NOTHING:
+			if (pressedOn == null){
+				//Click drag - multi select
+				dragged_x_start = e.getX();
+				dragged_y_start = e.getY();
+				mode = DRAG_SELECT;
+				selectedDevices.clear();
+				return;
+			} else if (e.getButton() == MouseEvent.BUTTON1){// && connectionFrom == null) {
+				// Recognize possible drag operation
+				dragged = pressedOn;
+				dragged_x = pressedOn.getX();
+				dragged_y = pressedOn.getY();
+				mode = DRAG_DEVICE;
+			} else if (e.getButton() == MouseEvent.BUTTON3){// && dragged == null) {
+				connectionFrom = pressedOn;
+				mode = DRAG_CONNECTION;
+			}
+			break;
+		case DRAG_SELECT:
+			mode = SELECTED;
+			break;
+		default:
+			mode = NOTHING;
+			break;
 		}
+		
 	}
 
 	@Override
 	public void mouseReleased(MouseEvent e) {
-		// Finish drag operation
-		// if (e.getButton() == MouseEvent.BUTTON1)
-		if(mode == DRAG_SELECT){
+		// Finish operation
+		switch (mode) {
+		case DRAG_SELECT:
 			mode = SELECTED;
 			panel.repaint();
-		}
-		if(mode == DRAG_DEVICE)
+			break;
+		case DRAG_DEVICE:
 			finishDrag();
-		// else if(e.getButton() == MouseEvent.BUTTON3){
-		if(mode == DRAG_CONNECTION)
+			break;
+		case DRAG_CONNECTION:
 			finishConnectionCreation();
-		// }
+			break;
+		default:
+			break;
+		}
 	}
 
 	@Override
@@ -351,17 +373,18 @@ public class VisualisationInteractor implements MouseInputListener,
 				dragged_y = model.getHeight()-1;
 			else
 				dragged_y = e.getY();
-			if(mode == DRAG_CONNECTION)break;
-				int min_x = Math.min(dragged_x, dragged_x_start);
-				int min_y = Math.min(dragged_y, dragged_y_start);
-				int max_x = Math.max(dragged_x, dragged_x_start);
-				int max_y = Math.max(dragged_y, dragged_y_start);
-				selectedDevices.removeIf(s->(s.getX()<min_x||s.getX()>max_x||s.getY()<min_y||s.getY()>max_y));
-				for(SmartDevice sel:model.getDevices()){
-					if(sel.getX()>=min_x&&sel.getX()<=max_x&&sel.getY()>=min_y&&sel.getY()<=max_y&&!selectedDevices.contains(sel)){
-						selectedDevices.add(sel);
-					}
+			if(mode == DRAG_CONNECTION)
+				break;
+			int min_x = Math.min(dragged_x, dragged_x_start);
+			int min_y = Math.min(dragged_y, dragged_y_start);
+			int max_x = Math.max(dragged_x, dragged_x_start);
+			int max_y = Math.max(dragged_y, dragged_y_start);
+			selectedDevices.removeIf(s->(s.getX()<min_x||s.getX()>max_x||s.getY()<min_y||s.getY()>max_y));
+			for(SmartDevice sel:model.getDevices()){
+				if(sel.getX()>=min_x&&sel.getX()<=max_x&&sel.getY()>=min_y&&sel.getY()<=max_y&&!selectedDevices.contains(sel)){
+					selectedDevices.add(sel);
 				}
+			}
 			break;
 		case DRAG_DEVICE:
 			if (e.getX() <= control.getDevice_visualization_radius())
@@ -384,7 +407,7 @@ public class VisualisationInteractor implements MouseInputListener,
 		}
 		
 		// repaint the dragged smartDevice or new connection
-		if (dragged != null || connectionFrom != null||mode==DRAG_SELECT)
+		if (mode == DRAG_DEVICE || mode == DRAG_CONNECTION || mode == DRAG_SELECT)
 			panel.repaint();
 	}
 
@@ -398,13 +421,15 @@ public class VisualisationInteractor implements MouseInputListener,
 	 * panel and set dragged to null
 	 */
 	public void finishDrag() {
-		if (dragged != null
+		if (mode == DRAG_DEVICE){
+			if(dragged != null
 				&& (dragged.getX() != dragged_x || dragged.getY() != dragged_y)) {
-			control.moveSmartDevice(dragged, dragged_x, dragged_y,
-					dragged.getZ());
+				control.moveSmartDevice(dragged, dragged_x, dragged_y, dragged.getZ());
+			}
 			dragged = null;
-			model.notifyObservers();
 			mode = NOTHING;
+			panel.repaint();
+			model.notifyObservers();
 		}
 	}
 
@@ -418,7 +443,7 @@ public class VisualisationInteractor implements MouseInputListener,
 		 */
 		SmartDevice connectionTo = getSmartDeviceAtPosition(dragged_x,
 				dragged_y);
-		if (connectionFrom != null && connectionTo != null
+		if (mode == DRAG_CONNECTION && connectionFrom != null && connectionTo != null
 				&& connectionFrom != connectionTo) {
 			// Create new Connection
 			Link l = new SimpleLink("Ethernet: " + connectionFrom.getName()
@@ -438,12 +463,12 @@ public class VisualisationInteractor implements MouseInputListener,
 			connectionFrom.addPort(p1);
 			l.addConnection(c);
 			model.addConnection(c);
-			connectionFrom = null;
-		} else {
-			connectionFrom = null;
+			
 		}
-		control.notifyObservers();
+		connectionFrom = null;
 		mode = NOTHING;
+		panel.repaint();
+		control.notifyObservers();
 	}
 
 	/**

+ 54 - 17
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/view/VisualisationPanel.java

@@ -57,7 +57,6 @@ public class VisualisationPanel extends JPanel implements Observer {
 		this.control = control;
 		
 		this.interactor = new VisualisationInteractor(model, control, this);
-
 		this.addMouseMotionListener(interactor);
 		this.addMouseListener(interactor);
 	}
@@ -104,6 +103,33 @@ public class VisualisationPanel extends JPanel implements Observer {
 		paintDevices(g);
 		
 		paintDrag(g);
+		
+		g.setColor(Color.RED);
+		String modus;
+		switch (interactor.mode) {
+		case VisualisationInteractor.DRAG_CONNECTION:
+			modus="Drag Connection";
+			break;
+		case VisualisationInteractor.DRAG_DEVICE:
+			modus="Drag Device";
+			break;
+		case VisualisationInteractor.DRAG_SELECT:
+			modus="Drag Select";
+			break;
+		case VisualisationInteractor.NOTHING:
+			modus="Nothing";
+			break;
+		case VisualisationInteractor.RIGHTCLICK_MENU:
+			modus="RightClick Menu";
+			break;
+		case VisualisationInteractor.SELECTED:
+			modus="Selected";
+			break;
+		default:
+			modus="Uknown";
+			break;
+		}
+		g.drawString("Debug(Modus:"+modus+")", 0, 10);
 	}
 
 	/**
@@ -117,7 +143,7 @@ public class VisualisationPanel extends JPanel implements Observer {
 		for (SmartDevice s: model.getDevices()) {
 			int x = s.getX();
 			int y = s.getY();
-			if (s == interactor.dragged) {
+			if(interactor.mode==VisualisationInteractor.DRAG_DEVICE && s == interactor.dragged) {
 				// Update visualization of dragged object
 				x = interactor.dragged_x;
 				y = interactor.dragged_y;
@@ -160,10 +186,18 @@ public class VisualisationPanel extends JPanel implements Observer {
 			case Connection.HALTED:
 				connectionState = Color.ORANGE;
 				break;
+			case Connection.DONE:
+				connectionState = Color.GRAY;
+				break;
+			case Connection.TERMINATED:
+			case Connection.FINISHED:
+				connectionState = Color.RED;
+				break;
 			default:
-				connectionState = Color.GREEN;
+				connectionState = Color.BLUE;
 				break;
 			}
+			//g.setColor(connectionState);
 			/**
 			 * All Devices that are part of the connection
 			 */
@@ -180,7 +214,7 @@ public class VisualisationPanel extends JPanel implements Observer {
 					 * if broker was removed, paint broker at the average position
 					 */
 					for(Port p: c.getParticipants()){
-						if(p.getOwner()==interactor.dragged){
+						if(interactor.mode==VisualisationInteractor.DRAG_DEVICE && p.getOwner()==interactor.dragged){
 							broker_x+=interactor.dragged_x;
 							broker_y+=interactor.dragged_y;
 						}else{
@@ -208,7 +242,7 @@ public class VisualisationPanel extends JPanel implements Observer {
 					if (broker!= sd) {
 						// Check if dragged object
 						int s_x, s_y, sd_x, sd_y;
-						if (broker != null && broker.getOwner() == interactor.dragged) {
+						if (interactor.mode==VisualisationInteractor.DRAG_DEVICE && broker != null && broker.getOwner() == interactor.dragged) {
 							s_x = interactor.dragged_x;
 							s_y = interactor.dragged_y;
 						} else if(broker==null){
@@ -221,7 +255,7 @@ public class VisualisationPanel extends JPanel implements Observer {
 							s_x = broker_x;
 							s_y = broker_y;
 						}
-						if (sd.getOwner() == interactor.dragged) {
+						if (interactor.mode==VisualisationInteractor.DRAG_DEVICE && sd.getOwner() == interactor.dragged) {
 							sd_x = interactor.dragged_x;
 							sd_y = interactor.dragged_y;
 						} else {
@@ -237,19 +271,22 @@ public class VisualisationPanel extends JPanel implements Observer {
 				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 (s != sd) {
 						// Check if dragged object
 						int s_x, s_y, sd_x, sd_y;
-						if (s.getOwner() == interactor.dragged) {
+						if (interactor.mode==VisualisationInteractor.DRAG_DEVICE && s.getOwner() == interactor.dragged) {
 							s_x = interactor.dragged_x;
 							s_y = interactor.dragged_y;
 						} else {
 							s_x = s.getOwner().getX();
 							s_y = s.getOwner().getY();
 						}
-						if (sd.getOwner() == interactor.dragged) {
+						if (interactor.mode==VisualisationInteractor.DRAG_DEVICE && sd.getOwner() == interactor.dragged) {
 							sd_x = interactor.dragged_x;
 							sd_y = interactor.dragged_y;
 						} else {
@@ -270,11 +307,12 @@ public class VisualisationPanel extends JPanel implements Observer {
 		}
 		// paint new in progress connection, if a connection or link is in
 		// creation
-		g.setColor(Color.ORANGE);
-		if (interactor.connectionFrom != null)
+		if (interactor.mode == VisualisationInteractor.DRAG_CONNECTION && interactor.connectionFrom != null){
+			g.setColor(Color.ORANGE);
 			g.drawLine(interactor.connectionFrom.getX(),
 					interactor.connectionFrom.getY(), interactor.dragged_x,
 					interactor.dragged_y);
+		}
 	}
 	
 	/**
@@ -282,13 +320,12 @@ public class VisualisationPanel extends JPanel implements Observer {
 	 * @param g
 	 */
 	public void paintDrag(Graphics g){
-		if(interactor.mode!=VisualisationInteractor.DRAG_SELECT)return;
-		//interactor.dragged_x_start
-		
-		int low_x = interactor.dragged_x<interactor.dragged_x_start?interactor.dragged_x:interactor.dragged_x_start;
-		int low_y = interactor.dragged_y<interactor.dragged_y_start?interactor.dragged_y:interactor.dragged_y_start;
-		int high_x = interactor.dragged_x>=interactor.dragged_x_start?interactor.dragged_x:interactor.dragged_x_start;
-		int high_y = interactor.dragged_y>=interactor.dragged_y_start?interactor.dragged_y:interactor.dragged_y_start;
+		if(interactor.mode!=VisualisationInteractor.DRAG_SELECT)
+			return;	
+		int low_x = Math.min(interactor.dragged_x, interactor.dragged_x_start);
+		int low_y = Math.min(interactor.dragged_y, interactor.dragged_y_start);
+		int high_x = Math.max(interactor.dragged_x, interactor.dragged_x_start);
+		int high_y = Math.max(interactor.dragged_y, interactor.dragged_y_start);
 		g.setColor(Color.BLUE);
 		g.drawRect(low_x, low_y, high_x-low_x, high_y-low_y);