JAL-2937 Cygwin path preference, method refactoring
[jalview.git] / src / jalview / gui / Preferences.java
index df35b5e..9f9d6f8 100755 (executable)
@@ -37,6 +37,7 @@ import jalview.urls.UrlLinkTableModel;
 import jalview.urls.api.UrlProviderFactoryI;
 import jalview.urls.api.UrlProviderI;
 import jalview.urls.desktop.DesktopUrlProviderFactory;
+import jalview.util.FileUtils;
 import jalview.util.MessageManager;
 import jalview.util.Platform;
 import jalview.util.UrlConstants;
@@ -61,6 +62,7 @@ import javax.swing.JColorChooser;
 import javax.swing.JFileChooser;
 import javax.swing.JInternalFrame;
 import javax.swing.JPanel;
+import javax.swing.JTextField;
 import javax.swing.ListSelectionModel;
 import javax.swing.RowFilter;
 import javax.swing.RowSorter;
@@ -108,6 +110,8 @@ public class Preferences extends GPreferences
   
   public static final String HMMER_PATH = "HMMER_PATH";
 
+  public static final String CYGWIN_PATH = "CYGWIN_PATH";
+
   public static final String HMMSEARCH_DB_PATHS = "HMMSEARCH_DB_PATHS";
 
   public static final String HMMSEARCH_DBS = "HMMSEARCH_DBS";
@@ -235,7 +239,7 @@ public class Preferences extends GPreferences
       @Override
       public void actionPerformed(ActionEvent e)
       {
-        validateHMMERPath(true);
+        validateHmmerPath();
       }
     });
     hmmerPath.addFocusListener(new FocusAdapter()
@@ -243,7 +247,24 @@ public class Preferences extends GPreferences
       @Override
       public void focusLost(FocusEvent e)
       {
-        validateHMMERPath(true);
+        validateHmmerPath();
+      }
+    });
+    cygwinPath.setText(Cache.getProperty(CYGWIN_PATH));
+    cygwinPath.addActionListener(new ActionListener()
+    {
+      @Override
+      public void actionPerformed(ActionEvent e)
+      {
+        validateCygwinPath();
+      }
+    });
+    cygwinPath.addFocusListener(new FocusAdapter()
+    {
+      @Override
+      public void focusLost(FocusEvent e)
+      {
+        validateCygwinPath();
       }
     });
 
@@ -712,8 +733,8 @@ public class Preferences extends GPreferences
             Boolean.toString(hmmerBackgroundUniprot.isSelected()));
     Cache.applicationProperties.setProperty("SEQUENCES_TO_KEEP",
             hmmerSequenceCount.getText());
-    Cache.applicationProperties.setProperty(HMMER_PATH,
-            hmmerPath.getText());
+    Cache.setOrRemove(HMMER_PATH, hmmerPath.getText());
+    Cache.setOrRemove(CYGWIN_PATH, cygwinPath.getText());
     AlignFrame[] frames = Desktop.getAlignFrames();
     if (frames != null && frames.length > 0)
     {
@@ -1264,23 +1285,27 @@ public class Preferences extends GPreferences
   }
   
   /**
-   * Returns true if hmmer path is to a folder that contains an executable
-   * hmmbuild or hmmbuild.exe, else false (optionally after showing a warning
-   * dialog)
+   * Returns true if the given text field contains a path to a folder that
+   * contains an executable with the given name, else false (after showing a
+   * warning dialog). The executable name will be tried with .exe appended if not
+   * found.
+   * 
+   * @param textField
+   * @param executable
    */
-  @Override
-  protected boolean validateHMMERPath(boolean showWarning)
+  protected boolean validateExecutablePath(JTextField textField, String executable)
   {
-    String folder = hmmerPath.getText().trim();
+    String folder = textField.getText().trim();
 
-    if (HmmerCommand.getExecutable(HmmerCommand.HMMBUILD, folder) != null)
+    if (FileUtils.getExecutable(executable, folder) != null)
     {
       return true;
     }
-    if (showWarning && folder.length() > 0)
+    if (folder.length() > 0)
     {
       JvOptionPane.showInternalMessageDialog(Desktop.desktop,
-              MessageManager.getString("label.hmmbuild_not_found"),
+              MessageManager.formatMessage("label.executable_not_found",
+                      executable),
               MessageManager.getString("label.invalid_folder"),
               JvOptionPane.ERROR_MESSAGE);
     }
@@ -1357,6 +1382,18 @@ public class Preferences extends GPreferences
     }
   }
 
+  @Override
+  protected void validateHmmerPath()
+  {
+    validateExecutablePath(hmmerPath, HmmerCommand.HMMBUILD);
+  }
+
+  @Override
+  protected void validateCygwinPath()
+  {
+    validateExecutablePath(cygwinPath, "run");
+  }
+
   public class OptionsParam
   {
     private String name;