package be.msyben.vinylManager.tools.swing;

import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.util.Vector;
import javax.swing.ComboBoxModel;
import javax.swing.JComboBox;

/**
 * Cette classe défini ma combo box qui ajoute automatiquement le scroll avec la souris.
 * @author Mathieu Syben
 */
public class MyJComboBox<E> extends JComboBox<E> {

    /**
     * uid
     */
    private static final long serialVersionUID = 59426626269L;

    public MyJComboBox() {
        super();
        addMyWheel();
    }

    public MyJComboBox(Vector<E> items) {
        super(items);
        addMyWheel();
    }

    public MyJComboBox(E[] items) {
        super(items);
        addMyWheel();
    }

    public MyJComboBox(ComboBoxModel<E> aModel) {
        super(aModel);
        addMyWheel();
    }

    /**
     * Ajoute le scroll.
     */
    private void addMyWheel() {
        addMouseWheelListener(new MouseWheelListener() {

            @Override
            public void mouseWheelMoved(MouseWheelEvent e) {
                int selected = getSelectedIndex();
                int max = getItemCount();
                if (e.getWheelRotation() > 0) {
                    if (selected < max - 1) {
                        setSelectedIndex(selected + 1);
                    }
                }
                if (e.getWheelRotation() < 0) {
                    if (selected > 0) {
                        setSelectedIndex(selected - 1);
                    }
                }

            }
        });
    }
}
