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