JAL-3633 Small changes to allow future fitting with ApplicationSingletonI/Jalview...
authorBen Soares <b.soares@dundee.ac.uk>
Wed, 29 Jul 2020 18:48:02 +0000 (19:48 +0100)
committerBen Soares <b.soares@dundee.ac.uk>
Wed, 29 Jul 2020 18:48:02 +0000 (19:48 +0100)
src/jalview/bin/Cache.java
src/jalview/gui/Preferences.java
src/jalview/io/BackupFiles.java
src/jalview/jbgui/GPreferences.java

index dc28902..677e9ad 100755 (executable)
@@ -1226,9 +1226,11 @@ public class Cache
     setProxyProperties(startupProxyProperties[0], startupProxyProperties[1],
             startupProxyProperties[2], startupProxyProperties[3],
             startupProxyProperties[4],
-            startupProxyProperties[5].toCharArray(),
+            startupProxyProperties[5] == null ? null
+                    : startupProxyProperties[5].toCharArray(),
             startupProxyProperties[6],
-            startupProxyProperties[7].toCharArray(),
+            startupProxyProperties[7] == null ? null
+                    : startupProxyProperties[7].toCharArray(),
             startupProxyProperties[8]);
     StringBuilder sb = new StringBuilder();
     sb.append("Setting proxy properties to: http.proxyHost=")
