Pārlūkot izejas kodu

Adds Rightclick Edit connection, even if multiple cons overlap

Andreas T. Meyer-Berg 6 gadi atpakaļ
vecāks
revīzija
daefd1e74c

+ 45 - 3
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/view/VisualisationInteractor.java

@@ -9,6 +9,7 @@ import java.awt.event.MouseEvent;
 import java.awt.event.MouseMotionListener;
 import java.util.LinkedList;
 
+import javax.swing.JMenu;
 import javax.swing.JMenuItem;
 import javax.swing.JPopupMenu;
 import javax.swing.event.MouseInputListener;
@@ -69,6 +70,14 @@ public class VisualisationInteractor implements MouseInputListener,
 	 * RightClick MenuItem for Connection creation
 	 */
 	private JMenuItem itemCreateConnection;
+	/**
+	 * RightClickMenu for editing connections
+	 */
+	private JMenu itemEditConnection;
+	/**
+	 * Position of editCreate connection in the Rightclick menu 
+	 */
+	private int editCreateConnectionIndex = 2;
 
 	/**
 	 * RightClick MenuItem for SmartDevice deletion
@@ -584,10 +593,42 @@ public class VisualisationInteractor implements MouseInputListener,
 				itemCreate.setEnabled(true);
 				itemDelete.setEnabled(false);
 				itemDeleteSelected.setEnabled(false);
+				itemCreateLink.setEnabled(false);
+				if(clickedConnection.size()>0){
+					if(rightClickMenu.getComponentIndex(itemCreateConnection)!=-1){
+						rightClickMenu.remove(itemCreateConnection);
+						itemEditConnection.removeAll();
+						//List to prevent duplicate connection entries
+						LinkedList<Connection> editableConnections = new LinkedList<Connection>();
+						for(Pair<Connection, Pair<Port,Port>> p:clickedConnection){
+							//Don't add the same connection multiple times
+							if(editableConnections.contains(p.getLeft()))continue;
+							editableConnections.add(p.getLeft());
+							JMenuItem a = new JMenuItem(p.getLeft().getName());
+							a.addActionListener(e->new ConnectionCreationDialog(p.getLeft(), controller, panel));
+							itemEditConnection.add(a);
+						}
+						rightClickMenu.add(itemEditConnection,editCreateConnectionIndex);
+						itemEditConnection.setEnabled(true);
+					}
+					itemCreateConnection.setEnabled(true);				
+				}else{
+					if(rightClickMenu.getComponentIndex(itemEditConnection)!=-1){
+						rightClickMenu.remove(itemEditConnection);
+						rightClickMenu.add(itemCreateConnection,editCreateConnectionIndex);
+					}
+					itemCreateConnection.setEnabled(false);
+				}
 			} else {
 				itemCreate.setText("Edit Device");
 				itemCreate.setEnabled(true);
 				itemDelete.setEnabled(true);
+				//Remove edit connection if it exist
+				if(rightClickMenu.getComponentIndex(itemEditConnection)!=-1){
+					rightClickMenu.remove(itemEditConnection);
+					rightClickMenu.add(itemCreateConnection,editCreateConnectionIndex);
+				}
+				
 				if(selectedDevices.contains(clickedOn)){
 					itemDeleteSelected.setEnabled(true);
 					itemCreateLink.setEnabled(true);
@@ -649,8 +690,7 @@ public class VisualisationInteractor implements MouseInputListener,
 		
 		// Create Connection
 		itemCreateConnection = new JMenuItem("Create Connection");
-		
-		itemCreateConnection.addActionListener(e -> {
+		itemCreateConnection.addActionListener(e->{
 			LinkedList<Port> ports = new LinkedList<Port>();
 			for(SmartDevice d: selectedDevices)
 				ports.add(new Port(d, (short) 12));
@@ -660,8 +700,10 @@ public class VisualisationInteractor implements MouseInputListener,
 			else
 				mode = SELECTED;
 		});
-		
 		rightClickMenu.add(itemCreateConnection);
+		
+		// Edit connection
+		itemEditConnection = new JMenu("Edit Connection");
 
 		// Delete device option
 		itemDelete = new JMenuItem("Delete");