Changeset 12073 in josm for trunk


Ignore:
Timestamp:
2017-05-06T17:58:11+02:00 (8 years ago)
Author:
michael2402
Message:

Show sidebar scroll buttons when sidebar content changes (buttons enabled, ...).

File:
1 edited

Legend:

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

    r10634 r12073  
    2121import org.openstreetmap.josm.tools.ImageProvider;
    2222
    23 /** A viewport with UP and DOWN arrow buttons, so that the user can make the
     23/**
     24 * A viewport with UP and DOWN arrow buttons, so that the user can make the
    2425 * content scroll.
     26 *
     27 * This should be used for long, vertical toolbars.
    2528 */
    2629public class ScrollViewport extends JPanel {
     
    3235    public static final int LEFT_DIRECTION = 4;
    3336    public static final int RIGHT_DIRECTION = 8;
     37    /**
     38     * Allow vertical scrolling
     39     */
    3440    public static final int VERTICAL_DIRECTION = UP_DIRECTION | DOWN_DIRECTION;
     41
     42    /**
     43     * Allow horizontal scrolling
     44     */
    3545    public static final int HORIZONTAL_DIRECTION = LEFT_DIRECTION | RIGHT_DIRECTION;
     46
     47    /**
     48     * Allow scrolling in both directions
     49     */
    3650    public static final int ALL_DIRECTION = HORIZONTAL_DIRECTION | VERTICAL_DIRECTION;
    3751
     
    7286    private int scrollDirection = NO_SCROLL;
    7387
     88    private final int allowedScrollDirections;
     89
     90    private final ComponentAdapter refreshButtonsOnResize = new ComponentAdapter() {
     91        @Override
     92        public void componentResized(ComponentEvent e) {
     93            showOrHideButtons();
     94        }
     95    };
     96
     97    /**
     98     * Create a new scroll viewport
     99     * @param c The component to display as content.
     100     * @param direction The direction to scroll.
     101     *        Should be one of {@link #VERTICAL_DIRECTION}, {@link #HORIZONTAL_DIRECTION}, {@link #ALL_DIRECTION}
     102     */
    74103    public ScrollViewport(JComponent c, int direction) {
    75104        this(direction);
     
    77106    }
    78107
     108    /**
     109     * Create a new scroll viewport
     110     * @param direction The direction to scroll.
     111     *        Should be one of {@link #VERTICAL_DIRECTION}, {@link #HORIZONTAL_DIRECTION}, {@link #ALL_DIRECTION}
     112     */
    79113    public ScrollViewport(int direction) {
    80         setLayout(new BorderLayout());
    81 
    82         JButton button;
     114        super(new BorderLayout());
     115        this.allowedScrollDirections = direction;
    83116
    84117        // UP
    85118        if ((direction & UP_DIRECTION) != 0) {
    86             button = new JButton();
    87             button.addMouseListener(new ScrollViewPortMouseListener(UP_DIRECTION));
    88             button.setPreferredSize(new Dimension(10, 10));
    89             button.setIcon(ImageProvider.get("svpUp"));
    90             add(button, BorderLayout.NORTH);
    91             buttons.add(button);
     119            addScrollButton(UP_DIRECTION, "svpUp", BorderLayout.NORTH);
    92120        }
    93121
    94122        // DOWN
    95123        if ((direction & DOWN_DIRECTION) != 0) {
    96             button = new JButton();
    97             button.addMouseListener(new ScrollViewPortMouseListener(DOWN_DIRECTION));
    98             button.setPreferredSize(new Dimension(10, 10));
    99             button.setIcon(ImageProvider.get("svpDown"));
    100             add(button, BorderLayout.SOUTH);
    101             buttons.add(button);
     124            addScrollButton(DOWN_DIRECTION, "svpDown", BorderLayout.SOUTH);
    102125        }
    103126
    104127        // LEFT
    105128        if ((direction & LEFT_DIRECTION) != 0) {
    106             button = new JButton();
    107             button.addMouseListener(new ScrollViewPortMouseListener(LEFT_DIRECTION));
    108             button.setPreferredSize(new Dimension(10, 10));
    109             button.setIcon(ImageProvider.get("svpLeft"));
    110             add(button, BorderLayout.WEST);
    111             buttons.add(button);
     129            addScrollButton(LEFT_DIRECTION, "svpLeft", BorderLayout.WEST);
    112130        }
    113131
    114132        // RIGHT
    115133        if ((direction & RIGHT_DIRECTION) != 0) {
    116             button = new JButton();
    117             button.addMouseListener(new ScrollViewPortMouseListener(RIGHT_DIRECTION));
    118             button.setPreferredSize(new Dimension(10, 10));
    119             button.setIcon(ImageProvider.get("svpRight"));
    120             add(button, BorderLayout.EAST);
    121             buttons.add(button);
     134            addScrollButton(RIGHT_DIRECTION, "svpRight", BorderLayout.EAST);
    122135        }
    123136
    124137        add(vp, BorderLayout.CENTER);
    125138
    126         this.addComponentListener(new ComponentAdapter() {
    127             @Override public void componentResized(ComponentEvent e) {
    128                 showOrHideButtons();
    129             }
    130         });
     139        this.addComponentListener(refreshButtonsOnResize);
    131140
    132141        showOrHideButtons();
     
    136145    }
    137146
     147    private void addScrollButton(int direction, String icon, String borderLayoutPosition) {
     148        JButton button = new JButton();
     149        button.addMouseListener(new ScrollViewPortMouseListener(direction));
     150        button.setPreferredSize(new Dimension(10, 10));
     151        button.setIcon(ImageProvider.get(icon));
     152        add(button, borderLayoutPosition);
     153        buttons.add(button);
     154    }
     155
     156    /**
     157     * Scrolls in the currently selected scroll direction.
     158     */
    138159    public synchronized void scroll() {
    139160        int direction = scrollDirection;
     
    166187    }
    167188
     189    /**
     190     * Scrolls by the given offset
     191     * @param deltaX offset x
     192     * @param deltaY offset y
     193     */
    168194    public synchronized void scroll(int deltaX, int deltaY) {
    169195        if (component == null)
     
    196222     */
    197223    public void showOrHideButtons() {
    198         boolean needButtons = vp.getViewSize().height > vp.getViewRect().height ||
    199         vp.getViewSize().width > vp.getViewRect().width;
     224        boolean needButtons = false;
     225        if ((allowedScrollDirections & VERTICAL_DIRECTION) != 0) {
     226            needButtons |= getViewSize().height > getViewRect().height;
     227        }
     228        if ((allowedScrollDirections & HORIZONTAL_DIRECTION) != 0) {
     229            needButtons |= getViewSize().width > getViewRect().width;
     230        }
    200231        for (JButton b : buttons) {
    201232            b.setVisible(needButtons);
     
    215246    }
    216247
     248    @Override
     249    public Dimension getPreferredSize() {
     250        return vp.getPreferredSize();
     251    }
     252
     253    @Override
     254    public Dimension getMinimumSize() {
     255        return vp.getMinimumSize();
     256    }
     257
     258    /**
     259     * Sets the component to be used as content.
     260     * @param c The component
     261     */
    217262    public void add(JComponent c) {
    218263        vp.removeAll();
     264        if (this.component != null) {
     265            this.component.removeComponentListener(refreshButtonsOnResize);
     266        }
    219267        this.component = c;
     268        c.addComponentListener(refreshButtonsOnResize);
    220269        vp.add(c);
    221270    }
Note: See TracChangeset for help on using the changeset viewer.