begin process of allowing for column selection in wrapped mode.
[jalview.git] / src / MCview / PDBViewer.java
index d71e9bc..35e02b8 100755 (executable)
@@ -1,6 +1,6 @@
 /*
  * Jalview - A Sequence Alignment Editor and Viewer
- * Copyright (C) 2006 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
+ * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
  */
 package MCview;
 
-import javax.swing.*;
+import java.io.*;
+
 import java.awt.event.*;
+import javax.swing.*;
+
 import jalview.datamodel.*;
-import jalview.schemes.*;
 import jalview.gui.*;
-import jalview.io.EBIFetchClient;
-import java.awt.event.ActionListener;
-import java.awt.event.ActionEvent;
-import java.io.*;
-import jalview.io.JalviewFileChooser;
-import jalview.io.JalviewFileView;
+import jalview.io.*;
+import jalview.schemes.*;
+import jalview.ws.EBIFetchClient;
+
+import java.awt.BorderLayout;
 
-public class PDBViewer extends JInternalFrame implements Runnable
+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 pdbentry,
+                   SequenceI[] seq,
+                   String[] chains,
+                   AlignmentPanel ap,
+                   String protocol)
 
-  public PDBViewer(PDBEntry entry,
-                   Sequence seq,
-                   SeqCanvas seqcanvas)
   {
+    this.pdbentry = pdbentry;
+    this.seq = seq;
+    this.chains = chains;
+    this.ap = ap;
+    this.protocol = protocol;
+
     try
     {
       jbInit();
@@ -55,24 +70,29 @@ public class PDBViewer extends JInternalFrame implements Runnable
     }
 
 
-    if (entry==null)
-      return;
-    pdb = entry;
-    sequence = seq;
-    pdbcanvas = new PDBCanvas(seqcanvas, sequence);
+    StringBuffer title = new StringBuffer(seq[0].getName()
+                                          + ":"
+                                          + pdbentry.getFile());
+
+    pdbcanvas = new PDBCanvas();
 
-    if(pdb.getFile()!=null)
+    setContentPane(pdbcanvas);
+
+    if (pdbentry.getFile() != null)
     {
-        try{
-          tmpPDBFile = pdb.getFile();
-          PDBfile pdbfile = new PDBfile(tmpPDBFile,
-                                        jalview.io.AppletFormatAdapter.FILE);
-          pdbcanvas.setPDBFile(pdbfile);
+      try
+      {
+        tmpPDBFile = pdbentry.getFile();
+        PDBfile pdbfile = new PDBfile(tmpPDBFile,
+                                      jalview.io.AppletFormatAdapter.FILE);
 
-        }catch(java.io.IOException ex)
-        {
-          ex.printStackTrace();
-        }
+        pdbcanvas.init(pdbentry, seq, chains, ap, protocol);
+
+      }
+      catch (java.io.IOException ex)
+      {
+        ex.printStackTrace();
+      }
     }
     else
     {
@@ -82,46 +102,38 @@ public class PDBViewer extends JInternalFrame implements Runnable
 
 
 
-    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"));
       }
     }
-     Desktop.addInternalFrame(this,title.toString(),400, 400);
+    Desktop.addInternalFrame(this, title.toString(), 400, 400);
   }
 
   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);
-        }
+      EBIFetchClient ebi = new EBIFetchClient();
+      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();
     }
   }
 
@@ -129,12 +141,12 @@ public class PDBViewer extends JInternalFrame implements Runnable
       throws Exception
   {
     this.addKeyListener(new KeyAdapter()
-        {
-            public void keyPressed(KeyEvent evt)
-            {
-              pdbcanvas.keyPressed(evt);
-            }
-        });
+    {
+      public void keyPressed(KeyEvent evt)
+      {
+        pdbcanvas.keyPressed(evt);
+      }
+    });
 
     this.setJMenuBar(jMenuBar1);
     fileMenu.setText("File");
@@ -339,45 +351,49 @@ public class PDBViewer extends JInternalFrame implements Runnable
     bg.add(buried);
     bg.add(user);
 
