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;
8 import javax.swing.BorderFactory;
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.JPanel;
15 import javax.swing.JRadioButton;
18 import tim.prune.GenericFunction;
19 import tim.prune.I18nManager;
20 import tim.prune.data.sort.SortMode;
23 * Abstract superclass for the functions which rearrange points,
24 * such as waypoints or photo points
26 public abstract class RearrangeFunction extends GenericFunction
28 /** Function dialog */
29 private JDialog _dialog = null;
30 /** Radio buttons for start/end/nearest */
31 private JRadioButton[] _positionRadios = null;
32 /** Radio buttons for sorting */
33 private JRadioButton[] _sortRadios = null;
34 /** Is the "nearest" option available? */
35 private boolean _nearestAvailable = false;
38 /** Enumeration for rearrange commands */
39 protected enum Rearrange
41 /** Rearrange all waypoints to start */
43 /** Rearrange all waypoints to end */
45 /** Rearrange each waypoint to nearest track point */
52 * @param inApp app object
53 * @param isNearestAvailable true if nearest option is visible
55 public RearrangeFunction(App inApp, boolean isNearestAvailable)
58 _nearestAvailable = isNearestAvailable;
62 * Begin the function by showing the dialog
69 _dialog = new JDialog(_parentFrame, I18nManager.getText(getNameKey()), true);
70 _dialog.setLocationRelativeTo(_parentFrame);
71 _dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
72 _dialog.getContentPane().add(makeDialogComponents());
75 // If sorting by time isn't available, then disable radio button
76 _sortRadios[2].setEnabled(isSortByTimeAllowed());
78 _dialog.setVisible(true);
83 * Create dialog components
84 * @return Panel containing all gui elements in dialog
86 private JPanel makeDialogComponents()
88 JPanel dialogPanel = new JPanel();
89 dialogPanel.setLayout(new BorderLayout());
90 JLabel descLabel = new JLabel(I18nManager.getText(getDescriptionKey()));
91 descLabel.setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8));
92 dialogPanel.add(descLabel, BorderLayout.NORTH);
93 // Radios for position (start / end / nearest)
94 _positionRadios = new JRadioButton[3];
95 final String[] posNames = {"tostart", "toend", "tonearest"};
96 ButtonGroup posGroup = new ButtonGroup();
97 JPanel posPanel = new JPanel();
98 posPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
99 for (int i=0; i<posNames.length; i++)
101 _positionRadios[i] = new JRadioButton(I18nManager.getText("dialog.rearrange." + posNames[i]));
102 posGroup.add(_positionRadios[i]);
103 posPanel.add(_positionRadios[i]);
105 _positionRadios[0].setSelected(true);
106 _positionRadios[2].setVisible(_nearestAvailable);
108 // Radios for sort (none / filename / time)
109 _sortRadios = new JRadioButton[3];
110 final String[] sortNames = {"nosort", getSortNameKey(), "sortbytime"};
111 ButtonGroup sortGroup = new ButtonGroup();
112 JPanel sortPanel = new JPanel();
113 sortPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
114 for (int i=0; i<3; i++)
116 _sortRadios[i] = new JRadioButton(I18nManager.getText("dialog.rearrange." + sortNames[i]));
117 sortGroup.add(_sortRadios[i]);
118 sortPanel.add(_sortRadios[i]);
120 _sortRadios[0].setSelected(true);
121 // Use listener to disable all sort options if nearest type chosen, re-enable otherwise
122 ActionListener rearrListener = new ActionListener() {
123 public void actionPerformed(ActionEvent arg0) {
124 final boolean sortAvailable = !_positionRadios[2].isSelected();
125 for (int i=0; i<_sortRadios.length; i++) {
126 _sortRadios[i].setEnabled(sortAvailable);
130 for (int i=0; i<_positionRadios.length; i++) {
131 _positionRadios[i].addActionListener(rearrListener);
133 // add to middle of dialog
134 JPanel centrePanel = new JPanel();
135 centrePanel.setLayout(new BoxLayout(centrePanel, BoxLayout.Y_AXIS));
136 centrePanel.add(posPanel);
137 centrePanel.add(sortPanel);
138 dialogPanel.add(centrePanel, BorderLayout.CENTER);
139 // button panel at bottom
140 JPanel buttonPanel = new JPanel();
141 buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
142 JButton okButton = new JButton(I18nManager.getText("button.ok"));
143 okButton.addActionListener(new ActionListener() {
144 public void actionPerformed(ActionEvent e) {
149 buttonPanel.add(okButton);
150 JButton cancelButton = new JButton(I18nManager.getText("button.cancel"));
151 cancelButton.addActionListener(new ActionListener() {
152 public void actionPerformed(ActionEvent e) {
156 buttonPanel.add(cancelButton);
157 dialogPanel.add(buttonPanel, BorderLayout.SOUTH);
162 * @return true if sorting by time is allowed, false otherwise
164 protected boolean isSortByTimeAllowed()
170 * @return the selected rearrange option
172 protected Rearrange getRearrangeOption()
174 if (_positionRadios[0].isSelected()) {
175 return Rearrange.TO_START;
177 if (_positionRadios[1].isSelected()) {
178 return Rearrange.TO_END;
180 return Rearrange.TO_NEAREST;
184 * @return the selected sort mode
186 protected SortMode getSortMode()
188 if (_sortRadios[0].isSelected()) {
189 return SortMode.DONT_SORT;
191 if (_sortRadios[1].isSelected()) {
192 return SortMode.SORTBY_NAME;
194 return SortMode.SORTBY_TIME;
197 /** @return key for description */
198 protected abstract String getDescriptionKey();
200 /** @return partial key for the sort by name radio */
201 protected abstract String getSortNameKey();
204 * Perform the rearrange
206 protected abstract void finish();