All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.alee.laf.combobox.behavior.ComboBoxMouseWheelScrollBehavior Maven / Gradle / Ivy

There is a newer version: 1.2.14
Show newest version
/*
 * This file is part of WebLookAndFeel library.
 *
 * WebLookAndFeel library is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * WebLookAndFeel library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with WebLookAndFeel library.  If not, see .
 */

package com.alee.laf.combobox.behavior;

import com.alee.api.annotations.NotNull;
import com.alee.extended.behavior.AbstractComponentBehavior;
import com.alee.utils.MathUtils;
import com.alee.utils.SwingUtils;

import javax.swing.*;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;

/**
 * Combobox behavior that changes selection on mouse wheel scroll when combobox is focused.
 *
 * @author Mikle Garin
 */
public class ComboBoxMouseWheelScrollBehavior extends AbstractComponentBehavior implements MouseWheelListener
{
    /**
     * todo 1. Replace static methods with non-static ones
     */

    /**
     * Constructs new combobox mouse wheel scroll behavior.
     *
     * @param comboBox combobox using this behavior
     */
    public ComboBoxMouseWheelScrollBehavior ( @NotNull final JComboBox comboBox )
    {
        super ( comboBox );
    }

    @Override
    public void mouseWheelMoved ( @NotNull final MouseWheelEvent e )
    {
        if ( component.isEnabled () && SwingUtils.hasFocusOwner ( component ) )
        {
            // Changing selection in case index actually changed
            final int index = component.getSelectedIndex ();
            final int newIndex = MathUtils.limit ( 0, index + e.getWheelRotation (), component.getModel ().getSize () - 1 );
            if ( newIndex != index )
            {
                component.setSelectedIndex ( newIndex );
            }
        }
    }

    /**
     * Installs behavior into combobox and ensures that it is the only one installed.
     *
     * @param comboBox combobox to modify
     * @return installed behavior
     */
    @NotNull
    public static ComboBoxMouseWheelScrollBehavior install ( @NotNull final JComboBox comboBox )
    {
        // Uninstalling old behavior first
        uninstall ( comboBox );

        // Installing new behavior
        final ComboBoxMouseWheelScrollBehavior behavior = new ComboBoxMouseWheelScrollBehavior ( comboBox );
        comboBox.addMouseWheelListener ( behavior );
        return behavior;
    }

    /**
     * Uninstalls all behaviors from the specified combobox.
     *
     * @param comboBox combobox to modify
     */
    public static void uninstall ( @NotNull final JComboBox comboBox )
    {
        for ( final MouseWheelListener listener : comboBox.getMouseWheelListeners () )
        {
            if ( listener instanceof ComboBoxMouseWheelScrollBehavior )
            {
                comboBox.removeMouseWheelListener ( listener );
            }
        }
    }

    /**
     * Returns whether the specified combobox has any behaviors installed or not.
     *
     * @param comboBox combobox to process
     * @return true if the specified combobox has any behaviors installed, false otherwise
     */
    public static boolean isInstalled ( @NotNull final JComboBox comboBox )
    {
        for ( final MouseWheelListener listener : comboBox.getMouseWheelListeners () )
        {
            if ( listener instanceof ComboBoxMouseWheelScrollBehavior )
            {
                return true;
            }
        }
        return false;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy