JAL-629 Fix --structureimage and options
[jalview.git] / src / jalview / project / Jalview2XML.java
index bc2046e..400465d 100644 (file)
@@ -26,7 +26,6 @@ import static jalview.math.RotatableMatrix.Axis.Z;
 
 import java.awt.Color;
 import java.awt.Font;
-import java.awt.FontMetrics;
 import java.awt.Rectangle;
 import java.io.BufferedReader;
 import java.io.ByteArrayInputStream;
@@ -87,6 +86,7 @@ import jalview.api.analysis.SimilarityParamsI;
 import jalview.api.structures.JalviewStructureDisplayI;
 import jalview.bin.Cache;
 import jalview.bin.Console;
+import jalview.bin.Jalview;
 import jalview.datamodel.AlignedCodonFrame;
 import jalview.datamodel.Alignment;
 import jalview.datamodel.AlignmentAnnotation;
@@ -534,7 +534,8 @@ public class Jalview2XML
     }
     if (unresolved > 0)
     {
-      jalview.bin.Console.errPrintln("Jalview Project Import: There were " + unresolved
+      jalview.bin.Console.errPrintln("Jalview Project Import: There were "
+              + unresolved
               + " forward references left unresolved on the stack.");
     }
     if (failedtoresolve > 0)
@@ -1139,34 +1140,50 @@ public class Jalview2XML
            * only view *should* be coped with sensibly.
            */
           // This must have been loaded, is it still visible?
-          JInternalFrame[] frames = Desktop.desktop.getAllFrames();
+          JInternalFrame[] frames = null;
+          if (Desktop.desktop != null)
+          {
+            frames = Desktop.desktop.getAllFrames();
+          }
+          else if (Jalview.isHeadlessMode()
+                  && Jalview.getInstance().getCommands() != null)
+          {
+            // TODO
+          }
+          else
+          {
+            frames = null;
+          }
           String matchedFile = null;
-          for (int f = frames.length - 1; f > -1; f--)
+          if (frames != null)
           {
-            if (frames[f] instanceof StructureViewerBase)
+            for (int f = frames.length - 1; f > -1; f--)
             {
-              StructureViewerBase viewFrame = (StructureViewerBase) frames[f];
-              matchedFile = saveStructureViewer(ap, jds, pdb, entry,
-                      viewIds, matchedFile, viewFrame);
-              /*
-               * Only store each structure viewer's state once in the project
-               * jar. First time through only (storeDS==false)
-               */
-              String viewId = viewFrame.getViewId();
-              String viewerType = viewFrame.getViewerType().toString();
-              if (!storeDS && !viewIds.contains(viewId))
+              if (frames[f] instanceof StructureViewerBase)
               {
-                viewIds.add(viewId);
-                File viewerState = viewFrame.saveSession();
-                if (viewerState != null)
+                StructureViewerBase viewFrame = (StructureViewerBase) frames[f];
+                matchedFile = saveStructureViewer(ap, jds, pdb, entry,
+                        viewIds, matchedFile, viewFrame);
+                /*
+                 * Only store each structure viewer's state once in the project
+                 * jar. First time through only (storeDS==false)
+                 */
+                String viewId = viewFrame.getViewId();
+                String viewerType = viewFrame.getViewerType().toString();
+                if (!storeDS && !viewIds.contains(viewId))
                 {
-                  copyFileToJar(jout, viewerState.getPath(),
-                          getViewerJarEntryName(viewId), viewerType);
-                }
-                else
-                {
-                  Console.error(
-                          "Failed to save viewer state for " + viewerType);
+                  viewIds.add(viewId);
+                  File viewerState = viewFrame.saveSession();
+                  if (viewerState != null)
+                  {
+                    copyFileToJar(jout, viewerState.getPath(),
+                            getViewerJarEntryName(viewId), viewerType);
+                  }
+                  else
+                  {
+                    Console.error("Failed to save viewer state for "
+                            + viewerType);
+                  }
                 }
               }
             }
@@ -2516,6 +2533,7 @@ public class Jalview2XML
     return BitSet.valueOf(newlongvals);
 
   }
+
   private CalcIdParam createCalcIdParam(String calcId, AlignViewport av)
   {
     AutoCalcSetting settings = av.getCalcIdSettingsFor(calcId);
@@ -2959,7 +2977,8 @@ public class Jalview2XML
         });
       } catch (Exception x)
       {
-        jalview.bin.Console.errPrintln("Error loading alignment: " + x.getMessage());
+        jalview.bin.Console
+                .errPrintln("Error loading alignment: " + x.getMessage());
       }
     }
     return af;
