]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/save/UpDownToggler.java
Version 4, January 2008
[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 = 2;
17
18         /**
19          * Constructor giving buttons to enable/disable
20          * @param inUpButton up button
21          * @param inDownButton down button
22          */
23         public UpDownToggler(JButton inUpButton, JButton inDownButton)
24         {
25                 _upButton = inUpButton;
26                 _downButton = inDownButton;
27         }
28
29         /**
30          * Set the list size
31          * @param inListSize number of items in list
32          */
33         public void setListSize(int inListSize)
34         {
35                 _maxIndex = inListSize - 1;
36         }
37
38         /**
39          * list selection has changed
40          */
41         public void valueChanged(ListSelectionEvent e)
42         {
43                 ListSelectionModel lsm = (ListSelectionModel) e.getSource();
44                 if (lsm.isSelectionEmpty())
45                 {
46                         // no rows are selected
47                         _upButton.setEnabled(false);
48                         _downButton.setEnabled(false);
49                 }
50                 else
51                 {
52                         // single row is selected
53                         int row = lsm.getMinSelectionIndex();
54                         _upButton.setEnabled(row > 0);
55                         _downButton.setEnabled(row >= 0 && row < _maxIndex);
56                 }
57         }
58 }