]> gitweb.fperrin.net Git - GpsPrune.git/blob - src/tim/prune/data/FileInfo.java
Merge remote-tracking branch 'upstream/master' into fp-integration
[GpsPrune.git] / src / tim / prune / data / FileInfo.java
1 package tim.prune.data;
2
3 import java.util.ArrayList;
4
5 /**
6  * Class to hold the information about the file(s)
7  * from which the data was loaded from / saved to
8  */
9 public class FileInfo
10 {
11         /** List of sources */
12         private ArrayList<SourceInfo> _sources = new ArrayList<SourceInfo>();
13
14
15         /**
16          * Empty constructor
17          */
18         public FileInfo()
19         {}
20
21         /**
22          * Private constructor for creating clone
23          * @param inList list of sources
24          */
25         private FileInfo(ArrayList<SourceInfo> inList)
26         {
27                 _sources = inList;
28         }
29
30         /**
31          * Add a data source to the list
32          * @param inInfo info object to add
33          */
34         public void addSource(SourceInfo inInfo)
35         {
36                 _sources.add(inInfo);
37         }
38
39         /**
40          * Replace the list of data sources with the given source
41          * @param inInfo new source
42          */
43         public void replaceSource(SourceInfo inInfo)
44         {
45                 _sources.clear();
46                 addSource(inInfo);
47         }
48
49         /**
50          * remove the last source added
51          */
52         public void removeSource()
53         {
54                 if (!_sources.isEmpty()) {
55                         _sources.remove(_sources.size()-1);
56                 }
57         }
58
59         /**
60          * @return the number of files loaded
61          */
62         public int getNumFiles()
63         {
64                 return _sources.size();
65         }
66
67
68         /**
69          * @return The source name, if a single file
70          */
71         public String getFilename()
72         {
73                 if (getNumFiles() == 1) {
74                         return _sources.get(0).getName();
75                 }
76                 return "";
77         }
78
79         /**
80          * @return The source names
81          */
82         public ArrayList<String> getFilenames()
83         {
84                 ArrayList<String> filenames = new ArrayList<String>();
85                 for (SourceInfo source : _sources)
86                 {
87                         filenames.add(source.getName());
88                 }
89                 return filenames;
90         }
91
92         /**
93          * @param inIndex index number, starting from zero
94          * @return source info object
95          */
96         public SourceInfo getSource(int inIndex)
97         {
98                 return _sources.get(inIndex);
99         }
100
101         /**
102          * Get the SourceInfo object (if any) for the given point
103          * @param inPoint point object
104          * @return SourceInfo object if there is one, otherwise null
105          */
106         public SourceInfo getSourceForPoint(DataPoint inPoint)
107         {
108                 for (SourceInfo source : _sources)
109                 {
110                         if (source.getIndex(inPoint) >= 0) {
111                                 return source;
112                         }
113                 }
114                 return null;
115         }
116
117         /**
118          * @return the info about the last file loaded, if any
119          */
120         public SourceInfo getLastFileInfo()
121         {
122                 if (getNumFiles() == 0)
123                 {
124                         return null;
125                 }
126                 return getSource(getNumFiles()-1);
127         }
128
129         /**
130          * @return the most recent file title loaded, if any
131          */
132         public String getLastFileTitle()
133         {
134                 final int numFiles = getNumFiles();
135                 if (numFiles == 0)
136                 {
137                         return null;
138                 }
139                 for (int i=(numFiles-1); i>=0; i--)
140                 {
141                         SourceInfo info = getSource(i);
142                         if (info != null)
143                         {
144                                 String title = info.getFileTitle();
145                                 if (title != null && !title.equals(""))
146                                 {
147                                         return title;
148                                 }
149                         }
150                 }
151                 return null;
152         }
153
154         /**
155          * Clone contents of file info
156          */
157         @SuppressWarnings("unchecked")
158         public FileInfo clone()
159         {
160                 // copy source list
161                 ArrayList<SourceInfo> copy = (ArrayList<SourceInfo>) _sources.clone();
162                 return new FileInfo(copy);
163         }
164 }