
package org.mwc.debrief.lite.map;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.MouseEvent;

import javax.swing.JComponent;
import javax.swing.event.MouseInputAdapter;

/**
 * Draws a line on the parent component (e.g. JMapPane) as the mouse 
 * is dragged.
 * 
 * @author Ian Mayo = from Michael Bedward's MouseDragRectangle
 * 
 */
public class MouseDragLine extends MouseInputAdapter {
    
    private final JComponent parentComponent;
    private Point startPos;
    private Point endPos;
    private boolean dragged;
    private boolean enabled;
    private Graphics2D graphics;

    /**
     * Creates a new instance to work with the given component.
     * 
     * @param component the component on which the box will be drawn
     */
    public MouseDragLine(JComponent component) {
        parentComponent = component;
        dragged = false;
        enabled = false;
    }

    /**
     * Enables or disables the drag box. When enabled, the box 
     * is drawn on mouse dragging.
     * 
     * @param state {@code true} to enable; {@code false} to disable
     */
    public void setEnabled(boolean state) {
        enabled = state;
    }

    /**
     * If the line is enabled, records the start position for subsequent
     * drawing as the mouse is dragged.
     * 
     * @param ev input mouse event
     */
    @Override
    public void mousePressed(MouseEvent ev) {
      if(enabled)
      {
        startPos = new Point(ev.getPoint());
        endPos = new Point(startPos);
      }
    }

    /**
     * If the line is enabled, draws the line running from the
     * start position to the current mouse position. 
     * 
     * @param ev input mouse event
     */
    @Override
    public void mouseDragged(MouseEvent ev) {
        if (enabled) {
            ensureGraphics();
            if (dragged) {
                graphics.drawLine(startPos.x, startPos.y, endPos.x, endPos.y);
            }
            endPos = ev.getPoint();
            graphics.drawLine(startPos.x, startPos.y, endPos.x, endPos.y);
            dragged = true;
        }
    }

    /**
     * If the line is enabled, removes the final line.
     * 
     * @param ev the input mouse event
     */
    @Override
    public void mouseReleased(MouseEvent ev) {
        if (dragged) {
            ensureGraphics();
            graphics.drawLine(startPos.x, startPos.y, endPos.x, endPos.y);
            dragged = false;
            graphics.dispose();
            graphics = null;
        }
    }

    /**
     * Creates and initializes the graphics object if required.
     */
    private void ensureGraphics() {
        if (graphics == null) {
            graphics = (Graphics2D) parentComponent.getGraphics().create();
            graphics.setColor(Color.WHITE);
            graphics.setXORMode(Color.RED);
            graphics.setStroke(new BasicStroke(3f));
        }
    }
    
}

@@ -593,24 +593,54 @@
 an dragged;%0A
+    private boolean dragging;%0A
     private 
@@ -688,16 +688,53 @@
 raphics;
+%0A    private int buttonPressed = -1; 
 %0A%0A    /*
@@ -1016,24 +1016,50 @@
 ed = false;%0A
+        dragging = false;%0A
     %7D%0A%0A    /
@@ -1573,17 +1573,95 @@
 bled
-)%0A      %7B
+ && !dragging)%0A      %7B%0A        dragging = true;%0A        buttonPressed = ev.getButton();
 %0A   
@@ -2010,16 +2010,28 @@
 (enabled
+ && dragging
 ) %7B%0A    
@@ -2503,32 +2503,111 @@
 ouseEvent ev) %7B%0A
+      if ( buttonPressed == ev.getButton() )%0A      %7B%0A        dragging = false;%0A
         if (drag
@@ -2605,34 +2605,32 @@
  if (dragged) %7B%0A
-  
           ensure
@@ -2633,34 +2633,32 @@
 sureGraphics();%0A
-  
           graphi
@@ -2706,34 +2706,32 @@
 s.x, endPos.y);%0A
-  
           dragge
@@ -2743,34 +2743,32 @@
 alse;%0A          
-  
 graphics.dispose
@@ -2767,26 +2767,24 @@
 .dispose();%0A
-  
           gr
@@ -2796,35 +2796,50 @@
 s = null;%0A      
+%7D%0A      %7D%0A      
   
-%7D
 %0A    %7D%0A%0A    /**%0A

Fix #4525