JAL-3660 Fix for specified output format when using command line arg to convert a...
[jalview.git] / src / jalview / bin / Jalview.java
index 992ca01..642efdb 100755 (executable)
@@ -43,7 +43,6 @@ import java.util.logging.ConsoleHandler;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
-import javax.swing.LookAndFeel;
 import javax.swing.UIManager;
 import javax.swing.UIManager.LookAndFeelInfo;
 
@@ -61,6 +60,7 @@ import jalview.io.DataSourceType;
 import jalview.io.FileFormat;
 import jalview.io.FileFormatException;
 import jalview.io.FileFormatI;
+import jalview.io.FileFormats;
 import jalview.io.FileLoader;
 import jalview.io.HtmlSvgOutput;
 import jalview.io.IdentifyFile;
@@ -68,6 +68,7 @@ import jalview.io.NewickFile;
 import jalview.io.gff.SequenceOntologyFactory;
 import jalview.schemes.ColourSchemeI;
 import jalview.schemes.ColourSchemeProperty;
+import jalview.util.HttpUtils;
 import jalview.util.MessageManager;
 import jalview.util.Platform;
 import jalview.ws.jws2.Jws2Discoverer;
@@ -94,8 +95,9 @@ public class Jalview
     Platform.getURLCommandArguments();
   }
 
-  // singleton instance of this class
-
+  /*
+   * singleton instance of this class
+   */
   private static Jalview instance;
 
   private Desktop desktop;
@@ -328,8 +330,8 @@ public class Jalview
                   "Invalid jabaws parameter: " + jabawsUrl + " ignored");
         }
       }
-
     }
+
     String defs = aparser.getValue("setprop");
     while (defs != null)
     {
@@ -345,6 +347,9 @@ public class Jalview
         {
           Cache.setProperty(defs.substring(0, p), defs.substring(p + 1));
         }
+        // DISABLED FOR SECURITY REASONS
+        // TODO: add a property to allow properties to be overriden by cli args
+        // Cache.setProperty(defs.substring(0,p), defs.substring(p+1));
       }
       defs = aparser.getValue("setprop");
     }
@@ -368,68 +373,7 @@ public class Jalview
 
     desktop = null;
 
