Jalview 2.6 source licence
[jalview.git] / src / jalview / ext / jmol / JalviewJmolBinding.java
index c852a1a..c6871a9 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Jalview - A Sequence Alignment Editor and Viewer (Version 2.5)
+ * Jalview - A Sequence Alignment Editor and Viewer (Version 2.6)
  * Copyright (C) 2010 J Procter, AM Waterhouse, G Barton, M Clamp, S Searle
  * 
  * This file is part of Jalview.
@@ -39,12 +39,12 @@ import org.jmol.adapter.smarter.SmarterJmolAdapter;
 import org.jmol.popup.*;
 import org.jmol.viewer.JmolConstants;
 import org.jmol.viewer.Viewer;
-import org.openscience.jmol.app.jmolpanel.AppConsole;
 
 import jalview.schemes.*;
 
 public abstract class JalviewJmolBinding implements StructureListener,
-        JmolStatusListener, SequenceStructureBinding, JmolSelectionListener
+        JmolStatusListener, SequenceStructureBinding,
+        JmolSelectionListener, ComponentListener
 
 {
   /**
@@ -81,6 +81,8 @@ public abstract class JalviewJmolBinding implements StructureListener,
 
   public Vector chainNames;
 
+  Hashtable chainFile;
+
   /**
    * array of target chains for seuqences - tied to pdbentry and sequence[]
    */
@@ -211,8 +213,9 @@ public abstract class JalviewJmolBinding implements StructureListener,
         p = mlength;
         mlength = lbl.indexOf(":", p);
       } while (p < mlength && mlength < (lbl.length() - 2));
+      // TODO: lookup each pdb id and recover proper model number for it.
       cmd.append(":" + lbl.substring(mlength + 1) + " /"
-              + getModelNum(lbl.substring(0, mlength)) + " or ");
+              + (1 + getModelNum((String) chainFile.get(lbl))) + " or ");
     }
     if (cmd.length() > 0)
       cmd.setLength(cmd.length() - 4);
@@ -230,8 +233,15 @@ public abstract class JalviewJmolBinding implements StructureListener,
     viewer.setJmolStatusListener(null);
     lastCommand = null;
     viewer = null;
+    releaseUIResources();
   }
 
+  /**
+   * called by JalviewJmolbinding after closeViewer is called - release any
+   * resources and references so they can be garbage collected.
+   */
+  protected abstract void releaseUIResources();
+
   public void colourByChain()
   {
     colourBySequence = false;
@@ -377,7 +387,7 @@ public abstract class JalviewJmolBinding implements StructureListener,
       }
     }
     String[] selcom = new String[files.length];
