]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/function/RearrangePhotosFunction.java
466cd668230a023705f56b001836a0638b405e3d
[GpsPrune.git] / tim / prune / function / RearrangePhotosFunction.java
1 package tim.prune.function;
2
3 import java.util.Arrays;
4 import javax.swing.JOptionPane;
5 import tim.prune.App;
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;
12
13 /**
14  * Class to provide the function for rearranging photo points
15  */
16 public class RearrangePhotosFunction extends RearrangeFunction
17 {
18         /**
19          * Constructor
20          * @param inApp app object
21          */
22         public RearrangePhotosFunction(App inApp)
23         {
24                 super(inApp, false);
25         }
26
27         /** Get the name key */
28         public String getNameKey() {
29                 return "function.rearrangephotos";
30         }
31
32         /** Get the description key */
33         public String getDescriptionKey() {
34                 return "dialog.rearrangephotos.desc";
35         }
36
37         /** Sort by filename key */
38         protected String getSortNameKey() {
39                 return "sortbyfilename";
40         }
41
42         /**
43          * Perform the rearrange
44          */
45         protected void finish()
46         {
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];
53                 int numNonPhotos = 0;
54                 int numPhotos = 0;
55                 for (int i=0; i<numPoints; i++)
56                 {
57                         DataPoint point = track.getPoint(i);
58                         if (point.getPhoto() != null)
59                         {
60                                 photos[numPhotos] = point;
61                                 numPhotos++;
62                         }
63                         else
64                         {
65                                 nonPhotos[numNonPhotos] = point;
66                                 numNonPhotos++;
67                         }
68                 }
69                 boolean pointsChanged = false;
70                 if (numPhotos > 0)
71                 {
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);
77                         }
78                         // Put the non-photo points and photo points together
79                         DataPoint[] neworder = new DataPoint[numPoints];
80                         if (rearrangeOption == Rearrange.TO_START)
81                         {
82                                 // photos at front
83                                 System.arraycopy(photos, 0, neworder, 0, numPhotos);
84                                 System.arraycopy(nonPhotos, 0, neworder, numPhotos, numNonPhotos);
85                         }
86                         else
87                         {
88                                 // photos at end
89                                 System.arraycopy(nonPhotos, 0, neworder, 0, numNonPhotos);
90                                 System.arraycopy(photos, 0, neworder, numNonPhotos, numPhotos);
91                         }
92                         
93                         // Give track the new point order
94                         pointsChanged = track.replaceContents(neworder);
95                 }
96                 // did anything change?
97                 if (pointsChanged)
98                 {
99                         _app.getTrackInfo().getSelection().clearAll();
100                         _app.completeFunction(undo, I18nManager.getText("confirm.rearrangephotos"));
101                         // Note: subscribers are informed up to three times now
102                 }
103                 else
104                 {
105                         JOptionPane.showMessageDialog(_parentFrame, I18nManager.getText("error.rearrange.noop"),
106                                 I18nManager.getText("error.function.noop.title"), JOptionPane.WARNING_MESSAGE);
107                 }
108         }
109
110         /**
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
115          */
116         private static void sortPhotos(DataPoint[] inPhotos, SortMode inSortMode)
117         {
118                 PhotoComparer comparer = new PhotoComparer(inSortMode);
119                 Arrays.sort(inPhotos, comparer);
120         }
121 }