1 package tim.prune.function;
3 import java.util.Arrays;
4 import javax.swing.JOptionPane;
6 import tim.prune.I18nManager;
7 import tim.prune.data.DataPoint;
8 import tim.prune.data.Track;
9 import tim.prune.data.sort.PhotoComparer;
10 import tim.prune.data.sort.SortMode;
11 import tim.prune.undo.UndoRearrangePhotos;
14 * Class to provide the function for rearranging photo points
16 public class RearrangePhotosFunction extends RearrangeFunction
20 * @param inApp app object
22 public RearrangePhotosFunction(App inApp)
27 /** Get the name key */
28 public String getNameKey() {
29 return "function.rearrangephotos";
32 /** Get the description key */
33 public String getDescriptionKey() {
34 return "dialog.rearrangephotos.desc";
37 /** Sort by filename key */
38 protected String getSortNameKey() {
39 return "sortbyfilename";
43 * Perform the rearrange
45 protected void finish()
47 Track track = _app.getTrackInfo().getTrack();
48 UndoRearrangePhotos undo = new UndoRearrangePhotos(track);
49 // Loop through track collecting non-photo points and photo points
50 final int numPoints = track.getNumPoints();
51 DataPoint[] nonPhotos = new DataPoint[numPoints];
52 DataPoint[] photos = new DataPoint[numPoints];
55 for (int i=0; i<numPoints; i++)
57 DataPoint point = track.getPoint(i);
58 if (point.getPhoto() != null)
60 photos[numPhotos] = point;
65 nonPhotos[numNonPhotos] = point;
69 boolean pointsChanged = false;
72 Rearrange rearrangeOption = getRearrangeOption();
73 SortMode sortOption = getSortMode();
74 // Sort photos if necessary
75 if (sortOption != SortMode.DONT_SORT && numPhotos > 1) {
76 sortPhotos(photos, sortOption);
78 // Put the non-photo points and photo points together
79 DataPoint[] neworder = new DataPoint[numPoints];
80 if (rearrangeOption == Rearrange.TO_START)
83 System.arraycopy(photos, 0, neworder, 0, numPhotos);
84 System.arraycopy(nonPhotos, 0, neworder, numPhotos, numNonPhotos);
89 System.arraycopy(nonPhotos, 0, neworder, 0, numNonPhotos);
90 System.arraycopy(photos, 0, neworder, numNonPhotos, numPhotos);
93 // Give track the new point order
94 pointsChanged = track.replaceContents(neworder);
96 // did anything change?
99 _app.getTrackInfo().getSelection().clearAll();
100 _app.completeFunction(undo, I18nManager.getText("confirm.rearrangephotos"));
101 // Note: subscribers are informed up to three times now
105 JOptionPane.showMessageDialog(_parentFrame, I18nManager.getText("error.rearrange.noop"),
106 I18nManager.getText("error.function.noop.title"), JOptionPane.WARNING_MESSAGE);
111 * Sort the given photo list either by filename or by time
112 * @param inPhotos array of DataPoint objects to sort
113 * @param inSortOrder sort order
114 * @return sorted array
116 private static void sortPhotos(DataPoint[] inPhotos, SortMode inSortMode)
118 PhotoComparer comparer = new PhotoComparer(inSortMode);
119 Arrays.sort(inPhotos, comparer);