JAL-3141 Reworked a lot of BackupFiles to allow the Customise Checkbox
[jalview.git] / src / jalview / io / BackupFilesPresetEntry.java
diff --git a/src/jalview/io/BackupFilesPresetEntry.java b/src/jalview/io/BackupFilesPresetEntry.java
new file mode 100644 (file)
index 0000000..a5bea91
--- /dev/null
@@ -0,0 +1,166 @@
+package jalview.io;
+
+import jalview.bin.Cache;
+import jalview.util.MessageManager;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.StringTokenizer;
+
+public class BackupFilesPresetEntry
+{
+
+  public String suffix;
+
+  public static final int DIGITSMIN = 1;
+
+  public static final int DIGITSMAX = 6;
+
+  public int digits;
+
+  public boolean reverse;
+
+  public boolean keepAll;
+
+  public static final int ROLLMAXMIN = 1;
+
+  public static final int ROLLMAXMAX = 999;
+
+  public int rollMax;
+
+  public boolean confirmDelete;
+
+  public static final String SAVEDCONFIG = BackupFiles.NS + "_SAVED";
+
+  public static final String CUSTOMCONFIG = BackupFiles.NS + "_CUSTOM";
+
+  private static final String stringDelim = "\t";
+
+  public static final int BACKUPFILESSCHEMECUSTOM = 0;
+
+  public static final int BACKUPFILESSCHEMEDEFAULT = 1;
+
+  public BackupFilesPresetEntry(String suffix, int digits, boolean reverse,
+          boolean keepAll, int rollMax, boolean confirmDelete)
+  {
+    this.suffix = suffix == null ? "" : suffix;
+    this.digits = digits < DIGITSMIN ? DIGITSMIN
+            : (digits > DIGITSMAX ? DIGITSMAX : digits);
+    this.reverse = reverse;
+    this.keepAll = keepAll;
+    this.rollMax = rollMax < ROLLMAXMIN ? ROLLMAXMIN
+            : (rollMax > ROLLMAXMAX ? ROLLMAXMAX : rollMax);
+    this.confirmDelete = confirmDelete;
+  }
+
+  public boolean equals(BackupFilesPresetEntry compare)
+  {
+    return suffix.equals(compare.suffix) && digits == compare.digits
+            && reverse == compare.reverse && keepAll == compare.keepAll
+            && rollMax == compare.rollMax
+            && confirmDelete == compare.confirmDelete;
+  }
+
+  @Override
+  public String toString()
+  {
+    StringBuilder sb = new StringBuilder();
+    sb.append(suffix);
+    sb.append(stringDelim);
+    sb.append(digits);
+    sb.append(stringDelim);
+    sb.append(reverse);
+    sb.append(stringDelim);
+    sb.append(keepAll);
+    sb.append(stringDelim);
+    sb.append(rollMax);
+    sb.append(stringDelim);
+    sb.append(confirmDelete);
+    return sb.toString();
+  }
+
+  public static BackupFilesPresetEntry createBackupFilesPresetEntry(
+          String line)
+  {
+    if (line == null)
+    {
+      return null;
+    }
+    StringTokenizer st = new StringTokenizer(line, stringDelim);
+    String suffix = null;
+    int digits = 0;
+    boolean reverse = false;
+    boolean keepAll = false;
+    int rollMax = 0;
+    boolean confirmDelete = false;
+
+    try
+    {
+      suffix = st.nextToken();
+      digits = Integer.valueOf(st.nextToken());
+      reverse = Boolean.valueOf(st.nextToken());
+      keepAll = Boolean.valueOf(st.nextToken());
+      rollMax = Integer.valueOf(st.nextToken());
+      confirmDelete = Boolean.valueOf(st.nextToken());
+    } catch (Exception e)
+    {
+      System.out.println("Error parsing backupfiles scheme '" + line + "'");
+    }
+
+    return new BackupFilesPresetEntry(suffix, digits, reverse, keepAll,
+            rollMax, confirmDelete);
+  }
+
+  public static BackupFilesPresetEntry getSavedBackupEntry()
+  {
+    String savedPresetString = Cache
+            .getDefault(BackupFilesPresetEntry.SAVEDCONFIG, null);
+    BackupFilesPresetEntry savedPreset = BackupFilesPresetEntry
+            .createBackupFilesPresetEntry(savedPresetString);
+    if (savedPreset == null)
+    {
+      savedPreset = backupfilesPresetEntriesValues
+              .get(BACKUPFILESSCHEMEDEFAULT);
+    }
+    return savedPreset;
+  }
+
+  public static final IntKeyStringValueEntry[] backupfilesPresetEntries = {
+      new IntKeyStringValueEntry(BACKUPFILESSCHEMEDEFAULT,
+              MessageManager.getString("label.default")),
+      new IntKeyStringValueEntry(2,
+              MessageManager.getString("label.single_file")),
+      new IntKeyStringValueEntry(3,
+              MessageManager.getString("label.keep_all_versions")),
+      new IntKeyStringValueEntry(4,
+              MessageManager.getString("label.rolled_backups")),
+      // ...
+      // IMPORTANT, keep "Custom" entry with key 0 (even though it appears last)
+      new IntKeyStringValueEntry(BACKUPFILESSCHEMECUSTOM,
+              MessageManager.getString("label.custom")) };
+
+  public static final Map<Integer, BackupFilesPresetEntry> backupfilesPresetEntriesValues = new HashMap<>()
+  {
+    /**
+     * 
+     */
+    private static final long serialVersionUID = 125L;
+
+    {
+      put(1, new BackupFilesPresetEntry(
+              ".bak" + BackupFiles.NUM_PLACEHOLDER, 3, false, false, 3,
+              false));
+      put(2, new BackupFilesPresetEntry("~", 1, false, false, 1, false));
+      put(3, new BackupFilesPresetEntry(".v" + BackupFiles.NUM_PLACEHOLDER,
+              3, false, true, 10, true));
+      put(4, new BackupFilesPresetEntry(
+              "_bak." + BackupFiles.NUM_PLACEHOLDER, 1, true, false, 9,
+              false));
+
+      // This gets replaced by GPreferences
+      put(BACKUPFILESSCHEMECUSTOM,
+              new BackupFilesPresetEntry("", 0, false, false, 0, false));
+    }
+  };
+
+}