File: FindFilesFunction.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 (371 lines) | stat: -rw-r--r-- 12,345 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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
package tim.prune.function.filesleuth;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.TimeZone;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.border.EtchedBorder;
import javax.swing.event.ListSelectionEvent;

import tim.prune.App;
import tim.prune.GenericFunction;
import tim.prune.I18nManager;
import tim.prune.config.TimezoneHelper;
import tim.prune.function.filesleuth.data.DateRange;
import tim.prune.function.filesleuth.data.Filter;
import tim.prune.function.filesleuth.data.LocationFilter;
import tim.prune.function.filesleuth.data.TrackFile;
import tim.prune.function.filesleuth.data.TrackFileList;
import tim.prune.function.filesleuth.data.TrackFileStatus;
import tim.prune.function.filesleuth.gui.DateRangeEditor;
import tim.prune.function.filesleuth.gui.DateRangeUser;
import tim.prune.function.filesleuth.gui.LocationFilterEditor;
import tim.prune.function.filesleuth.gui.LocationFilterUser;
import tim.prune.function.filesleuth.gui.ResultsTableModel;
import tim.prune.gui.GuiGridLayout;
import tim.prune.gui.StatusIcon;

/**
 * Function to do the actual looking for track files meeting certain criteria.
 */
public class FindFilesFunction extends GenericFunction implements DateRangeUser, LocationFilterUser
{
	private JDialog _dialog = null;
	private final File _startDir;
	private final boolean _subdirectories;
	private final TrackFileList _trackList = new TrackFileList();
	private ScanController _scanController = null;
	private ResultsTableModel _resultsModel = null;
	private TrackMatcher _matcher = null;
	private JProgressBar _progressBar;
	private JTextField _searchText = null;
	private JTextField _dateRangeField = null;
	private StatusIcon _dateValidStatus = null;
	private JTextField _locationFilterField = null;
	private JButton _editLocationButton = null;
	private JTable _resultsTable = null;
	private JButton _loadButton = null;
	private DateRangeEditor _dateRangeEditor = null;
	private LocationFilter _locationFilter = null;
	private LocationFilterEditor _locationFilterEditor = null;
	private JLabel _filePathLabel = null;


	public FindFilesFunction(App inApp, File inStartDir, boolean inSubdirectories)
	{
		super(inApp);
		_startDir = inStartDir;
		_subdirectories = inSubdirectories;
	}

	@Override
	public String getNameKey() {
		return "menu.file.findfile";
	}

	@Override
	public void begin()
	{
		_dialog = new JDialog(_parentFrame, getName());
		_dialog.setLocationRelativeTo(_parentFrame);
		_dialog.getContentPane().add(makeContents());
		_dialog.pack();

		// Add listener(s) to the track list
		_trackList.addListener(idx -> updateProgressBar());
		TimeZone timezone = TimezoneHelper.getSelectedTimezone(getConfig());
		// Make a scan controller, which will attach itself to the list
		_scanController = new ScanController(_trackList, timezone);
		_progressBar.setIndeterminate(true);
		new TrackFileFinder(_trackList, _startDir, _subdirectories).begin();
		_dialog.setVisible(true);
	}

