Dont add null description to tooltip
[jalview.git] / src / MCview / PDBViewer.java
index f0b37df..d71e9bc 100755 (executable)
@@ -1,6 +1,6 @@
 /*
  * Jalview - A Sequence Alignment Editor and Viewer
- * Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
+ * Copyright (C) 2006 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
@@ -26,19 +26,25 @@ 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;
 
 public class PDBViewer extends JInternalFrame implements Runnable
 {
   PDBEntry pdb;
+  /**
+   * The associated sequence in an alignment
+   */
   Sequence sequence;
   PDBCanvas pdbcanvas;
+  String tmpPDBFile;
 
 
   public PDBViewer(PDBEntry entry,
                    Sequence seq,
                    SeqCanvas seqcanvas)
   {
-    /*Uncomment this to modify in Jbuilder
     try
     {
       jbInit();
@@ -46,17 +52,35 @@ public class PDBViewer extends JInternalFrame implements Runnable
     catch (Exception ex)
     {
       ex.printStackTrace();
-    }*/
+    }
+
 
     if (entry==null)
       return;
     pdb = entry;
     sequence = seq;
+    pdbcanvas = new PDBCanvas(seqcanvas, sequence);
+
+    if(pdb.getFile()!=null)
+    {
+        try{
+          tmpPDBFile = pdb.getFile();
+          PDBfile pdbfile = new PDBfile(tmpPDBFile,
+                                        jalview.io.AppletFormatAdapter.FILE);
+          pdbcanvas.setPDBFile(pdbfile);
+
+        }catch(java.io.IOException ex)
+        {
+          ex.printStackTrace();
+        }
+    }
+    else
+    {
+      Thread worker = new Thread(this);
+      worker.start();
+    }
 
-    Thread worker = new Thread(this);
-    worker.start();
 
-    pdbcanvas = new PDBCanvas(seqcanvas, seq);
 
     setContentPane(pdbcanvas);
     StringBuffer title = new StringBuffer(sequence.getName() + ":" + pdb.getId());
@@ -80,24 +104,18 @@ public class PDBViewer extends JInternalFrame implements Runnable
   {
     try
     {
-      EBIFetchClient ebi = new EBIFetchClient();
-      String query = "pdb:" + pdb.getId();
-      String[] result = ebi.fetchData(query, "default","raw");
-      if (result!=null) {
-        PDBfile pdbfile = new PDBfile(result);
-        pdbcanvas.setPDBFile(pdbfile);
-      } else {
-        throw new Exception("Empty result for WSDbFetch Query: "+query);
-      }
-
-      try
-      {
-        jbInit();
-      }
-      catch (Exception ex)
-      {
-        ex.printStackTrace();
-      }
+        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)
     {
@@ -272,11 +290,28 @@ public class PDBViewer extends JInternalFrame implements Runnable
       }
     });
     viewMenu.setText("View");
+    background.setText("Background Colour...");
+    background.addActionListener(new ActionListener()
+    {
+      public void actionPerformed(ActionEvent e)
+      {
+        background_actionPerformed(e);
+      }
+    });
+    savePDB.setText("PDB File");
+    savePDB.addActionListener(new ActionListener()
+    {
+      public void actionPerformed(ActionEvent e)
+      {
+        savePDB_actionPerformed(e);
+      }
+    });
     jMenuBar1.add(fileMenu);
     jMenuBar1.add(coloursMenu);
     jMenuBar1.add(viewMenu);
     fileMenu.add(saveMenu);
     fileMenu.add(mapping);
+    saveMenu.add(savePDB);
     saveMenu.add(png);
     saveMenu.add(eps);
     coloursMenu.add(seqButton);
@@ -290,6 +325,7 @@ public class PDBViewer extends JInternalFrame implements Runnable
     coloursMenu.add(turn);
     coloursMenu.add(buried);
     coloursMenu.add(user);
+    coloursMenu.add(background);
     ButtonGroup bg = new ButtonGroup();
     bg.add(seqButton);
     bg.add(chain);
@@ -384,6 +420,8 @@ public class PDBViewer extends JInternalFrame implements Runnable
   JRadioButtonMenuItem strand = new JRadioButtonMenuItem();
   JRadioButtonMenuItem helix = new JRadioButtonMenuItem();
   JMenu viewMenu = new JMenu();
+  JMenuItem background = new JMenuItem();
+  JMenuItem savePDB = new JMenuItem();
 
   /**
    * DOCUMENT ME!
@@ -572,4 +610,54 @@ public class PDBViewer extends JInternalFrame implements Runnable
       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();
+       }
+  }
+
+  public void savePDB_actionPerformed(ActionEvent e)
+  {
+    JalviewFileChooser chooser = new JalviewFileChooser(
+        jalview.bin.Cache.getProperty(
+            "LAST_DIRECTORY"));
+
+    chooser.setFileView(new JalviewFileView());
+    chooser.setDialogTitle("Save PDB File");
+    chooser.setToolTipText("Save");
+
+    int value = chooser.showSaveDialog(this);
+
+    if (value == JalviewFileChooser.APPROVE_OPTION)
+    {
+        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)
+          {
+            if (
+                !( data.indexOf("<PRE>") > -1 || data.indexOf("</PRE>") > -1)
+                )
+              out.println(data);
+          }
+          out.close();
+        }
+        catch (Exception ex)
+        {
+          ex.printStackTrace();
+        }
+      }
+  }
 }