]> gitweb.fperrin.net Git - GpsPrune.git/blobdiff - tim/prune/gui/SelectorDisplay.java
Version 11, August 2010
[GpsPrune.git] / tim / prune / gui / SelectorDisplay.java
index 38e41652fca02918fcbeb3209ac34c748ba95ace..ed244d0bdbff57816492f5d23d7a6407e4151ce4 100644 (file)
@@ -4,6 +4,7 @@ import java.awt.BorderLayout;
 import java.awt.Component;
 import java.awt.Dimension;
 import java.awt.Font;
+import java.awt.GridLayout;
 import java.awt.event.AdjustmentEvent;
 import java.awt.event.AdjustmentListener;
 
@@ -15,14 +16,13 @@ import javax.swing.JList;
 import javax.swing.JPanel;
 import javax.swing.JScrollBar;
 import javax.swing.JScrollPane;
+import javax.swing.ListSelectionModel;
 import javax.swing.border.EtchedBorder;
 import javax.swing.event.ListSelectionEvent;
 import javax.swing.event.ListSelectionListener;
 
 import tim.prune.DataSubscriber;
 import tim.prune.I18nManager;
-import tim.prune.data.DataPoint;
-import tim.prune.data.Photo;
 import tim.prune.data.TrackInfo;
 
 /**
@@ -92,30 +92,41 @@ public class SelectorDisplay extends GenericDisplay
 
                // Add panel for waypoints / photos
                JPanel listsPanel = new JPanel();
-               listsPanel.setLayout(new BoxLayout(listsPanel, BoxLayout.Y_AXIS));
+               listsPanel.setLayout(new GridLayout(0, 1));
                listsPanel.setBorder(BorderFactory.createCompoundBorder(
                        BorderFactory.createEtchedBorder(EtchedBorder.LOWERED), BorderFactory.createEmptyBorder(3, 3, 3, 3))
                );
                _waypointListModel = new WaypointListModel(_trackInfo.getTrack());
                _waypointList = new JList(_waypointListModel);
                _waypointList.setVisibleRowCount(NUM_LIST_ENTRIES);
+               _waypointList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
                _waypointList.addListSelectionListener(new ListSelectionListener() {
                        public void valueChanged(ListSelectionEvent e)
                        {
                                if (!e.getValueIsAdjusting()) selectWaypoint(_waypointList.getSelectedIndex());
                        }});
-               listsPanel.add(new JLabel(I18nManager.getText("details.waypointsphotos.waypoints")));
-               listsPanel.add(new JScrollPane(_waypointList));
+               JPanel waypointListPanel = new JPanel();
+               waypointListPanel.setLayout(new BorderLayout());
+               waypointListPanel.add(new JLabel(I18nManager.getText("details.waypointsphotos.waypoints")), BorderLayout.NORTH);
+               waypointListPanel.add(new JScrollPane(_waypointList), BorderLayout.CENTER);
+               listsPanel.add(waypointListPanel);
+               // photo list
                _photoListModel = new PhotoListModel(_trackInfo.getPhotoList());
                _photoList = new JList(_photoListModel);
                _photoList.setVisibleRowCount(NUM_LIST_ENTRIES);
+               _photoList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
                _photoList.addListSelectionListener(new ListSelectionListener() {
                        public void valueChanged(ListSelectionEvent e)
                        {
-                               if (!e.getValueIsAdjusting()) selectPhoto(_photoList.getSelectedIndex());
+                               if (!e.getValueIsAdjusting()) {
+                                       selectPhoto(_photoList.getSelectedIndex());
+                               }
                        }});
-               listsPanel.add(new JLabel(I18nManager.getText("details.waypointsphotos.photos")));
-               listsPanel.add(new JScrollPane(_photoList));
+               JPanel photoListPanel = new JPanel();
+               photoListPanel.setLayout(new BorderLayout());
+               photoListPanel.add(new JLabel(I18nManager.getText("details.waypointsphotos.photos")), BorderLayout.NORTH);
+               photoListPanel.add(new JScrollPane(_photoList), BorderLayout.CENTER);
+               listsPanel.add(photoListPanel);
                listsPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
 
                // add the controls to the main panel
@@ -123,10 +134,11 @@ public class SelectorDisplay extends GenericDisplay
                mainPanel.add(Box.createVerticalStrut(5));
                mainPanel.add(_scroller);
                mainPanel.add(Box.createVerticalStrut(5));
-               mainPanel.add(listsPanel);
 
                // add the main panel at the top
                add(mainPanel, BorderLayout.NORTH);
+               // and lists in the centre
+               add(listsPanel, BorderLayout.CENTER);
                // set preferred width to be small
                setPreferredSize(new Dimension(100, 100));
        }
@@ -140,7 +152,7 @@ public class SelectorDisplay extends GenericDisplay
        {
                if (_track != null && !_ignoreScrollEvents)
                {
-                       _trackInfo.getSelection().selectPoint(inValue);
+                       _trackInfo.selectPoint(inValue);
                }
        }
 
@@ -208,7 +220,7 @@ public class SelectorDisplay extends GenericDisplay
                }
                else
                {
-                       _scroller.setMaximum(_track.getNumPoints() + SCROLLBAR_INTERVAL);
+                       _scroller.setMaximum(_track.getNumPoints() -1 + SCROLLBAR_INTERVAL);
                        if (currentPointIndex >= 0)
                                _scroller.setValue(currentPointIndex);
                        _scroller.setEnabled(true);
@@ -230,29 +242,27 @@ public class SelectorDisplay extends GenericDisplay
                if (_waypointList.getSelectedIndex() >= 0)
                {
                        if (_trackInfo.getCurrentPoint() == null
+                        || _waypointList.getSelectedIndex() >= _waypointListModel.getSize()
                         || !_waypointListModel.getWaypoint(_waypointList.getSelectedIndex()).equals(_trackInfo.getCurrentPoint()))
                        {
                                // point is selected in list but different from current point - deselect
                                _waypointList.clearSelection();
                        }
                }
-               // Do the same for the photos
-               if (_photoList.getSelectedIndex() >= 0)
+               // Make sure correct photo is selected
+               if (_photoListModel.getSize() > 0)
                {
-                       DataPoint trackPoint = _trackInfo.getCurrentPoint();
-                       Photo selectedPhoto = _photoListModel.getPhoto(_photoList.getSelectedIndex());
-                       // Get selected Photo, if it's still there
-                       DataPoint photoPoint = null;
-                       if (selectedPhoto != null) {
-                               photoPoint = _photoListModel.getPhoto(_photoList.getSelectedIndex()).getDataPoint();
-                       }
-                       // Compare selected photo with selected point
-                       if ( (photoPoint != null && (trackPoint == null || !photoPoint.equals(trackPoint)))
-                               || (_trackInfo.getSelection().getCurrentPhotoIndex() < 0) )
+                       int photoIndex = _trackInfo.getSelection().getCurrentPhotoIndex();
+                       int listSelection = _photoList.getSelectedIndex();
+                       // Change listbox selection if indexes not equal
+                       if (listSelection != photoIndex)
                        {
-                               // photo is selected in list but different from current point - deselect
-                               _photoList.clearSelection();
-                               _trackInfo.getSelection().deselectPhoto();
+                               if (photoIndex < 0) {
+                                       _photoList.clearSelection();
+                               }
+                               else {
+                                       _photoList.setSelectedIndex(photoIndex);
+                               }
                        }
                }
        }