File: AudioLoader.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 (107 lines) | stat: -rw-r--r-- 2,997 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
package tim.prune.load;

import java.io.File;
import java.util.ArrayList;

import javax.swing.JFileChooser;

import tim.prune.App;
import tim.prune.GenericFunction;
import tim.prune.cmd.AppendMediaCmd;
import tim.prune.config.Config;
import tim.prune.data.AudioClip;
import tim.prune.data.MediaObject;
import tim.prune.function.Describer;

/**
 * Class to manage the loading of audio clips
 */
public class AudioLoader extends GenericFunction
{
	private JFileChooser _fileChooser = null;
	private final GenericFileFilter _fileFilter;
	private ArrayList<MediaObject> _audioList = 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(getName());
			// start from directory in config if already set by other operations
			String configDir = getConfig().getConfigString(Config.KEY_PHOTO_DIR);
			if (configDir == null) {configDir = getConfig().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)
		{
			_audioList = new ArrayList<>();
			processFileList(_fileChooser.getSelectedFiles());
			if (_audioList.isEmpty()) {
				_app.showErrorMessage(getNameKey(), "error.audioload.nofilesfound");
			}
			else
			{
				_audioList.sort(new MediaSorter());
				AppendMediaCmd command = new AppendMediaCmd(_audioList);
				Describer confirmDescriber = new Describer("confirm.audiosloaded.single", "confirm.audiosloaded");
				command.setConfirmText(confirmDescriber.getDescriptionWithCount(_audioList.size()));
				Describer undoDescriber = new Describer("undo.loadaudio", "undo.loadaudios");
				String firstAudioName = _audioList.get(0).getName();
				command.setDescription(undoDescriber.getDescriptionWithNameOrCount(firstAudioName, _audioList.size()));
				_app.execute(command);
			}
		}
	}

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