File: RearrangeFunction.java

package info (click to toggle)
gpsprune 17-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 3,984 kB
  • ctags: 5,218
  • sloc: java: 39,403; sh: 25; makefile: 17; python: 15
file content (207 lines) | stat: -rw-r--r-- 5,990 bytes parent folder | download | duplicates (6)
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
package tim.prune.function;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

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.data.sort.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 boolean _nearestAvailable = false;


	/** 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, I18nManager.getText(getNameKey()), 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 = new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				final boolean sortAvailable = !_positionRadios[2].isSelected();
				for (int i=0; i<_sortRadios.length; i++) {
					_sortRadios[i].setEnabled(sortAvailable);
				}
			}
		};
		for (int i=0; i<_positionRadios.length; i++) {
			_positionRadios[i].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(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				finish();
				_dialog.dispose();
			}
		});
		buttonPanel.add(okButton);
		JButton cancelButton = new JButton(I18nManager.getText("button.cancel"));
		cancelButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent 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();
}