JAL-1596 refactoring to allow unit tests to start and shut down Chimera
authorgmungoc <g.m.carstairs@dundee.ac.uk>
Mon, 30 Mar 2015 09:05:22 +0000 (10:05 +0100)
committergmungoc <g.m.carstairs@dundee.ac.uk>
Mon, 30 Mar 2015 09:05:22 +0000 (10:05 +0100)
src/jalview/api/structures/JalviewStructureDisplayI.java
src/jalview/gui/AppJmol.java
src/jalview/gui/ChimeraViewFrame.java
src/jalview/gui/Jalview2XML.java
test/jalview/ext/rbvi/chimera/ChimeraConnect.java
test/jalview/ext/rbvi/chimera/JalviewChimeraView.java

index a846e94..edbbbbf 100644 (file)
@@ -48,9 +48,12 @@ public interface JalviewStructureDisplayI
   void dispose();
 
   /**
-   * shutdown any structure viewing processes started by this display
+   * Shutdown any Jalview structure viewing processes started by this display
+   * 
+   * @param closeExternalViewer
+   *          if true, force close any linked external viewer process
    */
-  void closeViewer();
+  void closeViewer(boolean closeExternalViewer);
   /**
    * apply a colourscheme to the structures in the viewer
    * @param colourScheme
index 86ab536..04235be 100644 (file)
  */
 package jalview.gui;
 
-import jalview.bin.Cache;
-import jalview.datamodel.Alignment;
-import jalview.datamodel.AlignmentI;
-import jalview.datamodel.ColumnSelection;
-import jalview.datamodel.PDBEntry;
-import jalview.datamodel.SequenceI;
-import jalview.io.AppletFormatAdapter;
-import jalview.io.JalviewFileChooser;
-import jalview.io.JalviewFileView;
-import jalview.schemes.BuriedColourScheme;
-import jalview.schemes.ColourSchemeI;
-import jalview.schemes.HelixColourScheme;
-import jalview.schemes.HydrophobicColourScheme;
-import jalview.schemes.PurinePyrimidineColourScheme;
-import jalview.schemes.StrandColourScheme;
-import jalview.schemes.TaylorColourScheme;
-import jalview.schemes.TurnColourScheme;
-import jalview.schemes.ZappoColourScheme;
-import jalview.structures.models.AAStructureBindingModel;
-import jalview.util.MessageManager;
-import jalview.util.Platform;
-
 import java.awt.BorderLayout;
 import java.awt.Color;
 import java.awt.Dimension;
@@ -73,6 +51,28 @@ import javax.swing.event.InternalFrameEvent;
 import javax.swing.event.MenuEvent;
 import javax.swing.event.MenuListener;
 
+import jalview.bin.Cache;
+import jalview.datamodel.Alignment;
+import jalview.datamodel.AlignmentI;
+import jalview.datamodel.ColumnSelection;
+import jalview.datamodel.PDBEntry;
+import jalview.datamodel.SequenceI;
+import jalview.io.AppletFormatAdapter;
+import jalview.io.JalviewFileChooser;
+import jalview.io.JalviewFileView;
+import jalview.schemes.BuriedColourScheme;
+import jalview.schemes.ColourSchemeI;
+import jalview.schemes.HelixColourScheme;
+import jalview.schemes.HydrophobicColourScheme;
+import jalview.schemes.PurinePyrimidineColourScheme;
+import jalview.schemes.StrandColourScheme;
+import jalview.schemes.TaylorColourScheme;
+import jalview.schemes.TurnColourScheme;
+import jalview.schemes.ZappoColourScheme;
+import jalview.structures.models.AAStructureBindingModel;
+import jalview.util.MessageManager;
+import jalview.util.Platform;
+
 public class AppJmol extends StructureViewerBase
 {
   AppJmolBinding jmb;
@@ -184,7 +184,7 @@ public class AppJmol extends StructureViewerBase
     {
       public void internalFrameClosing(InternalFrameEvent internalFrameEvent)
       {
-        closeViewer();
+        closeViewer(false);
       }
     });
     initJmol(loadStatus); // pdbentry, seq, JBPCHECK!
