package com.defano.hypercard.parts.editor;

import com.defano.hypercard.context.PartToolContext;
import com.defano.hypercard.parts.ToolEditablePart;
import com.defano.hypercard.parts.card.CardPart;
import com.defano.hypercard.runtime.WindowManager;

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

public class PartEditor implements AWTEventListener {

    public static void start() {
        Toolkit.getDefaultToolkit().addAWTEventListener(new PartEditor(), AWTEvent.MOUSE_EVENT_MASK);
    }

    @Override
    public void eventDispatched(AWTEvent event) {

        // User pressed the mouse
        if (event.getID() == MouseEvent.MOUSE_PRESSED) {

            // Is a part currently selected on the card?
            ToolEditablePart part = PartToolContext.getInstance().getSelectedPartProvider().get();
            if (part != null) {

                CardPart theCard = WindowManager.getStackWindow().getDisplayedCard();
                Point cardLocalMouseLoc = MouseInfo.getPointerInfo().getLocation();
                SwingUtilities.convertPointFromScreen(cardLocalMouseLoc, theCard);
                Point partLocalMouseLoc = SwingUtilities.convertPoint(theCard, cardLocalMouseLoc, part.getComponent());

                Rectangle r = new Rectangle();
                part.getComponent().getBounds(r);

                // User clicked resize drag handle
                if (part.getResizeDragHandle().contains(partLocalMouseLoc)) {
                    new PartResizer(part, theCard);
                }

                // User click inside of part
                else if (r.contains(cardLocalMouseLoc)) {
                    new PartMover(part, theCard).startMoving();
                }
            }

        }
    }
}

@@ -1755,24 +1755,169 @@
           %7D%0A
+%0A                // User clicked away from part%0A                else %7B%0A                    PartToolContext.getInstance().deselectAllParts();%0A    
             
@@ -1910,32 +1910,45 @@
               %7D%0A
+            %7D
 %0A        %7D%0A    %7D
@@ -1938,20 +1938,29 @@
 %7D%0A        %7D%0A
+        %0A
     %7D%0A%7D%0A

Enable deselection of parts by clicking away