Merge branch 'develop' into spike/JAL-4047/JAL-4048_columns_in_sequenceID
[jalview.git] / src / jalview / gui / StructureViewerBase.java
index 6c3f6d8..ed42ffa 100644 (file)
@@ -26,6 +26,7 @@ import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
 import java.awt.event.ItemEvent;
 import java.awt.event.ItemListener;
+import java.beans.PropertyVetoException;
 import java.io.BufferedReader;
 import java.io.File;
 import java.io.FileOutputStream;
@@ -46,6 +47,7 @@ import javax.swing.event.MenuEvent;
 import javax.swing.event.MenuListener;
 
 import jalview.api.AlignmentViewPanel;
+import jalview.api.structures.JalviewStructureDisplayI;
 import jalview.bin.Cache;
 import jalview.bin.Console;
 import jalview.datamodel.AlignmentI;
@@ -87,6 +89,29 @@ public abstract class StructureViewerBase extends GStructureViewer
   }
 
   /**
+   * Singleton list of all (open) instances of structureViewerBase
+   * TODO: JAL-3362 - review and adopt the swingJS-safe singleton pattern so each structure viewer base instance is kept to its own JalviewJS parent
+   */
+  private static List<JalviewStructureDisplayI> svbs = new ArrayList<>();
+
+  /**
+   * 
+   * @return list with all existing StructureViewers instance
+   */
+  public static List<JalviewStructureDisplayI> getAllStructureViewerBases()
+  {
+    List<JalviewStructureDisplayI> goodSvbs = new ArrayList<>();
+    for (JalviewStructureDisplayI s : svbs)
+    {
+      if (s != null && !goodSvbs.contains(s))
+      {
+        goodSvbs.add(s);
+      }
+    }
+    return goodSvbs;
+  }
+
+  /**
    * list of sequenceSet ids associated with the view
    */
   protected List<String> _aps = new ArrayList<>();
@@ -136,6 +161,7 @@ public abstract class StructureViewerBase extends GStructureViewer
   {
     super();
     setFrameIcon(null);
+    svbs.add(this);
   }
 
   /**
@@ -179,6 +205,7 @@ public abstract class StructureViewerBase extends GStructureViewer
     return _aps.contains(ap2.av.getSequenceSetId());
   }
 
+  @Override
   public boolean isUsedforaligment(AlignmentViewPanel ap2)
   {
 
@@ -1196,7 +1223,7 @@ public abstract class StructureViewerBase extends GStructureViewer
       }
     } catch (Exception e)
     {
-      System.err.println(
+      jalview.bin.Console.errPrintln(
               "Error retrieving PDB id " + pdbid + ": " + e.getMessage());
     } finally
     {
@@ -1251,6 +1278,20 @@ public abstract class StructureViewerBase extends GStructureViewer
     return session;
   }
 
+  private static boolean quitClose = false;
+
+  public static void setQuitClose(boolean b)
+  {
+    quitClose = b;
+  }
+
+  @Override
+  public boolean stillRunning()
+  {
+    AAStructureBindingModel binding = getBinding();
+    return binding != null && binding.isViewerRunning();
+  }
+
   /**
    * Close down this instance of Jalview's Chimera viewer, giving the user the
    * option to close the associated Chimera window (process). They may wish to
@@ -1263,24 +1304,48 @@ public abstract class StructureViewerBase extends GStructureViewer
   public void closeViewer(boolean forceClose)
   {
     AAStructureBindingModel binding = getBinding();
-    if (binding != null && binding.isViewerRunning())
+    if (stillRunning())
     {
       if (!forceClose)
       {
         String viewerName = getViewerName();
-        String prompt = MessageManager
-                .formatMessage("label.confirm_close_viewer", new Object[]
-                { binding.getViewerTitle(viewerName, false), viewerName });
-        prompt = JvSwingUtils.wrapTooltip(true, prompt);
-        int confirm = JvOptionPane.showConfirmDialog(this, prompt,
-                MessageManager.getString("label.close_viewer"),
-                JvOptionPane.YES_NO_CANCEL_OPTION);
+
+        int confirm = JvOptionPane.CANCEL_OPTION;
+        if (QuitHandler.quitting())
+        {
+          // already asked about closing external windows
+          confirm = quitClose ? JvOptionPane.YES_OPTION
+                  : JvOptionPane.NO_OPTION;
+        }
+        else
+        {
+          String prompt = MessageManager
+                  .formatMessage("label.confirm_close_viewer", new Object[]
+                  { binding.getViewerTitle(viewerName, false),
+                      viewerName });
+          prompt = JvSwingUtils.wrapTooltip(true, prompt);
+          String title = MessageManager.getString("label.close_viewer");
+          confirm = showCloseDialog(title, prompt);
+        }
+
         /*
          * abort closure if user hits escape or Cancel
          */
         if (confirm == JvOptionPane.CANCEL_OPTION
                 || confirm == JvOptionPane.CLOSED_OPTION)
         {
+          // abort possible quit handling if CANCEL chosen
+          if (confirm == JvOptionPane.CANCEL_OPTION)
+          {
+            try
+            {
+              // this is a bit futile
+              this.setClosed(false);
+            } catch (PropertyVetoException e)
+            {
+            }
+            QuitHandler.abortQuit();
+          }
           return;
         }
         forceClose = confirm == JvOptionPane.YES_OPTION;
@@ -1297,9 +1362,26 @@ public abstract class StructureViewerBase extends GStructureViewer
     // TODO: check for memory leaks where instance isn't finalised because jmb
     // holds a reference to the window
     // jmb = null;
+    
+    try {
+      svbs.remove(this);
+    } catch (Throwable t)
+    {
+      Console.info("Unexpected exception when deregistering structure viewer",t);
+    }
     dispose();
   }
 
+  private int showCloseDialog(final String title, final String prompt)
+  {
+    int confirmResponse = JvOptionPane.CANCEL_OPTION;
+    confirmResponse = JvOptionPane.showConfirmDialog(this, prompt,
+            MessageManager.getString("label.close_viewer"),
+            JvOptionPane.YES_NO_CANCEL_OPTION,
+            JvOptionPane.WARNING_MESSAGE);
+    return confirmResponse;
+  }
+
   @Override
   public void showHelp_actionPerformed()
   {
@@ -1330,4 +1412,5 @@ public abstract class StructureViewerBase extends GStructureViewer
             && viewerActionMenu.getItemCount() > 0
             && viewerActionMenu.isVisible();
   }
+
 }