JAL-1603 if Chimera selected in Preferences, warn if not installed/found
[jalview.git] / src / jalview / gui / Preferences.java
index 2e8aa89..bdc83e5 100755 (executable)
@@ -22,6 +22,7 @@ package jalview.gui;
 
 import jalview.analysis.AnnotationSorter.SequenceAnnotationOrder;
 import jalview.bin.Cache;
+import jalview.gui.Help.HelpId;
 import jalview.gui.StructureViewer.Viewer;
 import jalview.io.JalviewFileChooser;
 import jalview.io.JalviewFileView;
@@ -35,17 +36,23 @@ import java.awt.Color;
 import java.awt.Dimension;
 import java.awt.Font;
 import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
 import java.awt.event.MouseEvent;
+import java.io.File;
 import java.util.Collection;
+import java.util.List;
 import java.util.StringTokenizer;
 import java.util.Vector;
 
+import javax.help.HelpSetException;
 import javax.swing.JColorChooser;
 import javax.swing.JFileChooser;
 import javax.swing.JInternalFrame;
 import javax.swing.JOptionPane;
 import javax.swing.JPanel;
 
+import ext.edu.ucsf.rbvi.strucviz2.StructureManager;
+
 /**
  * DOCUMENT ME!
  * 
@@ -65,6 +72,8 @@ public class Preferences extends GPreferences
 
   public static final String STRUCTURE_DISPLAY = "STRUCTURE_DISPLAY";
 
+  public static final String CHIMERA_PATH = "CHIMERA_PATH";
+
   public static final String SORT_ANNOTATIONS = "SORT_ANNOTATIONS";
 
   public static final String SHOW_AUTOCALC_ABOVE = "SHOW_AUTOCALC_ABOVE";
@@ -281,6 +290,15 @@ public class Preferences extends GPreferences
     addTempFactor.setEnabled(structSelected);
     structViewer.setSelectedItem(Cache.getDefault(STRUCTURE_DISPLAY,
             Viewer.JMOL.name()));
+    chimeraPath.setText(Cache.getDefault(CHIMERA_PATH, ""));
+    chimeraPath.addActionListener(new ActionListener()
+    {
+      @Override
+      public void actionPerformed(ActionEvent e)
+      {
+        validateChimeraPath();
+      }
+    });
 
     /*
      * Set Connections tab defaults
@@ -353,6 +371,11 @@ public class Preferences extends GPreferences
    */
   public void ok_actionPerformed(ActionEvent e)
   {
+    if (!validateSettings())
+    {
+      return;
+    }
+
     /*
      * Save Visual settings
      */
@@ -449,6 +472,7 @@ public class Preferences extends GPreferences
             Boolean.toString(structFromPdb.isSelected()));
     Cache.applicationProperties.setProperty(STRUCTURE_DISPLAY, structViewer
             .getSelectedItem().toString());
+    Cache.setOrRemove(CHIMERA_PATH, chimeraPath.getText());
 
     /*
      * Save Output settings
@@ -466,15 +490,7 @@ public class Preferences extends GPreferences
     /*
      * Save Connections settings
      */
-    if (defaultBrowser.getText().trim().length() < 1)
-    {
-      Cache.applicationProperties.remove("DEFAULT_BROWSER");
-    }
-    else
-    {
-      Cache.applicationProperties.setProperty("DEFAULT_BROWSER",
-              defaultBrowser.getText());
-    }
+    Cache.setOrRemove("DEFAULT_BROWSER", defaultBrowser.getText());
 
     jalview.util.BrowserLauncher.resetBrowser();
 
@@ -502,25 +518,9 @@ public class Preferences extends GPreferences
     Cache.applicationProperties.setProperty("USE_PROXY",
             Boolean.toString(useProxy.isSelected()));
 
