JAL-4193 JAL-4194 sessionProperties layer in Cache, CLI arg Opts to deal with -Pkey...
[jalview.git] / src / jalview / gui / Preferences.java
index 834d779..c4f32c3 100755 (executable)
@@ -27,7 +27,6 @@ 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.ArrayList;
 import java.util.List;
@@ -35,7 +34,6 @@ import java.util.concurrent.CompletableFuture;
 
 import javax.help.HelpSetException;
 import javax.swing.JComboBox;
-import javax.swing.JFileChooser;
 import javax.swing.JInternalFrame;
 import javax.swing.JPanel;
 import javax.swing.ListSelectionModel;
@@ -51,9 +49,12 @@ import javax.swing.table.TableColumn;
 import javax.swing.table.TableModel;
 import javax.swing.table.TableRowSorter;
 
+//import edu.stanford.ejalbert.launching.IBrowserLaunching;
 import ext.edu.ucsf.rbvi.strucviz2.StructureManager;
 import jalview.analysis.AnnotationSorter.SequenceAnnotationOrder;
 import jalview.bin.Cache;
+import jalview.bin.Console;
+import jalview.bin.MemorySetting;
 import jalview.ext.pymol.PymolManager;
 import jalview.gui.Help.HelpId;
 import jalview.gui.StructureViewer.ViewerType;
@@ -146,15 +147,18 @@ public class Preferences extends GPreferences
 
   /**
    * Holds name and link separated with | character. Sequence IDS and Sequences
-   * must be $SEQUENCEIDS$ or $SEQUENCEIDS=/.possible | chars ./=$ and $SEQUENCES$
-   * or $SEQUENCES=/.possible | chars ./=$ and separation character for first and
-   * second token specified after a pipe character at end |,|. (TODO: proper
-   * escape for using | to separate ids or sequences
+   * must be $SEQUENCEIDS$ or $SEQUENCEIDS=/.possible | chars ./=$ and
+   * $SEQUENCES$ or $SEQUENCES=/.possible | chars ./=$ and separation character
+   * for first and second token specified after a pipe character at end |,|.
+   * (TODO: proper escape for using | to separate ids or sequences
    */
 
   public static List<String> groupURLLinks;
   static
   {
+    // don't populate with session properties
+    Cache.disableSessionProperties();
+
     // get links selected to be in the menu (SEQUENCE_LINKS)
     // and links entered by the user but not selected (STORED_LINKS)
     String inMenuString = Cache.getDefault("SEQUENCE_LINKS", "");
@@ -179,6 +183,9 @@ public class Preferences extends GPreferences
      */
 
     groupURLLinks = new ArrayList<>();
+
+    // reenable
+    Cache.enableSessionProperties();
   }
 
   JInternalFrame frame;
@@ -187,7 +194,7 @@ public class Preferences extends GPreferences
 
   private OptionsParam promptEachTimeOpt = new OptionsParam(
           MessageManager.getString("label.prompt_each_time"),
-          "Prompt each time");
+          LineartOptions.PROMPT_EACH_TIME);
 
   private OptionsParam lineArtOpt = new OptionsParam(
           MessageManager.getString("label.lineart"), "Lineart");
@@ -247,7 +254,11 @@ public class Preferences extends GPreferences
   private Preferences()
   {
     super();
+    // don't populate with session properties
+    Cache.disableSessionProperties();
+
     frame = new JInternalFrame();
+    frame.setFrameIcon(null);
     frame.setContentPane(this);
     if (!Platform.isJS())
     /**
@@ -355,8 +366,8 @@ public class Preferences extends GPreferences
     startupCheckbox
             .setSelected(Cache.getDefault("SHOW_STARTUP_FILE", true));
     startupFileTextfield.setText(Cache.getDefault("STARTUP_FILE",
-            Cache.getDefault("www.jalview.org", "http://www.jalview.org")
-                    + "/examples/exampleFile_2_3.jar"));
+            Cache.getDefault("www.jalview.org", "https://www.jalview.org")
+                    + "/examples/exampleFile_2_7.jvp"));
 
     /*
      * Set Colours tab defaults
@@ -384,9 +395,9 @@ public class Preferences extends GPreferences
      * Set overview panel defaults
      */
     gapColour.setBackground(Cache.getDefaultColour(GAP_COLOUR,
-            jalview.renderer.OverviewResColourFinder.OVERVIEW_DEFAULT_GAP));
+            OverviewCanvas.OVERVIEW_DEFAULT_GAP));
     hiddenColour.setBackground(Cache.getDefaultColour(HIDDEN_COLOUR,
-            jalview.renderer.OverviewResColourFinder.OVERVIEW_DEFAULT_HIDDEN));
+            OverviewCanvas.OVERVIEW_DEFAULT_HIDDEN));
     useLegacyGap.setSelected(Cache.getDefault(USE_LEGACY_GAP, false));
     gapLabel.setEnabled(!useLegacyGap.isSelected());
     gapColour.setEnabled(!useLegacyGap.isSelected());