@@ -406,7 +406,7 @@ public class AppJmol extends StructureViewerBase
     {
       public void internalFrameClosing(InternalFrameEvent internalFrameEvent)
       {
-        closeViewer();
+        closeViewer(false);
       }
     });
 
@@ -602,8 +602,9 @@ public class AppJmol extends StructureViewerBase
     jmb.centerViewer(toshow);
   }
 
-  public void closeViewer()
+  public void closeViewer(boolean closeExternalViewer)
   {
+    // JMol does not use an external viewer
     jmb.closeViewer();
     setAlignmentPanel(null);
     _aps.clear();
index 83ea379..fdc7099 100644 (file)
  */
 package jalview.gui;
 
-import jalview.bin.Cache;
-import jalview.datamodel.Alignment;
-import jalview.datamodel.AlignmentI;
-import jalview.datamodel.ColumnSelection;
-import jalview.datamodel.PDBEntry;
-import jalview.datamodel.SequenceI;
-import jalview.ext.rbvi.chimera.JalviewChimeraBinding;
-import jalview.io.AppletFormatAdapter;
-import jalview.io.JalviewFileChooser;
-import jalview.io.JalviewFileView;
-import jalview.schemes.BuriedColourScheme;
-import jalview.schemes.ColourSchemeI;
-import jalview.schemes.HelixColourScheme;
-import jalview.schemes.HydrophobicColourScheme;
-import jalview.schemes.PurinePyrimidineColourScheme;
-import jalview.schemes.StrandColourScheme;
-import jalview.schemes.TaylorColourScheme;
-import jalview.schemes.TurnColourScheme;
-import jalview.schemes.ZappoColourScheme;
-import jalview.structures.models.AAStructureBindingModel;
-import jalview.util.MessageManager;
-import jalview.util.Platform;
-import jalview.ws.dbsources.Pdb;
-
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
 import java.awt.event.ItemEvent;
@@ -69,6 +45,30 @@ import javax.swing.event.InternalFrameEvent;
 import javax.swing.event.MenuEvent;
 import javax.swing.event.MenuListener;
 
+import jalview.bin.Cache;
+import jalview.datamodel.Alignment;
+import jalview.datamodel.AlignmentI;
+import jalview.datamodel.ColumnSelection;
+import jalview.datamodel.PDBEntry;
+import jalview.datamodel.SequenceI;
+import jalview.ext.rbvi.chimera.JalviewChimeraBinding;
+import jalview.io.AppletFormatAdapter;
+import jalview.io.JalviewFileChooser;
+import jalview.io.JalviewFileView;
+import jalview.schemes.BuriedColourScheme;
+import jalview.schemes.ColourSchemeI;
+import jalview.schemes.HelixColourScheme;
+import jalview.schemes.HydrophobicColourScheme;
+import jalview.schemes.PurinePyrimidineColourScheme;
+import jalview.schemes.StrandColourScheme;
+import jalview.schemes.TaylorColourScheme;
+import jalview.schemes.TurnColourScheme;
+import jalview.schemes.ZappoColourScheme;
+import jalview.structures.models.AAStructureBindingModel;
+import jalview.util.MessageManager;
+import jalview.util.Platform;
+import jalview.ws.dbsources.Pdb;
+
 /**
  * GUI elements for handlnig an external chimera display
  * 
@@ -321,7 +321,7 @@ public class ChimeraViewFrame extends StructureViewerBase
     {
       public void internalFrameClosing(InternalFrameEvent internalFrameEvent)
       {
-        closeViewer();
+        closeViewer(false);
       }
     });
 
@@ -545,19 +545,26 @@ public class ChimeraViewFrame extends StructureViewerBase
    * 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
    * keep it open until they have had an opportunity to save any work.
+   * 
+   * @param closeChimera
+   *          if true, close any linked Chimera process; if false, prompt first
    */
-  public void closeViewer()
+  public void closeViewer(boolean closeChimera)
   {
     if (jmb.isChimeraRunning())
     {
-      String prompt = MessageManager
-              .formatMessage("label.confirm_close_chimera", new Object[]
-              { jmb.getViewerTitle("Chimera", false) });
-      prompt = JvSwingUtils.wrapTooltip(true, prompt);
-      int confirm = JOptionPane.showConfirmDialog(this, prompt,
-              MessageManager.getString("label.close_viewer"),
-              JOptionPane.YES_NO_OPTION);
-      jmb.closeViewer(confirm == JOptionPane.YES_OPTION);
+      if (!closeChimera)
+      {
+        String prompt = MessageManager.formatMessage(
+                "label.confirm_close_chimera", new Object[]
+                { jmb.getViewerTitle("Chimera", false) });
+        prompt = JvSwingUtils.wrapTooltip(true, prompt);
+        int confirm = JOptionPane.showConfirmDialog(this, prompt,
+                MessageManager.getString("label.close_viewer"),
+                JOptionPane.YES_NO_OPTION);
+        closeChimera = confirm == JOptionPane.YES_OPTION;
+      }
+      jmb.closeViewer(closeChimera);
     }
     setAlignmentPanel(null);
     _aps.clear();
index 8beb26f..5317d4c 100644 (file)
@@ -3452,7 +3452,7 @@ public class Jalview2XML
                       (OutOfMemoryError) ex.getCause());
               if (sview != null && sview.isVisible())
               {
-                sview.closeViewer();
+                sview.closeViewer(false);
                 sview.setVisible(false);
                 sview.dispose();
               }
index ad4f997..de7235b 100644 (file)
@@ -1,40 +1,42 @@
 package jalview.ext.rbvi.chimera;
 
+import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertTrue;
 
-import java.util.Collection;
-
 import org.junit.Test;
 
 import ext.edu.ucsf.rbvi.strucviz2.ChimeraManager;
-import ext.edu.ucsf.rbvi.strucviz2.ChimeraModel;
 import ext.edu.ucsf.rbvi.strucviz2.StructureManager;
 
 public class ChimeraConnect
 {
 
   @Test
-  public void test()
+  public void testLaunchAndExit()
   {
-    StructureManager csm;
-            ext.edu.ucsf.rbvi.strucviz2.ChimeraManager cm = new ChimeraManager(csm = new ext.edu.ucsf.rbvi.strucviz2.StructureManager(true));
+    final StructureManager structureManager = new StructureManager(true);
+    ChimeraManager cm = new ChimeraManager(structureManager);
     assertTrue("Couldn't launch chimera",
             cm.launchChimera(StructureManager.getChimeraPaths()));
-    int n=0;
-    while (n++<100)
-    {
-      try { 
-        Thread.sleep(1000);
-      } catch (Exception q)
-      {
-        
-      }
-      Collection<ChimeraModel> cms = cm.getChimeraModels();
-      for (ChimeraModel cmod :cms) {
-        System.out.println(cmod.getModelName());
-      }
-    }
+    assertTrue(cm.isChimeraLaunched()); // Chimera process is alive
+    // int n=0;
+    // not sure of the point of this is unless the tester is loading models
+    // manually?
+    // while (n++ < 100)
+    // {
+    // try {
+    // Thread.sleep(1000);
+    // } catch (Exception q)
+    // {
+    //
+    // }
+    // Collection<ChimeraModel> cms = cm.getChimeraModels();
+    // for (ChimeraModel cmod :cms) {
+    // System.out.println(cmod.getModelName());
+    // }
+    // }
     cm.exitChimera();
+    assertFalse(cm.isChimeraLaunched()); // Chimera process has ended
   }
 
 }
index adbf230..f2535cb 100644 (file)
@@ -6,6 +6,7 @@ import org.junit.AfterClass;
 import org.junit.BeforeClass;
 import org.junit.Test;
 
+import jalview.api.structures.JalviewStructureDisplayI;
 import jalview.datamodel.PDBEntry;
 import jalview.datamodel.SequenceI;
 import jalview.gui.AlignFrame;
@@ -42,48 +43,96 @@ public class JalviewChimeraView
 
   }
 