-    if (proxyServerTB.getText().trim().length() < 1)
-    {
-      Cache.applicationProperties.remove("PROXY_SERVER");
-    }
-    else
-    {
-      Cache.applicationProperties.setProperty("PROXY_SERVER",
-              proxyServerTB.getText());
-    }
+    Cache.setOrRemove("PROXY_SERVER", proxyServerTB.getText());
 
-    if (proxyPortTB.getText().trim().length() < 1)
-    {
-      Cache.applicationProperties.remove("PROXY_PORT");
-    }
-    else
-    {
-      Cache.applicationProperties.setProperty("PROXY_PORT",
-              proxyPortTB.getText());
-    }
+    Cache.setOrRemove("PROXY_PORT", proxyPortTB.getText());
 
     if (useProxy.isSelected())
     {
@@ -590,9 +590,8 @@ public class Preferences extends GPreferences
 
     dasSource.saveProperties(Cache.applicationProperties);
     wsPrefs.updateAndRefreshWsMenuConfig(false);
-
     Cache.saveProperties();
-
+    Desktop.instance.doConfigureStructurePrefs();
     try
     {
       frame.setClosed(true);
@@ -602,6 +601,28 @@ public class Preferences extends GPreferences
   }
 
   /**
+   * Do any necessary validation before saving settings. Return focus to the
+   * first tab which fails validation.
+   * 
+   * @return
+   */
+  private boolean validateSettings()
+  {
+    if (!validateStructure())
+    {
+      structureTab.requestFocusInWindow();
+      return false;
+    }
+    return true;
+  }
+
+  @Override
+  protected boolean validateStructure()
+  {
+    return validateChimeraPath();
+
+  }
+  /**
    * DOCUMENT ME!
    */
   public void startupFileTextfield_mouseClicked()
@@ -843,4 +864,74 @@ public class Preferences extends GPreferences
     userIdWidthlabel.setEnabled(!autoIdWidth.isSelected());
   }
 
+  /**
+   * Returns true if chimera path is to a valid executable, else show an error
+   * dialog.
+   */
+  private boolean validateChimeraPath()
+  {
+    if (chimeraPath.getText().trim().length() > 0)
+    {
+      File f = new File(chimeraPath.getText());
+      if (!f.canExecute())
+      {
+        JOptionPane.showInternalMessageDialog(Desktop.desktop,
+                MessageManager.getString("label.invalid_chimera_path"),
+                MessageManager.getString("label.invalid_name"),
+                JOptionPane.ERROR_MESSAGE);
+        return false;
+      }
+    }
+    return true;
+  }
+
+  /**
+   * If Chimera is selected, check it can be found on default or user-specified
+   * path, if not show a warning/help dialog.
+   */
+  @Override
+  protected void structureViewer_actionPerformed(String selectedItem)
+  {
+    if (!selectedItem.equals(Viewer.CHIMERA.name()))
+    {
+      return;
+    }
+    boolean found = false;
+
+    /*
+     * Try user-specified and standard paths for Chimera executable.
+     */
+    List<String> paths = StructureManager.getChimeraPaths();
+    paths.add(0, chimeraPath.getText());
+    for (String path : paths)
+    {
+      if (new File(path.trim()).canExecute())
+      {
+        found = true;
+        break;
+      }
+    }
+    if (!found)
+    {
+      String[] options =
+      { "OK", "Help" };
+      int showHelp = JOptionPane.showInternalOptionDialog(
+              Desktop.desktop,
+              JvSwingUtils.wrapTooltip(true,
+                      MessageManager.getString("label.chimera_missing")),
+              "", JOptionPane.YES_NO_OPTION,
+              JOptionPane.WARNING_MESSAGE, null, options, options[0]);
+      if (showHelp == JOptionPane.NO_OPTION)
+      {
+        try
+        {
+          Help.showHelpWindow(HelpId.StructureViewer);
+        } catch (HelpSetException e)
+        {
+          e.printStackTrace();
+        }
+      }
+    }
+  }
+
 }