File: AudioLoader.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 (99 lines) | stat: -rw-r--r-- 2,693 bytes parent folder | download | duplicates (7)
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
package tim.prune.load;

import java.io.File;
import java.util.TreeSet;
import javax.swing.JFileChooser;

import tim.prune.App;
import tim.prune.GenericFunction;
import tim.prune.I18nManager;
import tim.prune.UpdateMessageBroker;
import tim.prune.config.Config;
import tim.prune.data.AudioClip;
import tim.prune.undo.UndoLoadAudios;

/**
 * Class to manage the loading of audio clips
 */
public class AudioLoader extends GenericFunction
{
	private JFileChooser _fileChooser = null;
	private GenericFileFilter _fileFilter = null;
	private TreeSet<AudioClip> _fileList = null;


	/**
	 * Constructor
	 * @param inApp Application object to inform of data load
	 */
	public AudioLoader(App inApp)
	{
		super(inApp);
		_fileFilter = new AudioFileFilter();
	}

	/** Get the name key */
	public String getNameKey() {
		return "function.loadaudio";
	}

	/**
	 * Open the GUI to select options and start the load
	 */
	public void begin()
	{
		// Create file chooser if necessary
		if (_fileChooser == null)
		{
			_fileChooser = new JFileChooser();
			_fileChooser.setMultiSelectionEnabled(true);
			_fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
			_fileChooser.setFileFilter(_fileFilter);
			_fileChooser.setDialogTitle(I18nManager.getText(getNameKey()));
			// start from directory in config if already set by other operations
			String configDir = Config.getConfigString(Config.KEY_PHOTO_DIR);
			if (configDir == null) {configDir = Config.getConfigString(Config.KEY_TRACK_DIR);}
			if (configDir != null) {_fileChooser.setCurrentDirectory(new File(configDir));}
		}
		// Show file dialog to choose file / directory(ies)
		if (_fileChooser.showOpenDialog(_parentFrame) == JFileChooser.APPROVE_OPTION)
		{
			_fileList = new TreeSet<AudioClip>(new MediaSorter());
			processFileList(_fileChooser.getSelectedFiles());
			final int numFiles = _fileList.size();
			if (numFiles == 0) {
				_app.showErrorMessage(getNameKey(), "error.audioload.nofilesfound");
			}
			else
			{
				// Construct undo object
				UndoLoadAudios undo = new UndoLoadAudios(numFiles);
				_app.getTrackInfo().addAudios(_fileList);
				_app.completeFunction(undo, I18nManager.getText("confirm.audioload"));
				UpdateMessageBroker.informSubscribers();
			}
		}
	}

	/**
	 * Process an array of File objects to load
	 * @param inFiles array of selected Files
	 */
	private void processFileList(File[] inFiles)
	{
		for (File file : inFiles)
		{
			if (file.exists() && file.canRead())
			{
				if (file.isFile()) {
					if (_fileFilter.accept(file)) {
						_fileList.add(new AudioClip(file));
					}
				}
				else if (file.isDirectory()) {
					processFileList(file.listFiles());
				}
			}
		}
	}
}