Ticket #3368: scrolllistener_for_prefs.2.patch

File scrolllistener_for_prefs.2.patch, 2.1 KB (added by xeen, 15 years ago)

Eclipse decided for some reason to stop removing unused imports automatically…

  • src/org/openstreetmap/josm/gui/preferences/PreferenceDialog.java

     
    66import java.awt.Font;
    77import java.awt.GridBagLayout;
    88import java.awt.ScrollPane;
     9import java.awt.event.MouseWheelEvent;
     10import java.awt.event.MouseWheelListener;
    911import java.util.ArrayList;
    1012import java.util.Collection;
    1113import java.util.Iterator;
     
    3234 *
    3335 * @author imi
    3436 */
    35 public class PreferenceDialog extends JTabbedPane {
     37public class PreferenceDialog extends JTabbedPane implements MouseWheelListener {
    3638
    3739    private final static Collection<PreferenceSettingFactory> settingsFactory = new LinkedList<PreferenceSettingFactory>();
    3840    private final List<PreferenceSetting> settings = new ArrayList<PreferenceSetting>();
     
    116118    public PreferenceDialog() {
    117119        super(JTabbedPane.LEFT, JTabbedPane.SCROLL_TAB_LAYOUT);
    118120
     121        super.addMouseWheelListener(this);
     122
    119123        for (PreferenceSettingFactory factory:settingsFactory) {
    120124
    121125            PreferenceSetting setting = factory.createPreferenceSetting();
     
    173177        // always the last: advanced tab
    174178        settingsFactory.add(new AdvancedPreference.Factory());
    175179    }
     180
     181    /**
     182     * This mouse wheel listener reacts when a scroll is carried out over the
     183     * tab strip and scrolls one tab/down or up, selecting it immediately.
     184     */
     185    public void mouseWheelMoved(MouseWheelEvent wev) {
     186        // Ensure the cursor is over the tab strip
     187        if(super.indexAtLocation(wev.getPoint().x, wev.getPoint().y) < 0)
     188            return;
     189
     190        // Get currently selected tab
     191        int newTab = super.getSelectedIndex() + wev.getWheelRotation();
     192
     193        // Ensure the new tab index is sound
     194        newTab = newTab < 0 ? 0 : newTab;
     195        newTab = newTab >= super.getTabCount() ? super.getTabCount() - 1 : newTab;
     196
     197        // select new tab
     198        super.setSelectedIndex(newTab);
     199    }
    176200}