]> gitweb.fperrin.net Git - GpsPrune.git/blob - src/tim/prune/undo/UndoConvertNamesToTimes.java
dc0ffbeccce1290de451c9b2d6fe050a2ea84752
[GpsPrune.git] / src / tim / prune / undo / UndoConvertNamesToTimes.java
1 package tim.prune.undo;\r
2 \r
3 import tim.prune.I18nManager;\r
4 import tim.prune.UpdateMessageBroker;\r
5 import tim.prune.data.DataPoint;\r
6 import tim.prune.data.Field;\r
7 import tim.prune.data.Track;\r
8 import tim.prune.data.TrackInfo;\r
9 \r
10 /**\r
11  * Operation to undo a conversion from names to times\r
12  */\r
13 public class UndoConvertNamesToTimes implements UndoOperation\r
14 {\r
15         /** Start and end indices of section */\r
16         private int _startIndex, _endIndex;\r
17         /** Waypoint names before operation */\r
18         private String[] _waypointNames = null;\r
19         /** Timestamp strings before operation */\r
20         private String[] _timestamps = null;\r
21 \r
22         /**\r
23          * Constructor\r
24          * @param inTrackInfo track info object to copy values from\r
25          */\r
26         public UndoConvertNamesToTimes(TrackInfo inTrackInfo)\r
27         {\r
28                 _startIndex = inTrackInfo.getSelection().getStart();\r
29                 _endIndex = inTrackInfo.getSelection().getEnd();\r
30                 final int numPoints = _endIndex - _startIndex + 1;\r
31                 _waypointNames = new String[numPoints];\r
32                 _timestamps = new String[numPoints];\r
33                 // Loop over points in selection, and copy names and timestamps\r
34                 for (int i=_startIndex; i<=_endIndex; i++)\r
35                 {\r
36                         DataPoint point = inTrackInfo.getTrack().getPoint(i);\r
37                         if (point.isWaypoint())\r
38                         {\r
39                                 _waypointNames[i-_startIndex] = point.getWaypointName();\r
40                                 _timestamps[i-_startIndex] = point.getFieldValue(Field.TIMESTAMP);\r
41                         }\r
42                 }\r
43         }\r
44 \r
45 \r
46         /**\r
47          * @return description of operation\r
48          */\r
49         public String getDescription()\r
50         {\r
51                 return I18nManager.getText("undo.convertnamestotimes");\r
52         }\r
53 \r
54 \r
55         /**\r
56          * Perform the undo operation on the given Track\r
57          * @param inTrackInfo TrackInfo object on which to perform the operation\r
58          */\r
59         public void performUndo(TrackInfo inTrackInfo) throws UndoException\r
60         {\r
61                 // Sanity check\r
62                 Track track = inTrackInfo.getTrack();\r
63                 if (track.getNumPoints() <= _endIndex || _endIndex <= _startIndex) {\r
64                         throw new UndoException("Cannot undo conversion, track length doesn't match");\r
65                 }\r
66                 // Loop over points in selection and replace names and timestamps\r
67                 for (int i=_startIndex; i<=_endIndex; i++)\r
68                 {\r
69                         String storedName = _waypointNames[i-_startIndex];\r
70                         if (storedName != null)\r
71                         {\r
72                                 // point had a name before the operation, so might have been converted\r
73                                 DataPoint point = track.getPoint(i);\r
74                                 point.setFieldValue(Field.WAYPT_NAME, storedName, true);\r
75                                 point.setFieldValue(Field.TIMESTAMP, _timestamps[i-_startIndex], true);\r
76                         }\r
77                 }\r
78                 track.requestRescale();\r
79                 UpdateMessageBroker.informSubscribers();\r
80         }\r
81 }\r