From db80eb8e1acf352e72a33e1e3825d40f7c6e4046 Mon Sep 17 00:00:00 2001 From: Ben Soares Date: Fri, 25 Jan 2019 17:32:37 +0000 Subject: [PATCH] JAL-3141. This adds quite a lot of code to allow an 'Include backup files' checkbox in the 'Load Project' JalviewFileChooser. However I cannot for the life of me get the file list in the FileChooser to refresh /reliably/ on a mac at least anyway (it does about 1 time in 9git status). --- resources/lang/Messages.properties | 4 +- resources/lang/Messages_es.properties | 4 +- src/jalview/gui/Desktop.java | 3 +- src/jalview/io/BackupFilenameFilter.java | 10 ---- src/jalview/io/BackupFilenameParts.java | 83 +++++++++++++++++++++++++++--- src/jalview/io/BackupFiles.java | 4 +- src/jalview/io/JalviewFileChooser.java | 82 ++++++++++++++++++++++++++++- src/jalview/io/JalviewFileFilter.java | 26 ++++++++++ src/jalview/io/JalviewFileView.java | 26 +++++++++- src/jalview/jbgui/GPreferences.java | 6 +-- 10 files changed, 219 insertions(+), 29 deletions(-) diff --git a/resources/lang/Messages.properties b/resources/lang/Messages.properties index 4e5baea..681f4e3 100644 --- a/resources/lang/Messages.properties +++ b/resources/lang/Messages.properties @@ -1365,6 +1365,7 @@ label.backupfiles_confirm_delete_old_files = Delete the following older backup f label.backupfiles_confirm_save_file = Confirm save file label.backupfiles_confirm_save_file_backupfiles_roll_wrong = Something possibly went wrong with the backups of this file, write the new file anyway? label.backups = Backups +label.backup = Backup label.backup_files = Backup Files label.enable_backupfiles = Enable backup files label.backup_filename_strategy = Backup filename strategy @@ -1392,4 +1393,5 @@ label.single_file = Single backup label.keep_all_versions = Keep all versions label.rolled_backups = Rolled backup files label.previously_saved_scheme = Previously saved scheme -label.no_backup_files = NO BACKUP FILES \ No newline at end of file +label.no_backup_files = NO BACKUP FILES +label.include_backup_files = Include backup files \ No newline at end of file diff --git a/resources/lang/Messages_es.properties b/resources/lang/Messages_es.properties index b55b19d..d3f8c41 100644 --- a/resources/lang/Messages_es.properties +++ b/resources/lang/Messages_es.properties @@ -1366,6 +1366,7 @@ label.backupfiles_confirm_delete_old_files = label.backupfiles_confirm_save_file = Confirmar guardar archivo label.backupfiles_confirm_save_file_backupfiles_roll_wrong = Posiblemente algo está mal con los archivos de copia de seguridad. ¿Guardar el nuevo archivo? label.backups = Respaldos +label.backup = Respaldo label.backup_files = Archivos de respaldos label.enable_backupfiles = Habilitar archivos de respaldos label.backup_filename_strategy = Estrategia de nombres de archivo de respaldos @@ -1393,4 +1394,5 @@ label.single_file = Solo uno respaldo label.keep_all_versions = Mantener todas las versiones label.rolled_backups = Ciclos resaldos label.previously_saved_scheme = Esquema previamente guardado -label.no_backup_files = NO ARCHIVOS DE RESPALDOS \ No newline at end of file +label.no_backup_files = NO ARCHIVOS DE RESPALDOS +label.include_backup_files = Incluir archivos de respaldos \ No newline at end of file diff --git a/src/jalview/gui/Desktop.java b/src/jalview/gui/Desktop.java index e9b08a7..4107157 100644 --- a/src/jalview/gui/Desktop.java +++ b/src/jalview/gui/Desktop.java @@ -1714,7 +1714,8 @@ public class Desktop extends jalview.jbgui.GDesktop "Jalview Project (old)" }; JalviewFileChooser chooser = new JalviewFileChooser( Cache.getProperty("LAST_DIRECTORY"), suffix, desc, - "Jalview Project"); + "Jalview Project", true, true); // last two booleans: allFiles, + // allowBackupFiles chooser.setFileView(new JalviewFileView()); chooser.setDialogTitle(MessageManager.getString("label.restore_state")); diff --git a/src/jalview/io/BackupFilenameFilter.java b/src/jalview/io/BackupFilenameFilter.java index 41c6859..573040f 100644 --- a/src/jalview/io/BackupFilenameFilter.java +++ b/src/jalview/io/BackupFilenameFilter.java @@ -43,14 +43,4 @@ public class BackupFilenameFilter implements FilenameFilter return bffp.isBackupFile(); } - public static String getBackupFilename(int index, String base, - String template, int digits) - { - String numString = String.format("%0" + digits + "d", index); - String backupSuffix = template.replaceAll(BackupFiles.NUM_PLACEHOLDER, - numString); - String backupfilename = base + backupSuffix; - return backupfilename; - } - } diff --git a/src/jalview/io/BackupFilenameParts.java b/src/jalview/io/BackupFilenameParts.java index 6f7da93..a4fbb3b 100644 --- a/src/jalview/io/BackupFilenameParts.java +++ b/src/jalview/io/BackupFilenameParts.java @@ -1,20 +1,27 @@ package jalview.io; +import jalview.bin.Cache; + import java.io.File; public class BackupFilenameParts { - String base; + private String base; - String templateStart; + private String templateStart; - int num; + private int num; - int digits; + private int digits; - String templateEnd; + private String templateEnd; - boolean isBackupFile; + private boolean isBackupFile; + + private BackupFilenameParts() + { + this.isBackupFile = false; + } public BackupFilenameParts(File file, String base, String template, int digits) @@ -25,6 +32,12 @@ public class BackupFilenameParts public BackupFilenameParts(String filename, String base, String template, int suggesteddigits) { + this(filename, base, template, suggesteddigits, false); + } + + public BackupFilenameParts(String filename, String base, String template, + int suggesteddigits, boolean extensionMatch) + { this.isBackupFile = false; int numcharstart = template.indexOf(BackupFiles.NUM_PLACEHOLDER); @@ -39,11 +52,33 @@ public class BackupFilenameParts digits = suggesteddigits; } + String savedFilename = ""; + // if extensionOnly is set then reset the filename to the last occurrence of the extension+templateStart and try the match + if (extensionMatch) + { + // only trying to match from extension onwards + + int extensioncharstart = filename + .lastIndexOf('.' + base + templateStart); + if (extensioncharstart == -1) + { + return; + } + + savedFilename = filename.substring(0, extensioncharstart + 1); // include + // the "." + filename = filename.substring(extensioncharstart + 1); + } + + // full filename match + // calculate minimum length of a backup filename int minlength = base.length() + template.length() - BackupFiles.NUM_PLACEHOLDER.length() + digits; - if (!(filename.startsWith(base) && filename.length() >= minlength)) + if (!(filename.startsWith(base + templateStart) + && filename.endsWith(templateEnd) + && filename.length() >= minlength)) { // non-starter return; @@ -60,7 +95,7 @@ public class BackupFilenameParts && filename.endsWith(templateEnd) && numString.matches("[0-9]*")) { - this.base = base; + this.base = extensionMatch ? savedFilename + base : base; this.templateStart = templateStart; this.num = numString.length() > 0 ? Integer.parseInt(numString) : 0; this.digits = digits; @@ -70,6 +105,28 @@ public class BackupFilenameParts } + public static BackupFilenameParts currentBackupFilenameParts( + String filename, String base, boolean extensionMatch) + { + BackupFilenameParts bfp = new BackupFilenameParts(); + String template = Cache.getDefault(BackupFiles.SUFFIX, null); + if (template == null) + { + return bfp; + } + int digits; + try + { + digits = Integer + .parseInt(Cache.getDefault(BackupFiles.SUFFIX_DIGITS, null)); + } catch (Exception e) + { + return bfp; + } + return new BackupFilenameParts(filename, base, template, digits, + extensionMatch); + } + public boolean isBackupFile() { return this.isBackupFile; @@ -79,4 +136,14 @@ public class BackupFilenameParts { return this.num; } + + public static String getBackupFilename(int index, String base, + String template, int digits) + { + String numString = String.format("%0" + digits + "d", index); + String backupSuffix = template.replaceFirst(BackupFiles.NUM_PLACEHOLDER, + numString); + String backupfilename = base + backupSuffix; + return backupfilename; + } } diff --git a/src/jalview/io/BackupFiles.java b/src/jalview/io/BackupFiles.java index 71b31a7..65232fd 100644 --- a/src/jalview/io/BackupFiles.java +++ b/src/jalview/io/BackupFiles.java @@ -280,7 +280,7 @@ public class BackupFiles { // int n = tempMax - m; String backupfilename = dir + File.separatorChar - + BackupFilenameFilter.getBackupFilename(n, basename, + + BackupFilenameParts.getBackupFilename(n, basename, suffix, digits); File backupfile_n = new File(backupfilename); @@ -352,7 +352,7 @@ public class BackupFiles // Let's make the new backup file!! yay, got there at last! String latestBackupFilename = dir + File.separatorChar - + BackupFilenameFilter.getBackupFilename(nextIndexNum, basename, + + BackupFilenameParts.getBackupFilename(nextIndexNum, basename, suffix, digits); File latestBackupFile = new File(latestBackupFilename); ret = ret && file.renameTo(latestBackupFile); diff --git a/src/jalview/io/JalviewFileChooser.java b/src/jalview/io/JalviewFileChooser.java index 192948d..7c3cf92 100755 --- a/src/jalview/io/JalviewFileChooser.java +++ b/src/jalview/io/JalviewFileChooser.java @@ -30,6 +30,8 @@ import java.awt.Component; import java.awt.Dimension; import java.awt.EventQueue; import java.awt.HeadlessException; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.io.File; @@ -38,7 +40,9 @@ import java.util.List; import java.util.StringTokenizer; import java.util.Vector; +import javax.swing.BoxLayout; import javax.swing.DefaultListCellRenderer; +import javax.swing.JCheckBox; import javax.swing.JFileChooser; import javax.swing.JList; import javax.swing.JPanel; @@ -58,6 +62,14 @@ import javax.swing.plaf.basic.BasicFileChooserUI; public class JalviewFileChooser extends JFileChooser { /** + * backupfilesCheckBox = "Include backup files" checkbox includeBackupfiles = + * flag set by checkbox + */ + private JCheckBox backupfilesCheckBox = null; + + protected boolean includeBackupFiles = false; + + /** * Factory method to return a file chooser that offers readable alignment file * formats * @@ -139,6 +151,12 @@ public class JalviewFileChooser extends JFileChooser JalviewFileChooser(String dir, String[] extensions, String[] descs, String selected, boolean allFiles) { + this(dir, extensions, descs, selected, allFiles, false); + } + + public JalviewFileChooser(String dir, String[] extensions, String[] descs, + String selected, boolean allFiles, boolean allowBackupFiles) + { super(safePath(dir)); if (extensions.length == descs.length) { @@ -147,7 +165,7 @@ public class JalviewFileChooser extends JFileChooser { formats.add(new String[] { extensions[i], descs[i] }); } - init(formats, selected, allFiles); + init(formats, selected, allFiles, allowBackupFiles); } else { @@ -181,6 +199,12 @@ public class JalviewFileChooser extends JFileChooser */ void init(List formats, String selected, boolean allFiles) { + init(formats, selected, allFiles, false); + } + + void init(List formats, String selected, boolean allFiles, + boolean allowBackupFiles) + { JalviewFileFilter chosen = null; @@ -191,6 +215,10 @@ public class JalviewFileChooser extends JFileChooser for (String[] format : formats) { JalviewFileFilter jvf = new JalviewFileFilter(format[0], format[1]); + if (allowBackupFiles) + { + jvf.setParentJFC(this); + } addChoosableFileFilter(jvf); if ((selected != null) && selected.equalsIgnoreCase(format[1])) { @@ -203,7 +231,57 @@ public class JalviewFileChooser extends JFileChooser setFileFilter(chosen); } - setAccessory(new RecentlyOpened()); + if (allowBackupFiles) + { + JPanel multi = new JPanel(); + multi.setLayout(new BoxLayout(multi, BoxLayout.PAGE_AXIS)); + if (backupfilesCheckBox == null) + { + try { + includeBackupFiles = Boolean.parseBoolean( + Cache.getProperty(BackupFiles.NS + "_FC_INCLUDE")); + } catch (Exception e) + { + includeBackupFiles = false; + } + backupfilesCheckBox = new JCheckBox( + MessageManager.getString("label.include_backup_files"), + includeBackupFiles); + backupfilesCheckBox.setAlignmentX(Component.CENTER_ALIGNMENT); + JalviewFileChooser jfc = this; + backupfilesCheckBox.addActionListener(new ActionListener() + { + @Override + public void actionPerformed(ActionEvent e) + { + includeBackupFiles = backupfilesCheckBox.isSelected(); + Cache.setProperty(BackupFiles.NS + "_FC_INCLUDE", + String.valueOf(includeBackupFiles)); + jfc.rescanCurrentDirectory(); + jfc.revalidate(); + jfc.repaint(); + // This is a kludge. Cannot find out how to get the file list to + // refresh! + /* + Object o = jfc.getClientProperty( + CHOOSABLE_FILE_FILTER_CHANGED_PROPERTY); + jfc.firePropertyChange(CHOOSABLE_FILE_FILTER_CHANGED_PROPERTY, + o, o); + */ + } + }); + } + multi.add(new RecentlyOpened()); + multi.add(backupfilesCheckBox); + setAccessory(multi); + } + else + { + // set includeBackupFiles=false to avoid other file choosers from picking + // up backup files (Just In Case) + includeBackupFiles = false; + setAccessory(new RecentlyOpened()); + } } @Override diff --git a/src/jalview/io/JalviewFileFilter.java b/src/jalview/io/JalviewFileFilter.java index 21f5b0f..8c6b802 100755 --- a/src/jalview/io/JalviewFileFilter.java +++ b/src/jalview/io/JalviewFileFilter.java @@ -41,6 +41,8 @@ public class JalviewFileFilter extends FileFilter private boolean useExtensionsInDescription = true; + private JalviewFileChooser parentJFC = null; + public JalviewFileFilter(String extension, String description) { StringTokenizer st = new StringTokenizer(extension, ","); @@ -81,6 +83,7 @@ public class JalviewFileFilter extends FileFilter @Override public boolean accept(File f) { + if (f != null) { String extension = getExtension(f); @@ -94,8 +97,25 @@ public class JalviewFileFilter extends FileFilter { return true; } + } + if (parentJFC != null && parentJFC.includeBackupFiles) + { + Iterator it = filters.keySet().iterator(); + while (it.hasNext()) + { + String ext = it.next(); + + BackupFilenameParts bfp = BackupFilenameParts + .currentBackupFilenameParts(f.getName(), ext, true); + if (bfp.isBackupFile()) + { + return true; + } + } + } + return false; } @@ -178,4 +198,10 @@ public class JalviewFileFilter extends FileFilter { return useExtensionsInDescription; } + + protected void setParentJFC(JalviewFileChooser p) + { + this.parentJFC = p; + } + } diff --git a/src/jalview/io/JalviewFileView.java b/src/jalview/io/JalviewFileView.java index 18114f3..857d043 100755 --- a/src/jalview/io/JalviewFileView.java +++ b/src/jalview/io/JalviewFileView.java @@ -20,9 +20,12 @@ */ package jalview.io; +import jalview.util.MessageManager; + import java.io.File; import java.net.URL; import java.util.HashMap; +import java.util.Iterator; import java.util.Map; import javax.swing.Icon; @@ -55,7 +58,9 @@ public class JalviewFileView extends FileView public String getTypeDescription(File f) { String extension = getExtension(f); + String type = getDescriptionForExtension(extension); + if (extension != null) { if (extensions.containsKey(extension)) @@ -84,8 +89,27 @@ public class JalviewFileView extends FileView { String extension = getExtension(f); Icon icon = null; + String type = getDescriptionForExtension(extension); + + if (type == null) + { + Iterator it = extensions.keySet().iterator(); + while (it.hasNext()) + { + String ext = it.next(); + BackupFilenameParts bfp = BackupFilenameParts + .currentBackupFilenameParts(f.getName(), ext, true); + if (bfp.isBackupFile()) + { + extension = ext; + type = getDescriptionForExtension(extension) + + MessageManager.getString("label.backup"); + break; + } + } + } - if (getDescriptionForExtension(extension) != null) + if (type != null) { icon = getImageIcon("/images/file.png"); } diff --git a/src/jalview/jbgui/GPreferences.java b/src/jalview/jbgui/GPreferences.java index 7c50674..cc3d001 100755 --- a/src/jalview/jbgui/GPreferences.java +++ b/src/jalview/jbgui/GPreferences.java @@ -27,7 +27,7 @@ import jalview.fts.service.pdb.PDBFTSRestClient; import jalview.gui.JalviewBooleanRadioButtons; import jalview.gui.JvSwingUtils; import jalview.gui.StructureViewer.ViewerType; -import jalview.io.BackupFilenameFilter; +import jalview.io.BackupFilenameParts; import jalview.io.BackupFiles; import jalview.util.MessageManager; @@ -2247,7 +2247,7 @@ public class GPreferences extends JPanel { exampleSB.append("\n"); } - exampleSB.append(BackupFilenameFilter.getBackupFilename(index, + exampleSB.append(BackupFilenameParts.getBackupFilename(index, base, suffix, digits)); if (min == max) { @@ -2300,7 +2300,7 @@ public class GPreferences extends JPanel { exampleSB.append("\n"); } - exampleSB.append(BackupFilenameFilter.getBackupFilename(index, + exampleSB.append(BackupFilenameParts.getBackupFilename(index, base, suffix, digits)); if (min == max) { -- 1.7.10.2