JAL-3746 apply copyright to source
[jalview.git] / src / jalview / gui / PymolViewer.java
index 4e0ac95..9dc28c8 100644 (file)
@@ -1,3 +1,23 @@
+/*
+ * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
+ * Copyright (C) $$Year-Rel$$ The Jalview Authors
+ * 
+ * This file is part of Jalview.
+ * 
+ * Jalview is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License 
+ * as published by the Free Software Foundation, either version 3
+ * of the License, or (at your option) any later version.
+ *  
+ * Jalview is distributed in the hope that it will be useful, but 
+ * WITHOUT ANY WARRANTY; without even the implied warranty 
+ * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
+ * PURPOSE.  See the GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
+ * The Jalview Authors are detailed in the 'AUTHORS' file.
+ */
 package jalview.gui;
 
 import java.awt.event.ActionEvent;
@@ -5,6 +25,7 @@ import java.awt.event.ActionListener;
 import java.io.File;
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Map;
 
 import javax.swing.JInternalFrame;
 import javax.swing.JMenuItem;
@@ -13,9 +34,11 @@ import javax.swing.event.InternalFrameEvent;
 
 import jalview.api.AlignmentViewPanel;
 import jalview.api.FeatureRenderer;
-import jalview.bin.Cache;
+import jalview.bin.Console;
 import jalview.datamodel.PDBEntry;
 import jalview.datamodel.SequenceI;
+import jalview.datamodel.StructureViewerModel;
+import jalview.datamodel.StructureViewerModel.StructureData;
 import jalview.gui.StructureViewer.ViewerType;
 import jalview.io.DataSourceType;
 import jalview.io.StructureFile;
