1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
|
package tim.prune.function;
import tim.prune.App;
import tim.prune.I18nManager;
import tim.prune.cmd.Command;
import tim.prune.cmd.PointFlag;
import tim.prune.cmd.ReverseRangeCmd;
import tim.prune.data.DataPoint;
import tim.prune.data.Track;
import java.util.ArrayList;
import java.util.List;
/**
* Class to provide the function to reverse the currently selected range
*/
public class ReverseSelectedRange extends TimeSensitiveFunction
{
/**
* Constructor
* @param inApp application object for callback
*/
public ReverseSelectedRange(App inApp) {
super(inApp);
}
/** Get the name key */
public String getNameKey() {
return "function.reverserange";
}
/**
* Begin the function
*/
public void begin()
{
if (!confirmTimestampMangling()) {
return;
}
final int startIndex = _app.getTrackInfo().getSelection().getStart();
final int endIndex = _app.getTrackInfo().getSelection().getEnd();
if (startIndex < 0 || endIndex <= startIndex) {
return;
}
Track track = _app.getTrackInfo().getTrack();
Command command = makeReverseCommand(track, startIndex, endIndex);
command.setDescription(getName());
command.setConfirmText(I18nManager.getText("confirm.reverserange"));
_app.execute(command);
}
/**
* Make a reverse command combining the required operations
* @param inTrack track object, used to interrogate segments
* @param inStartIndex start index of the range to be reversed
* @param inEndIndex end index, inclusive
* @return command to be executed
*/
public static Command makeReverseCommand(Track inTrack, int inStartIndex, int inEndIndex)
{
List<Integer> pointIndexes = makeNewOrdering(inTrack.getNumPoints(),
inStartIndex, inEndIndex);
List<PointFlag> pointFlags = null;
if (inTrack.isTrackPointWithin(inStartIndex, inEndIndex))
{
pointFlags = new ArrayList<>();
pointFlags.add(new PointFlag(inTrack.getNextTrackPoint(inEndIndex + 1), true));
boolean newSeg = true;
for (int i=inEndIndex; i>=inStartIndex; i--)
{
DataPoint point = inTrack.getPoint(i);
if (!point.isWaypoint())
{
// Shift segment flag to next point
pointFlags.add(new PointFlag(point, newSeg));
newSeg = point.getSegmentStart();
}
}
}
return new ReverseRangeCmd(pointIndexes, pointFlags);
}
/**
* Make the new ordering of the points based on the reversal
* @param totalPoints total number of points
* @param startIndex start index of selection to reverse
* @param endIndex end index of selection to reverse
* @return new ordering
*/
private static List<Integer> makeNewOrdering(int totalPoints, int startIndex, int endIndex)
{
ArrayList<Integer> references = new ArrayList<>();
for (int i=0; i<startIndex; i++) {
references.add(i);
}
for (int i=endIndex; i>=startIndex; i--) {
references.add(i);
}
for (int i=endIndex + 1; i<totalPoints; i++) {
references.add(i);
}
return references;
}
}
|