-    // property laf = "crossplatform", "system", "gtk", "metal" or "mac"
-    // If not set (or chosen laf fails), use the normal SystemLaF and if on Mac,
-    // try Quaqua/Vaqua.
-    String laf = System.getProperty("laf", "none");
-    boolean lafSet = false;
-    switch (laf)
-    {
-    case "crossplatform":
-      lafSet = setCrossPlatformLookAndFeel();
-      if (!lafSet)
-      {
-        System.err.println("Could not set requested laf=" + laf);
-      }
-      break;
-    case "system":
-      lafSet = setSystemLookAndFeel();
-      if (!lafSet)
-      {
-        System.err.println("Could not set requested laf=" + laf);
-      }
-      break;
-    case "gtk":
-      lafSet = setGtkLookAndFeel();
-      {
-        System.err.println("Could not set requested laf=" + laf);
-      }
-      break;
-    case "metal":
-      lafSet = setMetalLookAndFeel();
-      {
-        System.err.println("Could not set requested laf=" + laf);
-      }
-      break;
-    case "nimbus":
-      lafSet = setNimbusLookAndFeel();
-      {
-        System.err.println("Could not set requested laf=" + laf);
-      }
-      break;
-    case "mac":
-      lafSet = setMacLookAndFeel();
-      if (!lafSet)
-      {
-        System.err.println("Could not set requested laf=" + laf);
-      }
-      break;
-    case "none":
-      break;
-    default:
-      System.err.println("Requested laf=" + laf + " not implemented");
-    }
-    if (!lafSet)
-    {
-      setSystemLookAndFeel();
-      if (Platform.isLinux() && ! Platform.isJS()) {
-        setMetalLookAndFeel();
-      }
-      if (Platform.isAMacAndNotJS())
-      {
-        setMacLookAndFeel();
-      }
-    }
+    setLookAndFeel();
 
     /*
      * configure 'full' SO model if preferences say to, else use the default (full SO)
@@ -443,17 +387,28 @@ public class Jalview
 
     if (!headless)
     {
+      Desktop.nosplash = aparser.contains("nosplash");
       desktop = new Desktop();
       desktop.setInBatchMode(true); // indicate we are starting up
 
       try
       {
         JalviewTaskbar.setTaskbar(this);
+      } catch (Exception e)
+      {
+        Cache.log.info("Cannot set Taskbar");
+        Cache.log.error(e.getMessage());
+        // e.printStackTrace();
       } catch (Throwable t)
       {
-        System.out.println("Error setting Taskbar: " + t.getMessage());
+        Cache.log.info("Cannot set Taskbar");
+        Cache.log.error(t.getMessage());
+        // t.printStackTrace();
       }
 
+      // set Proxy settings before all the internet calls
+      Cache.setProxyPropertiesFromPreferences();
+
       desktop.setVisible(true);
 
       if (!Platform.isJS())
@@ -463,7 +418,10 @@ public class Jalview
        * @j2sIgnore
        */
       {
-        desktop.startServiceDiscovery();
+        if (!aparser.contains("nowebservicediscovery"))
+        {
+          desktop.startServiceDiscovery();
+        }
         if (!aparser.contains("nousagestats"))
         {
           startUsageStats(desktop);
@@ -571,8 +529,7 @@ public class Jalview
        * @j2sIgnore
        */
       {
-        if (!file.startsWith("http://") && !file.startsWith("https://"))
-        // BH 2019 added https check for Java
+        if (!HttpUtils.startsWithHttpOrHttps(file))
         {
           if (!(new File(file)).exists())
           {
@@ -770,17 +727,37 @@ public class Jalview
             af.createEPS(outputFile);
             continue;
           }
-
-          af.saveAlignment(file, format);
-          if (af.isSaveAlignmentSuccessful())
+          FileFormatI outFormat = null;
+          try
+          {
+            outFormat = FileFormats.getInstance().forName(outputFormat);
+          } catch (Exception formatP)
           {
-            System.out.println("Written alignment in " + format
-                    + " format to " + file);
+            System.out.println("Couldn't parse " + outFormat
+                    + " as a valid Jalview format string.");
           }
-          else
+          if (outFormat != null)
           {
-            System.out.println("Error writing file " + file + " in "
-                    + format + " format!!");
+            if (!outFormat.isWritable())
+            {
+              System.out.println(
+                      "This version of Jalview does not support alignment export as "
+                              + outputFormat);
+            }
+            else
+            {
+              af.saveAlignment(file, outFormat);
+              if (af.isSaveAlignmentSuccessful())
+              {
+                System.out.println("Written alignment in "
+                        + outFormat.getName() + " format to " + file);
+              }
+              else
+              {
+                System.out.println("Error writing file " + file + " in "
+                        + outFormat.getName() + " format!!");
+              }
+            }
           }
 
         }
@@ -816,12 +793,7 @@ public class Jalview
         Cache.removeProperty("STARTUP_FILE");
       }
 
-      protocol = DataSourceType.FILE;
-
-      if (file.indexOf("http:") > -1)
-      {
-        protocol = DataSourceType.URL;
-      }
+      protocol = AppletFormatAdapter.checkProtocol(file);
 
       if (file.endsWith(".jar"))
       {
@@ -869,6 +841,101 @@ public class Jalview
     }
   }
 
+  private static void setLookAndFeel()
+  {
+    // property laf = "crossplatform", "system", "gtk", "metal", "nimbus" or
+    // "mac"
+    // If not set (or chosen laf fails), use the normal SystemLaF and if on Mac,
+    // try Quaqua/Vaqua.
+    String lafProp = System.getProperty("laf");
+    String lafSetting = Cache.getDefault("PREFERRED_LAF", null);
+    String laf = "none";
+    if (lafProp != null)
+    {
+      laf = lafProp;
+    }
+    else if (lafSetting != null)
+    {
+      laf = lafSetting;
+    }
+    boolean lafSet = false;
+    switch (laf)
+    {
+    case "crossplatform":
+      lafSet = setCrossPlatformLookAndFeel();
+      if (!lafSet)
+      {
+        Cache.log.error("Could not set requested laf=" + laf);
+      }
+      break;
+    case "system":
+      lafSet = setSystemLookAndFeel();
+      if (!lafSet)
+      {
+        Cache.log.error("Could not set requested laf=" + laf);
+      }
+      break;
+    case "gtk":
+      lafSet = setGtkLookAndFeel();
+      if (!lafSet)
+      {
+        Cache.log.error("Could not set requested laf=" + laf);
+      }
+      break;
+    case "metal":
+      lafSet = setMetalLookAndFeel();
+      if (!lafSet)
+      {
+        Cache.log.error("Could not set requested laf=" + laf);
+      }
+      break;
+    case "nimbus":
+      lafSet = setNimbusLookAndFeel();
+      if (!lafSet)
+      {
+        Cache.log.error("Could not set requested laf=" + laf);
+      }
+      break;
+    case "quaqua":
+      lafSet = setQuaquaLookAndFeel();
+      if (!lafSet)
+      {
+        Cache.log.error("Could not set requested laf=" + laf);
+      }
+      break;
+    case "vaqua":
+      lafSet = setVaquaLookAndFeel();
+      if (!lafSet)
+      {
+        Cache.log.error("Could not set requested laf=" + laf);
+      }
+      break;
+    case "mac":
+      lafSet = setMacLookAndFeel();
+      if (!lafSet)
+      {
+        Cache.log.error("Could not set requested laf=" + laf);
+      }
+      break;
+    case "none":
+      break;
+    default:
+      Cache.log.error("Requested laf=" + laf + " not implemented");
+    }
+    if (!lafSet)
+    {
+      setSystemLookAndFeel();
+      if (Platform.isLinux())
+      {
+        setMetalLookAndFeel();
+      }
+      if (Platform.isMac())
+      {
+        setMacLookAndFeel();
+      }
+    }
+  }
+
   private static boolean setCrossPlatformLookAndFeel()
   {
     boolean set = false;
@@ -879,8 +946,9 @@ public class Jalview
       set = true;
     } catch (Exception ex)
     {
-      System.err.println("Unexpected Look and Feel Exception");
-      ex.printStackTrace();
+      Cache.log.error("Unexpected Look and Feel Exception");
+      Cache.log.error(ex.getMessage());
+      Cache.log.debug(Cache.getStackTraceString(ex));
     }
     return set;
   }
