]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/load/MediaHelper.java
9b029d72729e62b5066761f05a90014c357f683b
[GpsPrune.git] / tim / prune / load / MediaHelper.java
1 package tim.prune.load;
2
3 import java.io.File;
4
5 import tim.prune.data.AudioFile;
6 import tim.prune.data.MediaFile;
7 import tim.prune.data.MediaList;
8 import tim.prune.data.Photo;
9 import tim.prune.data.Track;
10
11 /**
12  * Class to provide helper functions for loading media
13  */
14 public abstract class MediaHelper
15 {
16         /** File filters */
17         private static GenericFileFilter _jpegFilter = null, _audioFilter = null;
18
19         /**
20          * Construct a MediaFile object for the given path
21          * @param inPath path to file
22          * @return either Photo or AudioFile object as appropriate, or null
23          */
24         public static MediaFile createMediaFile(String inPath)
25         {
26                 if (inPath == null) {return null;}
27                 File file = new File(inPath);
28                 if (!file.exists() || !file.canRead() || !file.isFile()) {return null;}
29                 // Initialise filters if necessary
30                 if (_jpegFilter == null) {
31                         _jpegFilter = new JpegFileFilter();
32                         _audioFilter = new AudioFileFilter();
33                 }
34                 // Check if filename looks like a jpeg
35                 if (_jpegFilter.acceptFilename(file.getName())) {
36                         return JpegLoader.createPhoto(file);
37                 }
38                 // Check if filename looks like an audio file
39                 if (_audioFilter.acceptFilename(file.getName())) {
40                         return new AudioFile(file);
41                 }
42                 // Neither photo nor audio
43                 return null;
44         }
45
46         /**
47          * Add all the media from the given track into the specified list
48          * @param inTrack track from which media to take
49          * @param inMediaList list to which media should be added
50          * @param inMediaClass class of media, either Photo or AudioFile
51          */
52         public static void addMediaFromTrack(Track inTrack, MediaList inMediaList,
53                 Class<?> inMediaClass)
54         {
55                 final int numPoints = inTrack.getNumPoints();
56                 for (int i=0; i<numPoints; i++)
57                 {
58                         MediaFile media = null;
59                         if (inMediaClass == Photo.class) {
60                                 media = inTrack.getPoint(i).getPhoto();
61                         }
62                         else if (inMediaClass == AudioFile.class) {
63                                 media = inTrack.getPoint(i).getAudio();
64                         }
65                         if (media != null) {
66                                 inMediaList.addMedia(media);
67                         }
68                 }
69         }
70 }