package com.metacodestudio.hotsuploader.utils;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.EventHandler;
import javafx.scene.control.ComboBox;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;

/**
 * @author Mateus Viccari
 * @author Eivind Vegsundvåg
 */
public class FXUtils {

    public enum AutoCompleteMode {
        STARTS_WITH,CONTAINING,;
    }

    public static<T> void autoCompleteComboBox(ComboBox<T> comboBox, AutoCompleteMode mode) {
        ObservableList<T> data = comboBox.getItems();

        comboBox.setEditable(true);
        comboBox.getEditor().focusedProperty().addListener(observable -> {
            if (0 > comboBox.getSelectionModel().getSelectedIndex()) {
                comboBox.getEditor().setText(null);
            }
        });
        comboBox.addEventHandler(KeyEvent.KEY_PRESSED, t -> comboBox.hide());
        comboBox.addEventHandler(KeyEvent.KEY_RELEASED, new EventHandler<KeyEvent>() {

            private boolean moveCaretToPos = false;
            private int caretPos;

            @Override
            public void handle(KeyEvent event) {
                if (KeyCode.UP == event.getCode()) {
                    caretPos = -1;
                    moveCaret(comboBox.getEditor().getText().length());
                    return;
                } else if (KeyCode.DOWN == event.getCode()) {
                    if (!comboBox.isShowing()) {
                        comboBox.show();
                    }
                    caretPos = -1;
                    moveCaret(comboBox.getEditor().getText().length());
                    return;
                } else if (KeyCode.BACK_SPACE == event.getCode()) {
                    moveCaretToPos = true;
                    caretPos = comboBox.getEditor().getCaretPosition();
                } else if (KeyCode.DELETE == event.getCode()) {
                    moveCaretToPos = true;
                    caretPos = comboBox.getEditor().getCaretPosition();
                }

                if (KeyCode.RIGHT == event.getCode() || KeyCode.LEFT == event.getCode()
                        || event.isControlDown() || KeyCode.HOME == event.getCode()
                        || KeyCode.END == event.getCode() || KeyCode.TAB == event.getCode()) {
                    return;
                }

                ObservableList<T> list = FXCollections.observableArrayList();
                for (T aData : data) {
                    if (mode.equals(AutoCompleteMode.STARTS_WITH) && aData.toString().toLowerCase().startsWith(comboBox.getEditor().getText().toLowerCase())) {
                        list.add(aData);
                    } else if (mode.equals(AutoCompleteMode.CONTAINING) && aData.toString().toLowerCase().contains(comboBox.getEditor().getText().toLowerCase())) {
                        list.add(aData);
                    }
                }
                String t = comboBox.getEditor().getText();

                comboBox.setItems(list);
                comboBox.getEditor().setText(t);
                if (!moveCaretToPos) {
                    caretPos = -1;
                }
                moveCaret(t.length());
                if (!list.isEmpty()) {
                    comboBox.show();
                }
            }

            private void moveCaret(int textLength) {
                if (-1 == caretPos) {
                    comboBox.getEditor().positionCaret(textLength);
                } else {
                    comboBox.getEditor().positionCaret(caretPos);
                }
                moveCaretToPos = false;
            }
        });
    }

    public static<T> T getComboBoxValue(ComboBox<T> comboBox){
        if (0 > comboBox.getSelectionModel().getSelectedIndex()) {
            return null;
        } else {
            return comboBox.getItems().get(comboBox.getSelectionModel().getSelectedIndex());
        }
    }
}

@@ -1183,26 +1183,16 @@
     
-if (KeyCode.UP == 
+switch (
 even
@@ -1198,32 +1198,65 @@
 nt.getCode()) %7B%0A
+                    case UP:%0A    
                 
@@ -1266,32 +1266,36 @@
  caretPos = -1;%0A
+    
                 
@@ -1362,32 +1362,36 @@
                 
+    
 return;%0A        
@@ -1402,54 +1402,27 @@
     
-%7D else if (KeyCode.DOWN == event.getCode()) %7B%0A
+    case DOWN:%0A    
     
@@ -1482,32 +1482,36 @@
                 
+ 
+   
 comboBox.show();
@@ -1523,34 +1523,40 @@
                 
-%7D%0A
+    %7D%0A  
                 
@@ -1551,32 +1551,34 @@
                 
+  
 caretPos = -1;%0A 
@@ -1568,32 +1568,36 @@
  caretPos = -1;%0A
+    
                 
@@ -1664,32 +1664,36 @@
                 
+  
+  
 return;%0A        
@@ -1704,60 +1704,33 @@
     
-%7D else if (KeyCode.BACK_SPACE == event.getCode()) %7B%0A
+    case BACK_SPACE:%0A    
     
@@ -1760,32 +1760,36 @@
 etToPos = true;%0A
+    
                 
@@ -1864,56 +1864,60 @@
     
-%7D else if (KeyCode.DELETE == event.getCode()) %7B%0A
+        break;%0A                    case DELETE:%0A    
     
@@ -1967,32 +1967,36 @@
                 
+ 
+   
 caretPos = combo
@@ -2023,32 +2023,63 @@
 aretPosition();%0A
+                        break;%0A
                 

replace if with many branches with switch