@@ -894,115 +962,84 @@ public class Jalview
       set = true;
     } catch (Exception ex)
     {
-      System.err.println("Unexpected Look and Feel Exception");
-      ex.printStackTrace();
+      Cache.log.error("Unexpected Look and Feel Exception");
+      Cache.log.error(ex.getMessage());
+      Cache.log.debug(Cache.getStackTraceString(ex));
     }
     return set;
   }
 
-  private static boolean setGtkLookAndFeel()
+  private static boolean setSpecificLookAndFeel(String name,
+          String className, boolean nameStartsWith)
   {
     boolean set = false;
-    String laf = "com.sun.java.swing.plaf.gtk.GTKLookAndFeel";
     try
     {
-      for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
-        if (info.getName() != null && info.getName().startsWith("Gtk")) {
-          laf = info.getClassName();
+      for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels())
+      {
+        if (info.getName() != null && nameStartsWith
+                ? info.getName().toLowerCase()
+                        .startsWith(name.toLowerCase())
+                : info.getName().toLowerCase().equals(name.toLowerCase()))
+        {
+          className = info.getClassName();
           break;
         }
       }
-      UIManager.setLookAndFeel(laf);
+      UIManager.setLookAndFeel(className);
       set = true;
     } catch (Exception ex)
     {
-      System.err.println("Unexpected Look and Feel Exception");
-      ex.printStackTrace();
+      Cache.log.error("Unexpected Look and Feel Exception");
+      Cache.log.error(ex.getMessage());
+      Cache.log.debug(Cache.getStackTraceString(ex));
     }
     return set;
   }
 
