Merge branch 'documentation/JAL-3407_2.11.1_release' into releases/Release_2_11_1_Branch
[jalview.git] / src / jalview / io / BackupFilesPresetEntry.java
1 package jalview.io;
2
3 import jalview.bin.Cache;
4 import jalview.util.MessageManager;
5
6 import java.util.HashMap;
7 import java.util.Map;
8 import java.util.StringTokenizer;
9
10 public class BackupFilesPresetEntry
11 {
12
13   public String suffix;
14
15   public static final int DIGITSMIN = 1;
16
17   public static final int DIGITSMAX = 6;
18
19   public int digits;
20
21   public boolean reverse;
22
23   public boolean keepAll;
24
25   public static final int ROLLMAXMIN = 1;
26
27   public static final int ROLLMAXMAX = 999;
28
29   public int rollMax;
30
31   public boolean confirmDelete;
32
33   public static final String SAVEDCONFIG = BackupFiles.NS + "_SAVED";
34
35   public static final String CUSTOMCONFIG = BackupFiles.NS + "_CUSTOM";
36
37   private static final String stringDelim = "\t";
38
39   public static final int BACKUPFILESSCHEMECUSTOM = 0;
40
41   public static final int BACKUPFILESSCHEMEDEFAULT = 1;
42
43   public BackupFilesPresetEntry(String suffix, int digits, boolean reverse,
44           boolean keepAll, int rollMax, boolean confirmDelete)
45   {
46     this.suffix = suffix == null ? "" : suffix;
47     this.digits = digits < DIGITSMIN ? DIGITSMIN
48             : (digits > DIGITSMAX ? DIGITSMAX : digits);
49     this.reverse = reverse;
50     this.keepAll = keepAll;
51     this.rollMax = rollMax < ROLLMAXMIN ? ROLLMAXMIN
52             : (rollMax > ROLLMAXMAX ? ROLLMAXMAX : rollMax);
53     this.confirmDelete = confirmDelete;
54   }
55
56   public boolean equals(BackupFilesPresetEntry compare)
57   {
58     return suffix.equals(compare.suffix) && digits == compare.digits
59             && reverse == compare.reverse && keepAll == compare.keepAll
60             && rollMax == compare.rollMax
61             && confirmDelete == compare.confirmDelete;
62   }
63
64   @Override
65   public String toString()
66   {
67     StringBuilder sb = new StringBuilder();
68     sb.append(suffix);
69     sb.append(stringDelim);
70     sb.append(digits);
71     sb.append(stringDelim);
72     sb.append(reverse);
73     sb.append(stringDelim);
74     sb.append(keepAll);
75     sb.append(stringDelim);
76     sb.append(rollMax);
77     sb.append(stringDelim);
78     sb.append(confirmDelete);
79     return sb.toString();
80   }
81
82   public static BackupFilesPresetEntry createBackupFilesPresetEntry(
83           String line)
84   {
85     if (line == null)
86     {
87       return null;
88     }
89     StringTokenizer st = new StringTokenizer(line, stringDelim);
90     String suffix = null;
91     int digits = 0;
92     boolean reverse = false;
93     boolean keepAll = false;
94     int rollMax = 0;
95     boolean confirmDelete = false;
96
97     try
98     {
99       suffix = st.nextToken();
100       digits = Integer.valueOf(st.nextToken());
101       reverse = Boolean.valueOf(st.nextToken());
102       keepAll = Boolean.valueOf(st.nextToken());
103       rollMax = Integer.valueOf(st.nextToken());
104       confirmDelete = Boolean.valueOf(st.nextToken());
105     } catch (Exception e)
106     {
107       Cache.log.error("Error parsing backupfiles scheme '" + line + "'");
108     }
109
110     return new BackupFilesPresetEntry(suffix, digits, reverse, keepAll,
111             rollMax, confirmDelete);
112   }
113
114   public static BackupFilesPresetEntry getSavedBackupEntry()
115   {
116     String savedPresetString = Cache
117             .getDefault(BackupFilesPresetEntry.SAVEDCONFIG, null);
118     BackupFilesPresetEntry savedPreset = BackupFilesPresetEntry
119             .createBackupFilesPresetEntry(savedPresetString);
120     if (savedPreset == null)
121     {
122       savedPreset = backupfilesPresetEntriesValues
123               .get(BACKUPFILESSCHEMEDEFAULT);
124     }
125     return savedPreset;
126   }
127
128   public static final IntKeyStringValueEntry[] backupfilesPresetEntries = {
129       new IntKeyStringValueEntry(BACKUPFILESSCHEMEDEFAULT,
130               MessageManager.getString("label.default")),
131       new IntKeyStringValueEntry(2,
132               MessageManager.getString("label.single_file")),
133       new IntKeyStringValueEntry(3,
134               MessageManager.getString("label.keep_all_versions")),
135       new IntKeyStringValueEntry(4,
136               MessageManager.getString("label.rolled_backups")),
137       // ...
138       // IMPORTANT, keep "Custom" entry with key 0 (even though it appears last)
139       new IntKeyStringValueEntry(BACKUPFILESSCHEMECUSTOM,
140               MessageManager.getString("label.custom")) };
141
142   public static final String[] backupfilesPresetEntryDescriptions = {
143       MessageManager.getString("label.default_description"),
144       MessageManager.getString("label.single_file_description"),
145       MessageManager.getString("label.keep_all_versions_description"),
146       MessageManager.getString("label.rolled_backups_description"),
147       MessageManager.getString("label.custom_description") };
148
149   public static final Map<Integer, BackupFilesPresetEntry> backupfilesPresetEntriesValues = new HashMap<Integer, BackupFilesPresetEntry>()
150   {
151     /**
152      * 
153      */
154     private static final long serialVersionUID = 125L;
155
156     {
157       put(1, new BackupFilesPresetEntry(
158               ".bak" + BackupFiles.NUM_PLACEHOLDER, 3, false, false, 3,
159               false));
160       put(2, new BackupFilesPresetEntry("~", 1, false, false, 1, false));
161       put(3, new BackupFilesPresetEntry(".v" + BackupFiles.NUM_PLACEHOLDER,
162               3, false, true, 10, true));
163       put(4, new BackupFilesPresetEntry(
164               "_bak." + BackupFiles.NUM_PLACEHOLDER, 1, true, false, 9,
165               false));
166
167       // This gets replaced by GPreferences
168       put(BACKUPFILESSCHEMECUSTOM,
169               new BackupFilesPresetEntry("", 0, false, false, 0, false));
170     }
171   };
172
173 }