]> gitweb.fperrin.net Git - GpsPrune.git/blob - src/tim/prune/function/sew/SplitSegmentsFunction.java
Version 20.4, May 2021
[GpsPrune.git] / src / tim / prune / function / sew / SplitSegmentsFunction.java
1 package tim.prune.function.sew;
2
3 import javax.swing.JOptionPane;
4
5 import tim.prune.App;
6 import tim.prune.I18nManager;
7 import tim.prune.UpdateMessageBroker;
8 import tim.prune.data.DataPoint;
9 import tim.prune.function.DistanceTimeLimitFunction;
10 import tim.prune.undo.UndoSplitSegments;
11
12 /**
13  * Function to split a track into segments using
14  * either a distance limit or a time limit
15  */
16 public class SplitSegmentsFunction extends DistanceTimeLimitFunction
17 {
18         /**
19          * Constructor
20          */
21         public SplitSegmentsFunction(App inApp) {
22                 super(inApp, false);
23         }
24
25         /**
26          * @return name key
27          */
28         public String getNameKey() {
29                 return "function.splitsegments";
30         }
31
32
33         /**
34          * The dialog has been completed and OK pressed, so do the split
35          */
36         protected void performFunction()
37         {
38                 // Split either by distance or time
39                 final int timeLimitSeconds = getTimeLimitInSeconds();
40                 final boolean splitByTime = (timeLimitSeconds > 0);
41                 final double distLimitRadians = getDistanceLimitRadians();
42                 final boolean splitByDistance = (distLimitRadians > 0.0);
43                 if (!splitByTime && !splitByDistance) {
44                         return; // neither option selected
45                 }
46
47                 // Make undo object
48                 UndoSplitSegments undo = new UndoSplitSegments(_app.getTrackInfo().getTrack());
49                 final int numPoints = _app.getTrackInfo().getTrack().getNumPoints();
50                 DataPoint currPoint = null, prevPoint = null;
51                 int numSplitsMade = 0;
52
53                 // Now actually do it, looping through the points in the track
54                 for (int i=0; i<numPoints; i++)
55                 {
56                         currPoint = _app.getTrackInfo().getTrack().getPoint(i);
57                         if (!currPoint.isWaypoint())
58                         {
59                                 boolean splitHere = (prevPoint != null)
60                                         && ((splitByDistance && DataPoint.calculateRadiansBetween(prevPoint, currPoint) > distLimitRadians)
61                                                 || (splitByTime && currPoint.hasTimestamp() && prevPoint.hasTimestamp()
62                                                         && currPoint.getTimestamp().getSecondsSince(prevPoint.getTimestamp()) > timeLimitSeconds));
63                                 if (splitHere && !currPoint.getSegmentStart())
64                                 {
65                                         currPoint.setSegmentStart(true);
66                                         numSplitsMade++;
67                                 }
68                                 prevPoint = currPoint;
69                         }
70                 }
71
72                 if (numSplitsMade > 0)
73                 {
74                         _app.completeFunction(undo, I18nManager.getTextWithNumber("confirm.splitsegments", numSplitsMade));
75                         UpdateMessageBroker.informSubscribers();
76                         _dialog.dispose();
77                 }
78                 else
79                 {
80                         // Complain that no split was made
81                         JOptionPane.showMessageDialog(_parentFrame, I18nManager.getText("error.tracksplit.nosplit"),
82                                 I18nManager.getText("error.function.noop.title"), JOptionPane.WARNING_MESSAGE);
83                 }
84         }
85 }