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