File: RearrangePhotosFunction.java

package info (click to toggle)
gpsprune 19.2-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 4,516 kB
  • sloc: java: 42,704; sh: 25; makefile: 24; python: 15
file content (121 lines) | stat: -rw-r--r-- 3,235 bytes parent folder | download | duplicates (4)
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package tim.prune.function;

import java.util.Arrays;
import javax.swing.JOptionPane;
import tim.prune.App;
import tim.prune.I18nManager;
import tim.prune.data.DataPoint;
import tim.prune.data.Track;
import tim.prune.data.sort.PhotoComparer;
import tim.prune.data.sort.SortMode;
import tim.prune.undo.UndoRearrangePhotos;

/**
 * Class to provide the function for rearranging photo points
 */
public class RearrangePhotosFunction extends RearrangeFunction
{
	/**
	 * Constructor
	 * @param inApp app object
	 */
	public RearrangePhotosFunction(App inApp)
	{
		super(inApp, false);
	}

	/** Get the name key */
	public String getNameKey() {
		return "function.rearrangephotos";
	}

	/** Get the description key */
	public String getDescriptionKey() {
		return "dialog.rearrangephotos.desc";
	}

	/** Sort by filename key */
	protected String getSortNameKey() {
		return "sortbyfilename";
	}

	/**
	 * Perform the rearrange
	 */
	protected void finish()
	{
		Track track = _app.getTrackInfo().getTrack();
		UndoRearrangePhotos undo = new UndoRearrangePhotos(track);
		// Loop through track collecting non-photo points and photo points
		final int numPoints = track.getNumPoints();
		DataPoint[] nonPhotos = new DataPoint[numPoints];
		DataPoint[] photos = new DataPoint[numPoints];
		int numNonPhotos = 0;
		int numPhotos = 0;
		for (int i=0; i<numPoints; i++)
		{
			DataPoint point = track.getPoint(i);
			if (point.getPhoto() != null)
			{
				photos[numPhotos] = point;
				numPhotos++;
			}
			else
			{
				nonPhotos[numNonPhotos] = point;
				numNonPhotos++;
			}
		}
		boolean pointsChanged = false;
		if (numPhotos > 0)
		{
			Rearrange rearrangeOption = getRearrangeOption();
			SortMode sortOption = getSortMode();
			// Sort photos if necessary
			if (sortOption != SortMode.DONT_SORT && numPhotos > 1) {
				sortPhotos(photos, sortOption);
			}
			// Put the non-photo points and photo points together
			DataPoint[] neworder = new DataPoint[numPoints];
			if (rearrangeOption == Rearrange.TO_START)
			{
				// photos at front
				System.arraycopy(photos, 0, neworder, 0, numPhotos);
				System.arraycopy(nonPhotos, 0, neworder, numPhotos, numNonPhotos);
			}
			else
			{
				// photos at end
				System.arraycopy(nonPhotos, 0, neworder, 0, numNonPhotos);
				System.arraycopy(photos, 0, neworder, numNonPhotos, numPhotos);
			}

			// Give track the new point order
			pointsChanged = track.replaceContents(neworder);
		}
		// did anything change?
		if (pointsChanged)
		{
			_app.getTrackInfo().getSelection().clearAll();
			_app.completeFunction(undo, I18nManager.getText("confirm.rearrangephotos"));
			// Note: subscribers are informed up to three times now
		}
		else
		{
			JOptionPane.showMessageDialog(_parentFrame, I18nManager.getText("error.rearrange.noop"),
				I18nManager.getText("error.function.noop.title"), JOptionPane.WARNING_MESSAGE);
		}
	}

	/**
	 * Sort the given photo list either by filename or by time
	 * @param inPhotos array of DataPoint objects to sort
	 * @param inSortOrder sort order
	 * @return sorted array
	 */
	private static void sortPhotos(DataPoint[] inPhotos, SortMode inSortMode)
	{
		PhotoComparer comparer = new PhotoComparer(inSortMode);
		Arrays.sort(inPhotos, comparer);
	}
}