package com.fwdekker.randomness.ui;

import com.intellij.ui.popup.list.ListPopupImpl;
import com.intellij.ui.speedSearch.SpeedSearch;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.KeyStroke;


/**
 * A collection of helper methods for dealing with {@link com.intellij.openapi.ui.popup.JBPopup}s.
 */
public final class JBPopupHelper {
    /**
     * Private constructor to prevent instantiation.
     */
    private JBPopupHelper() {
        // Do nothing
    }


    /**
     * Disables speed search.
     *
     * @param popup the popup to disable speed search for
     */
    public static void disableSpeedSearch(final ListPopupImpl popup) {
        final SpeedSearch speedSearch = popup.getSpeedSearch();
        speedSearch.setEnabled(false);
        speedSearch.addChangeListener(event -> speedSearch.updatePattern(""));
    }

    /**
     * Registers actions such that actions can be selected while holding {@code Shift}.
     *
     * @param popup       the popup to enable selecting with {@code Shift} for
     * @param normalTitle the title of the popup while the {@code Shift} key is not pressed
     * @param shiftTitle  the title of the popup while the {@code Shift} key is pressed
     */
    public static void registerShiftActions(final ListPopupImpl popup, final String normalTitle,
                                            final String shiftTitle) {
        final AbstractAction handleWithModifier = new AbstractAction() {
            @Override
            public void actionPerformed(final ActionEvent event) {
                final KeyEvent keyEvent = new KeyEvent(popup.getComponent(), event.getID(), event.getWhen(),
                                                       event.getModifiers(), KeyEvent.VK_ENTER, KeyEvent.CHAR_UNDEFINED,
                                                       KeyEvent.KEY_LOCATION_UNKNOWN);
                popup.handleSelect(true, keyEvent);
            }
        };

        popup.registerAction("shiftReleased", KeyStroke.getKeyStroke("released SHIFT"), new AbstractAction() {
            public void actionPerformed(final ActionEvent event) {
                popup.setCaption(normalTitle);
            }
        });
        popup.registerAction("shiftPressed", KeyStroke.getKeyStroke("shift pressed SHIFT"), new AbstractAction() {
            public void actionPerformed(final ActionEvent event) {
                popup.setCaption(shiftTitle);
            }
        });
        popup.registerAction("shiftInvokeAction", KeyStroke.getKeyStroke("shift ENTER"), handleWithModifier);

        final int nine = 9;
        for (int i = 0; i < nine; i++) {
            popup.registerAction("shiftInvoke" + i, KeyStroke.getKeyStroke("shift " + i), handleWithModifier);
        }
    }
}

@@ -190,24 +190,59 @@
 t.KeyEvent;%0A
+import java.util.stream.IntStream;%0A
 import javax
@@ -1488,49 +1488,593 @@
     
-final AbstractAction handleWithModifier =
+popup.registerAction(%22shiftReleased%22, KeyStroke.getKeyStroke(%22released SHIFT%22), new AbstractAction() %7B%0A            public void actionPerformed(final ActionEvent event) %7B%0A                popup.setCaption(normalTitle);%0A            %7D%0A        %7D);%0A        popup.registerAction(%22shiftPressed%22, KeyStroke.getKeyStroke(%22shift pressed SHIFT%22), new AbstractAction() %7B%0A            public void actionPerformed(final ActionEvent event) %7B%0A                popup.setCaption(shiftTitle);%0A            %7D%0A        %7D);%0A        popup.registerAction(%22shiftInvokeAction%22, KeyStroke.getKeyStroke(%22shift ENTER%22),
  new
@@ -2574,16 +2574,17 @@
        %7D
+)
 ;%0A%0A     
@@ -2578,37 +2578,120 @@
    %7D);%0A%0A        
-popup
+final int nine = 9;%0A        IntStream.range(1, nine).forEach(i -%3E popup%0A                
 .registerAction(
@@ -2692,33 +2692,35 @@
 ction(%22shift
-Released%22
+Invoke%22 + i
 , KeyStroke.
@@ -2737,49 +2737,147 @@
 ke(%22
-released SHIFT%22), new AbstractAction() %7B%0A
+shift %22 + i),%0A                                new AbstractAction() %7B%0A                                    @Override%0A                        
     
@@ -2959,39 +2959,111 @@
     
+                        final KeyEvent keyEvent = new KeyEvent(
 popup.
-setCaption(normalTitle);%0A
+getComponent(), event.getID(),%0A   
     
@@ -3066,34 +3066,32 @@
             
-%7D%0A
         
 %7D);%0A        
@@ -3074,36 +3074,32 @@
             
-%7D);%0A
         
 popup.regist
@@ -3090,225 +3090,405 @@
     
-popup.registerAction(%22shiftPressed%22, KeyStroke.getKeyStroke(%22shift pressed SHIFT%22), new AbstractAction() %7B%0A            public void actionPerformed(final ActionEvent event) %7B%0A                popup.setCaption(shiftTitle
+                                                event.getWhen(), event.getModifiers(),%0A                                                                               KeyEvent.VK_ENTER,%0A                                                                               KeyEvent.CHAR_UNDEFINED,%0A                                                                               KeyEvent.KEY_LOCATION_UNKNOWN
 );%0A 
@@ -3494,34 +3494,33 @@
             
-%7D%0A
+ 
         
 %7D);%0A        
@@ -3499,36 +3499,43 @@
                 
-%7D);%0A
+           
         popup.re
@@ -3536,113 +3536,62 @@
 pup.
-registerAction(%22shiftInvokeAction%22, KeyStroke.getKeyStroke(%22shift ENTER%22), handleWithModifier
+getList().addSelectionInterval(i - 1, i - 1
 );%0A
-%0A
         
 fina
@@ -3590,180 +3590,138 @@
     
-final int nine = 9;%0A        for (int i = 0; i %3C nine; i++) %7B%0A            popup.registerAction(%22shiftInvoke%22 + i, KeyStroke.getKeyStroke(%22shift %22 + i), handleWithModifier);%0A
+                                popup.handleSelect(true, keyEvent);%0A                                    %7D%0A                        
     
@@ -3721,21 +3721,24 @@
            %7D
+));
 %0A    %7D%0A%7D%0A

Fix Shift-selecting actions