]> gitweb.fperrin.net Git - GpsPrune.git/blobdiff - src/tim/prune/undo/UndoConvertNamesToTimes.java
Moved source into separate src directory due to popular request
[GpsPrune.git] / src / tim / prune / undo / UndoConvertNamesToTimes.java
diff --git a/src/tim/prune/undo/UndoConvertNamesToTimes.java b/src/tim/prune/undo/UndoConvertNamesToTimes.java
new file mode 100644 (file)
index 0000000..dc0ffbe
--- /dev/null
@@ -0,0 +1,81 @@
+package tim.prune.undo;\r
+\r
+import tim.prune.I18nManager;\r
+import tim.prune.UpdateMessageBroker;\r
+import tim.prune.data.DataPoint;\r
+import tim.prune.data.Field;\r
+import tim.prune.data.Track;\r
+import tim.prune.data.TrackInfo;\r
+\r
+/**\r
+ * Operation to undo a conversion from names to times\r
+ */\r
+public class UndoConvertNamesToTimes implements UndoOperation\r
+{\r
+       /** Start and end indices of section */\r
+       private int _startIndex, _endIndex;\r
+       /** Waypoint names before operation */\r
+       private String[] _waypointNames = null;\r
+       /** Timestamp strings before operation */\r
+       private String[] _timestamps = null;\r
+\r
+       /**\r
+        * Constructor\r
+        * @param inTrackInfo track info object to copy values from\r
+        */\r
+       public UndoConvertNamesToTimes(TrackInfo inTrackInfo)\r
+       {\r
+               _startIndex = inTrackInfo.getSelection().getStart();\r
+               _endIndex = inTrackInfo.getSelection().getEnd();\r
+               final int numPoints = _endIndex - _startIndex + 1;\r
+               _waypointNames = new String[numPoints];\r
+               _timestamps = new String[numPoints];\r
+               // Loop over points in selection, and copy names and timestamps\r
+               for (int i=_startIndex; i<=_endIndex; i++)\r
+               {\r
+                       DataPoint point = inTrackInfo.getTrack().getPoint(i);\r
+                       if (point.isWaypoint())\r
+                       {\r
+                               _waypointNames[i-_startIndex] = point.getWaypointName();\r
+                               _timestamps[i-_startIndex] = point.getFieldValue(Field.TIMESTAMP);\r
+                       }\r
+               }\r
+       }\r
+\r
+\r
+       /**\r
+        * @return description of operation\r
+        */\r
+       public String getDescription()\r
+       {\r
+               return I18nManager.getText("undo.convertnamestotimes");\r
+       }\r
+\r
+\r
+       /**\r
+        * Perform the undo operation on the given Track\r
+        * @param inTrackInfo TrackInfo object on which to perform the operation\r
+        */\r
+       public void performUndo(TrackInfo inTrackInfo) throws UndoException\r
+       {\r
+               // Sanity check\r
+               Track track = inTrackInfo.getTrack();\r
+               if (track.getNumPoints() <= _endIndex || _endIndex <= _startIndex) {\r
+                       throw new UndoException("Cannot undo conversion, track length doesn't match");\r
+               }\r
+               // Loop over points in selection and replace names and timestamps\r
+               for (int i=_startIndex; i<=_endIndex; i++)\r
+               {\r
+                       String storedName = _waypointNames[i-_startIndex];\r
+                       if (storedName != null)\r
+                       {\r
+                               // point had a name before the operation, so might have been converted\r
+                               DataPoint point = track.getPoint(i);\r
+                               point.setFieldValue(Field.WAYPT_NAME, storedName, true);\r
+                               point.setFieldValue(Field.TIMESTAMP, _timestamps[i-_startIndex], true);\r
+                       }\r
+               }\r
+               track.requestRescale();\r
+               UpdateMessageBroker.informSubscribers();\r
+       }\r
+}\r