]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/function/ConvertNamesToTimes.java
2368dfd11fea277656b4c32eb8c826ad3bdc149f
[GpsPrune.git] / tim / prune / function / ConvertNamesToTimes.java
1 package tim.prune.function;
2
3 import tim.prune.App;
4 import tim.prune.DataSubscriber;
5 import tim.prune.GenericFunction;
6 import tim.prune.I18nManager;
7 import tim.prune.UpdateMessageBroker;
8 import tim.prune.data.DataPoint;
9 import tim.prune.data.Field;
10 import tim.prune.data.TimestampUtc;
11 import tim.prune.data.Track;
12 import tim.prune.undo.UndoConvertNamesToTimes;
13
14 /**
15  * Class to provide the function to convert waypoint names to timestamps
16  */
17 public class ConvertNamesToTimes extends GenericFunction
18 {
19         /**
20          * Constructor
21          * @param inApp application object for callback
22          */
23         public ConvertNamesToTimes(App inApp)
24         {
25                 super(inApp);
26         }
27
28         /** Get the name key */
29         public String getNameKey() {
30                 return "function.convertnamestotimes";
31         }
32
33         /**
34          * Begin the function
35          */
36         public void begin()
37         {
38                 int selStart = _app.getTrackInfo().getSelection().getStart();
39                 int selEnd = _app.getTrackInfo().getSelection().getEnd();
40                 final Track track = _app.getTrackInfo().getTrack();
41                 if (!track.hasData(Field.WAYPT_NAME, selStart, selEnd))
42                 {
43                         _app.showErrorMessage(getNameKey(), "error.convertnamestotimes.nonames");
44                         return;
45                 }
46                 UndoConvertNamesToTimes undo = new UndoConvertNamesToTimes(_app.getTrackInfo());
47                 int numConverted = 0;
48                 // Loop over all points in selection
49                 for (int i=selStart; i<=selEnd; i++)
50                 {
51                         DataPoint point = track.getPoint(i);
52                         if (point.isWaypoint())
53                         {
54                                 TimestampUtc tstamp = new TimestampUtc(point.getWaypointName());
55                                 if (tstamp.isValid())
56                                 {
57                                         // timestamp could be parsed!
58                                         point.setFieldValue(Field.TIMESTAMP, point.getWaypointName(), false);
59                                         // set waypoint name to nothing (track point)
60                                         point.setFieldValue(Field.WAYPT_NAME, null, false);
61                                         // increment counter
62                                         numConverted++;
63                                 }
64                         }
65                 }
66                 if (numConverted > 0)
67                 {
68                         _app.getTrackInfo().getTrack().requestRescale();
69                         UpdateMessageBroker.informSubscribers(DataSubscriber.DATA_EDITED);
70                         _app.completeFunction(undo, I18nManager.getText("confirm.convertnamestotimes"));
71                 }
72         }
73
74 }