]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/data/RecentFileList.java
Version 13, August 2011
[GpsPrune.git] / tim / prune / data / RecentFileList.java
1 package tim.prune.data;
2
3 /**
4  * Class to hold and manage the list of recently used files
5  */
6 public class RecentFileList
7 {
8         private RecentFile[] _files = null;
9         private static final int DEFAULT_SIZE = 6;
10         private static final int MAX_SIZE = 20;
11
12         /**
13          * Default constructor
14          */
15         public RecentFileList()
16         {
17                 _files = new RecentFile[DEFAULT_SIZE];
18         }
19
20         /**
21          * Constructor
22          * @param inString String from config
23          */
24         public RecentFileList(String inString)
25         {
26                 _files = null;
27                 int pos = 0;
28                 if (inString != null && inString.length() > 0)
29                 {
30                         for (String s : inString.split(";"))
31                         {
32                                 if (pos == 0)
33                                 {
34                                         int listSize = DEFAULT_SIZE;
35                                         try
36                                         {
37                                                 listSize = Integer.parseInt(s);
38                                                 if (listSize < 1 || listSize > MAX_SIZE) {
39                                                         listSize = DEFAULT_SIZE;
40                                                 }
41                                         }
42                                         catch (NumberFormatException nfe) {}
43                                         _files = new RecentFile[listSize];
44                                         pos++;
45                                 }
46                                 else if (pos <= _files.length)
47                                 {
48                                         RecentFile rf = new RecentFile(s);
49                                         if (rf.isValid())
50                                         {
51                                                 _files[pos-1] = rf;
52                                                 pos++;
53                                         }
54                                 }
55                         }
56                 }
57                 if (_files == null) {
58                         _files = new RecentFile[DEFAULT_SIZE];
59                 }
60         }
61
62         /**
63          * @return size of list (may not have this many entries yet)
64          */
65         public int getSize()
66         {
67                 if (_files == null) return 0;
68                 return _files.length;
69         }
70
71         /**
72          * @return the number of valid entries in the list
73          */
74         public int getNumEntries()
75         {
76                 if (_files == null) return 0;
77                 int numFound = 0;
78                 for (RecentFile rf : _files) {
79                         if (rf != null && rf.isValid())
80                                 numFound++;
81                 }
82                 return numFound;
83         }
84
85         /**
86          * @return string to save in config
87          */
88         public String getConfigString()
89         {
90                 StringBuilder builder = new StringBuilder(100);
91                 int size = getSize();
92                 builder.append("" + size);
93                 for (RecentFile f : _files)
94                 {
95                         builder.append(';');
96                         if (f != null) builder.append(f.getConfigString());
97                 }
98                 return builder.toString();
99         }
100
101         /**
102          * Add the given file to the top of the list
103          * @param inRF file to add
104          */
105         public void addFile(RecentFile inRF)
106         {
107                 // Build a new array with the latest file at the top
108                 RecentFile[] files = new RecentFile[_files.length];
109                 int rfIndex = 0;
110                 if (inRF != null && inRF.isValid())
111                 {
112                         files[rfIndex] = inRF;
113                         rfIndex++;
114                 }
115                 // Loop, copying the other files
116                 for (RecentFile rf : _files)
117                 {
118                         if (rf != null && rf.isValid() && (inRF==null || !rf.isSameFile(inRF)))
119                         {
120                                 files[rfIndex] = rf;
121                                 rfIndex++;
122                                 if (rfIndex >= files.length) break;
123                         }
124                 }
125                 _files = files;
126         }
127
128         /**
129          * Verify all the entries and remove the invalid ones
130          */
131         public void verifyAll() {
132                 addFile(null);
133         }
134
135         /**
136          * Get the RecentFile object at the given index
137          * @param inIndex index, starting at 0
138          * @return RecentFile object or null if out of range
139          */
140         public RecentFile getFile(int inIndex)
141         {
142                 if (inIndex < 0 || inIndex >= _files.length) return null;
143                 return _files[inIndex];
144         }
145
146         /**
147          * Resize the list to the new size
148          * @param inNewSize new size of list
149          */
150         public void resizeList(int inNewSize)
151         {
152                 // don't do anything if size doesn't make sense
153                 if (inNewSize > 0 && inNewSize <= MAX_SIZE)
154                 {
155                         RecentFile[] files = new RecentFile[inNewSize];
156                         int numToCopy = _files.length;
157                         if (inNewSize < numToCopy) {
158                                 numToCopy = inNewSize;
159                         }
160                         System.arraycopy(_files, 0, files, 0, numToCopy);
161                         _files = files;
162                 }
163         }
164 }