JAL-3141 Preferences 'Backups' tab now working. Doesn't fit in default space though...
[jalview.git] / src / jalview / io / BackupFilenameFilter.java
1 package jalview.io;
2
3 import java.io.File;
4 import java.io.FilenameFilter;
5 import java.io.IOException;
6
7 public class BackupFilenameFilter implements FilenameFilter
8 {
9
10   public String base;
11
12   public String template;
13
14   public int digits;
15
16   public String extension;
17
18   public BackupFilenameFilter(String base, String template, int digits,
19           String extension)
20   {
21     this.base = base;
22     this.template = template;
23     this.digits = digits;
24     this.extension = extension;
25   }
26
27   @Override
28   public boolean accept(File dir, String filename)
29   {
30     boolean ret = false;
31     try
32     {
33       File file = new File(
34               dir.getCanonicalPath() + File.separatorChar + filename);
35       if (file.isDirectory())
36       {
37         // backup files aren't dirs!
38         return false;
39       }
40     } catch (IOException e)
41     {
42       System.out.println("IOException when checking file '" + filename
43               + "' is a backupfile");
44     }
45
46     BackupFilenameParts bffp = new BackupFilenameParts(filename, base,
47             template, digits, extension);
48     ret = bffp.isBackupFile();
49     return ret;
50   }
51
52   public static String getBackupFilename(int index, String base,
53           String template, int digits, String extension)
54   {
55     String numString = String.format("%0" + digits + "d", index);
56     String backupSuffix = template.replaceAll(BackupFiles.NUM_PLACEHOLDER,
57             numString);
58     String backupfilename = base + backupSuffix + extension;
59     return backupfilename;
60   }
61
62 }
63
64 class BackupFilenameParts
65 {
66   String base;
67
68   String templateStart;
69
70   int num;
71
72   int digits;
73
74   String templateEnd;
75
76   String extension;
77
78   boolean isBackupFile;
79
80   public BackupFilenameParts(File file, String base, String template, int digits,
81           String extension)
82   {
83     this(file.getName(), base, template, digits, extension);
84   }
85
86   public BackupFilenameParts(String filename, String base, String template,
87           int digits, String extension)
88   {
89     this.isBackupFile = false;
90
91     // calculate minimum length of a backup filename
92     int minlength = base.length() + template.length()
93             - BackupFiles.NUM_PLACEHOLDER.length() + digits
94             + extension.length();
95
96     if (!(filename.startsWith(base) && filename.endsWith(extension)
97             && filename.length() >= minlength))
98     {
99       // non-starter
100       return;
101     }
102
103     int numcharstart = template.indexOf(BackupFiles.NUM_PLACEHOLDER);
104     String templateStart = template;
105     String templateEnd = "";
106     if (numcharstart > -1)
107     {
108       templateStart = template.substring(0, numcharstart);
109       templateEnd = template.substring(numcharstart + BackupFiles.NUM_PLACEHOLDER.length());
110     }
111     
112     int startLength = base.length() + templateStart.length();
113     int endLength = templateEnd.length() + extension.length();
114     String numString = filename.substring(startLength,
115             filename.length() - endLength);
116
117     if (filename.length() >= startLength + digits + endLength
118             && filename.startsWith(base + templateStart)
119             && filename.endsWith(templateEnd + extension)
120             && numString.matches("[0-9]+"))
121     {
122       this.base = base;
123       this.templateStart = templateStart;
124       this.num = Integer.parseInt(numString);
125       this.digits = digits;
126       this.templateStart = templateStart;
127       this.templateEnd = templateEnd;
128       this.isBackupFile = true;
129     }
130     
131   }
132
133   public boolean isBackupFile()
134   {
135     return this.isBackupFile;
136   }
137
138   public int indexNum()
139   {
140     return this.num;
141   }
142 }