@@ -2998,19 +3017,22 @@ public class Jalview2XML
         {
           if (bytes != null)
           {
-            // jalview.bin.Console.outPrintln("Jalview2XML: opening byte jarInputStream for
+            // jalview.bin.Console.outPrintln("Jalview2XML: opening byte
+            // jarInputStream for
             // bytes.length=" + bytes.length);
             return new JarInputStream(new ByteArrayInputStream(bytes));
           }
           if (_url != null)
           {
-            // jalview.bin.Console.outPrintln("Jalview2XML: opening url jarInputStream for "
+            // jalview.bin.Console.outPrintln("Jalview2XML: opening url
+            // jarInputStream for "
             // + _url);
             return new JarInputStream(_url.openStream());
           }
           else
           {
-            // jalview.bin.Console.outPrintln("Jalview2XML: opening file jarInputStream for
+            // jalview.bin.Console.outPrintln("Jalview2XML: opening file
+            // jarInputStream for
             // " + file);
             return new JarInputStream(new FileInputStream(file));
           }
@@ -3121,7 +3143,8 @@ public class Jalview2XML
               "Exception whilst loading jalview XML file : " + ex + "\n");
     } catch (Exception ex)
     {
-      jalview.bin.Console.errPrintln("Parsing as Jalview Version 2 file failed.");
+      jalview.bin.Console
+              .errPrintln("Parsing as Jalview Version 2 file failed.");
       ex.printStackTrace(System.err);
       if (attemptversion1parse)
       {
@@ -3144,7 +3167,8 @@ public class Jalview2XML
     {
       // Don't use the OOM Window here
       errorMessage = "Out of memory loading jalview XML file";
-      jalview.bin.Console.errPrintln("Out of memory whilst loading jalview XML file");
+      jalview.bin.Console
+              .errPrintln("Out of memory whilst loading jalview XML file");
       e.printStackTrace();
     }
 
@@ -3248,8 +3272,8 @@ public class Jalview2XML
         Desktop.addInternalFrame(af, view.getTitle(),
                 safeInt(view.getWidth()), safeInt(view.getHeight()));
         af.setMenusForViewport();
-        jalview.bin.Console.errPrintln("Failed to restore view " + view.getTitle()
-                + " to split frame");
+        jalview.bin.Console.errPrintln("Failed to restore view "
+                + view.getTitle() + " to split frame");
       }
     }
 
@@ -3327,7 +3351,8 @@ public class Jalview2XML
       }
       else
       {
-        jalview.bin.Console.errPrintln("Problem loading Jalview file: " + errorMessage);
+        jalview.bin.Console.errPrintln(
+                "Problem loading Jalview file: " + errorMessage);
       }
     }
     errorMessage = null;
@@ -4491,7 +4516,7 @@ public class Jalview2XML
         {
           if (tree.isColumnWise())
           {
-            AlignmentAnnotation aa = (AlignmentAnnotation) annotationIds
+            AlignmentAnnotation aa = annotationIds
                     .get(tree.getColumnReference());
             if (aa == null)
             {
@@ -5377,8 +5402,9 @@ public class Jalview2XML
     }
     if (matchedAnnotation == null)
     {
-      jalview.bin.Console.errPrintln("Failed to match annotation colour scheme for "
-              + annotationId);
+      jalview.bin.Console
+              .errPrintln("Failed to match annotation colour scheme for "
+                      + annotationId);
       return null;
     }
     // belt-and-braces create a threshold line if the
@@ -6791,7 +6817,8 @@ public class Jalview2XML
       }
       else
       {
-        jalview.bin.Console.errPrintln("Malformed compound filter condition");
+        jalview.bin.Console
+                .errPrintln("Malformed compound filter condition");
       }
     }
   }