+  private static boolean setGtkLookAndFeel()
+  {
+    return setSpecificLookAndFeel("gtk",
+            "com.sun.java.swing.plaf.gtk.GTKLookAndFeel", true);
+  }
+
   private static boolean setMetalLookAndFeel()
   {
-    boolean set = false;
-    String laf = "javax.swing.plaf.metal.MetalLookAndFeel";
-    try
-    {
-      for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
-        if (info.getName() != null && info.getName().equals("Metal")) {
-          laf = info.getClassName();
-          break;
-        }
-      }
-      UIManager.setLookAndFeel(laf);
-      set = true;
-    } catch (Exception ex)
-    {
-      System.err.println("Unexpected Look and Feel Exception");
-      ex.printStackTrace();
-    }
-    return set;
+    return setSpecificLookAndFeel("metal",
+            "javax.swing.plaf.metal.MetalLookAndFeel", false);
   }
 
   private static boolean setNimbusLookAndFeel()
   {
-    boolean set = false;
-    String laf = "javax.swing.plaf.nimbus.NimbusLookAndFeel";
-    try
-    {
-      for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
-        if (info.getName() != null && info.getName().equals("Nimbus")) {
-          laf = info.getClassName();
-          break;
-        }
-      }
-      UIManager.setLookAndFeel(laf);
-      set = true;
-    } catch (Exception ex)
-    {
-      System.err.println("Unexpected Look and Feel Exception");
-      ex.printStackTrace();
-    }
-    return set;
+    return setSpecificLookAndFeel("nimbus",
+            "javax.swing.plaf.nimbus.NimbusLookAndFeel", false);
+  }
+
+  private static boolean setQuaquaLookAndFeel()
+  {
+    return setSpecificLookAndFeel("quaqua",
+            ch.randelshofer.quaqua.QuaquaManager.getLookAndFeel().getClass()
+                    .getName(),
+            false);
+  }
+
+  private static boolean setVaquaLookAndFeel()
+  {
+    return setSpecificLookAndFeel("vaqua",
+            "org.violetlib.aqua.AquaLookAndFeel", false);
   }
 
   private static boolean setMacLookAndFeel()
   {
     boolean set = false;
-    LookAndFeel lookAndFeel = ch.randelshofer.quaqua.QuaquaManager
-            .getLookAndFeel();
     System.setProperty("com.apple.mrj.application.apple.menu.about.name",
             "Jalview");
     System.setProperty("apple.laf.useScreenMenuBar", "true");
-    if (lookAndFeel != null)
+    set = setQuaquaLookAndFeel();
+    if ((!set) || !UIManager.getLookAndFeel().getClass().toString()
+            .toLowerCase().contains("quaqua"))
     {
-      try
-      {
-        UIManager.setLookAndFeel(lookAndFeel);
-        set = true;
-      } catch (Throwable e)
-      {
-        System.err.println(
-                "Failed to set QuaQua look and feel: " + e.toString());
-      }
-    }
-    if (lookAndFeel == null
-            || !(lookAndFeel.getClass().isAssignableFrom(
-                    UIManager.getLookAndFeel().getClass()))
-            || !UIManager.getLookAndFeel().getClass().toString()
-                    .toLowerCase().contains("quaqua"))
-    {
-      try
-      {
-        System.err.println(
-                "Quaqua LaF not available on this plaform. Using VAqua(4).\nSee https://issues.jalview.org/browse/JAL-2976");
-        UIManager.setLookAndFeel("org.violetlib.aqua.AquaLookAndFeel");
-        set = true;
-      } catch (Throwable e)
-      {
-        System.err
-                .println("Failed to reset look and feel: " + e.toString());
-      }
+      set = setVaquaLookAndFeel();
     }
     return set;
   }