JAL-629 Save structure frames in project files when opened and saved in headless...
authorBen Soares <b.soares@dundee.ac.uk>
Fri, 1 Sep 2023 23:03:21 +0000 (00:03 +0100)
committerBen Soares <b.soares@dundee.ac.uk>
Fri, 1 Sep 2023 23:03:21 +0000 (00:03 +0100)
src/jalview/gui/StructureViewerBase.java
src/jalview/project/Jalview2XML.java

index b8d2cce..6ebbf89 100644 (file)
@@ -87,6 +87,21 @@ public abstract class StructureViewerBase extends GStructureViewer
     BySequence, ByChain, ChargeCysteine, ByViewer
   }
 
+  private static List<StructureViewerBase> svbs = new ArrayList<>();
+
+  public static List<StructureViewerBase> getAllStructureViewerBases()
+  {
+    List<StructureViewerBase> goodSvbs = new ArrayList<>();
+    for (StructureViewerBase s : svbs)
+    {
+      if (s != null && !goodSvbs.contains(s))
+      {
+        goodSvbs.add(s);
+      }
+    }
+    return goodSvbs;
+  }
+
   /**
    * list of sequenceSet ids associated with the view
    */
@@ -137,6 +152,7 @@ public abstract class StructureViewerBase extends GStructureViewer
   {
     super();
     setFrameIcon(null);
+    svbs.add(this);
   }
 
   /**
index 400465d..9499acd 100644 (file)
@@ -1140,51 +1140,52 @@ public class Jalview2XML
            * only view *should* be coped with sensibly.
            */
           // This must have been loaded, is it still visible?
-          JInternalFrame[] frames = null;
+          List<StructureViewerBase> viewFrames = new ArrayList<>();
           if (Desktop.desktop != null)
           {
-            frames = Desktop.desktop.getAllFrames();
+            JInternalFrame[] jifs = Desktop.desktop.getAllFrames();
+            if (jifs != null)
+            {
+              for (JInternalFrame jif : jifs)
+              {
+                if (jif instanceof StructureViewerBase)
+                {
+                  viewFrames.add((StructureViewerBase) jif);
+                }
+              }
+            }
           }
           else if (Jalview.isHeadlessMode()
                   && Jalview.getInstance().getCommands() != null)
           {
-            // TODO
-          }
-          else
-          {
-            frames = null;
+            viewFrames.addAll(
+                    StructureViewerBase.getAllStructureViewerBases());
           }
+
           String matchedFile = null;
-          if (frames != null)
+          for (StructureViewerBase viewFrame : viewFrames)
           {
-            for (int f = frames.length - 1; f > -1; 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))
-                {
-                  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);
-                  }
-                }
+                copyFileToJar(jout, viewerState.getPath(),
+                        getViewerJarEntryName(viewId), viewerType);
+              }
+              else
+              {
+                Console.error(
+                        "Failed to save viewer state for " + viewerType);
               }
             }
           }