1 package tim.prune.function;
3 import java.awt.BorderLayout;
4 import java.awt.FlowLayout;
5 import java.awt.event.ActionEvent;
6 import java.awt.event.ActionListener;
7 import java.util.Arrays;
9 import javax.swing.BoxLayout;
10 import javax.swing.ButtonGroup;
11 import javax.swing.JButton;
12 import javax.swing.JDialog;
13 import javax.swing.JLabel;
14 import javax.swing.JOptionPane;
15 import javax.swing.JPanel;
16 import javax.swing.JRadioButton;
19 import tim.prune.GenericFunction;
20 import tim.prune.I18nManager;
21 import tim.prune.data.DataPoint;
22 import tim.prune.data.Track;
23 import tim.prune.undo.UndoRearrangePhotos;
26 * Class to provide the function for rearranging photo points
28 public class RearrangePhotosFunction extends GenericFunction
30 /** Function dialog */
31 private JDialog _dialog = null;
32 /** Radio buttons for start/end */
33 private JRadioButton[] _positionRadios = null;
34 /** Radio buttons for sorting */
35 private JRadioButton[] _sortRadios = null;
40 * @param inApp app object
42 public RearrangePhotosFunction(App inApp)
47 /** Begin the rearrange */
53 _dialog = new JDialog(_parentFrame, I18nManager.getText(getNameKey()), true);
54 _dialog.setLocationRelativeTo(_parentFrame);
55 _dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
56 _dialog.getContentPane().add(makeDialogComponents());
59 // Reset dialog and show
60 _dialog.setVisible(true);
63 /** Get the name key (not needed) */
64 public String getNameKey() {
65 return "function.rearrangephotos";
70 * Create dialog components
71 * @return Panel containing all gui elements in dialog
73 private JPanel makeDialogComponents()
75 JPanel dialogPanel = new JPanel();
76 dialogPanel.setLayout(new BorderLayout());
77 dialogPanel.add(new JLabel(I18nManager.getText("dialog.rearrangephotos.desc")), BorderLayout.NORTH);
78 // Radios for position (start / end)
79 _positionRadios = new JRadioButton[2];
80 final String[] posNames = {"tostart", "toend"};
81 ButtonGroup posGroup = new ButtonGroup();
82 JPanel posPanel = new JPanel();
83 posPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
84 for (int i=0; i<2; i++)
86 _positionRadios[i] = new JRadioButton(I18nManager.getText("dialog.rearrangephotos." + posNames[i]));
87 posGroup.add(_positionRadios[i]);
88 posPanel.add(_positionRadios[i]);
90 _positionRadios[0].setSelected(true);
91 // Radios for sort (none / filename / time)
92 _sortRadios = new JRadioButton[3];
93 final String[] sortNames = {"nosort", "sortbyfilename", "sortbytime"};
94 ButtonGroup sortGroup = new ButtonGroup();
95 JPanel sortPanel = new JPanel();
96 sortPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
97 for (int i=0; i<3; i++)
99 _sortRadios[i] = new JRadioButton(I18nManager.getText("dialog.rearrangephotos." + sortNames[i]));
100 sortGroup.add(_sortRadios[i]);
101 sortPanel.add(_sortRadios[i]);
103 _sortRadios[0].setSelected(true);
104 // add to middle of dialog
105 JPanel centrePanel = new JPanel();
106 centrePanel.setLayout(new BoxLayout(centrePanel, BoxLayout.Y_AXIS));
107 centrePanel.add(posPanel);
108 centrePanel.add(sortPanel);
109 dialogPanel.add(centrePanel, BorderLayout.CENTER);
110 // button panel at bottom
111 JPanel buttonPanel = new JPanel();
112 buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
113 JButton okButton = new JButton(I18nManager.getText("button.ok"));
114 okButton.addActionListener(new ActionListener() {
115 public void actionPerformed(ActionEvent e) {
120 buttonPanel.add(okButton);
121 JButton cancelButton = new JButton(I18nManager.getText("button.cancel"));
122 cancelButton.addActionListener(new ActionListener() {
123 public void actionPerformed(ActionEvent e) {
127 buttonPanel.add(cancelButton);
128 dialogPanel.add(buttonPanel, BorderLayout.SOUTH);
133 * Perform the rearrange
135 private void finish()
137 Track track = _app.getTrackInfo().getTrack();
138 UndoRearrangePhotos undo = new UndoRearrangePhotos(track);
139 // Loop through track collecting non-photo points and photo points
140 final int numPoints = track.getNumPoints();
141 DataPoint[] nonPhotos = new DataPoint[numPoints];
142 DataPoint[] photos = new DataPoint[numPoints];
143 int numNonPhotos = 0;
145 for (int i=0; i<numPoints; i++)
147 DataPoint point = track.getPoint(i);
148 if (point.getPhoto() != null) {
149 photos[numPhotos] = point;
153 nonPhotos[numNonPhotos] = point;
157 // Sort photos if necessary
158 if (!_sortRadios[0].isSelected() && numPhotos > 1) {
159 sortPhotos(photos, _sortRadios[1].isSelected());
161 // Put the non-photo points and photo points together
162 DataPoint[] neworder = new DataPoint[numPoints];
163 if (_positionRadios[0].isSelected()) {
165 System.arraycopy(photos, 0, neworder, 0, numPhotos);
166 System.arraycopy(nonPhotos, 0, neworder, numPhotos, numNonPhotos);
170 System.arraycopy(nonPhotos, 0, neworder, 0, numNonPhotos);
171 System.arraycopy(photos, 0, neworder, numNonPhotos, numPhotos);
173 // Give track the new point order
174 if (track.replaceContents(neworder))
176 _app.getTrackInfo().getSelection().clearAll();
177 _app.completeFunction(undo, I18nManager.getText("confirm.rearrangephotos"));
178 // Note: subscribers are informed up to three times now
182 JOptionPane.showMessageDialog(_parentFrame, I18nManager.getText("error.rearrange.noop"),
183 I18nManager.getText("error.function.noop.title"), JOptionPane.WARNING_MESSAGE);
188 * Sort the given photo list either by filename or by time
189 * @param inPhotos array of DataPoint objects to sort
190 * @param inSortByFile true to sort by filename, false to sort by timestamp
191 * @return sorted array
193 private static void sortPhotos(DataPoint[] inPhotos, boolean inSortByFile)
195 PhotoComparer comparer = new PhotoComparer(inSortByFile ? PhotoComparer.SortMode.SORTBY_NAME : PhotoComparer.SortMode.SORTBY_TIME);
196 Arrays.sort(inPhotos, comparer);