refactored ebi soap client and AbstractSequenceFetcher machinery to their own packages.
[jalview.git] / src / MCview / PDBViewer.java
index c70bc6f..e586707 100755 (executable)
@@ -20,37 +20,47 @@ package MCview;
 
 import java.io.*;
 
-import java.awt.*;
 import java.awt.event.*;
 import javax.swing.*;
-import java.util.BitSet;
 
 import jalview.datamodel.*;
 import jalview.gui.*;
 import jalview.io.*;
 import jalview.schemes.*;
+import jalview.ws.ebi.EBIFetchClient;
 
-import org.jmol.api.*;
-import org.jmol.adapter.smarter.SmarterJmolAdapter;
-import org.jmol.util.Logger;
-import org.jmol.popup.*;
+import java.awt.BorderLayout;
 
 public class PDBViewer
     extends JInternalFrame implements Runnable
 {
-  PDBEntry pdb;
+
   /**
    * The associated sequence in an alignment
    */
-  Sequence sequence;
   PDBCanvas pdbcanvas;
+
+  PDBEntry pdbentry;
+  SequenceI[]seq;
+  String[]chains;
+  AlignmentPanel ap;
+  String protocol;
   String tmpPDBFile;
 
-  public PDBViewer(PDBEntry entry,
-                   Sequence seq,
-                   SeqCanvas seqcanvas)
+  public PDBViewer(PDBEntry pdbentry,
+                   SequenceI[] seq,
+                   String[] chains,
+                   AlignmentPanel ap,
+                   String protocol)
+
   {
-   /* try
+    this.pdbentry = pdbentry;
+    this.seq = seq;
+    this.chains = chains;
+    this.ap = ap;
+    this.protocol = protocol;
+
+    try
     {
       jbInit();
     }
@@ -59,22 +69,24 @@ public class PDBViewer
       ex.printStackTrace();
     }
 
-    if (entry == null)
-    {
-      return;
-    }
-    pdb = entry;
-    sequence = seq;
-    pdbcanvas = new PDBCanvas(seqcanvas, sequence);
 
-    if (pdb.getFile() != null)
+    StringBuffer title = new StringBuffer(seq[0].getName()
+                                          + ":"
+                                          + pdbentry.getFile());
+
+    pdbcanvas = new PDBCanvas();
+
+    setContentPane(pdbcanvas);
+
+    if (pdbentry.getFile() != null)
     {
       try
       {
-        tmpPDBFile = pdb.getFile();
+        tmpPDBFile = pdbentry.getFile();
         PDBfile pdbfile = new PDBfile(tmpPDBFile,
                                       jalview.io.AppletFormatAdapter.FILE);
-        pdbcanvas.setPDBFile(pdbfile);
+
+        pdbcanvas.init(pdbentry, seq, chains, ap, protocol);
 
       }
       catch (java.io.IOException ex)
@@ -88,277 +100,40 @@ public class PDBViewer
       worker.start();
     }
 
-      public void run()
-      {
-        try
-        {
-          EBIFetchClient ebi = new EBIFetchClient();
-          String query = "pdb:" + pdb.getId();
-          tmpPDBFile = ebi.fetchDataAsFile(query, "default", "raw").getAbsolutePath();
-          if (tmpPDBFile != null)
-          {
-            PDBfile pdbfile = new PDBfile(tmpPDBFile,
-                                          jalview.io.AppletFormatAdapter.FILE);
-            pdbcanvas.setPDBFile(pdbfile);
-          }
-          else
-          {
-            throw new Exception("Empty result for WSDbFetch Query: " + query);
-          }
-        }
-        catch (Exception ex)
-        {
-          ex.printStackTrace();
-          showErrorMessage("Failed to retrieve PDB structure.");
-//      this.dispose();
-        }
-      }
 
 
-    setContentPane(pdbcanvas);
-    StringBuffer title = new StringBuffer(sequence.getName() + ":" + pdb.getId());
-    if (pdb.getProperty() != null)
+    if (pdbentry.getProperty() != null)
     {
-      if (pdb.getProperty().get("method") != null)
+      if (pdbentry.getProperty().get("method") != null)
       {
         title.append(" Method: ");
-        title.append(pdb.getProperty().get("method"));
+        title.append(pdbentry.getProperty().get("method"));
       }
-      if (pdb.getProperty().get("chains") != null)
+      if (pdbentry.getProperty().get("chains") != null)
       {
         title.append(" Chain:");
-        title.append(pdb.getProperty().get("chains"));
+        title.append(pdbentry.getProperty().get("chains"));
       }
     }
-    */
-
-     Container contentPane = getContentPane();
-     JmolPanel jmolPanel = new JmolPanel();
-     contentPane.add(jmolPanel);
-
-     JmolViewer viewer = jmolPanel.viewer;
-
-
-     System.out.println(entry.getFile()+" "+entry.getId());
-     viewer.openFile(entry.getFile());
-
-
-     StringBuffer string = new StringBuffer();
-
-     for(int i=0; i<seq.getLength(); i++)
-     {
-       Color col = seqcanvas.getSequenceRenderer().getResidueBoxColour(seq, i);
-
-       string.append("select " + i + "B; color [" +
-                     col.getRed()+","
-                     +col.getGreen()+","
-                     +col.getBlue()+"]; ");
-     }
-    System.out.println(string);
-
-    viewer.evalString(string.toString());
-
-
-     String strError = viewer.getOpenFileError();
-     if (strError != null)
-       Logger.error(strError);
-
-
-  //   Desktop.addInternalFrame(this,
-   //                           seq.getName() + ":1GAQ"
-   //                           , 400, 400);
-
+    Desktop.addInternalFrame(this, title.toString(), 400, 400);
   }
 
-
-
-
-  static class JmolPanel
-      extends JPanel implements JmolSelectionListener
-  {
-    JmolViewer viewer;
-    JmolAdapter adapter;
-    JmolPanel()
-    {
-      adapter = new SmarterJmolAdapter(null);
-      viewer = org.jmol.viewer.Viewer.allocateViewer(this, adapter);
-      viewer.addSelectionListener(this);
-
-      MyStatusListener myStatusListener;
-      myStatusListener = new MyStatusListener();
-      viewer.setJmolStatusListener(myStatusListener);
-
-      JFrame frame = new JFrame("script window");
-     // scriptWindow = new ScriptWindow(viewer, frame);
-
-      myStatusListener.jmolpopup = JmolPopup.newJmolPopup(viewer);
-    }
-     public ScriptWindow scriptWindow;
- class MyStatusListener implements JmolStatusListener
- {
-
-   JmolPopup jmolpopup ;
-
-
-   public String eval(String strEval) {
-     return "# 'eval' is implemented only for the applet.";
-   }
-
-   public void createImage(String file, String type, int quality) {
-   System.out.println("CREATE IMAGE");
-     // ImageCreator c = new ImageCreator(viewer, status);
-   //  c.createImage(file, type, quality);
-   }
-
-   public void setCallbackFunction(String callbackType, String callbackFunction) {
-     // applet only?
-   }
-
-   public void notifyFileLoaded(String fullPathName, String fileName,
-                                String modelName, Object clientFile,
-                                String errorMsg) {
-
-   }
-
-   public void notifyFrameChanged(int frameNo) {
-     // Note: twos-complement. To get actual frame number, use
-     // Math.max(frameNo, -2 - frameNo)
-     // -1 means all frames are now displayed
-
-     boolean isAnimationRunning = (frameNo <= -2);
-
-   }
-
-   public void notifyScriptStart(String statusMessage, String additionalInfo) {
-     //System.out.println("notifyScriptStart:" + statusMessage + (additionalInfo == "" ? "" : additionalInfo));
-   }
-
-   public void sendConsoleEcho(String strEcho) {
-     if (scriptWindow != null)
-       scriptWindow.sendConsoleEcho(strEcho);
-   }
-
-   public void sendConsoleMessage(String strStatus) {
-     if (scriptWindow != null)
-       scriptWindow.sendConsoleMessage(strStatus);
-   }
-
-   public void notifyScriptTermination(String strStatus, int msWalltime) {
-     if (scriptWindow != null)
-       scriptWindow.notifyScriptTermination(strStatus, msWalltime);
-   }
-
-   public void handlePopupMenu(int x, int y) {
-     jmolpopup.show(x, y);
-   }
-
-   public void notifyNewPickingModeMeasurement(int iatom, String strMeasure) {
-     notifyAtomPicked(iatom, strMeasure);
-   }
-
-   public void notifyNewDefaultModeMeasurement(int count, String strInfo) {
-   //  measurementTable.updateTables();
-   }
-
-   public void notifyAtomPicked(int atomIndex, String strInfo) {
-
-     System.out.println("ATOM PICKED "+atomIndex+" "+strInfo);
-     if (scriptWindow != null) {
-       scriptWindow.sendConsoleMessage(strInfo);
-       scriptWindow.sendConsoleMessage("\n");
-     }
-   }
-
-   public void notifyAtomHovered(int atomIndex, String strInfo) {
-System.out.println("HOVER "+atomIndex+" "+strInfo);
-   }
-
-   public void sendSyncScript(String script, String appletName) {
-   }
-
-   public void showUrl(String url) {
-   }
-
-   public void showConsole(boolean showConsole) {
-     System.out.println("SHOW CONSOLE" +(scriptWindow==null));
-     if (scriptWindow == null)
-       return;
-     if (showConsole)
-       scriptWindow.show();
-     else
-       scriptWindow.hide();
-   }
-
-   public float functionXY(String functionName, int x, int y) {
-     return 0;  // for user-defined isosurface functions (testing only -- bob hanson)
-   }
-
-    }
-
-
-    class ScriptWindowAction extends AbstractAction {
-
-      public ScriptWindowAction() {
-        super("BLAH");
-      }
-
-      public void actionPerformed(ActionEvent e) {
-        if (scriptWindow != null)
-        scriptWindow.show();
-      }
-    }
-
-    /**
-     * Called when the selected atoms change
-     * @param selection bit set giving selection of atom indexes
-     */
-    public void selectionChanged(BitSet selection)
-    {
-  System.out.println("SELECTION: "+selection);
-    }
-
-
-    public JmolSimpleViewer getViewer()
-    {
-      return viewer;
-    }
-
-    final Dimension currentSize = new Dimension();
-    final Rectangle rectClip = new Rectangle();
-
-    public void paint(Graphics g)
-    {
-
-      getSize(currentSize);
-      g.getClipBounds(rectClip);
-      viewer.renderScreenImage(g, currentSize, rectClip);
-    }
-  }
-
-
   public void run()
   {
     try
     {
       EBIFetchClient ebi = new EBIFetchClient();
-      String query = "pdb:" + pdb.getId();
-      tmpPDBFile = ebi.fetchDataAsFile(query, "default", "raw").getAbsolutePath();
-      if (tmpPDBFile != null)
-      {
-        PDBfile pdbfile = new PDBfile(tmpPDBFile,
-                                      jalview.io.AppletFormatAdapter.FILE);
-        pdbcanvas.setPDBFile(pdbfile);
-      }
-      else
-      {
-        throw new Exception("Empty result for WSDbFetch Query: " + query);
-      }
+      String query = "pdb:" + pdbentry.getId();
+      pdbentry.setFile(ebi.fetchDataAsFile(query, "default", "raw")
+                       .getAbsolutePath());
+
+      if(pdbentry.getFile()!=null)
+        pdbcanvas.init(pdbentry, seq, chains, ap, protocol);
     }
     catch (Exception ex)
     {
+      pdbcanvas.errorMessage = "Error retrieving file: "+pdbentry.getId();
       ex.printStackTrace();
-      showErrorMessage("Failed to retrieve PDB structure.");
-//      this.dispose();
     }
   }
 
@@ -776,7 +551,7 @@ System.out.println("HOVER "+atomIndex+" "+strInfo);
   public void mapping_actionPerformed(ActionEvent e)
   {
     jalview.gui.CutAndPasteTransfer cap = new jalview.gui.CutAndPasteTransfer();
-  //  Desktop.addInternalFrame(cap, "PDB - Sequence Mapping", 550, 600);
+    Desktop.addInternalFrame(cap, "PDB - Sequence Mapping", 550, 600);
     cap.setText(pdbcanvas.mappingDetails.toString());
   }
 
@@ -785,12 +560,6 @@ System.out.println("HOVER "+atomIndex+" "+strInfo);
     pdbcanvas.setAllchainsVisible(allchains.getState());
   }
 
-  void showErrorMessage(String error)
-  {
-  //  JOptionPane.showInternalMessageDialog(Desktop.desktop,
-  //                                        error, "PDB Viewer Error",
-  //                                        JOptionPane.WARNING_MESSAGE);
-  }
 
   public void zappo_actionPerformed(ActionEvent e)
   {
@@ -844,7 +613,7 @@ System.out.println("HOVER "+atomIndex+" "+strInfo);
   {
     if (e.getActionCommand().equals("User Defined..."))
     {
-      new UserDefinedColours(pdbcanvas, null);
+     // new UserDefinedColours(pdbcanvas, null);
     }
     else
     {