	/**
	 * @return the contents of the window as a Component
	 */
	private Component makeContents()
	{
		JPanel dialogPanel = new JPanel();
		dialogPanel.setBorder(BorderFactory.createCompoundBorder(
			BorderFactory.createEtchedBorder(EtchedBorder.LOWERED), BorderFactory.createEmptyBorder(4, 4, 4, 4))
		);
		dialogPanel.setLayout(new BorderLayout(0, 5));

		// Filter panel
		JPanel filterPanel = new JPanel();
		double[] weights = new double[] {0.2, 3.0, 0.2, 0.2};
		boolean[] aligns = new boolean[] {true, false, false, false};
		GuiGridLayout grid = new GuiGridLayout(filterPanel, weights, aligns);
		// Search by text
		_searchText = new JTextField(20);
		_searchText.addActionListener(e -> filterChanged());
		JButton searchButton = new JButton(I18nManager.getText("button.search"));
		searchButton.addActionListener(e -> filterChanged());
		grid.add(new JLabel(I18nManager.getText("dialog.findfile.searchtext")));
		grid.add(_searchText, true);
		grid.add(new StatusIcon(getIconManager()));
		grid.add(searchButton);
		// Search by date
		grid.add(new JLabel(I18nManager.getText("dialog.findfile.daterange")));
		_dateRangeField = new JTextField(20);
		_dateRangeField.addActionListener(e -> filterChanged());
		_dateValidStatus = new StatusIcon(getIconManager());
		JButton editDateRangeButton = new JButton(I18nManager.getText("button.edit"));
		editDateRangeButton.addActionListener(e -> editDateRange());
		grid.add(_dateRangeField, true);
		grid.add(_dateValidStatus);
		grid.add(editDateRangeButton);
		// Search by location
		grid.add(new JLabel(I18nManager.getText("dialog.findfile.locationfilter")));
		_locationFilterField = new JTextField(20);
		_locationFilterField.setEditable(false);
		_locationFilterField.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				super.mouseClicked(e);
				checkLocation();
			}
		});
		_editLocationButton = new JButton(I18nManager.getText("button.edit"));
		_editLocationButton.setEnabled(_app.getTrackInfo().getTrack().getNumPoints() > 0);
		_editLocationButton.addActionListener(e -> editLocationFilter());
		grid.add(_locationFilterField, true);
		grid.add(new StatusIcon(getIconManager()));
		grid.add(_editLocationButton);
		// the filter panel goes at the top of the outer border panel
		dialogPanel.add(filterPanel, BorderLayout.NORTH);

		// Center panel
		JPanel centerPanel = new JPanel();
		centerPanel.setLayout(new BorderLayout(0, 5));
		_progressBar = new JProgressBar(0, 10);
		_progressBar.setPreferredSize(new Dimension(300, 30));
		_progressBar.setValue(0);
		_progressBar.setStringPainted(true);
		// Progress panel below the filters at the top of the inner border panel
		centerPanel.add(_progressBar, BorderLayout.NORTH);

		_resultsModel = new ResultsTableModel();
		_matcher = new TrackMatcher(_trackList, _resultsModel);
		_resultsTable = new JTable(_resultsModel);
		_resultsTable.getSelectionModel().addListSelectionListener(this::fileSelected);
		centerPanel.add(new JScrollPane(_resultsTable), BorderLayout.CENTER);
		_filePathLabel = new JLabel(I18nManager.getText("dialog.findfile.filepath.none"));
		centerPanel.add(_filePathLabel, BorderLayout.SOUTH);
		dialogPanel.add(centerPanel, BorderLayout.CENTER);

		// Cancel button at the bottom right
		JPanel buttonPanelr = new JPanel();
		JButton cancelButton = new JButton(I18nManager.getText("button.cancel"));
		cancelButton.addActionListener(e -> cancel());
		buttonPanelr.add(cancelButton);

		// Load button at bottom left
		JPanel buttonPanell = new JPanel();
		_loadButton = new JButton(I18nManager.getText("button.load"));
		_loadButton.setEnabled(false);
		_loadButton.addActionListener(e -> loadSelectedTracks());
		buttonPanell.add(_loadButton);

		// Put them together
		JPanel buttonPanel = new JPanel();
		buttonPanel.setLayout(new BorderLayout());
		buttonPanel.add(buttonPanell, BorderLayout.WEST);
		buttonPanel.add(buttonPanelr, BorderLayout.EAST);
		dialogPanel.add(buttonPanel, BorderLayout.SOUTH);
		return dialogPanel;
	}

	private void checkLocation()
	{
		_locationFilterField.setSelectionStart(0);
		_locationFilterField.setSelectionEnd(0);
		final boolean hasPoints = _app.getTrackInfo().getTrack().getNumPoints() > 0;
		_editLocationButton.setEnabled(hasPoints);
		if (!hasPoints) {
			updateLocationFilter(null);
		}
	}

	private void editDateRange()
	{
		String currentRange = _dateRangeField.getText().trim();
		if (_dateRangeEditor == null) {
			_dateRangeEditor = new DateRangeEditor(_dialog, this, getIconManager());
		}
		_dateRangeEditor.show(currentRange);
	}

	private void editLocationFilter()
	{
		if (_app.getTrackInfo().getCurrentPoint() == null)
		{
			if (_locationFilter == null) {
				_app.showErrorMessage(getNameKey(), "dialog.locationfilter.nopointselected");
			}
			updateLocationFilter(null);
			return;
		}
		if (_locationFilterEditor == null) {
			_locationFilterEditor = new LocationFilterEditor(_dialog, this);
		}
		_locationFilterEditor.show(_locationFilter, _app.getTrackInfo().getCurrentPoint());
	}

	private void cancel()
	{
		if (_scanController != null) {
			_scanController.cancel();
		}
		_dialog.dispose();
	}

	/** React to one of the filter settings being changed, pass to Matcher */
	private void filterChanged()
	{
		_matcher.setFilter(new Filter(_searchText.getText(), _dateRangeField.getText(), _locationFilter));
		setDateValidStatus();
		updateProgressBar();
	}

	private void setDateValidStatus()
	{
		if (_dateRangeField.getText().isEmpty()) {
			_dateValidStatus.setStatusBlank();
		}
		else
		{
			final DateRange dateRange = DateRange.parseString(_dateRangeField.getText());
			if (dateRange.isValid()) {
				_dateValidStatus.setStatusValid();
			}
			else {
				_dateValidStatus.setStatusInvalid();
			}
		}
	}

	private void updateProgressBar()
	{
		if (_trackList.isListComplete() && _progressBar.isVisible())
		{
			_progressBar.setIndeterminate(false);
			List<TrackFile> tracks = _trackList.getCurrentContents();
			_progressBar.setMinimum(0);
			_progressBar.setMaximum(tracks.size());
			final int numScanned = countScanned(tracks);
			_progressBar.setValue(numScanned);
			_progressBar.setVisible(numScanned != tracks.size());
		}
	}

	/** Given the list of track files, count the ones which have already been scanned */
	private int countScanned(List<TrackFile> inTracks)
	{
		if (inTracks == null) {
			return 0;
		}
		int numScanned = 0;
		for (TrackFile track : inTracks)
		{
			if (track.getStatus() == TrackFileStatus.COMPLETE) {
				numScanned++;
			}
		}
		return numScanned;
	}

	private void loadSelectedTracks()
	{
		ArrayList<File> files = new ArrayList<>();
		int[] selectedRows = _resultsTable.getSelectedRows();
		for (int row : selectedRows) {
			files.add(_resultsModel.getFile(row));
		}
		if (!files.isEmpty()) {
			_app.loadDataFiles(files);
		}
	}

	public void updateDateRange(DateRange inRange)
	{
		if (inRange == null || inRange.isEmpty())
		{
			_dateRangeField.setText("");
			_dateValidStatus.setStatusBlank();
		}
		else
		{
			_dateRangeField.setText(inRange.toShortString());
			_dateValidStatus.setStatusValid();
		}
		filterChanged();
	}

	public void updateLocationFilter(LocationFilter inFilter)
	{
		_locationFilter = inFilter;
		_locationFilterField.setText(makeLocationFilterText(inFilter));
		filterChanged();
	}

	private static String makeLocationFilterText(LocationFilter inFilter)
	{
		if (inFilter == null || inFilter.getPoint() == null || inFilter.getDistanceValue() <= 0) {
			return "";
		}
		final String pointId = inFilter.getPointDescription();
		final String distanceString = inFilter.getDistanceValue() + " "
			+ I18nManager.getText(inFilter.getDistanceUnit().getShortnameKey());
		return I18nManager.getText("dialog.locationfilter.describe", distanceString, pointId);
	}

	/**
	 * Update the status of the load button and the file path label when a file
	 * or multiple files are selected
	 */
	private void fileSelected(ListSelectionEvent e)
	{
		final int rowsSelected = _resultsTable.getSelectedRowCount();
		_loadButton.setEnabled(rowsSelected > 0);
		if (rowsSelected == 0) {
			_filePathLabel.setText(I18nManager.getText("dialog.findfile.filepath.none"));
		}
		else if (rowsSelected == 1)
		{
			File file = _resultsModel.getFile(_resultsTable.getSelectedRow());
			_filePathLabel.setText(I18nManager.getText("dialog.findfile.filepath.single",
				file.getAbsolutePath()));
		}
		else
		{
			// Get the file path common to all selected files
			String commonPath = _resultsModel.getFile(_resultsTable.getSelectedRow()).getAbsolutePath();
			int[] selectedRows = _resultsTable.getSelectedRows();
			for (int row : selectedRows)
			{
				String currPath = _resultsModel.getFile(row).getAbsolutePath();
				while (!currPath.startsWith(commonPath)) {
					commonPath = new File(commonPath).getParentFile().getAbsolutePath();
				}
			}
			_filePathLabel.setText(I18nManager.getText("dialog.findfile.filepath.multi",
				"" + rowsSelected, commonPath));
		}
	}
}