From: Ben Soares Date: Fri, 24 May 2019 01:24:18 +0000 (+0100) Subject: JAL-3141 Reworked a lot of BackupFiles to allow the Customise Checkbox X-Git-Tag: Release_2_11_1_0~53^2~19 X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=682ebcd21b2123786463a3f56cc6f7c17f361bae;p=jalview.git JAL-3141 Reworked a lot of BackupFiles to allow the Customise Checkbox --- diff --git a/resources/lang/Messages.properties b/resources/lang/Messages.properties index 04e8982..345d9a7 100644 --- a/resources/lang/Messages.properties +++ b/resources/lang/Messages.properties @@ -1369,6 +1369,7 @@ label.configuration = Configuration label.configure_feature_tooltip = Click to configure variable colour or filters label.schemes = Schemes label.customise = Customise +label.custom = Custom label.default = Default label.single_file = Single backup label.keep_all_versions = Keep all versions diff --git a/resources/lang/Messages_es.properties b/resources/lang/Messages_es.properties index 9efeca1..b6a0c12 100644 --- a/resources/lang/Messages_es.properties +++ b/resources/lang/Messages_es.properties @@ -1369,7 +1369,8 @@ label.braced_newest = (mas nuevo) label.configuration = ConfiguraciĆ³n label.configure_feature_tooltip = Haga clic para configurar el color o los filtros label.schemes = Esquemas -label.customise = Personalizado +label.customise = Personalizar +label.custom = Personal label.default = Defecto label.single_file = Solo uno respaldo label.keep_all_versions = Mantener todas las versiones diff --git a/src/jalview/gui/APQHandlers.java b/src/jalview/gui/APQHandlers.java index 31f7a6d..5f6651c 100644 --- a/src/jalview/gui/APQHandlers.java +++ b/src/jalview/gui/APQHandlers.java @@ -86,7 +86,8 @@ public class APQHandlers QuitEvent e, QuitResponse r) { boolean confirmQuit = jalview.bin.Cache - .getDefault(jalviewDesktop.CONFIRM_KEYBOARD_QUIT, + .getDefault( + jalview.gui.Desktop.CONFIRM_KEYBOARD_QUIT, true); int n; if (confirmQuit) diff --git a/src/jalview/gui/Desktop.java b/src/jalview/gui/Desktop.java index 804e531..ac5ed6d 100644 --- a/src/jalview/gui/Desktop.java +++ b/src/jalview/gui/Desktop.java @@ -1299,8 +1299,6 @@ public class Desktop extends jalview.jbgui.GDesktop @Override public void quit() { - //System.out.println("********** Desktop.quit()"); - //System.out.println(savingFiles.toString()); Dimension screen = Toolkit.getDefaultToolkit().getScreenSize(); jalview.bin.Cache.setProperty("SCREENGEOMETRY_WIDTH", screen.width + ""); diff --git a/src/jalview/gui/Preferences.java b/src/jalview/gui/Preferences.java index ab09f74..25e22b5 100755 --- a/src/jalview/gui/Preferences.java +++ b/src/jalview/gui/Preferences.java @@ -25,6 +25,7 @@ import jalview.bin.Cache; import jalview.gui.Help.HelpId; import jalview.gui.StructureViewer.ViewerType; import jalview.io.BackupFiles; +import jalview.io.BackupFilesPresetEntry; import jalview.io.FileFormatI; import jalview.io.JalviewFileChooser; import jalview.io.JalviewFileView; @@ -400,7 +401,7 @@ public class Preferences extends GPreferences doReset.addActionListener(onReset); // filter to display only custom urls - final RowFilter customUrlFilter = new RowFilter() + final RowFilter customUrlFilter = new RowFilter<>() { @Override public boolean include( @@ -805,22 +806,25 @@ public class Preferences extends GPreferences /* * Save Backups settings */ - Cache.applicationProperties.setProperty(BackupFiles.CONFIRM_DELETE_OLD, - Boolean.toString(backupfilesConfirmDelete.isSelected())); Cache.applicationProperties.setProperty(BackupFiles.ENABLED, Boolean.toString(enableBackupFiles.isSelected())); - Cache.applicationProperties.setProperty(BackupFiles.NO_MAX, - Boolean.toString(backupfilesKeepAll.isSelected())); - Cache.applicationProperties.setProperty(BackupFiles.REVERSE_ORDER, - Boolean.toString(suffixReverse.isSelected())); - Cache.applicationProperties.setProperty(BackupFiles.SUFFIX, - suffixTemplate.getText()); - Cache.applicationProperties.setProperty(BackupFiles.ROLL_MAX, - Integer.toString(getSpinnerInt(backupfilesRollMaxSpinner, 4))); - Cache.applicationProperties.setProperty(BackupFiles.SUFFIX_DIGITS, - Integer.toString(getSpinnerInt(suffixDigitsSpinner, 3))); - Cache.applicationProperties.setProperty(BackupFiles.NS+"_PRESET", - Integer.toString(getComboIntStringKey(backupfilesPresetsCombo))); + int preset = getComboIntStringKey(backupfilesPresetsCombo); + Cache.applicationProperties.setProperty(BackupFiles.NS + "_PRESET", Integer.toString(preset)); + + if (preset == BackupFilesPresetEntry.BACKUPFILESSCHEMECUSTOM) + { + BackupFilesPresetEntry customBFPE = getBackupfilesCurrentEntry(); + BackupFilesPresetEntry.backupfilesPresetEntriesValues.put( + BackupFilesPresetEntry.BACKUPFILESSCHEMECUSTOM, customBFPE); + Cache.applicationProperties + .setProperty(BackupFilesPresetEntry.CUSTOMCONFIG, + customBFPE.toString()); + } + + BackupFilesPresetEntry savedBFPE = BackupFilesPresetEntry.backupfilesPresetEntriesValues + .get(preset); + Cache.applicationProperties.setProperty( + BackupFilesPresetEntry.SAVEDCONFIG, savedBFPE.toString()); Cache.saveProperties(); Desktop.instance.doConfigureStructurePrefs(); diff --git a/src/jalview/io/BackupFilenameParts.java b/src/jalview/io/BackupFilenameParts.java index 4f93ece..1504404 100644 --- a/src/jalview/io/BackupFilenameParts.java +++ b/src/jalview/io/BackupFilenameParts.java @@ -1,7 +1,5 @@ package jalview.io; -import jalview.bin.Cache; - import java.io.File; public class BackupFilenameParts @@ -113,7 +111,9 @@ public class BackupFilenameParts String filename, String base, boolean extensionMatch) { BackupFilenameParts bfp = new BackupFilenameParts(); - String template = Cache.getDefault(BackupFiles.SUFFIX, null); + BackupFilesPresetEntry bfpe = BackupFilesPresetEntry + .getSavedBackupEntry(); + String template = bfpe.suffix; if (template == null) { return bfp; @@ -121,8 +121,7 @@ public class BackupFilenameParts int digits; try { - digits = Integer - .parseInt(Cache.getDefault(BackupFiles.SUFFIX_DIGITS, null)); + digits = bfpe.digits; } catch (Exception e) { return bfp; diff --git a/src/jalview/io/BackupFiles.java b/src/jalview/io/BackupFiles.java index dbda022..7bb5d31 100644 --- a/src/jalview/io/BackupFiles.java +++ b/src/jalview/io/BackupFiles.java @@ -17,12 +17,8 @@ import java.util.TreeMap; * BackupFiles used for manipulating (naming rolling/deleting) backup/version files when an alignment or project file is saved. * User configurable options are: * BACKUPFILES_ENABLED - boolean flag as to whether to use this mechanism or act as before, including overwriting files as saved. - * BACKUPFILES_SUFFIX - a template to insert after the file extension. Use '%n' to be replaced by a 0-led SUFFIX_DIGITS long integer. - * BACKUPFILES_NO_MAX - flag to turn off setting a maximum number of backup files to keep. - * BACKUPFILES_ROLL_MAX - the maximum number of backupfiles to keep for any one alignment or project file. - * BACKUPFILES_SUFFIX_DIGITS - the number of digits to insert replace %n with (e.g. BACKUPFILES_SUFFIX_DIGITS = 3 would make "001", "002", etc) - * BACKUPFILES_REVERSE_ORDER - if true then "logfile" style numbering and file rolling will occur. If false then ever-increasing version numbering will occur, but old files will still be deleted if there are more than ROLL_MAX backup files. - * BACKUPFILES_CONFIRM_DELETE_OLD - if true then prompt/confirm with the user when deleting older backup/version files. + * The rest of the options are now saved as BACKUPFILES_PRESET, BACKUPFILES_SAVED and BACKUPFILES_CUSTOM + * (see BackupFilesPresetEntry) */ public class BackupFiles @@ -33,21 +29,8 @@ public class BackupFiles public static final String ENABLED = NS + "_ENABLED"; - public static final String SUFFIX = NS + "_SUFFIX"; - - public static final String NO_MAX = NS + "_NO_MAX"; - - public static final String ROLL_MAX = NS + "_ROLL_MAX"; - - public static final String SUFFIX_DIGITS = NS + "_SUFFIX_DIGITS"; - public static final String NUM_PLACEHOLDER = "%n"; - public static final String REVERSE_ORDER = NS + "_REVERSE_ORDER"; - - public static final String CONFIRM_DELETE_OLD = NS - + "_CONFIRM_DELETE_OLD"; - private static final String DEFAULT_TEMP_FILE = "jalview_temp_file_" + NS; private static final String TEMP_FILE_EXT = ".tmp"; @@ -101,20 +84,14 @@ public class BackupFiles // REVERSE_ORDER public BackupFiles(File file) { - this(file, ".bak" + NUM_PLACEHOLDER, false, 3, 3, false); - } - - public BackupFiles(File file, String defaultSuffix, boolean defaultNoMax, - int defaultMax, int defaultDigits, boolean defaultReverseOrder) - { classInit(); this.file = file; - this.suffix = Cache.getDefault(SUFFIX, defaultSuffix); - this.noMax = Cache.getDefault(NO_MAX, defaultNoMax); - this.max = Cache.getDefault(ROLL_MAX, defaultMax); - this.digits = Cache.getDefault(SUFFIX_DIGITS, defaultDigits); - this.reverseOrder = Cache.getDefault(REVERSE_ORDER, - defaultReverseOrder); + BackupFilesPresetEntry bfpe = BackupFilesPresetEntry.getSavedBackupEntry(); + this.suffix = bfpe.suffix; + this.noMax = bfpe.keepAll; + this.max = bfpe.rollMax; + this.digits = bfpe.digits; + this.reverseOrder = bfpe.reverse; // create a temp file to save new data in File temp = null; @@ -145,7 +122,9 @@ public class BackupFiles public static void classInit() { setEnabled(Cache.getDefault(ENABLED, true)); - setConfirmDelete(Cache.getDefault(CONFIRM_DELETE_OLD, true)); + BackupFilesPresetEntry bfpe = BackupFilesPresetEntry + .getSavedBackupEntry(); + setConfirmDelete(bfpe.confirmDelete); } public static void setEnabled(boolean flag) diff --git a/src/jalview/io/BackupFilesPresetEntry.java b/src/jalview/io/BackupFilesPresetEntry.java new file mode 100644 index 0000000..a5bea91 --- /dev/null +++ b/src/jalview/io/BackupFilesPresetEntry.java @@ -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 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)); + } + }; + +} diff --git a/src/jalview/io/IntKeyStringValueEntry.java b/src/jalview/io/IntKeyStringValueEntry.java new file mode 100644 index 0000000..f246ad2 --- /dev/null +++ b/src/jalview/io/IntKeyStringValueEntry.java @@ -0,0 +1,31 @@ +package jalview.io; + +public class IntKeyStringValueEntry +{ + public int k; + + public String v; + + public IntKeyStringValueEntry(int k, String v) + { + this.k = k; + this.v = v; + } + + @Override + public String toString() + { + return this.getValue(); + } + + public int getKey() + { + return k; + } + + public String getValue() + { + return v; + } + +} diff --git a/src/jalview/jbgui/GPreferences.java b/src/jalview/jbgui/GPreferences.java index da13e9e..c132e49 100755 --- a/src/jalview/jbgui/GPreferences.java +++ b/src/jalview/jbgui/GPreferences.java @@ -31,6 +31,8 @@ import jalview.gui.JvSwingUtils; import jalview.gui.StructureViewer.ViewerType; import jalview.io.BackupFilenameParts; import jalview.io.BackupFiles; +import jalview.io.BackupFilesPresetEntry; +import jalview.io.IntKeyStringValueEntry; import jalview.util.MessageManager; import java.awt.BorderLayout; @@ -51,8 +53,6 @@ import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; -import java.util.HashMap; -import java.util.Map; import javax.swing.AbstractCellEditor; import javax.swing.BorderFactory; @@ -63,7 +63,6 @@ import javax.swing.JCheckBox; import javax.swing.JComboBox; import javax.swing.JFileChooser; import javax.swing.JLabel; -import javax.swing.JList; import javax.swing.JPanel; import javax.swing.JRadioButton; import javax.swing.JScrollPane; @@ -298,10 +297,14 @@ public class GPreferences extends JPanel protected JLabel presetsComboLabel = new JLabel(); + protected JCheckBox customiseCheckbox = new JCheckBox(); + protected JButton revertButton = new JButton(); protected JComboBox backupfilesPresetsCombo = new JComboBox<>(); + private int backupfilesPresetsComboLastSelected = 0; + protected JPanel suffixPanel = new JPanel(); protected JPanel keepfilesPanel = new JPanel(); @@ -1697,34 +1700,54 @@ public class GPreferences extends JPanel protected void loadLastSavedBackupsOptions() { + BackupFilesPresetEntry savedPreset = BackupFilesPresetEntry + .getSavedBackupEntry(); enableBackupFiles .setSelected(Cache.getDefault(BackupFiles.ENABLED, true)); + + BackupFilesPresetEntry backupfilesCustomEntry = BackupFilesPresetEntry + .createBackupFilesPresetEntry(Cache + .getDefault(BackupFilesPresetEntry.CUSTOMCONFIG, null)); + if (backupfilesCustomEntry == null) + { + backupfilesCustomEntry = BackupFilesPresetEntry.backupfilesPresetEntriesValues + .get(BackupFilesPresetEntry.BACKUPFILESSCHEMEDEFAULT); + } + BackupFilesPresetEntry.backupfilesPresetEntriesValues.put( + BackupFilesPresetEntry.BACKUPFILESSCHEMECUSTOM, + backupfilesCustomEntry); + setComboIntStringKey(backupfilesPresetsCombo, - Cache.getDefault(BackupFiles.NS + "_PRESET", 1)); - suffixTemplate.setText(Cache.getDefault(BackupFiles.SUFFIX, - ".bak" + BackupFiles.NUM_PLACEHOLDER)); - suffixDigitsSpinner - .setValue(Cache.getDefault(BackupFiles.SUFFIX_DIGITS, 3)); - suffixReverse.setSelected( - Cache.getDefault(BackupFiles.REVERSE_ORDER, false)); - backupfilesKeepAll - .setSelected(Cache.getDefault(BackupFiles.NO_MAX, false)); - backupfilesRollMaxSpinner - .setValue(Cache.getDefault(BackupFiles.ROLL_MAX, 3)); - backupfilesConfirmDelete.setSelected( - Cache.getDefault(BackupFiles.CONFIRM_DELETE_OLD, true)); + Cache.getDefault(BackupFiles.NS + "_PRESET", + BackupFilesPresetEntry.BACKUPFILESSCHEMEDEFAULT)); + // backupsTabUpdatePresets(); + + System.out + .println("LOADLASTSAVEDBACKUPSOPTIONS SETTING BACKUPS OPTIONS"); + backupsSetOptions(savedPreset); backupsOptionsSetEnabled(); updateBackupFilesExampleLabel(); } + private void setBackupfilesCustomPreset() + { + if (customiseCheckbox.isSelected() && getComboIntStringKey( + backupfilesPresetsCombo) == BackupFilesPresetEntry.BACKUPFILESSCHEMECUSTOM) + { + BackupFilesPresetEntry.backupfilesPresetEntriesValues + .put(BackupFilesPresetEntry.BACKUPFILESSCHEMECUSTOM, + getBackupfilesCurrentEntry()); + } + } + private boolean warnAboutSuffixReverseChange() { - boolean savedSuffixReverse = Cache.getDefault(BackupFiles.REVERSE_ORDER, - false); - int savedSuffixDigits = Cache.getDefault(BackupFiles.SUFFIX_DIGITS, 3); - String savedSuffixTemplate = Cache.getDefault(BackupFiles.SUFFIX, - ".bak" + BackupFiles.NUM_PLACEHOLDER); + BackupFilesPresetEntry bfpe = BackupFilesPresetEntry + .getSavedBackupEntry(); + boolean savedSuffixReverse = bfpe.reverse; + int savedSuffixDigits = bfpe.digits; + String savedSuffixTemplate = bfpe.suffix; boolean nowSuffixReverse = suffixReverse.isSelected(); int nowSuffixDigits = getSpinnerInt(suffixDigitsSpinner, 3); @@ -1823,42 +1846,6 @@ public class GPreferences extends JPanel return backupsTab; } - protected static final int BACKUPFILESSCHEMECUSTOMISE = 0; - - private static final IntKeyStringValueEntry[] backupfilesPresetEntries = { - new IntKeyStringValueEntry(1, - 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(BACKUPFILESSCHEMECUSTOMISE, - MessageManager.getString("label.customise")) }; - - private static final Map 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)); - } - }; - private JPanel initBackupsTabPresetsPanel() { @@ -1885,22 +1872,41 @@ public class GPreferences extends JPanel presetsComboLabel = new JLabel(title + ":"); presetsPanel.add(presetsComboLabel, gbc); - for (int i = 0; i < backupfilesPresetEntries.length; i++) + for (int i = 0; i < BackupFilesPresetEntry.backupfilesPresetEntries.length; i++) { - backupfilesPresetsCombo.addItem(backupfilesPresetEntries[i]); + backupfilesPresetsCombo + .addItem(BackupFilesPresetEntry.backupfilesPresetEntries[i]); } - // put "Previously saved scheme" item in italics (it's not really - // selectable, as such -- it deselects itself when selected) and - // "Customise" in bold - backupfilesPresetsCombo - .setRenderer(new BackupFilesPresetsComboBoxRenderer()); backupfilesPresetsCombo.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - backupsTabUpdatePresets(); + int key = getComboIntStringKey(backupfilesPresetsCombo); + if (!customiseCheckbox.isSelected()) + { + backupfilesPresetsComboLastSelected = key; + } + if (key == BackupFilesPresetEntry.BACKUPFILESSCHEMECUSTOM) + { + if (customiseCheckbox.isSelected()) + { + // got here by clicking on customiseCheckbox so don't change the values + backupfilesCustomOptionsSetEnabled(); + } + else + { + backupsTabUpdatePresets(); + backupfilesCustomOptionsSetEnabled(); + } + } + else + { + customiseCheckbox.setSelected(false); + backupsTabUpdatePresets(); + backupfilesCustomOptionsSetEnabled(); + } } }); @@ -1914,15 +1920,46 @@ public class GPreferences extends JPanel @Override public void actionPerformed(ActionEvent e) { - loadLastSavedBackupsOptions(); + backupsSetOptions( + BackupFilesPresetEntry.backupfilesPresetEntriesValues.get( + BackupFilesPresetEntry.BACKUPFILESSCHEMECUSTOM)); + backupfilesCustomOptionsSetEnabled(); } }); revertButton.setFont(LABEL_FONT); + customiseCheckbox.setFont(LABEL_FONT); + customiseCheckbox.setText(MessageManager.getString("label.customise")); + customiseCheckbox.addActionListener(new ActionListener() + { + @Override + public void actionPerformed(ActionEvent e) + { + int currently = getComboIntStringKey(backupfilesPresetsCombo); + if (customiseCheckbox.isSelected()) + { + backupfilesPresetsComboLastSelected = currently; + setComboIntStringKey(backupfilesPresetsCombo, + BackupFilesPresetEntry.BACKUPFILESSCHEMECUSTOM); + } + else + { + setComboIntStringKey(backupfilesPresetsCombo, + backupfilesPresetsComboLastSelected); + + } + backupfilesCustomOptionsSetEnabled(); + } + }); + + // customise checkbox + gbc.gridx = 0; + gbc.gridy++; + presetsPanel.add(customiseCheckbox, gbc); + // "Cancel changes" button (aligned with combo box above) gbc.gridx = 1; - gbc.gridy++; presetsPanel.add(revertButton, gbc); return presetsPanel; @@ -1960,20 +1997,22 @@ public class GPreferences extends JPanel int key = entry.getKey(); String value = entry.getValue(); - // BACKUPFILESSCHEMECUSTOMISE (==0) reserved for "Custom" - if (key != BACKUPFILESSCHEMECUSTOMISE) + if (BackupFilesPresetEntry.backupfilesPresetEntriesValues + .containsKey(key)) { - if (backupfilesPresetEntriesValues.containsKey(key)) - { - backupsSetOptions(backupfilesPresetEntriesValues.get(key)); - } - else - { - System.out.println("Preset '" + value + "' not implemented"); - } + backupsSetOptions( + BackupFilesPresetEntry.backupfilesPresetEntriesValues + .get(key)); + } + else + { + System.out.println( + "Preset '" + value + "' [key:" + key + "] not implemented"); } - backupfilesCustomOptionsSetEnabled(); + // Custom options will now be enabled when the customiseCheckbox is checked + // (performed above) + // backupfilesCustomOptionsSetEnabled(); updateBackupFilesExampleLabel(); } @@ -1995,7 +2034,7 @@ public class GPreferences extends JPanel break; } } - backupsTabUpdatePresets(); + // backupsTabUpdatePresets(); } private JPanel initBackupsTabSuffixPanel() @@ -2019,16 +2058,18 @@ public class GPreferences extends JPanel { updateBackupFilesExampleLabel(); backupfilesCustomOptionsSetEnabled(); + backupfilesRevertButtonSetEnabled(true); } }); - KeyListener kl = new KeyListener() + suffixTemplate.addKeyListener(new KeyListener() { @Override public void keyReleased(KeyEvent e) { updateBackupFilesExampleLabel(); backupfilesCustomOptionsSetEnabled(); + backupfilesRevertButtonSetEnabled(true); } @Override @@ -2040,6 +2081,7 @@ public class GPreferences extends JPanel @Override public void keyTyped(KeyEvent e) { + int count = 300; char c = e.getKeyChar(); if (c == ':' || c == '/' || c == '\\') { @@ -2048,26 +2090,25 @@ public class GPreferences extends JPanel } } - }; - suffixTemplate.addKeyListener(kl); + }); // digits spinner suffixDigitsLabel .setText(MessageManager.getString("label.index_digits")); suffixDigitsLabel.setHorizontalAlignment(SwingConstants.LEFT); suffixDigitsLabel.setFont(LABEL_FONT); - int defaultmin = 1; - int defaultmax = 6; ChangeListener c = new ChangeListener() { @Override public void stateChanged(ChangeEvent e) { + backupfilesRevertButtonSetEnabled(true); updateBackupFilesExampleLabel(); } }; - setIntegerSpinner(suffixDigitsSpinner, defaultmin, defaultmax, 3, c); + setIntegerSpinner(suffixDigitsSpinner, BackupFilesPresetEntry.DIGITSMIN, + BackupFilesPresetEntry.DIGITSMAX, 3, c); suffixReverse.setLabels(MessageManager.getString("label.reverse_roll"), MessageManager.getString("label.increment_index")); @@ -2084,12 +2125,13 @@ public class GPreferences extends JPanel } if (okay) { + backupfilesRevertButtonSetEnabled(true); updateBackupFilesExampleLabel(); } else { - boolean savedSuffixReverse = Cache - .getDefault(BackupFiles.REVERSE_ORDER, false); + boolean savedSuffixReverse = BackupFilesPresetEntry + .getSavedBackupEntry().reverse; suffixReverse.setSelected(savedSuffixReverse); } } @@ -2165,6 +2207,7 @@ public class GPreferences extends JPanel @Override public void actionPerformed(ActionEvent e) { + backupfilesRevertButtonSetEnabled(true); updateBackupFilesExampleLabel(); } }); @@ -2173,6 +2216,7 @@ public class GPreferences extends JPanel @Override public void actionPerformed(ActionEvent e) { + backupfilesRevertButtonSetEnabled(true); keepRollMaxOptionsEnabled(); updateBackupFilesExampleLabel(); } @@ -2183,15 +2227,26 @@ public class GPreferences extends JPanel @Override public void stateChanged(ChangeEvent e) { + backupfilesRevertButtonSetEnabled(true); updateBackupFilesExampleLabel(); } }; - setIntegerSpinner(backupfilesRollMaxSpinner, 1, 999, 4, true, c); + setIntegerSpinner(backupfilesRollMaxSpinner, + BackupFilesPresetEntry.ROLLMAXMIN, + BackupFilesPresetEntry.ROLLMAXMAX, 4, true, c); backupfilesConfirmDelete.setLabels( MessageManager.getString("label.always_ask"), MessageManager.getString("label.auto_delete")); + backupfilesConfirmDelete.addActionListener(new ActionListener() + { + @Override + public void actionPerformed(ActionEvent e) + { + backupfilesRevertButtonSetEnabled(true); + } + }); // update the enabled section keepRollMaxOptionsEnabled(); @@ -2457,7 +2512,7 @@ public class GPreferences extends JPanel private void backupfilesKeepAllSetEnabled(boolean tryEnabled) { boolean enabled = tryEnabled && enableBackupFiles.isSelected() - && getComboIntStringKey(backupfilesPresetsCombo) == 0 + && customiseCheckbox.isSelected() && suffixTemplate.getText() .indexOf(BackupFiles.NUM_PLACEHOLDER) > -1; keepfilesPanel.setEnabled(enabled); @@ -2478,18 +2533,52 @@ public class GPreferences extends JPanel private void backupfilesSuffixTemplateSetEnabled(boolean tryEnabled) { boolean enabled = tryEnabled && enableBackupFiles.isSelected() - && getComboIntStringKey(backupfilesPresetsCombo) == 0; + && customiseCheckbox.isSelected(); suffixPanel.setEnabled(enabled); suffixTemplateLabel.setEnabled(enabled); suffixTemplate.setEnabled(enabled); backupfilesSuffixTemplateDigitsSetEnabled(); } + private void backupfilesRevertButtonSetEnabled(boolean tryEnabled) + { + boolean enabled = tryEnabled && enableBackupFiles.isSelected() + && customiseCheckbox.isSelected() && backupfilesCustomChanged(); + revertButton.setEnabled(enabled); + } + + private boolean backupfilesCustomChanged() + { + BackupFilesPresetEntry custom = BackupFilesPresetEntry.backupfilesPresetEntriesValues + .get(BackupFilesPresetEntry.BACKUPFILESSCHEMECUSTOM); + BackupFilesPresetEntry current = getBackupfilesCurrentEntry(); + return !custom.equals(current); + } + + protected BackupFilesPresetEntry getBackupfilesCurrentEntry() + { + String suffix = suffixTemplate.getText(); + int digits = getSpinnerInt(suffixDigitsSpinner, 3); + boolean reverse = suffixReverse.isSelected(); + boolean keepAll = backupfilesKeepAll.isSelected(); + int rollMax = getSpinnerInt(backupfilesRollMaxSpinner, 3); + boolean confirmDelete = backupfilesConfirmDelete.isSelected(); + + BackupFilesPresetEntry bfpe = new BackupFilesPresetEntry(suffix, digits, + reverse, keepAll, rollMax, confirmDelete); + + return bfpe; + } + protected void backupfilesCustomOptionsSetEnabled() { - int scheme = getComboIntStringKey(backupfilesPresetsCombo); - boolean enabled = scheme == 0 && enableBackupFiles.isSelected(); + // int scheme = getComboIntStringKey(backupfilesPresetsCombo); + // boolean enabled = scheme == BackupFilesPresetEntry.BACKUPFILESSCHEMECUSTOM && + // enableBackupFiles.isSelected(); + boolean enabled = customiseCheckbox.isSelected(); + //boolean enabled = customiseCheckbox.isSelected() && enableBackupFiles.isSelected(); + backupfilesRevertButtonSetEnabled(enabled); backupfilesSuffixTemplateSetEnabled(enabled); backupfilesKeepAllSetEnabled(enabled); } @@ -2507,6 +2596,7 @@ public class GPreferences extends JPanel presetsPanel.setEnabled(enabled); presetsComboLabel.setEnabled(enabled); backupfilesPresetsCombo.setEnabled(enabled); + customiseCheckbox.setEnabled(enabled); revertButton.setEnabled(enabled); } @@ -2721,85 +2811,3 @@ public class GPreferences extends JPanel } } -class IntKeyStringValueEntry -{ - int k; - - String v; - - public IntKeyStringValueEntry(int k, String v) - { - this.k = k; - this.v = v; - } - - @Override - public String toString() - { - return this.getValue(); - } - - public int getKey() - { - return k; - } - - public String getValue() - { - return v; - } -} - -class BackupFilesPresetEntry -{ - String suffix; - - int digits; - - boolean reverse; - - boolean keepAll; - - int rollMax; - - boolean confirmDelete; - - public BackupFilesPresetEntry(String suffix, int digits, boolean reverse, - boolean keepAll, int rollMax, boolean confirmDelete) - { - this.suffix = suffix; - this.digits = digits; - this.reverse = reverse; - this.keepAll = keepAll; - this.rollMax = rollMax; - this.confirmDelete = confirmDelete; - } -} - -class BackupFilesPresetsComboBoxRenderer extends DefaultListCellRenderer -{ - /** - * - */ - private static final long serialVersionUID = 88L; - - @Override - public Component getListCellRendererComponent(JList list, Object value, - int index, boolean isSelected, boolean cellHasFocus) - { - super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); - - try { - IntKeyStringValueEntry e = (IntKeyStringValueEntry) value; - if (e.getKey() == GPreferences.BACKUPFILESSCHEMECUSTOMISE) - { - // "Customise" item - this.setFont(this.getFont().deriveFont(Font.BOLD)); - } - } catch (Exception e) { - return this; - } - - return this; - } -} diff --git a/test/jalview/io/BackupFilesTest.java b/test/jalview/io/BackupFilesTest.java index 723279d..6b5fef6 100644 --- a/test/jalview/io/BackupFilesTest.java +++ b/test/jalview/io/BackupFilesTest.java @@ -107,7 +107,7 @@ public class BackupFilesTest } // check 10 backup files - HashMap correctindexmap = new HashMap(); + HashMap correctindexmap = new HashMap<>(); correctindexmap.put(1, "backupfilestestTemp.fa_BACKUPTEST-00000001"); correctindexmap.put(2, "backupfilestestTemp.fa_BACKUPTEST-00000002"); correctindexmap.put(3, "backupfilestestTemp.fa_BACKUPTEST-00000003"); @@ -118,7 +118,7 @@ public class BackupFilesTest correctindexmap.put(8, "backupfilestestTemp.fa_BACKUPTEST-00000008"); correctindexmap.put(9, "backupfilestestTemp.fa_BACKUPTEST-00000009"); correctindexmap.put(10, "backupfilestestTemp.fa_BACKUPTEST-00000010"); - HashMap wrongindexmap = new HashMap(); + HashMap wrongindexmap = new HashMap<>(); wrongindexmap.put(1, "backupfilestestTemp.fa_BACKUPTEST-1"); wrongindexmap.put(2, "backupfilestestTemp.fa_BACKUPTEST-00000002"); wrongindexmap.put(3, "backupfilestestTemp.fa_BACKUPTEST-00000003"); @@ -158,7 +158,7 @@ public class BackupFilesTest // check there are "rollMax" backup files and they are all saved correctly // check 10 backup files - HashMap correctindexmap = new HashMap(); + HashMap correctindexmap = new HashMap<>(); correctindexmap.put(9, "backupfilestestTemp.fa_BACKUPTEST-00000009"); correctindexmap.put(10, "backupfilestestTemp.fa_BACKUPTEST-00000010"); int[] indexes2 = { 10 }; @@ -189,7 +189,7 @@ public class BackupFilesTest // check there are "rollMax" backup files and they are all saved correctly // check 10 backup files - HashMap correctindexmap = new HashMap(); + HashMap correctindexmap = new HashMap<>(); correctindexmap.put(1, "backupfilestestTemp.fa_BACKUPTEST-00000001"); correctindexmap.put(2, "backupfilestestTemp.fa_BACKUPTEST-00000002"); int[] indexes2 = { 1 }; @@ -209,6 +209,14 @@ public class BackupFilesTest { Cache.loadProperties("test/jalview/io/testProps.jvprops"); + BackupFilesPresetEntry bfpe = new BackupFilesPresetEntry(suffix, digits, + reverse, noMax, rollMax, false); + + Cache.applicationProperties.setProperty(BackupFiles.ENABLED, + Boolean.toString(enabled)); + Cache.applicationProperties.setProperty( + BackupFilesPresetEntry.SAVEDCONFIG, bfpe.toString()); + /* Cache.applicationProperties.setProperty(BackupFiles.ENABLED, Boolean.toString(enabled)); Cache.applicationProperties.setProperty(BackupFiles.SUFFIX, suffix); @@ -222,6 +230,7 @@ public class BackupFilesTest Integer.toString(rollMax)); Cache.applicationProperties.setProperty(BackupFiles.CONFIRM_DELETE_OLD, "false"); + */ } private void save() diff --git a/test/jalview/io/testProps.jvprops b/test/jalview/io/testProps.jvprops index f180395..cb92ecf 100644 --- a/test/jalview/io/testProps.jvprops +++ b/test/jalview/io/testProps.jvprops @@ -1,13 +1,10 @@ #---JalviewX Properties File--- #Fri Apr 25 09:54:25 BST 2014 # -BACKUPFILES_ROLL_MAX=2 -BACKUPFILES_REVERSE_ORDER=false -BACKUPFILES_SUFFIX=_BACKUPFILESTESTTMP%n -BACKUPFILES_CONFIRM_DELETE_OLD=false -BACKUPFILES_NO_MAX=false BACKUPFILES_ENABLED=true -BACKUPFILES_SUFFIX_DIGITS=8 +BACKUPFILES_PRESET=0 +BACKUPFILES_SAVED=_BACKUPFILESTESTTMP%n\t8\tfalse\tfalse\t2\tfalse +BACKUPFILES_CUSTOM=_BACKUPFILESTESTTMP%n\t8\tfalse\tfalse\t2\tfalse SCREEN_Y=768 SCREEN_X=936 SHOW_WSDISCOVERY_ERRORS=true