-
-    if(jalview.gui.UserDefinedColours.getUserColourSchemes()!=null)
+    if (jalview.gui.UserDefinedColours.getUserColourSchemes() != null)
     {
       java.util.Enumeration userColours = jalview.gui.UserDefinedColours.
           getUserColourSchemes().keys();
 
       while (userColours.hasMoreElements())
       {
-        final JRadioButtonMenuItem radioItem = new JRadioButtonMenuItem(userColours.
+        final JRadioButtonMenuItem radioItem = new JRadioButtonMenuItem(
+            userColours.
             nextElement().toString());
         radioItem.setName("USER_DEFINED");
         radioItem.addMouseListener(new MouseAdapter()
+        {
+          public void mousePressed(MouseEvent evt)
+          {
+            if (evt.isControlDown() || SwingUtilities.isRightMouseButton(evt))
             {
-              public void mousePressed(MouseEvent evt)
+              radioItem.removeActionListener(radioItem.getActionListeners()[0]);
+
+              int option = JOptionPane.showInternalConfirmDialog(jalview.gui.
+                  Desktop.desktop,
+                  "Remove from default list?",
+                  "Remove user defined colour",
+                  JOptionPane.YES_NO_OPTION);
+              if (option == JOptionPane.YES_OPTION)
+              {
+                jalview.gui.UserDefinedColours.removeColourFromDefaults(
+                    radioItem.getText());
+                coloursMenu.remove(radioItem);
+              }
+              else
               {
-                if(evt.isControlDown() || SwingUtilities.isRightMouseButton(evt))
+                radioItem.addActionListener(new ActionListener()
                 {
-                  radioItem.removeActionListener(radioItem.getActionListeners()[0]);
-
-                  int option = JOptionPane.showInternalConfirmDialog(jalview.gui.Desktop.desktop,
-                      "Remove from default list?",
-                      "Remove user defined colour",
-                      JOptionPane.YES_NO_OPTION);
-                  if(option == JOptionPane.YES_OPTION)
+                  public void actionPerformed(ActionEvent evt)
                   {
-                    jalview.gui.UserDefinedColours.removeColourFromDefaults(radioItem.getText());
-                    coloursMenu.remove(radioItem);
+                    user_actionPerformed(evt);
                   }
-                  else
-                    radioItem.addActionListener(new ActionListener()
-                    {
-                      public void actionPerformed(ActionEvent evt)
-                      {
-                        user_actionPerformed(evt);
-                      }
-                    });
-                }
+                });
               }
-            });
+            }
+          }
+        });
         radioItem.addActionListener(new ActionListener()
         {
           public void actionPerformed(ActionEvent evt)
@@ -413,7 +429,7 @@ public class PDBViewer extends JInternalFrame implements Runnable
   JRadioButtonMenuItem seqButton = new JRadioButtonMenuItem();
   JRadioButtonMenuItem hydro = new JRadioButtonMenuItem();
   JRadioButtonMenuItem taylor = new JRadioButtonMenuItem();
-  JRadioButtonMenuItem zappo = new  JRadioButtonMenuItem();
+  JRadioButtonMenuItem zappo = new JRadioButtonMenuItem();
   JRadioButtonMenuItem user = new JRadioButtonMenuItem();
   JRadioButtonMenuItem buried = new JRadioButtonMenuItem();
   JRadioButtonMenuItem turn = new JRadioButtonMenuItem();
@@ -440,7 +456,7 @@ public class PDBViewer extends JInternalFrame implements Runnable
    */
   public void png_actionPerformed(ActionEvent e)
   {
-     makePDBImage(jalview.util.ImageMaker.PNG);
+    makePDBImage(jalview.util.ImageMaker.PNG);
   }
 
   void makePDBImage(int type)
@@ -451,17 +467,21 @@ public class PDBViewer extends JInternalFrame implements Runnable
     jalview.util.ImageMaker im;
 
     if (type == jalview.util.ImageMaker.PNG)
+    {
       im = new jalview.util.ImageMaker(this,
                                        jalview.util.ImageMaker.PNG,
                                        "Make PNG image from view",
                                        width, height,
                                        null, null);
+    }
     else
+    {
       im = new jalview.util.ImageMaker(this,
                                        jalview.util.ImageMaker.EPS,
                                        "Make EPS file from view",
                                        width, height,
                                        null, this.getTitle());
+    }
 
     if (im.getGraphics() != null)
     {
@@ -469,11 +489,12 @@ public class PDBViewer extends JInternalFrame implements Runnable
       im.writeImage();
     }
   }
+
   public void charge_actionPerformed(ActionEvent e)
   {
     pdbcanvas.bysequence = false;
     pdbcanvas.pdb.setChargeColours();
-    pdbcanvas.redrawneeded=true;
+    pdbcanvas.redrawneeded = true;
     pdbcanvas.repaint();
   }
 
@@ -481,7 +502,7 @@ public class PDBViewer extends JInternalFrame implements Runnable
   {
     pdbcanvas.bysequence = false;
     pdbcanvas.pdb.setColours(new HydrophobicColourScheme());
-    pdbcanvas.redrawneeded=true;
+    pdbcanvas.redrawneeded = true;
     pdbcanvas.repaint();
   }
 
@@ -489,35 +510,35 @@ public class PDBViewer extends JInternalFrame implements Runnable
   {
     pdbcanvas.bysequence = false;
     pdbcanvas.pdb.setChainColours();
-    pdbcanvas.redrawneeded=true;
+    pdbcanvas.redrawneeded = true;
     pdbcanvas.repaint();
   }
 
   public void zbuffer_actionPerformed(ActionEvent e)
   {
-    pdbcanvas.zbuffer = ! pdbcanvas.zbuffer;
-    pdbcanvas.redrawneeded=true;
+    pdbcanvas.zbuffer = !pdbcanvas.zbuffer;
+    pdbcanvas.redrawneeded = true;
     pdbcanvas.repaint();
   }
 
   public void molecule_actionPerformed(ActionEvent e)
   {
-    pdbcanvas.bymolecule = ! pdbcanvas.bymolecule;
-    pdbcanvas.redrawneeded=true;
+    pdbcanvas.bymolecule = !pdbcanvas.bymolecule;
+    pdbcanvas.redrawneeded = true;
     pdbcanvas.repaint();
   }
 
   public void depth_actionPerformed(ActionEvent e)
   {
-  pdbcanvas.depthcue = ! pdbcanvas.depthcue;
-  pdbcanvas.redrawneeded=true;
+    pdbcanvas.depthcue = !pdbcanvas.depthcue;
+    pdbcanvas.redrawneeded = true;
     pdbcanvas.repaint();
   }
 
   public void wire_actionPerformed(ActionEvent e)
   {
-    pdbcanvas.wire = ! pdbcanvas.wire;
-    pdbcanvas.redrawneeded=true;
+    pdbcanvas.wire = !pdbcanvas.wire;
+    pdbcanvas.redrawneeded = true;
     pdbcanvas.repaint();
   }
 
@@ -527,7 +548,6 @@ public class PDBViewer extends JInternalFrame implements Runnable
     pdbcanvas.updateSeqColours();
   }
 
