]> gitweb.fperrin.net Git - GpsPrune.git/blob - src/tim/prune/undo/UndoDeleteOperation.java
73d9c4f3c50e984662822401b095fcefc94f3438
[GpsPrune.git] / src / tim / prune / undo / UndoDeleteOperation.java
1 package tim.prune.undo;
2
3 import tim.prune.data.TrackInfo;
4
5 /**
6  * Abstract class to hold the selection handling required by all
7  * Undo operations which have undeleted something
8  */
9 public abstract class UndoDeleteOperation implements UndoOperation
10 {
11         /** Flag to remember whether the deleted point was at the beginning or end of the selected range */
12         private boolean _isAtBoundaryOfSelectedRange = false;
13
14         /**
15          * @param inAtBoundary true if deleted point was at the beginning or end of the selected range
16          */
17         public void setAtBoundaryOfSelectedRange(boolean inAtBoundary)
18         {
19                 _isAtBoundaryOfSelectedRange = inAtBoundary;
20         }
21
22         /**
23          * Modify the current point/range selection after the delete operation is undone
24          * @param inTrackInfo track info object
25          * @param inStartIndex start index of reinserted range
26          * @param inEndIndex end index of reinserted range
27          */
28         protected void modifySelection(TrackInfo inTrackInfo, int inStartIndex, int inEndIndex)
29         {
30                 final int numPointsInserted = inEndIndex - inStartIndex + 1;
31                 // See if there is a currently selected point, if so does it need to be modified
32                 final int currentPoint = inTrackInfo.getSelection().getCurrentPointIndex();
33                 if (currentPoint >= inStartIndex)
34                 {
35                         inTrackInfo.selectPoint(currentPoint + numPointsInserted);
36                 }
37                 // Same for currently selected range
38                 int rangeStart = inTrackInfo.getSelection().getStart();
39                 int rangeEnd   = inTrackInfo.getSelection().getEnd();
40                 // Was the deleted point at the start or end of the selected range?
41                 final boolean wasAtStart = numPointsInserted == 1 && inStartIndex == rangeStart && _isAtBoundaryOfSelectedRange;
42                 final boolean wasAtEnd   = numPointsInserted == 1 && inStartIndex == (rangeEnd+1) && _isAtBoundaryOfSelectedRange;
43                 if (rangeEnd >= inStartIndex && rangeEnd > rangeStart || wasAtStart || wasAtEnd)
44                 {
45                         rangeEnd += numPointsInserted;
46                         if (rangeStart >= inStartIndex) {
47                                 rangeStart += numPointsInserted;
48                         }
49                         // Extend selection if the deleted point was at the start or end
50                         if (wasAtStart) {
51                                 rangeStart--;
52                         }
53                         inTrackInfo.getSelection().selectRange(rangeStart, rangeEnd);
54                 }
55         }
56 }