]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/undo/UndoLoad.java
Version 1, September 2006
[GpsPrune.git] / tim / prune / undo / UndoLoad.java
1 package tim.prune.undo;\r
2 \r
3 import tim.prune.I18nManager;\r
4 import tim.prune.data.DataPoint;\r
5 import tim.prune.data.TrackInfo;\r
6 \r
7 /**\r
8  * Operation to undo a load operation\r
9  */\r
10 public class UndoLoad implements UndoOperation\r
11 {\r
12         private int _cropIndex = -1;\r
13         private int _numLoaded = -1;\r
14         private DataPoint[] _contents = null;\r
15         private String _previousFilename = null;\r
16 \r
17 \r
18         /**\r
19          * Constructor for appending\r
20          * @param inIndex index number of crop point\r
21          * @param inNumLoaded number of points loaded\r
22          */\r
23         public UndoLoad(int inIndex, int inNumLoaded)\r
24         {\r
25                 _cropIndex = inIndex;\r
26                 _numLoaded = inNumLoaded;\r
27                 _contents = null;\r
28                 _previousFilename = null;\r
29         }\r
30 \r
31 \r
32         /**\r
33          * Constructor for replacing\r
34          * @param inOldTrack track being replaced\r
35          * @param inNumLoaded number of points loaded\r
36          */\r
37         public UndoLoad(TrackInfo inOldTrackInfo, int inNumLoaded)\r
38         {\r
39                 _cropIndex = -1;\r
40                 _numLoaded = inNumLoaded;\r
41                 _contents = inOldTrackInfo.getTrack().cloneContents();\r
42                 if (inOldTrackInfo.getFileInfo().getNumFiles() == 1)\r
43                         _previousFilename = inOldTrackInfo.getFileInfo().getFilename();\r
44         }\r
45 \r
46 \r
47         /**\r
48          * @return description of operation including number of points loaded\r
49          */\r
50         public String getDescription()\r
51         {\r
52                 String desc = I18nManager.getText("undo.load");\r
53                 if (_numLoaded > 0)\r
54                         desc = desc + " (" + _numLoaded + ")";\r
55                 return desc;\r
56         }\r
57 \r
58 \r
59         /**\r
60          * Perform the undo operation on the given Track\r
61          * @param inTrackInfo TrackInfo object on which to perform the operation\r
62          */\r
63         public void performUndo(TrackInfo inTrackInfo) throws UndoException\r
64         {\r
65                 // remove file from fileinfo\r
66                 inTrackInfo.getFileInfo().removeFile();\r
67                 if (_previousFilename != null)\r
68                 {\r
69                         inTrackInfo.getFileInfo().setFile(_previousFilename);\r
70                 }\r
71                 // Crop / replace\r
72                 if (_contents == null)\r
73                 {\r
74                         // crop track to previous size\r
75                         inTrackInfo.getTrack().cropTo(_cropIndex);\r
76                 }\r
77                 else\r
78                 {\r
79                         // replace track contents with old\r
80                         if (!inTrackInfo.getTrack().replaceContents(_contents))\r
81                         {\r
82                                 throw new UndoException(getDescription());\r
83                         }\r
84                 }\r
85                 // clear selection\r
86                 inTrackInfo.getSelection().clearAll();\r
87         }\r
88 }