-    int nmatched=0;
+    int nmatched = 0;
     // generate select statements to select regions to superimpose structures
     {
       for (int pdbfnum = 0; pdbfnum < files.length; pdbfnum++)
@@ -391,7 +401,8 @@ public abstract class JalviewJmolBinding implements StructureListener,
         {
           if (matched[r])
           {
-            if (pdbfnum==0) {
+            if (pdbfnum == 0)
+            {
               nmatched++;
             }
             if (lpos != commonrpositions[pdbfnum][r] - 1)
@@ -440,8 +451,10 @@ public abstract class JalviewJmolBinding implements StructureListener,
         }
       }
     }
-    // TODO: consider bailing if nmatched less than 4 because superposition not well defined.
-    // TODO: refactor superposable position search (above) from jmol selection construction (below)
+    // TODO: consider bailing if nmatched less than 4 because superposition not
+    // well defined.
+    // TODO: refactor superposable position search (above) from jmol selection
+    // construction (below)
     for (int pdbfnum = 0; pdbfnum < files.length; pdbfnum++)
     {
       if (pdbfnum == refStructure)
@@ -541,7 +554,7 @@ public abstract class JalviewJmolBinding implements StructureListener,
 
               Color col = sr.getResidueBoxColour(sequence[pdbfnum][s], r);
 
-              if (showFeatures)
+              if (showFeatures && fr != null)
                 col = fr.findFeatureColour(col, sequence[pdbfnum][s], r);
               String newSelcom = (mapping[m].getChain() != " " ? ":"
                       + mapping[m].getChain() : "")
@@ -690,7 +703,7 @@ public abstract class JalviewJmolBinding implements StructureListener,
   // /StructureListener
   public synchronized String[] getPdbFile()
   {
-    if (viewer==null)
+    if (viewer == null)
     {
       return new String[0];
     }
@@ -706,8 +719,8 @@ public abstract class JalviewJmolBinding implements StructureListener,
         mset[j] = viewer.getModelFileName(i);
         _modelFileNameMap[j] = i; // record the model index for the filename
         // skip any additional models in the same file (NMR structures)
-        if ((mset[j] == null ? mset[j] != mset[j - 1] : (mset[j - 1] == null
-                || !mset[j].equals(mset[j - 1]))))
+        if ((mset[j] == null ? mset[j] != mset[j - 1]
+                : (mset[j - 1] == null || !mset[j].equals(mset[j - 1]))))
         {
           j++;
         }
@@ -1020,12 +1033,16 @@ public abstract class JalviewJmolBinding implements StructureListener,
     return false;
   }
 
-  // incremented every time a load notification is successfully handled - lightweight mechanism for other threads to detect when they can start referrring to new structures. 
-  private long loadNotifiesHandled=0;
+  // incremented every time a load notification is successfully handled -
+  // lightweight mechanism for other threads to detect when they can start
+  // referrring to new structures.
+  private long loadNotifiesHandled = 0;
+
   public long getLoadNotifiesHandled()
   {
     return loadNotifiesHandled;
   }
+
   public void notifyFileLoaded(String fullPathName, String fileName2,
           String modelName, String errorMsg, int modelParts)
   {
@@ -1045,6 +1062,7 @@ public abstract class JalviewJmolBinding implements StructureListener,
     String[] oldmodels = modelFileNames;
     modelFileNames = null;
     chainNames = new Vector();
+    chainFile = new Hashtable();
     boolean notifyLoaded = false;
     String[] modelfilenames = getPdbFile();
     ssm = StructureSelectionManager.getStructureSelectionManager();
@@ -1094,9 +1112,10 @@ public abstract class JalviewJmolBinding implements StructureListener,
       if (loadedInline)
       {
         // calculate essential attributes for the pdb data imported inline.
-        // prolly need to resolve modelnumber properly - for now just use our 'best guess'
-        pdbfile = viewer.getData(""+(1+_modelFileNameMap[modelnum])+".0",
-                "PDB");
+        // prolly need to resolve modelnumber properly - for now just use our
+        // 'best guess'
+        pdbfile = viewer.getData("" + (1 + _modelFileNameMap[modelnum])
+                + ".0", "PDB");
         pdbfhash = "" + pdbfile.hashCode();
       }
       if (pdbentry != null)
@@ -1108,8 +1127,8 @@ public abstract class JalviewJmolBinding implements StructureListener,
           boolean matches = false;
           if (fileName == null)
           {
-            if (false) 
-              // see JAL-623 - need method of matching pasted data up
+            if (false)
+            // see JAL-623 - need method of matching pasted data up
             {
               pdb = ssm.setMapping(sequence[pe], chains[pe], pdbfile,
                       AppletFormatAdapter.PASTE);
@@ -1145,7 +1164,6 @@ public abstract class JalviewJmolBinding implements StructureListener,
               pdb = ssm.setMapping(sequence[pe], chains[pe],
                       pdbentry[pe].getFile(), protocol);
 
-
             }
           }
           if (matches)
@@ -1154,8 +1172,10 @@ public abstract class JalviewJmolBinding implements StructureListener,
             // add an entry for every chain in the model
             for (int i = 0; i < pdb.chains.size(); i++)
             {
-              chainNames.addElement(new String(pdb.id + ":"
-                      + ((MCview.PDBChain) pdb.chains.elementAt(i)).id));
+              String chid = new String(pdb.id + ":"
+                      + ((MCview.PDBChain) pdb.chains.elementAt(i)).id);
+              chainFile.put(chid, pdbentry[pe].getFile());
+              chainNames.addElement(chid);
             }
             notifyLoaded = true;
           }
@@ -1163,19 +1183,19 @@ public abstract class JalviewJmolBinding implements StructureListener,
       }
       if (!foundEntry && associateNewStructs)
       {
-          // this is a foreign pdb file that jalview doesn't know about - add
-          // it to the dataset and try to find a home - either on a matching
-          // sequence or as a new sequence.
-          String pdbcontent = viewer.getData("/" + (modelnum + 1) + ".1",
-                  "PDB");
-          // parse pdb file into a chain, etc.
-          // locate best match for pdb in associated views and add mapping to
-          // ssm
-          // if properly registered then
-          notifyLoaded = true;
+        // this is a foreign pdb file that jalview doesn't know about - add
+        // it to the dataset and try to find a home - either on a matching
+        // sequence or as a new sequence.
+        String pdbcontent = viewer.getData("/" + (modelnum + 1) + ".1",
+                "PDB");
+        // parse pdb file into a chain, etc.
+        // locate best match for pdb in associated views and add mapping to
+        // ssm
+        // if properly registered then
+        notifyLoaded = true;
 
-        }
       }
