JAL-1588 refactoring prior to 'save Chimera project'
[jalview.git] / src / jalview / gui / ChimeraViewFrame.java
index 6278536..567a14c 100644 (file)
  */
 package jalview.gui;
 
-import jalview.api.SequenceStructureBinding;
-import jalview.api.structures.JalviewStructureDisplayI;
 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.gui.ViewSelectionMenu.ViewSetProvider;
 import jalview.io.AppletFormatAdapter;
 import jalview.io.JalviewFileChooser;
@@ -42,6 +41,7 @@ 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;
@@ -79,26 +79,9 @@ import javax.swing.event.MenuListener;
  *
  */
 public class ChimeraViewFrame extends GStructureViewer implements Runnable,
-        ViewSetProvider, JalviewStructureDisplayI
-
+        ViewSetProvider
 {
-  private JalviewChimeraBindingModel jmb;
-
-  /*
-   * list of sequenceSet ids associated with the view
-   */
-  private ArrayList<String> _aps = new ArrayList<String>();
-
-  /*
-   * list of alignment panels to use for superposition
-   */
-  private Vector<AlignmentPanel> _alignwith = new Vector<AlignmentPanel>();
-
-  /*
-   * list of alignment panels that are used for colouring structures by aligned
-   * sequences
-   */
-  private Vector<AlignmentPanel> _colourwith = new Vector<AlignmentPanel>();
+  private JalviewChimeraBinding jmb;
 
   private boolean allChainsSelected = false;
 
@@ -115,8 +98,6 @@ public class ChimeraViewFrame extends GStructureViewer implements Runnable,
 
   private IProgressIndicator progressBar = null;
 
-  private String viewId = null;
-
   /*
    * pdb retrieval thread.
    */
@@ -144,6 +125,9 @@ public class ChimeraViewFrame extends GStructureViewer implements Runnable,
       _alignwith = new Vector<AlignmentPanel>();
     }
 
