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
|
package tim.prune.cmd;
import tim.prune.DataSubscriber;
import tim.prune.data.TrackInfo;
import java.util.ArrayList;
import java.util.List;
public class RearrangePointsCmd extends Command
{
private final List<Integer> _indexes;
/**
* Constructor
* @param inIndexes list of point indices
*/
public RearrangePointsCmd(List<Integer> inIndexes) {
this(null, inIndexes);
}
/**
* Constructor
* @param inParent parent command
* @param inIndexes list of point indices
*/
protected RearrangePointsCmd(RearrangePointsCmd inParent, List<Integer> inIndexes)
{
super(inParent);
_indexes = inIndexes;
}
/**
* @param inReferences list of point references
*/
public static RearrangePointsCmd from(List<PointReference> inReferences) {
return new RearrangePointsCmd(convertReferencesToIntegers(inReferences));
}
@Override
public int getUpdateFlags() {
return DataSubscriber.DATA_EDITED;
}
@Override
protected boolean executeCommand(TrackInfo inInfo)
{
if (getInverse() == null) {
return false;
}
return inInfo.getTrack().rearrangePoints(_indexes);
}
@Override
protected Command makeInverse(TrackInfo inInfo)
{
try {
return new RearrangePointsCmd(this, makeOppositeIndexes());
}
catch (NullPointerException | ArrayIndexOutOfBoundsException e) {
return null;
}
}
/**
* @return inverse list of indexes for undo operation
*/
private List<Integer> makeOppositeIndexes()
{
Integer[] copy = new Integer[_indexes.size()];
for (int i=0; i<_indexes.size(); i++) {
copy[_indexes.get(i)] = i;
}
return List.of(copy);
}
/**
* Convert the given list of point references into a simpler list for further processing
* @param inRefs point references
* @return list of indexes
*/
private static List<Integer> convertReferencesToIntegers(List<PointReference> inRefs)
{
ArrayList<Integer> indexes = new ArrayList<>();
for (PointReference ref : inRefs) {
indexes.add(ref.getIndex());
}
return indexes;
}
}
|