+  @Test
+  public void testSingleSeqViewJMol()
+  {
+    String inFile = "examples/1gaq.txt";
+    AlignFrame af = new jalview.io.FileLoader().LoadFileWaitTillLoaded(
+            inFile, FormatAdapter.FILE);
+    assertTrue("Didn't read input file " + inFile, af != null);
+    for (SequenceI sq : af.getViewport().getAlignment().getSequences())
+    {
+      SequenceI dsq = sq.getDatasetSequence();
+      while (dsq.getDatasetSequence() != null)
+      {
+        dsq = dsq.getDatasetSequence();
+      }
+      if (dsq.getPDBId() != null && dsq.getPDBId().size() > 0)
+      {
+        for (int q = 0; q < dsq.getPDBId().size(); q++)
+        {
+          final StructureViewer structureViewer = new StructureViewer(af
+                  .getViewport().getStructureSelectionManager());
+
+          JalviewStructureDisplayI jmolViewer = structureViewer
+                  .viewStructures(ViewerType.JMOL, af.getCurrentView()
+                          .getAlignPanel(), new PDBEntry[]
+                  { (PDBEntry) dsq.getPDBId().elementAt(q) },
+                          new SequenceI[][]
+                          { new SequenceI[]
+                          { sq } });
+          /*
+           * Wait for viewer thread to start
+           */
+          try
+          {
+            Thread.sleep(1000);
+          } catch (InterruptedException e)
+          {
+          }
+
+          jmolViewer.closeViewer(true);
+          // todo: break here means only once through this loop?
+          break;
+        }
+        break;
+      }
+    }
+  }
 
   @Test
