]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/save/UpDownToggler.java
274bfe98d59358c0ad0ef75e3fc14268625c9a69
[GpsPrune.git] / tim / prune / save / UpDownToggler.java
1 package tim.prune.save;
2
3 import javax.swing.JButton;
4 import javax.swing.ListSelectionModel;
5 import javax.swing.event.ListSelectionEvent;
6 import javax.swing.event.ListSelectionListener;
7
8
9 /**
10  * Class to enable and disable a pair of up and down buttons
11  */
12 public class UpDownToggler implements ListSelectionListener
13 {
14         private JButton _upButton = null;
15         private JButton _downButton = null;
16         private int _maxIndex = 0;
17
18         /**
19          * Constructor giving buttons and size
20          * @param inUpButton up button
21          * @param inDownButton down button
22          * @param inListSize size of list
23          */
24         public UpDownToggler(JButton inUpButton, JButton inDownButton, int inListSize)
25         {
26                 _upButton = inUpButton;
27                 _downButton = inDownButton;
28                 _maxIndex = inListSize - 1;
29         }
30
31
32         /**
33          * list selection has changed
34          */
35         public void valueChanged(ListSelectionEvent e)
36         {
37                 ListSelectionModel lsm = (ListSelectionModel) e.getSource();
38                 if (lsm.isSelectionEmpty())
39                 {
40                         // no rows are selected
41                         _upButton.setEnabled(false);
42                         _downButton.setEnabled(false);
43                 }
44                 else
45                 {
46                         // single row is selected
47                         int row = lsm.getMinSelectionIndex();
48                         _upButton.setEnabled(row > 0);
49                         _downButton.setEnabled(row >= 0 && row < _maxIndex);
50                 }
51         }
52 }