import java.awt.event.*;
import java.awt.event.MouseAdapter;
import java.awt.*;

public class MouseListener extends MouseAdapter {
   private BlockSpringConfiguration bsConfig;
   private Draggable currentElement;
   private LabPanel labPanel;
   public MouseListener (BlockSpringConfiguration bsc, LabPanel lp){
      bsConfig = bsc;
      labPanel = lp;
   } 
   public void mouseMoved(MouseEvent e) {
      Draggable newElement = bsConfig.find(e.getX(), e.getY());
      if (newElement == currentElement) return;
      if (currentElement != null) {
         currentElement.setDefaultColor();
         currentElement = null;
         labPanel.repaint();
      }
      if (newElement != null) { 
         currentElement = newElement;
         currentElement.setSelectedColor();
         labPanel.repaint();
      }
   }
   public void mouseDragged(MouseEvent e) {
      if (currentElement == null) return;
      currentElement.drag(e.getX(), e.getY());
      labPanel.repaint();
   }
   public void mouseReleased(MouseEvent e) {
      if (currentElement == null) return;
      currentElement.attachNearElement(e.getX(), e.getY(), bsConfig);
      currentElement.setDefaultColor();      
      currentElement = null;
      labPanel.repaint();      
   }
	
}