-  public void testSingleSeqView()
+  public void testSingleSeqViewChimera()
   {
     String inFile = "examples/1gaq.txt";
     AlignFrame af = new jalview.io.FileLoader().LoadFileWaitTillLoaded(
             inFile, FormatAdapter.FILE);
     assertTrue("Didn't read input file " + inFile, af != null);
-    for (SequenceI sq:af.getViewport().getAlignment().getSequences())
+    for (SequenceI sq : af.getViewport().getAlignment().getSequences())
     {
-      SequenceI dsq=sq.getDatasetSequence();
-      while (dsq.getDatasetSequence()!=null)
+      SequenceI dsq = sq.getDatasetSequence();
+      while (dsq.getDatasetSequence() != null)
       {
-        dsq=dsq.getDatasetSequence();
+        dsq = dsq.getDatasetSequence();
       }
-      if (dsq.getPDBId()!=null && dsq.getPDBId().size()>0) {
-        for (int q=0;q<dsq.getPDBId().size();q++) 
+      if (dsq.getPDBId() != null && dsq.getPDBId().size() > 0)
+      {
+        for (int q = 0; q < dsq.getPDBId().size(); q++)
         {
-          new StructureViewer(af.getViewport()
-                  .getStructureSelectionManager()).viewStructures(
-                  ViewerType.JMOL,
-                  af.getCurrentView().getAlignPanel(),
-                  new PDBEntry[] { (PDBEntry)dsq.getPDBId().elementAt(q) },
-                  new SequenceI[][] { new SequenceI[] { sq } });
+          final StructureViewer structureViewer = new StructureViewer(af
+                  .getViewport().getStructureSelectionManager());
 
-          new StructureViewer(af.getViewport()
-                  .getStructureSelectionManager()).viewStructures(
-                  ViewerType.CHIMERA,
-                  af.getCurrentView().getAlignPanel(),
-                  new PDBEntry[] { (PDBEntry)dsq.getPDBId().elementAt(q) },
-                  new SequenceI[][] { new SequenceI[] { sq } });
+          JalviewStructureDisplayI chimeraViewer = structureViewer
+                  .viewStructures(ViewerType.CHIMERA, af.getCurrentView()
+                          .getAlignPanel(), new PDBEntry[]
+                  { (PDBEntry) dsq.getPDBId().elementAt(q) },
+                          new SequenceI[][]
+                          { new SequenceI[]
+                          { sq } });
+          /*
+           * Wait for viewer thread to start
+           */
+          try
+          {
+            Thread.sleep(1000);
+          } catch (InterruptedException e)
+          {
+          }
+          chimeraViewer.closeViewer(true);
           // todo: break here means only once through this loop?
           break;
         }
         break;
       }
-   }
-    // try {
-      // why?
-//      Thread.sleep(200000);
-//    } catch (InterruptedException q)
-//    {
-    // }
+    }
   }
 }