JAL-3518 tidy method, remove obsolete comment
[jalview.git] / src / jalview / ext / jmol / JalviewJmolBinding.java
index fe5c980..08ea052 100644 (file)
  */
 package jalview.ext.jmol;
 
-import jalview.api.FeatureRenderer;
-import jalview.datamodel.AlignmentI;
-import jalview.datamodel.HiddenColumns;
-import jalview.datamodel.PDBEntry;
-import jalview.datamodel.SequenceI;
-import jalview.gui.IProgressIndicator;
-import jalview.gui.StructureViewer.ViewerType;
-import jalview.io.DataSourceType;
-import jalview.io.StructureFile;
-import jalview.structure.AtomSpec;
-import jalview.structure.StructureCommandsI.SuperposeData;
-import jalview.structure.StructureSelectionManager;
-import jalview.structures.models.AAStructureBindingModel;
-import jalview.util.MessageManager;
-
 import java.awt.Container;
 import java.awt.event.ComponentEvent;
 import java.awt.event.ComponentListener;
 import java.io.File;
 import java.net.URL;
 import java.util.ArrayList;
-import java.util.BitSet;
 import java.util.List;
 import java.util.Map;
 import java.util.StringTokenizer;
@@ -55,6 +39,19 @@ import org.jmol.api.JmolViewer;
 import org.jmol.c.CBK;
 import org.jmol.viewer.Viewer;
 
+import jalview.api.FeatureRenderer;
+import jalview.datamodel.PDBEntry;
+import jalview.datamodel.SequenceI;
+import jalview.gui.IProgressIndicator;
+import jalview.gui.StructureViewer.ViewerType;
+import jalview.io.DataSourceType;
+import jalview.io.StructureFile;
+import jalview.structure.AtomSpec;
+import jalview.structure.StructureCommand;
+import jalview.structure.StructureCommandI;
+import jalview.structure.StructureSelectionManager;
+import jalview.structures.models.AAStructureBindingModel;
+
 public abstract class JalviewJmolBinding extends AAStructureBindingModel
         implements JmolStatusListener, JmolSelectionListener,
         ComponentListener
@@ -125,19 +122,21 @@ public abstract class JalviewJmolBinding extends AAStructureBindingModel
   }
 
   @Override
-  public List<String> executeCommand(String command, boolean getReply)
+  public List<String> executeCommand(StructureCommandI command,
+          boolean getReply)
   {
     if (command == null)
     {
       return null;
     }
+    String cmd = command.getCommand();
     jmolHistory(false);
-    if (lastCommand == null || !lastCommand.equals(command))
+    if (lastCommand == null || !lastCommand.equals(cmd))
     {
-      jmolViewer.evalStringQuiet(command + "\n");
+      jmolViewer.evalStringQuiet(cmd + "\n");
     }
     jmolHistory(true);
-    lastCommand = command;
+    lastCommand = cmd;
     return null;
   }
 
