JAL-3949 - refactor logging from jalview.bin.Cache to jalview.bin.Console
[jalview.git] / src / jalview / gui / StructureViewerBase.java
index a0b199b..8290dd9 100644 (file)
@@ -47,6 +47,7 @@ import javax.swing.event.MenuListener;
 
 import jalview.api.AlignmentViewPanel;
 import jalview.bin.Cache;
+import jalview.bin.Console;
 import jalview.datamodel.AlignmentI;
 import jalview.datamodel.PDBEntry;
 import jalview.datamodel.SequenceI;
@@ -63,7 +64,9 @@ import jalview.structure.StructureMapping;
 import jalview.structures.models.AAStructureBindingModel;
 import jalview.util.BrowserLauncher;
 import jalview.util.MessageManager;
+import jalview.ws.dbsources.EBIAlfaFold;
 import jalview.ws.dbsources.Pdb;
+import jalview.ws.utils.UrlDownloadClient;
 
 /**
  * Base class with common functionality for JMol, Chimera or other structure
@@ -153,6 +156,15 @@ public abstract class StructureViewerBase extends GStructureViewer
   {
     alignAddedStructures = alignAdded;
   }
+  
+  /**
+   * called by the binding model to indicate when adding structures is happening or has been completed
+   * @param addingStructures
+   */
+  public synchronized void setAddingStructures(boolean addingStructures)
+  {
+    this.addingStructures = addingStructures;
+  }
 
   /**
    * 
@@ -210,6 +222,10 @@ public abstract class StructureViewerBase extends GStructureViewer
       _alignwith.add(ap);
     }
     ;
+    // TODO: refactor to allow concrete classes to register buttons for adding
+    // here
+    // currently have to override to add buttons back in after they are cleared
+    // in this loop
     for (Component c : viewerActionMenu.getMenuComponents())
     {
       if (c != alignStructs)
@@ -799,7 +815,7 @@ public abstract class StructureViewerBase extends GStructureViewer
       {
         sp.append("'" + alignPanel.getViewName() + "' ");
       }
-      Cache.log.info("Couldn't align structures with the " + sp.toString()
+      Console.info("Couldn't align structures with the " + sp.toString()
               + "associated alignment panels.", e);
     }
     return reply;
@@ -1076,7 +1092,7 @@ public abstract class StructureViewerBase extends GStructureViewer
     progressBar = pi;
   }
 
-  protected void setProgressMessage(String message, long id)
+  public void setProgressMessage(String message, long id)
   {
     if (progressBar != null)
     {
@@ -1121,6 +1137,7 @@ public abstract class StructureViewerBase extends GStructureViewer
   {
     String filePath = null;
     Pdb pdbclient = new Pdb();
+    EBIAlfaFold afclient =  new EBIAlfaFold();
     AlignmentI pdbseq = null;
     String pdbid = processingEntry.getId();
     long handle = System.currentTimeMillis()
@@ -1138,7 +1155,32 @@ public abstract class StructureViewerBase extends GStructureViewer
     // { pdbid }));
     try
     {
-      pdbseq = pdbclient.getSequenceRecords(pdbid);
+      if (afclient.isValidReference(pdbid))
+      {
+        pdbseq = afclient.getSequenceRecords(pdbid,processingEntry.getRetrievalUrl());
+      } else {
+          if (processingEntry.hasRetrievalUrl())
+          {
+            String safePDBId = java.net.URLEncoder.encode(pdbid,"UTF-8");
+                     
+            // retrieve from URL to new local tmpfile
+            File tmpFile = File.createTempFile(safePDBId,
+                    "." + (PDBEntry.Type.MMCIF.toString().equals(
+                            processingEntry.getType().toString()) ? "cif"
+                                    : "pdb"));
+            String fromUrl = processingEntry.getRetrievalUrl();
+            UrlDownloadClient.download(fromUrl, tmpFile);
+            
+            // may not need this check ?
+            String file = tmpFile.getAbsolutePath();
+            if (file != null)
+            {
+              pdbseq = EBIAlfaFold.importDownloadedStructureFromUrl(fromUrl,tmpFile,pdbid,null,null,null);
+            }
+          } else {
+            pdbseq = pdbclient.getSequenceRecords(pdbid);
+          }
+      }
     } catch (Exception e)
     {
       System.err.println(
@@ -1171,8 +1213,23 @@ public abstract class StructureViewerBase extends GStructureViewer
    */
   public File saveSession()
   {
-    // TODO: a wait loop to ensure the file is written fully before returning?
-    return getBinding() == null ? null : getBinding().saveSession();
+    if (getBinding() == null) { return  null;}
+    File session = getBinding().saveSession();
+    long l = session.length();
+    int wait=50;
+    do {
+      try {
+        Thread.sleep(5);
+      } catch (InterruptedException e) {
+      } 
+      long nextl = session.length();
+      if (nextl!=l)
+      {
+        wait = 50;
+        l=nextl;
+      }
+    } while (--wait>0);
+    return session;
   }
 
   /**
@@ -1241,5 +1298,11 @@ public abstract class StructureViewerBase extends GStructureViewer
                       + ex.getMessage());
     }
   }
-
+  @Override
+  public boolean hasViewerActionsMenu()
+  {
+    return viewerActionMenu != null && viewerActionMenu.isEnabled()
+            && viewerActionMenu.getItemCount() > 0
+            && viewerActionMenu.isVisible();
+  }
 }