@@ -396,13 +407,11 @@ public class Preferences extends GPreferences
     /*
      * Set Structure tab defaults
      */
-    final boolean structSelected = Cache.getDefault(STRUCT_FROM_PDB, false);
+    final boolean structSelected = Cache.getDefault(STRUCT_FROM_PDB, true);
     structFromPdb.setSelected(structSelected);
-    useRnaView.setSelected(Cache.getDefault(USE_RNAVIEW, false));
-    useRnaView.setEnabled(structSelected);
-    addSecondaryStructure.setSelected(Cache.getDefault(ADD_SS_ANN, false));
+    addSecondaryStructure.setSelected(Cache.getDefault(ADD_SS_ANN, true));
     addSecondaryStructure.setEnabled(structSelected);
-    addTempFactor.setSelected(Cache.getDefault(ADD_TEMPFACT_ANN, false));
+    addTempFactor.setSelected(Cache.getDefault(ADD_TEMPFACT_ANN, true));
     addTempFactor.setEnabled(structSelected);
 
     /*
@@ -433,7 +442,7 @@ public class Preferences extends GPreferences
       }
     } catch (IllegalArgumentException e)
     {
-      Cache.log.error("Unknown structure viewer type: " + viewerType
+      Console.error("Unknown structure viewer type: " + viewerType
               + ", defaulting to Jmol");
       type = ViewerType.JMOL;
     }
@@ -446,11 +455,28 @@ public class Preferences extends GPreferences
       {
         if (validateViewerPath())
         {
-          Cache.setProperty(
-                  structViewer.getSelectedItem().equals(
-                          ViewerType.CHIMERAX.name()) ? CHIMERAX_PATH
-                                  : CHIMERA_PATH,
-                  structureViewerPath.getText());
+          String path = structureViewerPath.getText();
+          try
+          {
+            ViewerType type = ViewerType.valueOf(viewerType);
+            switch (type)
+            {
+            case JMOL:
+              break;
+            case CHIMERA:
+              Cache.setProperty(CHIMERA_PATH, path);
+              break;
+            case CHIMERAX:
+              Cache.setProperty(CHIMERAX_PATH, path);
+              break;
+            case PYMOL:
+              Cache.setProperty(PYMOL_PATH, path);
+              break;
+            }
+          } catch (IllegalArgumentException x)
+          {
+            Console.error("Failed to set path - unknown viewer type", x);
+          }
         }
       }
     });
@@ -605,7 +631,7 @@ public class Preferences extends GPreferences
       proxyType.setSelected(customProxy.getModel(), true);
       break;
     default:
-      Cache.log.warn(
+      Console.warn(
               "Incorrect PROXY_TYPE - should be 'none' (clear proxy properties), 'false' (system settings), 'true' (custom settings): "
                       + proxyTypeString);
     }
@@ -622,8 +648,6 @@ public class Preferences extends GPreferences
     setCustomProxyEnabled();
     applyProxyButtonEnabled(false);
 
-    defaultBrowser.setText(Cache.getDefault("DEFAULT_BROWSER", ""));
-
     usagestats.setSelected(Cache.getDefault("USAGESTATS", false));
     // note antisense here: default is true
     questionnaire
@@ -669,6 +693,13 @@ public class Preferences extends GPreferences
      * Set Backups tab defaults
      */
     loadLastSavedBackupsOptions();
+
+    /*
+     * Set Startup tab defaults
+     */
+
+    // re-enable
+    Cache.enableSessionProperties();
   }
 
   /**
@@ -681,6 +712,8 @@ public class Preferences extends GPreferences
   protected void setupOutputCombo(JComboBox<Object> comboBox,
           String propertyKey)
   {
+    Cache.disableSessionProperties();
+
     comboBox.addItem(promptEachTimeOpt);
     comboBox.addItem(lineArtOpt);
     comboBox.addItem(textOpt);
@@ -702,6 +735,8 @@ public class Preferences extends GPreferences
     {
       comboBox.setSelectedItem(promptEachTimeOpt);
     }
+
+    Cache.enableSessionProperties();
   }
 
   /**
@@ -713,6 +748,8 @@ public class Preferences extends GPreferences
   @Override
   public void ok_actionPerformed(ActionEvent e)
   {
+    Cache.disableSessionProperties();
+
     if (!validateSettings())
     {
       return;
@@ -726,6 +763,7 @@ public class Preferences extends GPreferences
     /*
      * Save Visual settings
      */
