JAL-3676 Removed BACKUPFILESPRESETENTRY logging for opening the file browser. Changed...
[jalview.git] / src / jalview / io / BackupFilesPresetEntry.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.io;
22
23 import java.util.HashMap;
24 import java.util.Map;
25 import java.util.StringTokenizer;
26
27 import jalview.bin.Cache;
28 import jalview.util.MessageManager;
29
30 public class BackupFilesPresetEntry
31 {
32
33   public String suffix;
34
35   public static final int DIGITSMIN = 1;
36
37   public static final int DIGITSMAX = 6;
38
39   public int digits;
40
41   public boolean reverse;
42
43   public boolean keepAll;
44
45   public static final int ROLLMAXMIN = 1;
46
47   public static final int ROLLMAXMAX = 999;
48
49   public int rollMax;
50
51   public boolean confirmDelete;
52
53   public static final String SAVEDCONFIG = BackupFiles.NS + "_SAVED";
54
55   public static final String CUSTOMCONFIG = BackupFiles.NS + "_CUSTOM";
56
57   private static final String stringDelim = "\t";
58
59   public static final int BACKUPFILESSCHEMECUSTOM = 0;
60
61   public static final int BACKUPFILESSCHEMEDEFAULT = 1;
62
63   public BackupFilesPresetEntry(String suffix, int digits, boolean reverse,
64           boolean keepAll, int rollMax, boolean confirmDelete)
65   {
66     this.suffix = suffix == null ? "" : suffix;
67     this.digits = digits < DIGITSMIN ? DIGITSMIN
68             : (digits > DIGITSMAX ? DIGITSMAX : digits);
69     this.reverse = reverse;
70     this.keepAll = keepAll;
71     this.rollMax = rollMax < ROLLMAXMIN ? ROLLMAXMIN
72             : (rollMax > ROLLMAXMAX ? ROLLMAXMAX : rollMax);
73     this.confirmDelete = confirmDelete;
74   }
75
76   public boolean equals(BackupFilesPresetEntry compare)
77   {
78     return suffix.equals(compare.suffix) && digits == compare.digits
79             && reverse == compare.reverse && keepAll == compare.keepAll
80             && rollMax == compare.rollMax
81             && confirmDelete == compare.confirmDelete;
82   }
83
84   @Override
85   public String toString()
86   {
87     StringBuilder sb = new StringBuilder();
88     sb.append(suffix);
89     sb.append(stringDelim);
90     sb.append(digits);
91     sb.append(stringDelim);
92     sb.append(reverse);
93     sb.append(stringDelim);
94     sb.append(keepAll);
95     sb.append(stringDelim);
96     sb.append(rollMax);
97     sb.append(stringDelim);
98     sb.append(confirmDelete);
99     return sb.toString();
100   }
101
102   public static BackupFilesPresetEntry createBackupFilesPresetEntry(
103           String line)
104   {
105     if (line == null)
106     {
107       return null;
108     }
109     StringTokenizer st = new StringTokenizer(line, stringDelim);
110     String suffix = null;
111     int digits = 0;
112     boolean reverse = false;
113     boolean keepAll = false;
114     int rollMax = 0;
115     boolean confirmDelete = false;
116
117     try
118     {
119       suffix = st.nextToken();
120       digits = Integer.valueOf(st.nextToken());
121       reverse = Boolean.valueOf(st.nextToken());
122       keepAll = Boolean.valueOf(st.nextToken());
123       rollMax = Integer.valueOf(st.nextToken());
124       confirmDelete = Boolean.valueOf(st.nextToken());
125     } catch (Exception e)
126     {
127       Cache.log.error("Error parsing backupfiles scheme '" + line + "'");
128     }
129
130     return new BackupFilesPresetEntry(suffix, digits, reverse, keepAll,
131             rollMax, confirmDelete);
132   }
133
134   public static BackupFilesPresetEntry getSavedBackupEntry()
135   {
136     String savedPresetString = Cache
137             .getDefault(BackupFilesPresetEntry.SAVEDCONFIG, null);
138     BackupFilesPresetEntry savedPreset = BackupFilesPresetEntry
139             .createBackupFilesPresetEntry(savedPresetString);
140     if (savedPreset == null)
141     {
142       savedPreset = backupfilesPresetEntriesValues
143               .get(BACKUPFILESSCHEMEDEFAULT);
144     }
145     return savedPreset;
146   }
147
148   public static final IntKeyStringValueEntry[] backupfilesPresetEntries = {
149       new IntKeyStringValueEntry(BACKUPFILESSCHEMEDEFAULT,
150               MessageManager.getString("label.default")),
151       new IntKeyStringValueEntry(2,
152               MessageManager.getString("label.single_file")),
153       new IntKeyStringValueEntry(3,
154               MessageManager.getString("label.keep_all_versions")),
155       new IntKeyStringValueEntry(4,
156               MessageManager.getString("label.rolled_backups")),
157       // ...
158       // IMPORTANT, keep "Custom" entry with key 0 (even though it appears last)
159       new IntKeyStringValueEntry(BACKUPFILESSCHEMECUSTOM,
160               MessageManager.getString("label.custom")) };
161
162   public static final String[] backupfilesPresetEntryDescriptions = {
163       MessageManager.getString("label.default_description"),
164       MessageManager.getString("label.single_file_description"),
165       MessageManager.getString("label.keep_all_versions_description"),
166       MessageManager.getString("label.rolled_backups_description"),
167       MessageManager.getString("label.custom_description") };
168
169   public static final Map<Integer, BackupFilesPresetEntry> backupfilesPresetEntriesValues = new HashMap<Integer, BackupFilesPresetEntry>()
170   {
171     /**
172      * 
173      */
174     private static final long serialVersionUID = 125L;
175
176     {
177       put(1, new BackupFilesPresetEntry(
178               ".bak" + BackupFiles.NUM_PLACEHOLDER, 3, false, false, 3,
179               false));
180       put(2, new BackupFilesPresetEntry("~", 1, false, false, 1, false));
181       put(3, new BackupFilesPresetEntry(".v" + BackupFiles.NUM_PLACEHOLDER,
182               3, false, true, 10, true));
183       put(4, new BackupFilesPresetEntry(
184               "_bak." + BackupFiles.NUM_PLACEHOLDER, 1, true, false, 9,
185               false));
186
187       // This gets replaced by GPreferences
188       put(BACKUPFILESSCHEMECUSTOM,
189               new BackupFilesPresetEntry("", 0, false, false, 0, false));
190     }
191   };
192
193 }