@@ -1342,11 +1344,11 @@ public class Cache
     {
       try
       {
+        char[] displayHttpPw = new char[httpPassword == null ? 0
+                : httpPassword.length];
+        Arrays.fill(displayHttpPw, '*');
         Cache.debug("CACHE Proxy: setting new Authenticator with httpUser='"
-                + httpUser + "' httpPassword='"
-                + (proxyAuthPassword == null ? "null"
-                        : new String(proxyAuthPassword))
-                + "'"); // DELETE THIS LINE (password in logs)
+                + httpUser + "' httpPassword='" + displayHttpPw + "'");
         Authenticator.setDefault(new Authenticator()
         {
           @Override
@@ -1374,7 +1376,7 @@ public class Cache
                         .getString("label.proxy_password_required");
                 Preferences.openPreferences(Preferences.CONNECTIONS_TAB,
                         message);
-                Preferences.getPreferences().proxyAuthPasswordFocus();
+                Preferences.getInstance().proxyAuthPasswordHighlight(true);
               }
               else
               {
@@ -1385,8 +1387,6 @@ public class Cache
                           && getRequestingPort() == Integer
                                   .valueOf(httpPort))
                   {
-                    char[] displayHttpPw = new char[httpPassword.length];
-                    Arrays.fill(displayHttpPw, '*');
                     Cache.debug(
                             "AUTHENTICATOR returning PasswordAuthentication(\""
                                     + httpUser + "\", '"
@@ -1437,6 +1437,9 @@ public class Cache
     {
       // reset the Authenticator to protect http.proxyUser and
       // http.proxyPassword Just In Case
+      /* as noted above, due to bug in java this doesn't work if the sun.net.www.protocol.http.AuthCache
+       * has working credentials. No workaround for Java 11.
+       */
       Cache.debug("AUTHENTICATOR setting default Authenticator to null");
       Authenticator.setDefault(null);
     }
index 6237918..23a7cea 100755 (executable)
@@ -80,6 +80,10 @@ import jalview.ws.sifts.SiftsSettings;
  * @author $author$
  * @version $Revision$
  */
+/*
+ * for merge with Jalview-JS
+ public class Preferences extends GPreferences implements ApplicationSingletonI
+ */
 public class Preferences extends GPreferences
 {
   public static final String ENABLE_SPLIT_FRAME = "ENABLE_SPLIT_FRAME";
@@ -186,21 +190,26 @@ public class Preferences extends GPreferences
           MessageManager.getString("action.text"), "Text");
 
   // get singleton Preferences instance
-  public static Preferences getPreferences()
-  {
-    return getPreferences(0, null);
-  }
-
-  public static Preferences getPreferences(int selectTab, String message)
+  public static Preferences getInstance()
   {
     if (INSTANCE == null || INSTANCE.frame == null
             || INSTANCE.frame.isClosed())
     {
       INSTANCE = new Preferences();
     }
-    INSTANCE.selectTab(selectTab);
-    INSTANCE.setMessage(message);
     return INSTANCE;
+
+    /*
+     * Replace code with the following for Jalvew-JS
+    Preferences INSTANCE = ApplicationSingletonProvider.getInstance(Preferences.class);
+    if (INSTANCE == null || INSTANCE.frame == null
+            || INSTANCE.frame.isClosed())
+    {
+      ApplicationSingletonProvider.remove(Preferences.class);
+      INSTANCE = ApplicationSingletonProvider.getInstance(Preferences.class);
+    }
+    return INSTANCE;
+    */
   }
 
   public static void openPreferences()
@@ -210,7 +219,9 @@ public class Preferences extends GPreferences
 
   public static void openPreferences(int selectTab, String message)
   {
-    Preferences p = getPreferences(selectTab, message);
+    Preferences p = getInstance();
+    p.selectTab(selectTab);
+    p.setMessage(message);
     p.frame.show();
     p.frame.moveToFront();
     p.frame.grabFocus();
index e943b23..ef966c4 100644 (file)
@@ -131,17 +131,17 @@ public class BackupFiles
       {
         String tempfilename = file.getName();
         File tempdir = file.getParentFile();
-        Cache.log.debug(
+        Cache.debug(
                 "BACKUPFILES [file!=null] attempting to create temp file for "
                         + tempfilename + " in dir " + tempdir);
         temp = File.createTempFile(tempfilename,
                 TEMP_FILE_EXT + newTempFileSuffix, tempdir);
-        Cache.log.debug(
+        Cache.debug(
                 "BACKUPFILES using temp file " + temp.getAbsolutePath());
       }
       else
       {
-        Cache.log.debug(
+        Cache.debug(
                 "BACKUPFILES [file==null] attempting to create default temp file "
                         + DEFAULT_TEMP_FILE + " with extension "
                         + TEMP_FILE_EXT);
@@ -149,29 +149,28 @@ public class BackupFiles
       }
     } catch (IOException e)
     {
-      Cache.log
-              .error("Could not create temp file to save to (IOException)");
-      Cache.log.error(e.getMessage());
-      Cache.log.debug(Cache.getStackTraceString(e));
+      Cache.error("Could not create temp file to save to (IOException)");
+      Cache.error(e.getMessage());
+      Cache.debug(Cache.getStackTraceString(e));
     } catch (Exception e)
     {
-      Cache.log.error("Exception ctreating temp file for saving");
-      Cache.log.debug(Cache.getStackTraceString(e));
+      Cache.error("Exception ctreating temp file for saving");
+      Cache.debug(Cache.getStackTraceString(e));
     }
     this.setTempFile(temp);
   }
 
   public static void classInit()
   {
-    Cache.log.debug("BACKUPFILES classInit");
+    Cache.debug("BACKUPFILES classInit");
     boolean e = Cache.getDefault(ENABLED, true);
     setEnabled(e);
-    Cache.log.debug("BACKUPFILES " + (e ? "enabled" : "disabled"));
+    Cache.debug("BACKUPFILES " + (e ? "enabled" : "disabled"));
     BackupFilesPresetEntry bfpe = BackupFilesPresetEntry
             .getSavedBackupEntry();
-    Cache.log.debug("BACKUPFILES preset scheme " + bfpe.toString());
+    Cache.debug("BACKUPFILES preset scheme " + bfpe.toString());
     setConfirmDelete(bfpe.confirmDelete);
-    Cache.log.debug("BACKUPFILES confirm delete " + bfpe.confirmDelete);
+    Cache.debug("BACKUPFILES confirm delete " + bfpe.confirmDelete);
   }
 
   public static void setEnabled(boolean flag)
@@ -215,10 +214,9 @@ public class BackupFiles
       path = this.getTempFile().getCanonicalPath();
     } catch (IOException e)
     {
-      Cache.log.error(
-              "IOException when getting Canonical Path of temp file '"
-                      + this.getTempFile().getName() + "'");
-      Cache.log.debug(Cache.getStackTraceString(e));
+      Cache.error("IOException when getting Canonical Path of temp file '"
+              + this.getTempFile().getName() + "'");
+      Cache.debug(Cache.getStackTraceString(e));
     }
     return path;
   }
@@ -254,7 +252,7 @@ public class BackupFiles
             || suffix.length() == 0)
     {
       // nothing to do
-      Cache.log.debug("BACKUPFILES rollBackupFiles nothing to do." + ", "
+      Cache.debug("BACKUPFILES rollBackupFiles nothing to do." + ", "
               + "filename: " + (file != null ? file.getName() : "null")
               + ", " + "file exists: " + file.exists() + ", " + "enabled: "
               + enabled + ", " + "max: " + max + ", " + "suffix: '" + suffix
@@ -262,7 +260,7 @@ public class BackupFiles
       return true;
     }
 
-    Cache.log.debug("BACKUPFILES rollBackupFiles starting");
+    Cache.debug("BACKUPFILES rollBackupFiles starting");
 
     String dir = "";
     File dirFile;
@@ -270,19 +268,18 @@ public class BackupFiles
     {
       dirFile = file.getParentFile();
       dir = dirFile.getCanonicalPath();
-      Cache.log.debug("BACKUPFILES dir: " + dir);
+      Cache.debug("BACKUPFILES dir: " + dir);
     } catch (Exception e)
     {
-      Cache.log.error(
-              "Could not get canonical path for file '" + file + "'");
-      Cache.log.error(e.getMessage());
-      Cache.log.debug(Cache.getStackTraceString(e));
+      Cache.error("Could not get canonical path for file '" + file + "'");
+      Cache.error(e.getMessage());
+      Cache.debug(Cache.getStackTraceString(e));
       return false;
     }
     String filename = file.getName();
     String basename = filename;
 
-    Cache.log.debug("BACKUPFILES filename is " + filename);
+    Cache.debug("BACKUPFILES filename is " + filename);
     boolean ret = true;
     // Create/move backups up one
 
@@ -294,12 +291,11 @@ public class BackupFiles
     File[] backupFiles = dirFile.listFiles(bff);
     int nextIndexNum = 0;
 
-    Cache.log
-            .debug("BACKUPFILES backupFiles.length: " + backupFiles.length);
+    Cache.debug("BACKUPFILES backupFiles.length: " + backupFiles.length);
     if (backupFiles.length == 0)
     {
       // No other backup files. Just need to move existing file to backupfile_1
-      Cache.log.debug(
+      Cache.debug(
               "BACKUPFILES no existing backup files, setting index to 1");
       nextIndexNum = 1;
     }
@@ -313,7 +309,7 @@ public class BackupFiles
       if (reverseOrder)
       {
         // backup style numbering
-        Cache.log.debug("BACKUPFILES rolling files in reverse order");
+        Cache.debug("BACKUPFILES rolling files in reverse order");
 
         int tempMax = noMax ? -1 : max;
         // noMax == true means no limits
@@ -345,7 +341,7 @@ public class BackupFiles
             // no "oldest" file to delete
             previousFile = backupfile_n;
             fileToBeDeleted = null;
-            Cache.log.debug("BACKUPFILES No oldest file to delete");
+            Cache.debug("BACKUPFILES No oldest file to delete");
             continue;
           }
 
@@ -356,9 +352,9 @@ public class BackupFiles
             File replacementFile = backupfile_n;
             long fileToBeDeletedLMT = fileToBeDeleted.lastModified();
             long replacementFileLMT = replacementFile.lastModified();
-            Cache.log.debug("BACKUPFILES fileToBeDeleted is "
+            Cache.debug("BACKUPFILES fileToBeDeleted is "
                     + fileToBeDeleted.getAbsolutePath());
-            Cache.log.debug("BACKUPFILES replacementFile is "
+            Cache.debug("BACKUPFILES replacementFile is "
                     + backupfile_n.getAbsolutePath());
 
             try
@@ -372,7 +368,7 @@ public class BackupFiles
                         .format(fileToBeDeletedLMT);
                 String replacementFileLMTString = sdf
                         .format(replacementFileLMT);
-                Cache.log.warn("WARNING! I am set to delete backupfile "
+                Cache.warn("WARNING! I am set to delete backupfile "
                         + fileToBeDeleted.getName()
                         + " has modification time "
                         + fileToBeDeletedLMTString
@@ -383,8 +379,8 @@ public class BackupFiles
 
                 boolean delete = confirmNewerDeleteFile(fileToBeDeleted,
                         replacementFile, true);
-                Cache.log.debug("BACKUPFILES "
-                        + (delete ? "confirmed" : "not") + " deleting file "
+                Cache.debug("BACKUPFILES " + (delete ? "confirmed" : "not")
+                        + " deleting file "
                         + fileToBeDeleted.getAbsolutePath()
                         + " which is newer than "
                         + replacementFile.getAbsolutePath());
@@ -396,7 +392,7 @@ public class BackupFiles
                 }
                 else
                 {
-                  Cache.log.debug("BACKUPFILES moving "
+                  Cache.debug("BACKUPFILES moving "
                           + fileToBeDeleted.getAbsolutePath() + " to "
                           + oldestTempFile.getAbsolutePath());
                   moveFileToFile(fileToBeDeleted, oldestTempFile);
@@ -404,7 +400,7 @@ public class BackupFiles
               }
               else
               {
-                Cache.log.debug("BACKUPFILES going to move "
+                Cache.debug("BACKUPFILES going to move "
                         + fileToBeDeleted.getAbsolutePath() + " to "
                         + oldestTempFile.getAbsolutePath());
                 moveFileToFile(fileToBeDeleted, oldestTempFile);
@@ -413,10 +409,10 @@ public class BackupFiles
 
             } catch (Exception e)
             {
-              Cache.log.error(
+              Cache.error(
                       "Error occurred, probably making new temp file for '"
                               + fileToBeDeleted.getName() + "'");
-              Cache.log.error(Cache.getStackTraceString(e));
+              Cache.error(Cache.getStackTraceString(e));
             }
 
             // reset
@@ -458,13 +454,13 @@ public class BackupFiles
           }
           bfsb.append(backupFiles[i].getName());
         }
-        Cache.log.debug("BACKUPFILES backupFiles: " + bfsb.toString());
+        Cache.debug("BACKUPFILES backupFiles: " + bfsb.toString());
 
         // noMax == true means keep all backup files
         if ((!noMax) && bfTreeMap.size() >= max)
         {
-          Cache.log.debug("BACKUPFILES noMax: " + noMax + ", " + "max: "
-                  + max + ", " + "bfTreeMap.size(): " + bfTreeMap.size());
+          Cache.debug("BACKUPFILES noMax: " + noMax + ", " + "max: " + max
+                  + ", " + "bfTreeMap.size(): " + bfTreeMap.size());
           // need to delete some files to keep number of backups to designated
           // max.
           // Note that if the suffix is not numbered then do not delete any
@@ -473,7 +469,7 @@ public class BackupFiles
           int numToDelete = suffix.indexOf(NUM_PLACEHOLDER) > -1
                   ? bfTreeMap.size() - max + 1
                   : 0;
-          Cache.log.debug("BACKUPFILES numToDelete: " + numToDelete);
+          Cache.debug("BACKUPFILES numToDelete: " + numToDelete);
           // the "replacement" file is the latest backup file being kept (it's
           // not replacing though)
           File replacementFile = numToDelete < backupFiles.length
@@ -486,8 +482,7 @@ public class BackupFiles
             File fileToBeDeleted = backupFiles[i];
             boolean delete = true;
 
-            Cache.log.debug(
-                    "BACKUPFILES fileToBeDeleted: " + fileToBeDeleted);
+            Cache.debug("BACKUPFILES fileToBeDeleted: " + fileToBeDeleted);
 
             boolean newer = false;
             if (replacementFile != null)
@@ -503,7 +498,7 @@ public class BackupFiles
                 String replacementFileLMTString = sdf
                         .format(replacementFileLMT);
 
-                Cache.log.warn("WARNING! I am set to delete backupfile '"
+                Cache.warn("WARNING! I am set to delete backupfile '"
                         + fileToBeDeleted.getName()
                         + "' has modification time "
                         + fileToBeDeletedLMTString
@@ -518,14 +513,14 @@ public class BackupFiles
                 {
                   // User has confirmed delete -- no need to add it to the list
                   fileToBeDeleted.delete();
-                  Cache.log.debug("BACKUPFILES deleting fileToBeDeleted: "
+                  Cache.debug("BACKUPFILES deleting fileToBeDeleted: "
                           + fileToBeDeleted);
                   delete = false;
                 }
                 else
                 {
                   // keeping file, nothing to do!
-                  Cache.log.debug("BACKUPFILES keeping fileToBeDeleted: "
+                  Cache.debug("BACKUPFILES keeping fileToBeDeleted: "
                           + fileToBeDeleted);
                 }
               }
@@ -533,7 +528,7 @@ public class BackupFiles
             if (delete)
             {
               addDeleteFile(fileToBeDeleted);
-              Cache.log.debug("BACKUPFILES addDeleteFile(fileToBeDeleted): "
+              Cache.debug("BACKUPFILES addDeleteFile(fileToBeDeleted): "
                       + fileToBeDeleted);
             }
 
@@ -549,17 +544,16 @@ public class BackupFiles
     String latestBackupFilename = dir + File.separatorChar
             + BackupFilenameParts.getBackupFilename(nextIndexNum, basename,
                     suffix, digits);
-    Cache.log.debug("BACKUPFILES Moving old file [" + file
+    Cache.debug("BACKUPFILES Moving old file [" + file
             + "] to latestBackupFilename [" + latestBackupFilename + "]");
     // using boolean '&' instead of '&&' as don't want moveFileToFile attempt to
     // be conditional (short-circuit)
     ret = ret & moveFileToFile(file, new File(latestBackupFilename));
-    Cache.log.debug(
-            "BACKUPFILES moving " + file + " to " + latestBackupFilename
-                    + " was " + (ret ? "" : "NOT ") + "successful");
+    Cache.debug("BACKUPFILES moving " + file + " to " + latestBackupFilename
+            + " was " + (ret ? "" : "NOT ") + "successful");
     if (tidyUp)
     {
-      Cache.log.debug("BACKUPFILES tidying up files");
+      Cache.debug("BACKUPFILES tidying up files");
       tidyUpFiles();
     }
 
@@ -615,7 +609,7 @@ public class BackupFiles
         saveFile = nextTempFile(ftbd.getName(), ftbd.getParentFile());
       } catch (Exception e)
       {
-        Cache.log.error(
+        Cache.error(
                 "Error when confirming to keep backup file newer than other backup files.");
         e.printStackTrace();
       }
@@ -725,10 +719,9 @@ public class BackupFiles
         for (int i = 0; i < deleteFiles.size(); i++)
         {
           File fileToDelete = deleteFiles.get(i);
-          Cache.log.debug(
-                  "BACKUPFILES deleting fileToDelete:" + fileToDelete);
+          Cache.debug("BACKUPFILES deleting fileToDelete:" + fileToDelete);
           fileToDelete.delete();
-          Cache.log.warn("deleting '" + fileToDelete.getName() + "'");
+          Cache.warn("deleting '" + fileToDelete.getName() + "'");
         }
       }
 
@@ -837,8 +830,7 @@ public class BackupFiles
       dirFile = file.getParentFile();
     } catch (Exception e)
     {
-      Cache.log.error(
-              "Could not get canonical path for file '" + file + "'");
+      Cache.error("Could not get canonical path for file '" + file + "'");
       return new TreeMap<>();
     }
 
@@ -879,14 +871,14 @@ public class BackupFiles
     int pos = deleteFiles.indexOf(fileToBeDeleted);
     if (pos > -1)
     {
-      Cache.log.debug("BACKUPFILES not adding file "
+      Cache.debug("BACKUPFILES not adding file "
               + fileToBeDeleted.getAbsolutePath()
               + " to the delete list (already at index" + pos + ")");
       return true;
     }
     else
     {
-      Cache.log.debug("BACKUPFILES adding file "
+      Cache.debug("BACKUPFILES adding file "
               + fileToBeDeleted.getAbsolutePath() + " to the delete list");
       deleteFiles.add(fileToBeDeleted);
     }
@@ -901,24 +893,24 @@ public class BackupFiles
     try
     {
       // delete destination file - not usually necessary but Just In Case...
-      Cache.log.debug("BACKUPFILES deleting " + newFile.getAbsolutePath());
+      Cache.debug("BACKUPFILES deleting " + newFile.getAbsolutePath());
       newFile.delete();
-      Cache.log.debug("BACKUPFILES moving " + oldFile.getAbsolutePath()
-              + " to " + newFile.getAbsolutePath());
+      Cache.debug("BACKUPFILES moving " + oldFile.getAbsolutePath() + " to "
+              + newFile.getAbsolutePath());
       Files.move(oldPath, newPath, StandardCopyOption.REPLACE_EXISTING);
       ret = true;
-      Cache.log.debug("BACKUPFILES move seems to have succeeded");
+      Cache.debug("BACKUPFILES move seems to have succeeded");
     } catch (IOException e)
     {
-      Cache.log.warn("Could not move file '" + oldPath.toString() + "' to '"
+      Cache.warn("Could not move file '" + oldPath.toString() + "' to '"
               + newPath.toString() + "'");
-      Cache.log.error(e.getMessage());
-      Cache.log.debug(Cache.getStackTraceString(e));
+      Cache.error(e.getMessage());
+      Cache.debug(Cache.getStackTraceString(e));
       ret = false;
     } catch (Exception e)
     {
-      Cache.log.error(e.getMessage());
-      Cache.log.debug(Cache.getStackTraceString(e));
+      Cache.error(e.getMessage());
+      Cache.debug(Cache.getStackTraceString(e));
       ret = false;
     }
     return ret;
index 9e47c6a..624041c 100755 (executable)
@@ -958,25 +958,19 @@ public class GPreferences extends JPanel
               @Override
               public void changedUpdate(DocumentEvent e)
               {
-                if (!proxyAuthPasswordPB.getBackground()
-                        .equals(Color.WHITE))
-                  proxyAuthPasswordPB.setBackground(Color.WHITE);
+                proxyAuthPasswordHighlight(true);
               }
 
               @Override
               public void insertUpdate(DocumentEvent e)
               {
-                if (!proxyAuthPasswordPB.getBackground()
-                        .equals(Color.WHITE))
-                  proxyAuthPasswordPB.setBackground(Color.WHITE);
+                proxyAuthPasswordHighlight(true);
               }
 
               @Override
               public void removeUpdate(DocumentEvent e)
               {
-                if (!proxyAuthPasswordPB.getBackground()
-                        .equals(Color.WHITE))
-                  proxyAuthPasswordPB.setBackground(Color.WHITE);
+                proxyAuthPasswordHighlight(true);
               }
 
             });
@@ -1198,10 +1192,21 @@ public class GPreferences extends JPanel
     return proxyPanel;
   }
 
-  public void proxyAuthPasswordFocus()
+  public void proxyAuthPasswordHighlight(boolean enabled)
   {
-    proxyAuthPasswordPB.grabFocus();
-    proxyAuthPasswordPB.setBackground(Color.PINK);
+    if (enabled && proxyType.isSelected(customProxy.getModel())
+            && proxyAuth.isSelected()
+            && !proxyAuthUsernameTB.getText().isEmpty()
+            && proxyAuthPasswordPB.getDocument().getLength() == 0)
+    {
+
+      proxyAuthPasswordPB.grabFocus();
+      proxyAuthPasswordPB.setBackground(Color.PINK);
+    }
+    else
+    {
+      proxyAuthPasswordPB.setBackground(Color.WHITE);
+    }
   }
 
   public void saveProxySettings()
@@ -3119,11 +3124,13 @@ public class GPreferences extends JPanel
   public void proxyType_actionPerformed()
   {
     setCustomProxyEnabled();
+    proxyAuthPasswordHighlight(true);
   }
 
   public void proxyAuth_actionPerformed()
   {
     setProxyAuthEnabled();
+    proxyAuthPasswordHighlight(true);
   }
 
   /**