X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fgui%2FStructureViewerBase.java;h=ed42ffa7c98761275a3e42a9015e8dc12e183c9e;hb=7f09e6ac5717bc78373c5d77f309831403e6ebf8;hp=9a575ffc06cb1b7b8f072db319dcc6bd71ded4aa;hpb=b865b46f6f34e82f5d10fccfe646f00b8ae2e7a1;p=jalview.git diff --git a/src/jalview/gui/StructureViewerBase.java b/src/jalview/gui/StructureViewerBase.java index 9a575ff..ed42ffa 100644 --- a/src/jalview/gui/StructureViewerBase.java +++ b/src/jalview/gui/StructureViewerBase.java @@ -37,9 +37,6 @@ import java.util.ArrayList; import java.util.List; import java.util.Random; import java.util.Vector; -import java.util.concurrent.Callable; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.TimeUnit; import javax.swing.ButtonGroup; import javax.swing.JCheckBoxMenuItem; @@ -50,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; @@ -91,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 svbs = new ArrayList<>(); + + /** + * + * @return list with all existing StructureViewers instance + */ + public static List getAllStructureViewerBases() + { + List 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 _aps = new ArrayList<>(); @@ -140,6 +161,7 @@ public abstract class StructureViewerBase extends GStructureViewer { super(); setFrameIcon(null); + svbs.add(this); } /** @@ -183,6 +205,7 @@ public abstract class StructureViewerBase extends GStructureViewer return _aps.contains(ap2.av.getSequenceSetId()); } + @Override public boolean isUsedforaligment(AlignmentViewPanel ap2) { @@ -1200,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 { @@ -1255,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 @@ -1267,19 +1304,29 @@ 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(); int confirm = JvOptionPane.CANCEL_OPTION; - 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); + 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 @@ -1315,59 +1362,23 @@ 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) { - confirmResponse = JvOptionPane.CANCEL_OPTION; - - if (QuitHandler.quitting()) - { - - Callable yesCall = () -> { - Console.debug("YES"); - confirmResponse = JvOptionPane.YES_OPTION; - return null; - }; - Callable noCall = () -> { - Console.debug("NO"); - confirmResponse = JvOptionPane.NO_OPTION; - return null; - }; - Callable cancelCall = () -> { - Console.debug("CANCEL"); - confirmResponse = JvOptionPane.CANCEL_OPTION; - return null; - }; - Callable[] calls = new Callable[] { yesCall, noCall, - cancelCall }; - String cancelQuit = MessageManager.getString("action.cancel_quit"); - String[] buttonsText = { MessageManager.getString("action.yes"), - MessageManager.getString("action.no"), cancelQuit }; - JvOptionPane dialog = JvOptionPane.frameDialog(prompt, - MessageManager.getString("label.close_viewer"), - JvOptionPane.WARNING_MESSAGE, buttonsText, cancelQuit, calls, - false); - // wait for response - ExecutorService executor = dialog.getExecutor(); - executor.shutdown(); - try - { - Console.debug("### executor.awaitTermination() starting"); - executor.awaitTermination(60, TimeUnit.SECONDS); - Console.debug("### executor.awaitTermination() finished"); - } catch (InterruptedException e) - { - } - } - else - { - confirmResponse = JvOptionPane.showConfirmDialog(this, prompt, - MessageManager.getString("label.close_viewer"), - JvOptionPane.YES_NO_CANCEL_OPTION, - JvOptionPane.WARNING_MESSAGE); - } + 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; } @@ -1402,5 +1413,4 @@ public abstract class StructureViewerBase extends GStructureViewer && viewerActionMenu.isVisible(); } - private static int confirmResponse = 0; }