/* Copyright (C) 2005-2011 Fabio Riccardi */

package com.lightcrafts.ui.operation.drag;

import javax.swing.event.MouseInputAdapter;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;

class StackDragListener extends MouseInputAdapter {

    private final static int DragThreshold = 5;

    private DraggableStack stack;

    private Point startCursor;
    private Point startComponent;
    private boolean dragging;

    StackDragListener(DraggableStack stack) {
        this.stack = stack;
    }

    public void mousePressed(MouseEvent event) {
        JComponent comp = (JComponent) event.getComponent();
        comp = (JComponent) SwingUtilities.getAncestorOfClass(
            StackableComponent.class, comp
        );
        event.translatePoint(comp.getX(), comp.getY());
        startCursor = event.getPoint();
    }

    public void mouseDragged(MouseEvent event) {
        JComponent comp = (JComponent) event.getComponent();
        comp = (JComponent) SwingUtilities.getAncestorOfClass(
            StackableComponent.class, comp
        );
        event.translatePoint(comp.getX(), comp.getY());
        if ((startCursor != null) && (! dragging)) {
            Point p = event.getPoint();
            if (startCursor.distance(p) > DragThreshold) {
                startComponent = comp.getLocation();
                boolean isSwappable = ((StackableComponent) comp).isSwappable();
                stack.dragStart(comp, isSwappable);
                dragging = true;
            }
        }
        if (dragging) {
            Point p1 = startCursor;
            Point p2 = event.getPoint();

            Point interval = new Point(p2.x - p1.x, p2.y - p1.y);

            stack.dragTo(comp, startComponent.y + interval.y);
        }
    }

    public void mouseReleased(MouseEvent event) {
        if (dragging) {
            JComponent comp = (JComponent) event.getComponent();
            comp = (JComponent) SwingUtilities.getAncestorOfClass(
                StackableComponent.class, comp
            );
            stack.dragEnd(comp);
            dragging = false;
        }
        startCursor = null;
    }
}

@@ -509,32 +509,46 @@
 = stack;%0A    %7D%0A%0A
+    @Override%0A
     public void 
@@ -610,213 +610,35 @@
 p = 
-(JComponent) event.getComponent();%0A        comp = (JComponent) SwingUtilities.getAncestorOfClass(%0A            StackableComponent.class, comp%0A        );%0A        event.translatePoint(comp.getX(), comp.getY()
+getAncestorJComponent(event
 );%0A 
@@ -675,32 +675,46 @@
 Point();%0A    %7D%0A%0A
+    @Override%0A
     public void 
@@ -776,213 +776,35 @@
 p = 
-(JComponent) event.getComponent();%0A        comp = (JComponent) SwingUtilities.getAncestorOfClass(%0A            StackableComponent.class, comp%0A        );%0A        event.translatePoint(comp.getX(), comp.getY()
+getAncestorJComponent(event
 );%0A 
@@ -1298,17 +1298,16 @@
 oint();%0A
-%0A
         
@@ -1364,17 +1364,16 @@
  p1.y);%0A
-%0A
         
@@ -1440,24 +1440,38 @@
    %7D%0A    %7D%0A%0A
+    @Override%0A
     public v
@@ -1528,32 +1528,197 @@
 dragging) %7B%0A    
+        tearDown(event);%0A            dragging = false;%0A        %7D%0A        startCursor = null;%0A    %7D%0A%0A    private JComponent getAncestorJComponent(MouseEvent event) %7B%0A
         JCompone
@@ -1758,28 +1758,24 @@
 omponent();%0A
-    
         comp
@@ -1884,16 +1884,20 @@
         
+    
 );%0A     
@@ -1903,99 +1903,213 @@
     
-    stack.dragEnd(comp);%0A            dragging = false;%0A        %7D%0A        startCursor = null
+event.translatePoint(comp.getX(), comp.getY());%0A        return comp;%0A    %7D%0A%0A    private void tearDown(MouseEvent event) %7B%0A        JComponent comp = getAncestorJComponent(event);%0A        stack.dragEnd(comp)
 ;%0A  

Refactor StackDragListener