Ignore:
Timestamp:
2014-07-05T18:38:07+02:00 (10 years ago)
Author:
Don-vip
Message:

fix #10207 - fix menu scroller for multi monitors systems

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/MenuScroller.java

    r6987 r7291  
    99import java.awt.Dimension;
    1010import java.awt.Graphics;
     11import java.awt.GraphicsConfiguration;
     12import java.awt.Insets;
    1113import java.awt.event.ActionEvent;
    1214import java.awt.event.ActionListener;
     
    2628import javax.swing.event.PopupMenuListener;
    2729
     30import org.openstreetmap.josm.Main;
     31
    2832/**
    2933 * A class that provides scrolling capabilities to a long menu dropdown or
     
    3236 * <P>
    3337 * <B>Implementation note:</B>  The default number of items to display
    34  * at a time is 15, and the default scrolling interval is 125 milliseconds.
     38 * at a time is 15, and the default scrolling interval is 150 milliseconds.
    3539 * <P>
    3640 * @author Darryl, https://tips4java.wordpress.com/2009/02/01/menu-scroller/
     
    5155    private int keepVisibleIndex = -1;
    5256
     57    private static final int ARROW_ICON_HEIGHT = 10;
     58
     59    /**
     60     * Computes the number of items to display at once for the given component and a given item height.
     61     * @param comp The menu
     62     * @param itemHeight Average item height
     63     * @return the number of items to display at once
     64     * @since 7291
     65     */
     66    public static int computeScrollCount(JComponent comp, int itemHeight) {
     67        int result = 15;
     68        if (comp != null && itemHeight > 0) {
     69            // Compute max height of current screen
     70            int maxHeight = 0;
     71            GraphicsConfiguration gc = comp.getGraphicsConfiguration();
     72            if (gc == null && Main.parent != null) {
     73                gc = Main.parent.getGraphicsConfiguration();
     74            }
     75            if (gc != null) {
     76                // Max displayable height (max screen height - vertical insets)
     77                Insets insets = comp.getToolkit().getScreenInsets(gc);
     78                maxHeight = gc.getBounds().height - insets.top - insets.bottom;
     79            }
     80
     81            // Remove height of our two arrow icons + 2 pixels each for borders (arbitrary value)
     82            maxHeight -= 2*(ARROW_ICON_HEIGHT+2);
     83
     84            if (maxHeight > 0) {
     85                result = (maxHeight/itemHeight)-2;
     86            }
     87        }
     88        return result;
     89    }
     90
    5391    /**
    5492     * Registers a menu to be scrolled with the default number of items to
     
    177215     */
    178216    public MenuScroller(JMenu menu) {
    179         this(menu, 15);
     217        this(menu, computeScrollCount(menu, 30));
    180218    }
    181219
     
    188226     */
    189227    public MenuScroller(JPopupMenu menu) {
    190         this(menu, 15);
     228        this(menu, computeScrollCount(menu, 30));
    191229    }
    192230
     
    594632        @Override
    595633        public int getIconHeight() {
    596             return 10;
     634            return ARROW_ICON_HEIGHT;
    597635        }
    598636    }
Note: See TracChangeset for help on using the changeset viewer.