+
     Cache.applicationProperties.setProperty("SHOW_JVSUFFIX",
             Boolean.toString(seqLimit.isSelected()));
     Cache.applicationProperties.setProperty("RIGHT_ALIGN_IDS",
@@ -829,8 +867,6 @@ public class Preferences extends GPreferences
             Boolean.toString(addTempFactor.isSelected()));
     Cache.applicationProperties.setProperty(ADD_SS_ANN,
             Boolean.toString(addSecondaryStructure.isSelected()));
-    Cache.applicationProperties.setProperty(USE_RNAVIEW,
-            Boolean.toString(useRnaView.isSelected()));
     Cache.applicationProperties.setProperty(STRUCT_FROM_PDB,
             Boolean.toString(structFromPdb.isSelected()));
     String viewer = structViewer.getSelectedItem().toString();
@@ -867,10 +903,6 @@ public class Preferences extends GPreferences
      */
     // Proxy settings set first (to catch web services)
 
-    Cache.setOrRemove("DEFAULT_BROWSER", defaultBrowser.getText());
-
-    jalview.util.BrowserLauncher.resetBrowser();
-
     // save user-defined and selected links
     String menuLinks = sequenceUrlLinks.writeUrlsAsString(true);
     if (menuLinks.isEmpty())
@@ -983,6 +1015,21 @@ public class Preferences extends GPreferences
     Cache.applicationProperties.setProperty(
             BackupFilesPresetEntry.SAVEDCONFIG, savedBFPE.toString());
 
+    /*
+     * Save Memory Settings
+     */
+    Cache.applicationProperties.setProperty(
+            MemorySetting.CUSTOMISED_SETTINGS,
+            Boolean.toString(customiseMemorySetting.isSelected()));
+    Cache.applicationProperties.setProperty(MemorySetting.MEMORY_JVMMEMPC,
+            Integer.toString(jvmMemoryPercentSlider.getValue()));
+    Cache.applicationProperties.setProperty(MemorySetting.MEMORY_JVMMEMMAX,
+            jvmMemoryMaxTextField.getText());
+
+    /*
+     * save and close Preferences
+     */
+
     Cache.saveProperties();
     Desktop.instance.doConfigureStructurePrefs();
     try
@@ -991,10 +1038,14 @@ public class Preferences extends GPreferences
     } catch (Exception ex)
     {
     }
+
+    Cache.enableSessionProperties();
   }
 
   public void saveProxySettings()
   {
+    Cache.disableSessionProperties();
+
     String newProxyType = customProxy.isSelected() ? Cache.PROXYTYPE_CUSTOM
             : noProxy.isSelected() ? Cache.PROXYTYPE_NONE
                     : Cache.PROXYTYPE_SYSTEM;
@@ -1015,11 +1066,13 @@ public class Preferences extends GPreferences
       wsPrefs.update++;
     }
     previousProxyType = newProxyType;
+
+    Cache.enableSessionProperties();
   }
 
   /**
-   * Do any necessary validation before saving settings. Return focus to the first
-   * tab which fails validation.
+   * Do any necessary validation before saving settings. Return focus to the
+   * first tab which fails validation.
    * 
    * @return
    */
@@ -1046,6 +1099,8 @@ public class Preferences extends GPreferences
   @Override
   public void startupFileTextfield_mouseClicked()
   {
+    Cache.disableSessionProperties();
+
     // TODO: JAL-3048 not needed for Jalview-JS
     String fileFormat = Cache.getProperty("DEFAULT_FILE_FORMAT");
     JalviewFileChooser chooser = JalviewFileChooser
@@ -1067,13 +1122,15 @@ public class Preferences extends GPreferences
       startupFileTextfield
               .setText(chooser.getSelectedFile().getAbsolutePath());
     }
+
+    Cache.enableSessionProperties();
   }
 
   /**
    * DOCUMENT ME!
    * 
    * @param e
-   *            DOCUMENT ME!
+   *          DOCUMENT ME!
    */
   @Override
   public void cancel_actionPerformed(ActionEvent e)