@@ -47,9 +70,7 @@ public class PymolViewer extends StructureViewerBase
           AlignmentPanel ap)
   {
     this();
-    openNewPymol(ap, new PDBEntry[] { pdb },
-            new SequenceI[][]
-            { seqs });
+    openNewPymol(ap, new PDBEntry[] { pdb }, new SequenceI[][] { seqs });
   }
 
   public PymolViewer(PDBEntry[] pe, boolean alignAdded, SequenceI[][] seqs,
@@ -71,22 +92,36 @@ public class PymolViewer extends StructureViewerBase
    * @param colourBySequence
    * @param newViewId
    */
-  public PymolViewer(String sessionFile, AlignmentPanel alignPanel,
-          PDBEntry[] pdbArray, SequenceI[][] seqsArray,
-          boolean colourByPymol, boolean colourBySequence, String newViewId)
+  public PymolViewer(StructureViewerModel viewerModel,
+          AlignmentPanel alignPanel, String sessionFile, String vid)
   {
     // TODO convert to base/factory class method
     this();
-    setViewId(newViewId);
+    setViewId(vid);
     this.pymolSessionFile = sessionFile;
+    Map<File, StructureData> pdbData = viewerModel.getFileData();
+    PDBEntry[] pdbArray = new PDBEntry[pdbData.size()];
+    SequenceI[][] seqsArray = new SequenceI[pdbData.size()][];
+    int i = 0;
+    for (StructureData data : pdbData.values())
+    {
+      PDBEntry pdbentry = new PDBEntry(data.getPdbId(), null,
+              PDBEntry.Type.PDB, data.getFilePath());
+      pdbArray[i] = pdbentry;
+      List<SequenceI> sequencesForPdb = data.getSeqList();
+      seqsArray[i] = sequencesForPdb
+              .toArray(new SequenceI[sequencesForPdb.size()]);
+      i++;
+    }
+
     openNewPymol(alignPanel, pdbArray, seqsArray);
-    if (colourByPymol)
+    if (viewerModel.isColourByViewer())
     {
       binding.setColourBySequence(false);
       seqColour.setSelected(false);
       viewerColour.setSelected(true);
     }
-    else if (colourBySequence)
+    else if (viewerModel.isColourWithAlignPanel())
     {
       binding.setColourBySequence(true);
       seqColour.setSelected(true);
@@ -144,11 +179,12 @@ public class PymolViewer extends StructureViewerBase
   public void run()
   {
     // todo pull up much of this
-  
+
     StringBuilder errormsgs = new StringBuilder(128);
     List<PDBEntry> filePDB = new ArrayList<>();
     List<Integer> filePDBpos = new ArrayList<>();
-    String[] curfiles = binding.getStructureFiles(); // files currently in viewer
+    String[] curfiles = binding.getStructureFiles(); // files currently in
+                                                     // viewer
     for (int pi = 0; pi < binding.getPdbCount(); pi++)
     {
       String file = null;
@@ -169,8 +205,7 @@ public class PymolViewer extends StructureViewerBase
         /*
          * got file already
          */
-        file = new File(thePdbEntry.getFile()).getAbsoluteFile()
-                .getPath();
+        file = new File(thePdbEntry.getFile()).getAbsoluteFile().getPath();
         // todo - skip if already loaded in PyMOL
       }
       if (file != null)
@@ -179,7 +214,7 @@ public class PymolViewer extends StructureViewerBase
         filePDBpos.add(Integer.valueOf(pi));
       }
     }
-        
+
     if (!filePDB.isEmpty())
     {
       /*
@@ -193,9 +228,19 @@ public class PymolViewer extends StructureViewerBase
           initPymol();
         } catch (Exception ex)
         {
-          Cache.log.error("Couldn't open PyMOL viewer!", ex);
+          Console.error("Couldn't open PyMOL viewer!", ex);
+          // if we couldn't open Pymol, no point continuing
+          return;
         }
       }
+      if (!binding.isViewerRunning())
+      {
+        // nothing to do
+        // TODO: ensure we tidy up JAL-3619
+
+        return;
+      }
+
       int num = -1;
       for (PDBEntry pe : filePDB)
       {
@@ -227,17 +272,15 @@ public class PymolViewer extends StructureViewerBase
 
             StructureFile pdb = binding.getSsm().setMapping(
                     binding.getSequence()[pos], binding.getChains()[pos],
-                    pe.getFile(), protocol,
-                    getProgressIndicator());
+                    pe.getFile(), protocol, getProgressIndicator());
             binding.stashFoundChains(pdb, pe.getFile());
           } catch (Exception ex)
           {
-            Cache.log.error(
-                    "Couldn't open " + pe.getFile() + " in Chimera viewer!",
-                    ex);
+            Console.error("Couldn't open " + pe.getFile() + " in "
+                    + getViewerName() + "!", ex);
           } finally
           {
-            // Cache.log.debug("File locations are " + files);
+            // Cache.debug("File locations are " + files);
           }
         }
       }
@@ -297,6 +340,7 @@ public class PymolViewer extends StructureViewerBase
                       getViewerName()),
               MessageManager.getString("label.error_loading_file"),
               JvOptionPane.ERROR_MESSAGE);
+      binding.closeViewer(true);
       this.dispose();
       return;
     }
@@ -306,7 +350,7 @@ public class PymolViewer extends StructureViewerBase
       boolean opened = binding.openSession(pymolSessionFile);
       if (!opened)
       {
-        System.err.println("An error occurred opening PyMOL session file "
+        Console.error("An error occurred opening PyMOL session file "
                 + pymolSessionFile);
       }
     }
@@ -331,6 +375,8 @@ public class PymolViewer extends StructureViewerBase
     return "PyMOL";
   }
 
+  JMenuItem writeFeatures = null;
+
   @Override
   protected void initMenus()
   {
@@ -339,10 +385,10 @@ public class PymolViewer extends StructureViewerBase
     savemenu.setVisible(false); // not yet implemented
     viewMenu.add(fitToWindow);
 
-    JMenuItem writeFeatures = new JMenuItem(
+    writeFeatures = new JMenuItem(
             MessageManager.getString("label.create_viewer_attributes"));
-    writeFeatures.setToolTipText(MessageManager
-            .getString("label.create_viewer_attributes_tip"));
+    writeFeatures.setToolTipText(
+            MessageManager.getString("label.create_viewer_attributes_tip"));
     writeFeatures.addActionListener(new ActionListener()
     {
       @Override
@@ -354,11 +400,18 @@ public class PymolViewer extends StructureViewerBase
     viewerActionMenu.add(writeFeatures);
   }
 
+  @Override
+  protected void buildActionMenu()
+  {
+    super.buildActionMenu();
+    viewerActionMenu.add(writeFeatures);
+  }
+
   protected void sendFeaturesToPymol()
   {
     int count = binding.sendFeaturesToViewer(getAlignmentPanel());
-    statusBar.setText(
-            MessageManager.formatMessage("label.attributes_set", count));
+    statusBar.setText(MessageManager.formatMessage("label.attributes_set",
+            count, getViewerName()));
   }
 
 }