File: RearrangeFunction.java

package info (click to toggle)
gpsprune 26.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,824 kB
  • sloc: java: 52,154; sh: 25; makefile: 21; python: 15
file content (216 lines) | stat: -rw-r--r-- 6,152 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package tim.prune.function;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

import tim.prune.App;
import tim.prune.GenericFunction;
import tim.prune.I18nManager;
import tim.prune.cmd.PointReference;
import tim.prune.data.SortMode;

/**
 * Abstract superclass for the functions which rearrange points,
 * such as waypoints or photo points
 */
public abstract class RearrangeFunction extends GenericFunction
{
	/** Function dialog */
	private JDialog _dialog = null;
	/** Radio buttons for start/end/nearest */
	private JRadioButton[] _positionRadios = null;
	/** Radio buttons for sorting */
	private JRadioButton[] _sortRadios = null;
	/** Is the "nearest" option available? */
	private final boolean _nearestAvailable;


	/** Enumeration for rearrange commands */
	protected enum Rearrange
	{
		/** Rearrange all waypoints to start */
		TO_START,
		/** Rearrange all waypoints to end */
		TO_END,
		/** Rearrange each waypoint to nearest track point */
		TO_NEAREST
	}


	/**
	 * Constructor
	 * @param inApp app object
	 * @param isNearestAvailable true if nearest option is visible
	 */
	public RearrangeFunction(App inApp, boolean isNearestAvailable)
	{
		super(inApp);
		_nearestAvailable = isNearestAvailable;
	}

	/**
	 * Begin the function by showing the dialog
	 */
	public void begin()
	{
		// Make dialog window
		if (_dialog == null)
		{
			_dialog = new JDialog(_parentFrame, getName(), true);
			_dialog.setLocationRelativeTo(_parentFrame);
			_dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			_dialog.getContentPane().add(makeDialogComponents());
			_dialog.pack();
		}
		// If sorting by time isn't available, then disable radio button
		_sortRadios[2].setEnabled(isSortByTimeAllowed());
		// Show dialog
		_dialog.setVisible(true);
	}


	/**
	 * Create dialog components
	 * @return Panel containing all gui elements in dialog
	 */
	private JPanel makeDialogComponents()
	{
		JPanel dialogPanel = new JPanel();
		dialogPanel.setLayout(new BorderLayout());
		JLabel descLabel = new JLabel(I18nManager.getText(getDescriptionKey()));
		descLabel.setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8));
		dialogPanel.add(descLabel, BorderLayout.NORTH);
		// Radios for position (start / end / nearest)
		_positionRadios = new JRadioButton[3];
		final String[] posNames = {"tostart", "toend", "tonearest"};
		ButtonGroup posGroup = new ButtonGroup();
		JPanel posPanel = new JPanel();
		posPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
		for (int i=0; i<posNames.length; i++)
		{
			_positionRadios[i] = new JRadioButton(I18nManager.getText("dialog.rearrange." + posNames[i]));
			posGroup.add(_positionRadios[i]);
			posPanel.add(_positionRadios[i]);
		}
		_positionRadios[0].setSelected(true);
		_positionRadios[2].setVisible(_nearestAvailable);

		// Radios for sort (none / filename / time)
		_sortRadios = new JRadioButton[3];
		final String[] sortNames = {"nosort", getSortNameKey(), "sortbytime"};
		ButtonGroup sortGroup = new ButtonGroup();
		JPanel sortPanel = new JPanel();
		sortPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
		for (int i=0; i<3; i++)
		{
			_sortRadios[i] = new JRadioButton(I18nManager.getText("dialog.rearrange." + sortNames[i]));
			sortGroup.add(_sortRadios[i]);
			sortPanel.add(_sortRadios[i]);
		}
		_sortRadios[0].setSelected(true);
		// Use listener to disable all sort options if nearest type chosen, re-enable otherwise
		ActionListener rearrListener = e -> {
			final boolean sortAvailable = !_positionRadios[2].isSelected();
			for (JRadioButton sortRadio : _sortRadios) {
				sortRadio.setEnabled(sortAvailable);
			}
		};
		for (JRadioButton positionRadio : _positionRadios) {
			positionRadio.addActionListener(rearrListener);
		}
		// add to middle of dialog
		JPanel centrePanel = new JPanel();
		centrePanel.setLayout(new BoxLayout(centrePanel, BoxLayout.Y_AXIS));
		centrePanel.add(posPanel);
		centrePanel.add(sortPanel);
		dialogPanel.add(centrePanel, BorderLayout.CENTER);
		// button panel at bottom
		JPanel buttonPanel = new JPanel();
		buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
		JButton okButton = new JButton(I18nManager.getText("button.ok"));
		okButton.addActionListener(e -> {
			finish();
			_dialog.dispose();
		});
		buttonPanel.add(okButton);
		JButton cancelButton = new JButton(I18nManager.getText("button.cancel"));
		cancelButton.addActionListener(e -> _dialog.dispose());
		buttonPanel.add(cancelButton);
		dialogPanel.add(buttonPanel, BorderLayout.SOUTH);
		return dialogPanel;
	}

	/**
	 * @return true if sorting by time is allowed, false otherwise
	 */
	protected boolean isSortByTimeAllowed() {
		return true;
	}

	/**
	 * @return the selected rearrange option
	 */
	protected Rearrange getRearrangeOption()
	{
		if (_positionRadios[0].isSelected()) {
			return Rearrange.TO_START;
		}
		if (_positionRadios[1].isSelected()) {
			return Rearrange.TO_END;
		}
		return Rearrange.TO_NEAREST;
	}

	/**
	 * @return the selected sort mode
	 */
	protected SortMode getSortMode()
	{
		if (_sortRadios[0].isSelected()) {
			return SortMode.DONT_SORT;
		}
		if (_sortRadios[1].isSelected()) {
			return SortMode.SORTBY_NAME;
		}
		return SortMode.SORTBY_TIME;
	}

	/** @return key for description */
	protected abstract String getDescriptionKey();

	/** @return partial key for the sort by name radio */
	protected abstract String getSortNameKey();

	/**
	 * Perform the rearrange
	 */
	protected abstract void finish();

	/**
	 * Verify a rearrange result and see if it does anything or not
	 * @return true if the operation has no effect
	 */
	protected static boolean isResultANop(List<PointReference> result)
	{
		// Compare all indices with their original ones
		for (int i=0; i<result.size(); i++)
		{
			if (result.get(i).getIndex() != i) {
				return false;
			}
		}
		// Must be all the same
		return true;
	}
}