@@ -1095,7 +1152,7 @@ public class Preferences extends GPreferences
    * DOCUMENT ME!
    * 
    * @param e
-   *            DOCUMENT ME!
+   *          DOCUMENT ME!
    */
   @Override
   public void annotations_actionPerformed(ActionEvent e)
@@ -1156,7 +1213,7 @@ public class Preferences extends GPreferences
     if (index == -1)
     {
       // button no longer enabled if row is not selected
-      Cache.log.debug("Edit with no row selected in linkUrlTable");
+      Console.debug("Edit with no row selected in linkUrlTable");
       return;
     }
 
@@ -1208,7 +1265,7 @@ public class Preferences extends GPreferences
     if (index == -1)
     {
       // button no longer enabled if row is not selected
-      Cache.log.debug("Delete with no row selected in linkUrlTable");
+      Console.debug("Delete with no row selected in linkUrlTable");
       return;
     }
     else
@@ -1220,30 +1277,6 @@ public class Preferences extends GPreferences
     ((UrlLinkTableModel) linkUrlTable.getModel()).removeRow(modelIndex);
   }
 
-  @Override
-  public void defaultBrowser_mouseClicked(MouseEvent e)
-  {
-    // TODO: JAL-3048 not needed for j2s
-    if (!Platform.isJS()) // BH 2019
-    /**
-     * Java only
-     * 
-     * @j2sIgnore
-     */
-    {
-      JFileChooser chooser = new JFileChooser(".");
-      chooser.setDialogTitle(
-              MessageManager.getString("label.select_default_browser"));
-
-      int value = chooser.showOpenDialog(this);
-
-      if (value == JFileChooser.APPROVE_OPTION)
-      {
-        defaultBrowser.setText(chooser.getSelectedFile().getAbsolutePath());
-      }
-    }
-  }
-
   /*
    * (non-Javadoc)
    * 
@@ -1302,13 +1335,11 @@ public class Preferences extends GPreferences
     boolean enabled = useLegacyGap.isSelected();
     if (enabled)
     {
-      gapColour.setBackground(
-              jalview.renderer.OverviewResColourFinder.OVERVIEW_DEFAULT_LEGACY_GAP);
+      gapColour.setBackground(OverviewCanvas.OVERVIEW_DEFAULT_LEGACY_GAP);
     }
     else
     {
-      gapColour.setBackground(
-              jalview.renderer.OverviewResColourFinder.OVERVIEW_DEFAULT_GAP);
+      gapColour.setBackground(OverviewCanvas.OVERVIEW_DEFAULT_GAP);
     }
     gapColour.setEnabled(!enabled);
     gapLabel.setEnabled(!enabled);
@@ -1320,8 +1351,7 @@ public class Preferences extends GPreferences
     useLegacyGap.setSelected(false);
     useLegacyGaps_actionPerformed(null);
     showHiddenAtStart.setSelected(false);
-    hiddenColour.setBackground(
-            jalview.renderer.OverviewResColourFinder.OVERVIEW_DEFAULT_HIDDEN);
+    hiddenColour.setBackground(OverviewCanvas.OVERVIEW_DEFAULT_HIDDEN);
   }
 
   @Override
@@ -1358,8 +1388,8 @@ public class Preferences extends GPreferences
   }
 
   /**
-   * Returns true if structure viewer path is to a valid executable, else shows an
-   * error dialog. Does nothing if the path is empty, as is the case for Jmol
+   * Returns true if structure viewer path is to a valid executable, else shows
+   * an error dialog. Does nothing if the path is empty, as is the case for Jmol
    * (built in to Jalview) or when Jalview is left to try default paths.
    */
   private boolean validateViewerPath()
@@ -1380,8 +1410,8 @@ public class Preferences extends GPreferences
   }
 
   /**
-   * If Chimera or ChimeraX or Pymol is selected, check it can be found on default
-   * or user-specified path, if not show a warning/help dialog
+   * If Chimera or ChimeraX or Pymol 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)
@@ -1403,6 +1433,7 @@ public class Preferences extends GPreferences
      */
     String viewerPath = "";
     List<String> paths = null;
+    Cache.disableSessionProperties();
     try
     {
       ViewerType viewerType = ViewerType.valueOf(selectedItem);
@@ -1428,6 +1459,7 @@ public class Preferences extends GPreferences
     {
       // only valid entries should be in the drop-down
     }
+    Cache.enableSessionProperties();
     structureViewerPath.setText(viewerPath);
 
     paths.add(0, structureViewerPath.getText());