package fortiss.gui.listeners.line;

import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Line2D;

import javax.swing.Popup;
import javax.swing.PopupFactory;
import javax.swing.SwingUtilities;

import fortiss.components.Connection;
import fortiss.gui.ConnectionPropertiesPanel;
import fortiss.gui.DesignerPanel;
import fortiss.simulation.helper.ConnectionManager;

public class LineListener extends MouseAdapter {

	private ConnectionPropertiesPanel connectionPropertiesPanel;
	private Popup popup;

	@Override
	public void mouseClicked(MouseEvent e) {
		Point clickLocation = e.getPoint();
		Point screenLocation = e.getLocationOnScreen();

		// requires double click
		if (SwingUtilities.isLeftMouseButton(e) && e.getClickCount() == 2) {
			
			Line2D clickedLine = getClickedLine(clickLocation);
			
			if (clickedLine != null) {				
				createPopUp(screenLocation, clickedLine);
				popup.show();
			}			
		}
		else {
			if (popup != null) {
				popup.hide();
			}
		}
	}

	/**
	 * @param clickLocation location clicked in the coordinates of the source object
	 *                      (panel)
	 * @return the line that was clicked by the user or null
	 */
	private Line2D getClickedLine(Point clickLocation) {
		Line2D clickedLine = null;
		
		ConnectionManager cm = ConnectionManager.getInstance();
		for (Line2D line : cm.getLines()) {
			
			if (line.intersects(clickLocation.getX() - 2, clickLocation.getY() - 2, 4, 4)) {
				clickedLine = line;
				break;
			}
		}
		

		
		
		return clickedLine;
	}

	/**
	 * @param screenLocation location clicked in the coordinates of the screen
	 * @param clickedLine
	 */
	private void createPopUp(Point screenLocation, Line2D clickedLine) {
		// Hide other opened pop-ups
		if (popup != null)
			popup.hide();

		Connection clickedConnection = getClickedConnection(clickedLine);

		PopupFactory pf = PopupFactory.getSharedInstance();
		connectionPropertiesPanel = new ConnectionPropertiesPanel(clickedConnection);
		popup = pf.getPopup(DesignerPanel.pl_ems, connectionPropertiesPanel, screenLocation.x, screenLocation.y);
		// Important! to close pop-up when connection is eliminated
		connectionPropertiesPanel.setPopup(popup);
	}

	/**
	 * @param clickedLine a line
	 * @return the corresponding connection to clickedLine
	 */
	private Connection getClickedConnection(Line2D clickedLine) {
		ConnectionManager cm = ConnectionManager.getInstance();
		Connection clickedConnection = null;
		for (Connection connection : cm.getConnectionList()) {
			if (connection.getLn().equals(clickedLine)) {
				clickedConnection = connection;
				break;
			}
		}
		return clickedConnection;
	}
}

@@ -49,24 +49,51 @@
 .awt.Point;%0A
+import java.awt.Rectangle;%0A
 import java.
@@ -817,27 +817,24 @@
 t() == 2) %7B%0A
-%09%09%09
 %0A%09%09%09Line2D c
@@ -877,20 +877,16 @@
 ation);%0A
-%09%09%09%0A
 %09%09%09if (c
@@ -906,20 +906,16 @@
  null) %7B
-%09%09%09%09
 %0A%09%09%09%09cre
@@ -975,71 +975,8 @@
 ();%0A
-%09%09%09%7D%09%09%09%0A%09%09%7D%0A%09%09else %7B%0A%09%09%09if (popup != null) %7B%0A%09%09%09%09popup.hide();%0A
 %09%09%09%7D
@@ -1250,18 +1250,16 @@
 = null;%0A
-%09%09
 %0A%09%09Conne
@@ -1347,19 +1347,16 @@
 es()) %7B%0A
-%09%09%09
 %0A%09%09%09if (
@@ -1480,18 +1480,8 @@
 %09%09%7D%0A
-%09%09%0A%0A%09%09%0A%09%09%0A
 %09%09re
@@ -2041,18 +2041,27 @@
 cation.x
-,
  
+- 10,%0A%09%09%09%09
 screenLo
@@ -2068,16 +2068,21 @@
 cation.y
+ - 10
 );%0A%09%09// 
@@ -2182,16 +2182,310 @@
 (popup);
+%0A%0A%09%09connectionPropertiesPanel.addMouseListener(new MouseAdapter() %7B%0A%09%09%09@Override%0A%09%09%09public void mouseExited(MouseEvent e) %7B%0A%09%09%09%09Rectangle r = connectionPropertiesPanel.getBounds();%0A%09%09%09%09Point clickLocation = e.getPoint();%0A%09%09%09%09if (!r.contains(clickLocation)) %7B%0A%09%09%09%09%09popup.hide();%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%09%09%7D);
 %0A%09%7D%0A%0A%09/*
@@ -2930,10 +2930,11 @@
 ion;%0A%09%7D%0A
+%0A
 %7D%0A

Fix pop-up closing mechanism