1 package tim.prune.data;
4 * Class to hold and manage the list of recently used files
6 public class RecentFileList
8 private RecentFile[] _files = null;
9 private static final int DEFAULT_SIZE = 6;
10 private static final int MAX_SIZE = 20;
15 public RecentFileList()
17 _files = new RecentFile[DEFAULT_SIZE];
22 * @param inString String from config
24 public RecentFileList(String inString)
28 if (inString != null && inString.length() > 0)
30 for (String s : inString.split(";"))
34 int listSize = DEFAULT_SIZE;
37 listSize = Integer.parseInt(s);
38 if (listSize < 1 || listSize > MAX_SIZE) {
39 listSize = DEFAULT_SIZE;
42 catch (NumberFormatException nfe) {}
43 _files = new RecentFile[listSize];
46 else if (pos <= _files.length)
48 RecentFile rf = new RecentFile(s);
58 _files = new RecentFile[DEFAULT_SIZE];
63 * @return size of list (may not have this many entries yet)
67 if (_files == null) return 0;
72 * @return the number of valid entries in the list
74 public int getNumEntries()
76 if (_files == null) return 0;
78 for (RecentFile rf : _files) {
79 if (rf != null && rf.isValid())
86 * @return string to save in config
88 public String getConfigString()
90 StringBuilder builder = new StringBuilder(100);
92 builder.append("" + size);
93 for (RecentFile f : _files)
96 if (f != null) builder.append(f.getConfigString());
98 return builder.toString();
102 * Add the given file to the top of the list
103 * @param inRF file to add
105 public void addFile(RecentFile inRF)
107 // Build a new array with the latest file at the top
108 RecentFile[] files = new RecentFile[_files.length];
110 if (inRF != null && inRF.isValid())
112 files[rfIndex] = inRF;
115 // Loop, copying the other files
116 for (RecentFile rf : _files)
118 if (rf != null && rf.isValid() && (inRF==null || !rf.isSameFile(inRF)))
122 if (rfIndex >= files.length) break;
129 * Verify all the entries and remove the invalid ones
131 public void verifyAll() {
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
140 public RecentFile getFile(int inIndex)
142 if (inIndex < 0 || inIndex >= _files.length) return null;
143 return _files[inIndex];
147 * Resize the list to the new size
148 * @param inNewSize new size of list
150 public void resizeList(int inNewSize)
152 // don't do anything if size doesn't make sense
153 if (inNewSize > 0 && inNewSize <= MAX_SIZE)
155 RecentFile[] files = new RecentFile[inNewSize];
156 int numToCopy = _files.length;
157 if (inNewSize < numToCopy) {
158 numToCopy = inNewSize;
160 System.arraycopy(_files, 0, files, 0, numToCopy);