@@ -189,7 +188,6 @@ public abstract class JalviewJmolBinding extends AAStructureBindingModel
   @Override
   public synchronized String[] getStructureFiles()
   {
-    List<String> mset = new ArrayList<>();
     if (jmolViewer == null)
     {
       return new String[0];
@@ -199,10 +197,15 @@ public abstract class JalviewJmolBinding extends AAStructureBindingModel
     {
       int modelCount = jmolViewer.ms.mc;
       String filePath = null;
+      List<String> mset = new ArrayList<>();
       for (int i = 0; i < modelCount; ++i)
       {
+        /*
+         * defensive check for null as getModelFileName can return null
+         * even when model count ms.mc is > 0
+         */
         filePath = jmolViewer.ms.getModelFileName(i);
-        if (!mset.contains(filePath))
+        if (filePath != null && !mset.contains(filePath))
         {
           mset.add(filePath);
         }
@@ -257,52 +260,32 @@ public abstract class JalviewJmolBinding extends AAStructureBindingModel
   public void highlightAtom(int atomIndex, int pdbResNum, String chain,
           String pdbfile)
   {
-    if (modelFileNames == null)
-    {
-      return;
-    }
-
-    // look up file model number for this pdbfile
-    int mdlNum = 0;
-    // may need to adjust for URLencoding here - we don't worry about that yet.
-    while (mdlNum < modelFileNames.length
-            && !pdbfile.equals(modelFileNames[mdlNum]))
-    {
-      mdlNum++;
-    }
-    if (mdlNum == modelFileNames.length)
+    String modelId = getModelIdForFile(pdbfile);
+    if (modelId.isEmpty())
     {
       return;
     }
 
     jmolHistory(false);
 
+    StringBuilder selection = new StringBuilder(32);
     StringBuilder cmd = new StringBuilder(64);
-    cmd.append("select ").append(String.valueOf(pdbResNum)); // +modelNum
-
-    resetLastRes.append("select ").append(String.valueOf(pdbResNum)); // +modelNum
-
-    cmd.append(":");
-    resetLastRes.append(":");
+    selection.append("select ").append(String.valueOf(pdbResNum));
+    selection.append(":");
     if (!chain.equals(" "))
     {
-      cmd.append(chain);
-      resetLastRes.append(chain);
-    }
-    {
-      cmd.append(" /").append(String.valueOf(mdlNum + 1));
-      resetLastRes.append("/").append(String.valueOf(mdlNum + 1));
+      selection.append(chain);
     }
-    cmd.append(";wireframe 100;" + cmd.toString() + " and not hetero;");
+    selection.append(" /").append(modelId);
 
-    resetLastRes.append(";wireframe 0;" + resetLastRes.toString()
-            + " and not hetero; spacefill 0;");
+    cmd.append(selection).append(";wireframe 100;").append(selection)
+            .append(" and not hetero;").append("spacefill 200;select none");
 
-    cmd.append("spacefill 200;select none");
+    resetLastRes.append(selection).append(";wireframe 0;").append(selection)
+            .append(" and not hetero; spacefill 0;");
 
     jmolViewer.evalStringQuiet(cmd.toString());
     jmolHistory(true);
-
   }
 
   private boolean debug = true;
@@ -425,7 +408,7 @@ public abstract class JalviewJmolBinding extends AAStructureBindingModel
       sb.append(";set hoverLabel \"").append(toks.nextToken()).append(" ")
               .append(toks.nextToken());
       sb.append("|").append(label).append("\"");
-      executeCommand(sb.toString(), false);
+      executeCommand(new StructureCommand(sb.toString()), false);
     }
   }
 
@@ -712,14 +695,7 @@ public abstract class JalviewJmolBinding extends AAStructureBindingModel
         }
         if (matches)
         {
-          // add an entry for every chain in the model
-          for (int i = 0; i < pdb.getChains().size(); i++)
-          {
-            String chid = pdb.getId() + ":"
-                    + pdb.getChains().elementAt(i).id;
-            addChainFile(chid, fileName);
-            getChainNames().add(chid);
-          }
+          stashFoundChains(pdb, fileName);
           notifyLoaded = true;
         }
       }
@@ -955,20 +931,20 @@ public abstract class JalviewJmolBinding extends AAStructureBindingModel
   }
 
   @Override
-  protected int getModelNoForFile(String pdbFile)
+  protected String getModelIdForFile(String pdbFile)
   {
     if (modelFileNames == null)
     {
-      return -1;
+      return "";
     }
     for (int i = 0; i < modelFileNames.length; i++)
     {
       if (modelFileNames[i].equalsIgnoreCase(pdbFile))
       {
-        return i;
+        return String.valueOf(i + 1);
       }
     }
-    return -1;
+    return "";
   }
 
   @Override
@@ -976,4 +952,22 @@ public abstract class JalviewJmolBinding extends AAStructureBindingModel
   {
     return ViewerType.JMOL;
   }
+
+  @Override
+  protected String getModelId(int pdbfnum, String file)
+  {
+    return String.valueOf(pdbfnum + 1);
+  }
+
+  /**
+   * Returns ".spt" - the Jmol session file extension
+   * 
+   * @return
+   * @see https://chemapps.stolaf.edu/jmol/docs/#writemodel
+   */
+  @Override
+  public String getSessionFileExtension()
+  {
+    return ".spt";
+  }
 }