File: StartFindFilesFunction.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 (199 lines) | stat: -rw-r--r-- 5,786 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
package tim.prune.function.filesleuth;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.File;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EtchedBorder;

import tim.prune.App;
import tim.prune.GenericFunction;
import tim.prune.I18nManager;
import tim.prune.config.Config;

/**
 * Function to start looking for track files meeting certain criteria.
 * This function just determines the start path and whether to search recursively,
 * before passing control to the FindFilesFunction.
 */
public class StartFindFilesFunction extends GenericFunction
{
	private JDialog _dialog = null;
	private JTextField _searchDirBox = null;
	private JCheckBox _subdirsCheckbox = null;
	private JButton _okButton = null;

	public StartFindFilesFunction(App inApp) {
		super(inApp);
	}

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

	@Override
	public void begin()
	{
		if (_dialog == null)
		{
			_dialog = new JDialog(_parentFrame, getName());
			_dialog.setLocationRelativeTo(_parentFrame);
			_dialog.getContentPane().add(makeContents());
			_dialog.pack();
		}
		// Set directory according to current config
		final String currPath = getConfig().getConfigString(Config.KEY_TRACK_DIR);
		_searchDirBox.setText(currPath == null ? "" : currPath);
		enableButtons();
		_dialog.setVisible(true);
	}

	/**
	 * Enable or disable the OK button
	 */
	private void enableButtons()
	{
		final String path = _searchDirBox.getText();
		boolean dirGood = false;
		if (!path.equals(""))
		{
			File dir = new File(path);
			dirGood = dir.exists() && dir.canRead() && dir.isDirectory();
		}
		_okButton.setEnabled(dirGood);
	}

	/**
	 * @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));
		// dir panel
		JPanel dirPanel = new JPanel();
		dirPanel.setLayout(new BorderLayout());
		dirPanel.add(new JLabel(I18nManager.getText("dialog.findfile.dir") + " : "), BorderLayout.WEST);
		_searchDirBox = new JTextField(24);
		_searchDirBox.addKeyListener(new KeyAdapter() {
			public void keyReleased(KeyEvent arg0) {
				super.keyReleased(arg0);
				enableButtons();
			}
		});
		_searchDirBox.addActionListener(e -> finish());
		dirPanel.add(_searchDirBox, BorderLayout.CENTER);
		JButton browseButton = new JButton(I18nManager.getText("button.browse"));
		browseButton.addActionListener(e -> chooseDir());
		dirPanel.add(browseButton, BorderLayout.EAST);
		_subdirsCheckbox = new JCheckBox(I18nManager.getText("dialog.open.includesubdirectories"), true);
		dirPanel.add(_subdirsCheckbox, BorderLayout.SOUTH);
		// holder panel so it doesn't expand vertically
		JPanel dirHolderPanel = new JPanel();
		dirHolderPanel.setLayout(new BorderLayout());
		dirHolderPanel.add(dirPanel, BorderLayout.NORTH);
		dialogPanel.add(dirHolderPanel, BorderLayout.CENTER);

		// OK, Cancel buttons at the bottom right
		JPanel buttonPanelr = new JPanel();
		buttonPanelr.setLayout(new FlowLayout(FlowLayout.RIGHT));
		_okButton = new JButton(I18nManager.getText("button.ok"));
		_okButton.addActionListener(e -> finish());
		buttonPanelr.add(_okButton);
		JButton cancelButton = new JButton(I18nManager.getText("button.cancel"));
		cancelButton.addActionListener(e -> _dialog.dispose());
		buttonPanelr.add(cancelButton);

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

	/**
	 * Function activated by the "Browse..." button to select a directory
	 */
	private void chooseDir()
	{
		JFileChooser chooser = new JFileChooser();
		chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		// Set start path from currently selected dir
		String path = _searchDirBox.getText();
		if (path.length() > 1) {
			chooser.setCurrentDirectory(new File(path));
		}
		if (chooser.showOpenDialog(_parentFrame) == JFileChooser.APPROVE_OPTION) {
			_searchDirBox.setText(chooser.getSelectedFile().getAbsolutePath());
		}
		enableButtons();
	}

	/**
	 * OK pressed, start the search
	 */
	private void finish()
	{
		if (!_okButton.isEnabled()) {
			return;
		}
		File startDir = new File(_searchDirBox.getText());
		if (hasAnyFiles(startDir, _subdirsCheckbox.isSelected()))
		{
			new FindFilesFunction(_app, startDir, _subdirsCheckbox.isSelected()).begin();
			_dialog.dispose();
		}
		else {
			_app.showErrorMessage(getNameKey(), "error.findfile.nofilesfound");
		}
	}

	/**
	 * Check if the given path has any files to search through
	 */
	private boolean hasAnyFiles(File inPath, boolean inSubdirs)
	{
		if (inPath == null || !inPath.exists() || !inPath.canRead() || !inPath.isDirectory()) {
			return false;
		}
		File[] files = inPath.listFiles();
		if (files == null) {
			return false;
		}
		// Look through files in this directory
		for (File file : files)
		{
			if (file != null && file.isFile() && file.canRead()) {
				return true;
			}
		}
		if (inSubdirs)
		{
			// Also look in subdirectories
			for (File dir : files)
			{
				if (dir != null && dir.isDirectory() && dir.canRead() && hasAnyFiles(dir, true)) {
					return true;
				}
			}
		}
		// Nothing found
		return false;
	}
}