]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/undo/UndoDeleteFieldValues.java
Version 11, August 2010
[GpsPrune.git] / tim / prune / undo / UndoDeleteFieldValues.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 the deletion of field values\r
12  */\r
13 public class UndoDeleteFieldValues implements UndoOperation\r
14 {\r
15         /** Start and end indices of section */\r
16         private int _startIndex, _endIndex;\r
17         /** Field to be deleted */\r
18         private Field _field = null;\r
19         /** Field values before operation */\r
20         private String[] _fieldValues = null;\r
21 \r
22         /**\r
23          * Constructor\r
24          * @param inTrackInfo track info object to copy values from\r
25          * @param inField field to delete\r
26          */\r
27         public UndoDeleteFieldValues(TrackInfo inTrackInfo, Field inField)\r
28         {\r
29                 _startIndex = inTrackInfo.getSelection().getStart();\r
30                 _endIndex = inTrackInfo.getSelection().getEnd();\r
31                 final int numPoints = _endIndex - _startIndex + 1;\r
32                 _fieldValues = new String[numPoints];\r
33                 _field = inField;\r
34                 // Loop over points in selection, and copy field values\r
35                 for (int i=_startIndex; i<=_endIndex; i++)\r
36                 {\r
37                         DataPoint point = inTrackInfo.getTrack().getPoint(i);\r
38                         _fieldValues[i-_startIndex] = point.getFieldValue(inField);\r
39                 }\r
40         }\r
41 \r
42 \r
43         /**\r
44          * @return description of operation\r
45          */\r
46         public String getDescription()\r
47         {\r
48                 return I18nManager.getText("undo.deletefieldvalues");\r
49         }\r
50 \r
51 \r
52         /**\r
53          * Perform the undo operation on the given Track\r
54          * @param inTrackInfo TrackInfo object on which to perform the operation\r
55          */\r
56         public void performUndo(TrackInfo inTrackInfo) throws UndoException\r
57         {\r
58                 // Sanity check\r
59                 Track track = inTrackInfo.getTrack();\r
60                 if (track.getNumPoints() <= _endIndex || _endIndex <= _startIndex) {\r
61                         throw new UndoException("Cannot undo conversion, track length doesn't match");\r
62                 }\r
63                 // Loop over points in selection and replace field values\r
64                 for (int i=_startIndex; i<=_endIndex; i++)\r
65                 {\r
66                         String storedValue = _fieldValues[i-_startIndex];\r
67                         if (storedValue != null) {\r
68                                 track.getPoint(i).setFieldValue(_field, storedValue, true);\r
69                         }\r
70                 }\r
71                 track.requestRescale();\r
72                 inTrackInfo.getSelection().markInvalid();\r
73                 UpdateMessageBroker.informSubscribers();\r
74         }\r
75 }\r