+ public boolean rollBackupsAndRenameTempFile()
+ {
+ boolean write = this.getWriteSuccess();
+
+ boolean roll = false;
+ if (write) {
+ roll = this.rollBackupFiles();
+ } else {
+ return false;
+ }
+
+ /*
+ * Not sure that this confirmation is desirable. By this stage the new file is
+ * already written successfully, but something (e.g. disk full) has happened while
+ * trying to roll the backup files, and most likely the filename needed will already
+ * be vacant so renaming the temp file is nearly always correct!
+ */
+ if (!roll)
+ {
+ int confirm = JvOptionPane.showConfirmDialog(Desktop.desktop,
+ MessageManager.getString(
+ "label.backupfiles_confirm_save_file_backupfiles_roll_wrong"),
+ MessageManager.getString("label.backupfiles_confirm_save_file"),
+ JvOptionPane.YES_NO_OPTION, JvOptionPane.WARNING_MESSAGE);
+
+ if (confirm == JvOptionPane.YES_OPTION)
+ {
+ roll = true;
+ }
+ }
+
+ boolean rename = false;
+ if (roll)
+ {
+ rename = this.renameTempFile();
+ }
+
+ return rename;
+ }
+
+ public static TreeMap<Integer, File> lsBackupFilesAsTreeMap(
+ String fileName,
+ String suffix, int digits)
+ {
+ File[] backupFiles = null;
+
+ File file = new File(fileName);
+
+ String dir = "";
+ File dirFile;
+ try
+ {
+ dirFile = file.getParentFile();
+ dir = dirFile.getCanonicalPath();
+ } catch (Exception e)
+ {
+ System.out.println(
+ "Could not get canonical path for file '" + file + "'");
+ return new TreeMap<>();
+ }
+
+ String filename = file.getName();
+ String basename = filename;
+ String extension = "";
+ int dotcharpos = filename.lastIndexOf('.');
+ // don't split of filenames with the last '.' at the very beginning or
+ // very end of the filename
+ if ((dotcharpos > 0) && (dotcharpos < filename.length() - 1))
+ {
+ basename = filename.substring(0, dotcharpos);
+ extension = filename.substring(dotcharpos); // NOTE this includes the '.'
+ }
+
+ // find existing backup files
+ BackupFilenameFilter bff = new BackupFilenameFilter(basename, suffix, digits, extension);
+ backupFiles = dirFile.listFiles(bff); // is clone needed?
+
+ // sort the backup files (based on integer found in the suffix) using a
+ // precomputed Hashmap for speed
+ HashMap<Integer, File> bfHashMap = new HashMap<>();
+ for (int i = 0; i < backupFiles.length; i++)
+ {
+ File f = backupFiles[i];
+ BackupFilenameParts bfp = new BackupFilenameParts(f, basename, suffix,
+ digits, extension);
+ bfHashMap.put(bfp.indexNum(), f);
+ }
+ TreeMap<Integer, File> bfTreeMap = new TreeMap<>();
+ bfTreeMap.putAll(bfHashMap);
+
+ return bfTreeMap;
+ }
+
+ public static File[] lsBackupFiles(String fileName, String suffix,
+ int digits)
+ {
+ TreeMap<Integer, File> bfTreeMap = lsBackupFilesAsTreeMap(fileName,
+ suffix, digits);
+ File[] backupFiles = new File[bfTreeMap.size()];
+ bfTreeMap.values().toArray(backupFiles);
+ return backupFiles;
+ }
+