+    // save As not yet implemented
+    savemenu.setVisible(false);
+
     ViewSelectionMenu seqColourBy = new ViewSelectionMenu(
             MessageManager.getString("label.colour_by"), this, _colourwith,
             new ItemListener()
@@ -241,9 +225,9 @@ public class ChimeraViewFrame extends GStructureViewer implements Runnable,
         // TODO : Fix multiple seq to one chain issue here.
         ap.getStructureSelectionManager().setMapping(seq, chains,
                 alreadyMapped, AppletFormatAdapter.FILE);
-        if (ap.seqPanel.seqCanvas.fr != null)
+        if (ap.getSeqPanel().seqCanvas.fr != null)
         {
-          ap.seqPanel.seqCanvas.fr.featuresAdded();
+          ap.getSeqPanel().seqCanvas.fr.featuresAdded();
           ap.paintAlignment(true);
         }
 
@@ -258,9 +242,11 @@ public class ChimeraViewFrame extends GStructureViewer implements Runnable,
             final ChimeraViewFrame topView = ((ChimeraViewFrame) frame);
             // JBPNOTE: this looks like a binding routine, rather than a gui
             // routine
-            for (int pe = 0; pe < topView.jmb.pdbentry.length; pe++)
+            for (int pe = 0; pe < topView.jmb.getPdbCount(); pe++)
             {
-              if (topView.jmb.pdbentry[pe].getFile().equals(alreadyMapped))
+              if (topView.jmb.getPdbEntry(pe).getFile()
+                      .equals(
+                      alreadyMapped))
               {
                 topView.jmb.addSequence(pe, seq);
                 topView.addAlignmentPanel(ap);
@@ -546,7 +532,7 @@ public class ChimeraViewFrame extends GStructureViewer implements Runnable,
     jmb.setFinishedInit(false);
     // TODO: consider waiting until the structure/view is fully loaded before
     // displaying
-    jalview.gui.Desktop.addInternalFrame(this, jmb.getViewerTitle(),
+    jalview.gui.Desktop.addInternalFrame(this, jmb.getViewerTitle("Chimera", true),
             getBounds().width, getBounds().height);
     if (command == null)
     {
@@ -619,9 +605,24 @@ public class ChimeraViewFrame extends GStructureViewer implements Runnable,
     jmb.centerViewer(toshow);
   }
 
+  /**
+   * 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.
+   */
   public void closeViewer()
   {
-    jmb.closeViewer();
+    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);
+    }
     ap = null;
     _aps.clear();
     _alignwith.clear();
@@ -649,10 +650,10 @@ public class ChimeraViewFrame extends GStructureViewer implements Runnable,
       String[] curfiles = jmb.getPdbFile(); // files currently in viewer
       // TODO: replace with reference fetching/transfer code (validate PDBentry
       // as a DBRef?)
-      for (int pi = 0; pi < jmb.pdbentry.length; pi++)
+      for (int pi = 0; pi < jmb.getPdbCount(); pi++)
       {
         String file = null;
-        thePdbEntry = jmb.pdbentry[pi];
+        thePdbEntry = jmb.getPdbEntry(pi);
         if (thePdbEntry.getFile() == null)
         {
           /*
@@ -698,7 +699,7 @@ public class ChimeraViewFrame extends GStructureViewer implements Runnable,
     } catch (Exception ex)
     {
       ex.printStackTrace();
-      errormsgs.append("When retrieving pdbfiles : current was: '"
+      errormsgs.append("When retrieving pdbfiles for '"
               + thePdbEntry.getId() + "'");
     }
     if (errormsgs.length() > 0)
@@ -734,7 +735,7 @@ public class ChimeraViewFrame extends GStructureViewer implements Runnable,
           {
             int pos = filePDBpos.get(num).intValue();
             jmb.openFile(pe);
-            jmb.addSequence(pos, jmb.sequence[pos]);
+            jmb.addSequence(pos, jmb.getSequence()[pos]);
             File fl = new File(pe.getFile());
             String protocol = AppletFormatAdapter.URL;
             try
@@ -748,7 +749,8 @@ public class ChimeraViewFrame extends GStructureViewer implements Runnable,
             }
             // Explicitly map to the filename used by Chimera ;
             // TODO: use pe.getId() instead of pe.getFile() ?
-            jmb.ssm.setMapping(jmb.sequence[pos], null, pe.getFile(),
+            jmb.getSsm().setMapping(jmb.getSequence()[pos], null,
+                    pe.getFile(),
                     protocol);
           } catch (OutOfMemoryError oomerror)
           {
@@ -818,11 +820,18 @@ public class ChimeraViewFrame extends GStructureViewer implements Runnable,
     } catch (OutOfMemoryError oomerror)
     {
       new OOMWarning("Retrieving PDB id " + pdbid, oomerror);
-    }
-    if (progressBar != null)
+    } finally
     {
-      progressBar.setProgressBar(
-              MessageManager.getString("label.state_completed"), hdl);
+      if (progressBar != null)
+      {
+        progressBar
+                .setProgressBar(
+                        pdbid
+                                + " "
+                                + MessageManager
+                                        .getString("label.state_completed"),
+                        hdl);
+      }
     }
     /*
      * If PDB data were saved and are not invalid (empty alignment), return the
@@ -894,9 +903,9 @@ public class ChimeraViewFrame extends GStructureViewer implements Runnable,
     jalview.gui.CutAndPasteTransfer cap = new jalview.gui.CutAndPasteTransfer();
     try
     {
-      for (int pdbe = 0; pdbe < jmb.pdbentry.length; pdbe++)
+      for (int pdbe = 0; pdbe < jmb.getPdbCount(); pdbe++)
       {
-        cap.appendText(jmb.printMapping(jmb.pdbentry[pdbe].getFile()));
+        cap.appendText(jmb.printMapping(jmb.getPdbEntry(pdbe).getFile()));
         cap.appendText("\n");
       }
     } catch (OutOfMemoryError e)
@@ -1064,15 +1073,6 @@ public class ChimeraViewFrame extends GStructureViewer implements Runnable,
     }
   }
 
-  public String getViewId()
-  {
-    if (viewId == null)
-    {
-      viewId = System.currentTimeMillis() + "." + this.hashCode();
-    }
-    return viewId;
-  }
-
   public void updateTitleAndMenus()
   {
     if (jmb.fileLoadingError != null && jmb.fileLoadingError.length() > 0)
@@ -1082,8 +1082,8 @@ public class ChimeraViewFrame extends GStructureViewer implements Runnable,
     }
     setChainMenuItems(jmb.chainNames);
 
-    this.setTitle(jmb.getViewerTitle());
-    if (jmb.getPdbFile().length > 1 && jmb.sequence.length > 1)
+    this.setTitle(jmb.getViewerTitle("Chimera", true));
+    if (jmb.getPdbFile().length > 1 && jmb.getSequence().length > 1)
     {
       viewerActionMenu.setVisible(true);
     }
@@ -1190,39 +1190,20 @@ public class ChimeraViewFrame extends GStructureViewer implements Runnable,
     return ap;
   }
 
-  /**
-   * 
-   * @param ap2
-   * @return true if this Chimera instance is linked with the given alignPanel
-   */
-  public boolean isLinkedWith(AlignmentPanel ap2)
-  {
-    return _aps.contains(ap2.av.getSequenceSetId());
-  }
-
-  public boolean isUsedforaligment(AlignmentPanel ap2)
-  {
-
-    return (_alignwith != null) && _alignwith.contains(ap2);
-  }
-
-  public boolean isUsedforcolourby(AlignmentPanel ap2)
+  @Override
+  public AAStructureBindingModel getBinding()
   {
-    return (_colourwith != null) && _colourwith.contains(ap2);
+    return jmb;
   }
 
   /**
+   * Ask Chimera to save its session to the designated file path. Returns true
+   * if successful, else false.
    * 
-   * @return TRUE if the view is NOT being coloured by sequence associations.
+   * @param filepath
    */
-  public boolean isColouredByChimera()
+  public boolean saveSession(String filepath)
   {
-    return !jmb.isColourBySequence();
+    return jmb.saveSession(filepath);
   }
-
-  public SequenceStructureBinding getBinding()
-  {
-    return jmb;
-  }
-
 }