-
   public void mapping_actionPerformed(ActionEvent e)
   {
     jalview.gui.CutAndPasteTransfer cap = new jalview.gui.CutAndPasteTransfer();
@@ -539,18 +559,13 @@ public class PDBViewer extends JInternalFrame implements Runnable
   {
     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)
   {
     pdbcanvas.bysequence = false;
     pdbcanvas.pdb.setColours(new ZappoColourScheme());
-    pdbcanvas.redrawneeded=true;
+    pdbcanvas.redrawneeded = true;
     pdbcanvas.repaint();
   }
 
@@ -558,7 +573,7 @@ public class PDBViewer extends JInternalFrame implements Runnable
   {
     pdbcanvas.bysequence = false;
     pdbcanvas.pdb.setColours(new TaylorColourScheme());
-    pdbcanvas.redrawneeded=true;
+    pdbcanvas.redrawneeded = true;
     pdbcanvas.repaint();
   }
 
@@ -566,7 +581,7 @@ public class PDBViewer extends JInternalFrame implements Runnable
   {
     pdbcanvas.bysequence = false;
     pdbcanvas.pdb.setColours(new HelixColourScheme());
-    pdbcanvas.redrawneeded=true;
+    pdbcanvas.redrawneeded = true;
     pdbcanvas.repaint();
   }
 
@@ -574,7 +589,7 @@ public class PDBViewer extends JInternalFrame implements Runnable
   {
     pdbcanvas.bysequence = false;
     pdbcanvas.pdb.setColours(new StrandColourScheme());
-    pdbcanvas.redrawneeded=true;
+    pdbcanvas.redrawneeded = true;
     pdbcanvas.repaint();
   }
 
@@ -582,7 +597,7 @@ public class PDBViewer extends JInternalFrame implements Runnable
   {
     pdbcanvas.bysequence = false;
     pdbcanvas.pdb.setColours(new TurnColourScheme());
-    pdbcanvas.redrawneeded=true;
+    pdbcanvas.redrawneeded = true;
     pdbcanvas.repaint();
   }
 
@@ -590,7 +605,7 @@ public class PDBViewer extends JInternalFrame implements Runnable
   {
     pdbcanvas.bysequence = false;
     pdbcanvas.pdb.setColours(new BuriedColourScheme());
-    pdbcanvas.redrawneeded=true;
+    pdbcanvas.redrawneeded = true;
     pdbcanvas.repaint();
   }
 
@@ -598,7 +613,7 @@ public class PDBViewer extends JInternalFrame implements Runnable
   {
     if (e.getActionCommand().equals("User Defined..."))
     {
-      new UserDefinedColours(pdbcanvas, null);
+     // new UserDefinedColours(pdbcanvas, null);
     }
     else
     {
@@ -606,22 +621,23 @@ public class PDBViewer extends JInternalFrame implements Runnable
           getUserColourSchemes().get(e.getActionCommand());
 
       pdbcanvas.pdb.setColours(udc);
-      pdbcanvas.redrawneeded=true;
+      pdbcanvas.redrawneeded = true;
       pdbcanvas.repaint();
     }
   }
 
   public void background_actionPerformed(ActionEvent e)
   {
-      java.awt.Color col = JColorChooser.showDialog(this, "Select Background Colour",
-                pdbcanvas.backgroundColour);
-
-      if(col!=null)
-       {
-         pdbcanvas.backgroundColour = col;
-         pdbcanvas.redrawneeded = true;
-         pdbcanvas.repaint();
-       }
+    java.awt.Color col = JColorChooser.showDialog(this,
+                                                  "Select Background Colour",
+                                                  pdbcanvas.backgroundColour);
+
+    if (col != null)
+    {
+      pdbcanvas.backgroundColour = col;
+      pdbcanvas.redrawneeded = true;
+      pdbcanvas.repaint();
+    }
   }
 
   public void savePDB_actionPerformed(ActionEvent e)
@@ -638,26 +654,28 @@ public class PDBViewer extends JInternalFrame implements Runnable
 
     if (value == JalviewFileChooser.APPROVE_OPTION)
     {
-        try
-        {
-          BufferedReader in = new BufferedReader(new FileReader(tmpPDBFile));
-          File outFile = chooser.getSelectedFile();
+      try
+      {
+        BufferedReader in = new BufferedReader(new FileReader(tmpPDBFile));
+        File outFile = chooser.getSelectedFile();
 
-          PrintWriter out = new PrintWriter(new FileOutputStream(outFile));
-          String data;
-          while ( (data = in.readLine()) != null)
+        PrintWriter out = new PrintWriter(new FileOutputStream(outFile));
+        String data;
+        while ( (data = in.readLine()) != null)
+        {
+          if (
+              ! (data.indexOf("<PRE>") > -1 || data.indexOf("</PRE>") > -1)
+              )
           {
-            if (
-                !( data.indexOf("<PRE>") > -1 || data.indexOf("</PRE>") > -1)
-                )
-              out.println(data);
+            out.println(data);
           }
-          out.close();
-        }
-        catch (Exception ex)
-        {
-          ex.printStackTrace();
         }
+        out.close();
+      }
+      catch (Exception ex)
+      {
+        ex.printStackTrace();
       }
+    }
   }
 }