]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/function/ConvertNamesToTimes.java
c0286ab6ca3419479454697f45e324f48ca53c00
[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.Timestamp;
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                                 Timestamp tstamp = new Timestamp(point.getWaypointName());
55                                 if (tstamp.isValid()) {
56                                         // timestamp could be parsed!
57                                         point.setFieldValue(Field.TIMESTAMP, point.getWaypointName(), false);
58                                         // set waypoint name to nothing (track point)
59                                         point.setFieldValue(Field.WAYPT_NAME, null, false);
60                                         // increment counter
61                                         numConverted++;
62                                 }
63                         }
64                 }
65                 if (numConverted > 0)
66                 {
67                         _app.getTrackInfo().getTrack().requestRescale();
68                         UpdateMessageBroker.informSubscribers(DataSubscriber.DATA_EDITED);
69                         _app.completeFunction(undo, I18nManager.getText("confirm.convertnamestotimes"));
70                 }
71         }
72
73 }