]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/data/MediaList.java
Version 14, October 2012
[GpsPrune.git] / tim / prune / data / MediaList.java
1 package tim.prune.data;
2
3 import java.util.ArrayList;
4
5 /**
6  * Class to hold a list of Media, either Photos or Audio files
7  */
8 public abstract class MediaList
9 {
10         /** list of media objects */
11         protected ArrayList<MediaObject> _media = null;
12
13
14         /**
15          * Empty constructor
16          */
17         public MediaList() {
18                 this(null);
19         }
20
21         /**
22          * Constructor
23          * @param inList ArrayList containing media objects
24          */
25         protected MediaList(ArrayList<MediaObject> inList)
26         {
27                 _media = inList;
28                 if (_media == null) {
29                         _media = new ArrayList<MediaObject>();
30                 }
31         }
32
33         /**
34          * @return the number of media in the list
35          */
36         public int getNumMedia() {
37                 return _media.size();
38         }
39
40         /**
41          * Add an object to the list
42          * @param inObject object to add
43          */
44         public void addMedia(MediaObject inObject)
45         {
46                 if (inObject != null) {
47                         _media.add(inObject);
48                 }
49         }
50
51         /**
52          * Add an object to the list at a specified index (used for undo)
53          * @param inObject object to add
54          * @param inIndex index at which to add
55          */
56         public void addMedia(MediaObject inObject, int inIndex)
57         {
58                 if (inObject != null) {
59                         _media.add(inIndex, inObject);
60                 }
61         }
62
63
64         /**
65          * Remove the selected media from the list
66          * @param inIndex index number to remove
67          */
68         public void deleteMedia(int inIndex)
69         {
70                 // Maybe throw exception if this fails?
71                 _media.remove(inIndex);
72         }
73
74
75         /**
76          * Checks if the specified object is already in the list
77          * @param inMedia media object to check
78          * @return true if it's already in the list
79          */
80         public boolean contains(MediaObject inMedia) {
81                 return (getMediaIndex(inMedia) > -1);
82         }
83
84
85         /**
86          * Get the index of the given media
87          * @param inMedia object to check
88          * @return index of this object in the list, or -1 if not found
89          */
90         public int getMediaIndex(MediaObject inMedia)
91         {
92                 // Check if we need to check
93                 final int num = getNumMedia();
94                 if (num <= 0 || inMedia == null)
95                         return -1;
96                 // Loop over list
97                 for (int i=0; i<num; i++)
98                 {
99                         MediaObject m = _media.get(i);
100                         if (m != null && m.equals(inMedia)) {
101                                 return i;
102                         }
103                 }
104                 // not found
105                 return -1;
106         }
107
108
109         /**
110          * Get the media at the given index
111          * @param inIndex index number, starting at 0
112          * @return specified object
113          */
114         public MediaObject getMedia(int inIndex)
115         {
116                 if (inIndex < 0 || inIndex >= getNumMedia()) return null;
117                 return _media.get(inIndex);
118         }
119
120
121         /**
122          * Crop the list to the specified size
123          * @param inIndex previous size
124          */
125         public void cropTo(int inIndex)
126         {
127                 if (inIndex <= 0)
128                 {
129                         // delete whole list
130                         _media.clear();
131                 }
132                 else
133                 {
134                         // delete to previous size
135                         while (_media.size() > inIndex) {
136                                 _media.remove(_media.size()-1);
137                         }
138                 }
139         }
140
141
142         /**
143          * @return array of file names
144          */
145         public String[] getNameList()
146         {
147                 final int num = getNumMedia();
148                 String[] names = new String[num];
149                 for (int i=0; i<num; i++) {
150                         names[i] = getMedia(i).getName();
151                 }
152                 return names;
153         }
154
155
156         /**
157          * @return true if list contains correlated media
158          */
159         public boolean hasCorrelatedMedia()
160         {
161                 for (MediaObject m : _media) {
162                         if (m.getDataPoint() != null) {
163                                 return true;
164                         }
165                 }
166                 return false;
167         }
168
169         /**
170          * @return true if list contains uncorrelated media
171          */
172         public boolean hasUncorrelatedMedia()
173         {
174                 for (MediaObject m : _media) {
175                         if (m.getDataPoint() == null && m.hasTimestamp()) {
176                                 return true;
177                         }
178                 }
179                 return false;
180         }
181
182
183         /**
184          * Remove all correlated media from the list
185          */
186         public void removeCorrelatedMedia()
187         {
188                 if (getNumMedia() > 0)
189                 {
190                         // Construct new list to copy into
191                         ArrayList<MediaObject> listCopy = new ArrayList<MediaObject>();
192                         // Loop over list
193                         for (MediaObject m : _media)
194                         {
195                                 // Copy media if it has no point
196                                 if (m != null)
197                                 {
198                                         if (m.getDataPoint() == null)
199                                                 listCopy.add(m);
200                                         else
201                                                 m.resetCachedData();
202                                 }
203                         }
204                         // Switch reference to new list
205                         _media = listCopy;
206                 }
207         }
208
209         /**
210          * @return true if any of the media objects have Files
211          */
212         public boolean hasMediaWithFile()
213         {
214                 for (MediaObject m: _media) {
215                         if (m.getFile() != null) {
216                                 return true;
217                         }
218                 }
219                 return false;
220         }
221
222         /**
223          * @return clone of list contents
224          */
225         public abstract MediaList cloneList();
226
227         /**
228          * Restore contents from other MediaList
229          * @param inOther MediaList with cloned contents
230          */
231         public void restore(MediaList inOther)
232         {
233                 _media.clear();
234                 if (inOther != null && inOther.getNumMedia() > 0)
235                 {
236                         // Copy contents from other list
237                         _media.addAll(inOther._media);
238                 }
239         }
240 }