]> gitweb.fperrin.net Git - GpsPrune.git/blobdiff - src/tim/prune/function/distance/DistanceFunction.java
Version 20.4, May 2021
[GpsPrune.git] / src / tim / prune / function / distance / DistanceFunction.java
index 5fe6e7e7d9e307d901ad25c23b2a4ea021acc3d3..f37332420e5b662f2d472d4e76e1132725826cb3 100644 (file)
@@ -77,8 +77,9 @@ public class DistanceFunction extends GenericFunction
                }
                _fromModel.init(pointList);
                _distModel.init(pointList);
-               _pointTable.getSelectionModel().setSelectionInterval(0, 0);
-               _distModel.recalculate(0);
+               final int pointIndex = getPointIndex(pointList, _app.getTrackInfo());
+               _pointTable.getSelectionModel().setSelectionInterval(pointIndex, pointIndex);
+               _distModel.recalculate(pointIndex);
                _dialog.setVisible(true);
        }
 
@@ -114,12 +115,7 @@ public class DistanceFunction extends GenericFunction
                // second table for distances
                _distModel = new DistanceTableModel();
                JTable distTable = new JTable(_distModel);
-               // Use reflection to call distTable.setAutoCreateRowSorter(true) which is new with Java 1.6
-               try {
-                       Class<?> d = Class.forName("javax.swing.JTable");
-                       d.getDeclaredMethod("setAutoCreateRowSorter", new Class[]{Boolean.TYPE}).invoke(distTable, Boolean.TRUE);
-               }
-               catch (Exception e) {}
+               distTable.setAutoCreateRowSorter(true);
                scrollPane = new JScrollPane(distTable);
                scrollPane.setPreferredSize(new Dimension(200, 250));
                mainPanel.add(scrollPane);
@@ -168,4 +164,25 @@ public class DistanceFunction extends GenericFunction
                }
                return pointList;
        }
+
+       /**
+        * Find the point to select from the given point list
+        * @param pointList list of points
+        * @param inTrackInfo current track info to get selected point (if any)
+        * @return index of point to be selected
+        */
+       private static int getPointIndex(ArrayList<DataPoint> pointList, TrackInfo inTrackInfo)
+       {
+               DataPoint currPoint = inTrackInfo.getCurrentPoint();
+               if (currPoint != null && currPoint.isWaypoint())
+               {
+                       // Currently selected point is a waypoint, so select this one for convenience
+                       for (int i=0; i<pointList.size(); i++) {
+                               if (pointList.get(i) == currPoint) {
+                                       return i;
+                               }
+                       }
+               }
+               return 0;
+       }
 }