+    }
     // FILE LOADED OK
     // so finally, update the jmol bits and pieces
     if (jmolpopup != null)
@@ -1293,9 +1313,11 @@ public abstract class JalviewJmolBinding implements StructureListener,
 
   /**
    * called to show or hide the associated console window container.
+   * 
    * @param show
    */
   public abstract void showConsole(boolean show);
+
   /**
    * @param renderPanel
    * @param jmolfileio
@@ -1306,12 +1328,14 @@ public abstract class JalviewJmolBinding implements StructureListener,
    * @param codeBase
    * @param commandOptions
    */
-  public void allocateViewer(Component renderPanel, boolean jmolfileio,
+  public void allocateViewer(Container renderPanel, boolean jmolfileio,
           String htmlName, URL documentBase, URL codeBase,
           String commandOptions)
   {
-    allocateViewer(renderPanel, jmolfileio, htmlName, documentBase, codeBase, commandOptions, null,null);
+    allocateViewer(renderPanel, jmolfileio, htmlName, documentBase,
+            codeBase, commandOptions, null, null);
   }
+
   /**
    * 
    * @param renderPanel
@@ -1322,83 +1346,58 @@ public abstract class JalviewJmolBinding implements StructureListener,
    * @param documentBase
    * @param codeBase
    * @param commandOptions
-   * @param consolePanel - panel to contain Jmol console
-   * @param buttonsToShow - buttons to show on the console, in ordr
+   * @param consolePanel
+   *          - panel to contain Jmol console
+   * @param buttonsToShow
+   *          - buttons to show on the console, in ordr
    */
-    public void allocateViewer(Component renderPanel, boolean jmolfileio,
-            String htmlName, URL documentBase, URL codeBase,
-            String commandOptions, final Container consolePanel, String buttonsToShow)
-    {
-      viewer = JmolViewer.allocateViewer(renderPanel,
+  public void allocateViewer(Container renderPanel, boolean jmolfileio,
+          String htmlName, URL documentBase, URL codeBase,
+          String commandOptions, final Container consolePanel,
+          String buttonsToShow)
+  {
+    viewer = JmolViewer.allocateViewer(renderPanel,
             (jmolfileio ? new SmarterJmolAdapter() : null), htmlName
                     + ((Object) this).toString(), documentBase, codeBase,
             commandOptions, this);
-      console = new AppConsole(viewer, null, consolePanel,
-              buttonsToShow);
-      viewer.setConsole(new JmolAppConsoleInterface() {
 
-        @Override
-        public JmolScriptEditorInterface getScriptEditor()
-        {
-          return console.getScriptEditor();
-        }
+    console = createJmolConsole(viewer, consolePanel, buttonsToShow);
+    if (consolePanel != null)
+    {
+      consolePanel.addComponentListener(this);
 
-        @Override
-        public JmolAppConsoleInterface getAppConsole(Viewer viewer,
-                Component display)
-        {
-          return console;
-        }
+    }
 
-        public String getText()
-        {
-          return console.getText();
-        }
+  }
 
-        @Override
-        public Object getMyMenuBar()
-        {
-          return console.getMyMenuBar();
-        }
+  protected abstract JmolAppConsoleInterface createJmolConsole(
+          JmolViewer viewer2, Container consolePanel, String buttonsToShow);
 
-        @Override
-        public void setVisible(boolean b)
-        {
-          showConsole(b);
-        }
+  protected org.jmol.api.JmolAppConsoleInterface console = null;
 
-        @Override
-        public void sendConsoleEcho(String strEcho)
-        {
-          console.sendConsoleEcho(strEcho);
-          
-        }
+  @Override
+  public void componentResized(ComponentEvent e)
+  {
 
-        @Override
-        public void sendConsoleMessage(String strInfo)
-        {
-          console.sendConsoleMessage(strInfo);
-        }
+  }
 
-        @Override
-        public void zap()
-        {
-          console.zap();
-        }
+  @Override
+  public void componentMoved(ComponentEvent e)
+  {
 
-        @Override
-        public void dispose()
-        {
-          console.dispose();
-        }
-        
-        
-      });
-      
-      
   }
-    
-  protected org.jmol.api.JmolAppConsoleInterface console = null;
+
+  @Override
+  public void componentShown(ComponentEvent e)
+  {
+    showConsole(true);
+  }
+
+  @Override
+  public void componentHidden(ComponentEvent e)
+  {
+    showConsole(false);
+  }
 
   public void setLoadingFromArchive(boolean loadingFromArchive)
   {