Ignore:
Timestamp:
2009-11-03T21:41:37+01:00 (14 years ago)
Author:
bastiK
Message:

improved Toolbar to the left:

  • removed strange left margin (in Metal L&F, see #3721: josm-default.png)
  • show scroll-buttons only if necessary
  • allow mousewheel scrolling
File:
1 edited

Legend:

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

    r1677 r2391  
    99import java.awt.event.ActionEvent;
    1010import java.awt.event.ActionListener;
     11import java.awt.event.ComponentAdapter;
     12import java.awt.event.ComponentEvent;
    1113import java.awt.event.MouseAdapter;
    1214import java.awt.event.MouseEvent;
     15
     16import java.util.ArrayList;
     17import java.util.List;
    1318
    1419import javax.swing.JButton;
     
    6267    private JViewport vp = new JViewport();
    6368    private JComponent component = null;
    64 
    65     private Timer timer = new Timer(200, new ActionListener() {
     69   
     70    private List<JButton> buttons = new ArrayList<JButton>();
     71
     72    private Timer timer = new Timer(100, new ActionListener() {
    6673        public void actionPerformed(ActionEvent arg0) {
    6774            ScrollViewport.this.scroll();
     
    7885    public ScrollViewport(int direction) {
    7986        setLayout(new BorderLayout());
    80 
     87       
    8188        JButton button;
    8289
     
    8895            button.setIcon(ImageProvider.get("svpUp"));
    8996            add(button, BorderLayout.NORTH);
     97            buttons.add(button);
    9098        }
    9199
     
    97105            button.setIcon(ImageProvider.get("svpDown"));
    98106            add(button, BorderLayout.SOUTH);
     107            buttons.add(button);
    99108        }
    100109
     
    106115            button.setIcon(ImageProvider.get("svpLeft"));
    107116            add(button, BorderLayout.WEST);
     117            buttons.add(button);
    108118        }
    109119
     
    115125            button.setIcon(ImageProvider.get("svpRight"));
    116126            add(button, BorderLayout.EAST);
     127            buttons.add(button);
    117128        }
    118129
    119130        add(vp, BorderLayout.CENTER);
    120131
     132        this.addComponentListener(new ComponentAdapter() {
     133            @Override public void  componentResized(ComponentEvent e) {
     134                showOrHideButtons();
     135            }
     136        });
     137
     138        showOrHideButtons();
     139       
    121140        timer.setRepeats(true);
    122141        timer.setInitialDelay(400);
     
    133152        Rectangle viewRect = vp.getViewRect();
    134153
    135         int delta;
    136         int newY = 0;
    137         int newX = 0;
     154        int deltaX = 0;
     155        int deltaY = 0;
    138156
    139157        if (direction < LEFT_DIRECTION) {
    140             newX = viewRect.x;
    141             delta = viewRect.height * 4 / 5;
     158            deltaY = viewRect.height * 2 / 7;
    142159        } else {
    143             newY = viewRect.y;
    144             delta = viewRect.width * 4 / 5;
     160            deltaX = viewRect.width * 2 / 7;
    145161        }
    146162
    147163        switch (direction) {
    148         case UP_DIRECTION :
    149             newY = viewRect.y - delta;
    150             if (newY < 0) {
    151                 newY = 0;
    152             }
    153             break;
    154         case DOWN_DIRECTION :
    155             newY = viewRect.y + delta;
    156             if (newY > compSize.height - viewRect.height) {
    157                 newY = compSize.height - viewRect.height;
    158             }
    159             break;
    160         case LEFT_DIRECTION :
    161             newX = viewRect.x - delta;
    162             if (newX < 0) {
    163                 newX = 0;
    164             }
    165             break;
    166         case RIGHT_DIRECTION :
    167             newX = viewRect.x + delta;
    168             if (newX > compSize.width - viewRect.width) {
    169                 newX = compSize.width - viewRect.width;
    170             }
    171             break;
    172         default :
    173             throw new IllegalStateException("Unknown direction : [0]" + direction);
     164            case UP_DIRECTION :
     165                deltaY *= -1;
     166                break;
     167            case LEFT_DIRECTION :
     168                deltaX *= -1;
     169                break;
     170        }
     171
     172        scroll(deltaX, deltaY);
     173    }
     174    public synchronized void scroll(int deltaX, int deltaY) {
     175        if (component == null) {
     176            return;
     177        }
     178        Dimension compSize = component.getSize();
     179        Rectangle viewRect = vp.getViewRect();
     180
     181        int newX = viewRect.x + deltaX;
     182        int newY = viewRect.y + deltaY;
     183
     184        if (newY < 0) {
     185            newY = 0;
     186        }
     187        if (newY > compSize.height - viewRect.height) {
     188            newY = compSize.height - viewRect.height;
     189        }
     190        if (newX < 0) {
     191            newX = 0;
     192        }
     193        if (newX > compSize.width - viewRect.width) {
     194            newX = compSize.width - viewRect.width;
    174195        }
    175196
     
    177198    }
    178199
     200    /**
     201     * Update the visibility of the buttons
     202     * Only show them if the Viewport is too small for the content.
     203     */
     204    public void showOrHideButtons() {
     205        boolean needButtons = vp.getViewSize().height > vp.getViewRect().height ||
     206                              vp.getViewSize().width > vp.getViewRect().width;
     207        for (JButton b : buttons) {
     208            b.setVisible(needButtons);
     209        }
     210    }
     211   
    179212    public Rectangle getViewRect() {
    180213        return vp.getViewRect();
Note: See TracChangeset for help on using the changeset viewer.