JAL-2089 patch broken merge to master for Release 2.10.0b1
[jalview.git] / src / MCview / AppletPDBCanvas.java
old mode 100755 (executable)
new mode 100644 (file)
index 73ed7a7..ad5837e
 /*
- * Jalview - A Sequence Alignment Editor and Viewer
- * 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
- * as published by the Free Software Foundation; either version 2
+ * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
+ * Copyright (C) $$Year-Rel$$ The Jalview Authors
+ * 
+ * This file is part of Jalview.
+ * 
+ * Jalview is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License 
+ * as published by the Free Software Foundation, either version 3
  * of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
+ *  
+ * Jalview is distributed in the hope that it will be useful, but 
+ * WITHOUT ANY WARRANTY; without even the implied warranty 
+ * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
+ * PURPOSE.  See the GNU General Public License for more details.
+ * 
  * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
+ * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
+ * The Jalview Authors are detailed in the 'AUTHORS' file.
  */
 package MCview;
 
-import java.io.*;
-import java.util.*;
-
+import jalview.analysis.AlignSeq;
+import jalview.appletgui.AlignmentPanel;
+import jalview.appletgui.FeatureRenderer;
+import jalview.appletgui.SequenceRenderer;
+import jalview.datamodel.PDBEntry;
+import jalview.datamodel.SequenceI;
+import jalview.io.StructureFile;
+import jalview.structure.AtomSpec;
+import jalview.structure.StructureListener;
+import jalview.structure.StructureMapping;
+import jalview.structure.StructureSelectionManager;
+import jalview.util.MessageManager;
+
+import java.awt.Color;
+import java.awt.Dimension;
+import java.awt.Event;
+import java.awt.Font;
+import java.awt.Graphics;
+import java.awt.Image;
 // JBPNote TODO: This class is quite noisy - needs proper log.info/log.debug
-import java.awt.*;
-import java.awt.event.*;
-
-import jalview.analysis.*;
-import jalview.datamodel.*;
-
-import jalview.appletgui.*;
-import jalview.structure.*;
-
-public class AppletPDBCanvas
-    extends Panel implements MouseListener, MouseMotionListener, StructureListener
+import java.awt.Panel;
+import java.awt.event.KeyAdapter;
+import java.awt.event.KeyEvent;
+import java.awt.event.MouseEvent;
+import java.awt.event.MouseListener;
+import java.awt.event.MouseMotionListener;
+import java.io.PrintStream;
+import java.util.List;
+import java.util.Vector;
+
+public class AppletPDBCanvas extends Panel implements MouseListener,
+        MouseMotionListener, StructureListener
 {
 
   MCMatrix idmat = new MCMatrix(3, 3);
+
   MCMatrix objmat = new MCMatrix(3, 3);
+
   boolean redrawneeded = true;
+
   int omx = 0;
+
   int mx = 0;
+
   int omy = 0;
+
   int my = 0;
-  public PDBfile pdb;
+
+  public StructureFile pdb;
+
   PDBEntry pdbentry;
+
   int bsize;
+
   Image img;
+
   Graphics ig;
+
   Dimension prefsize;
+
   float[] centre = new float[3];
+
   float[] width = new float[3];
+
   float maxwidth;
+
   float scale;
+
   String inStr;
+
   String inType;
+
   boolean bysequence = true;
+
   boolean depthcue = true;
+
   boolean wire = false;
+
   boolean bymolecule = false;
+
   boolean zbuffer = true;
+
   boolean dragging;
+
   int xstart;
+
   int xend;
+
   int ystart;
+
   int yend;
+
   int xmid;
+
   int ymid;
+
   Font font = new Font("Helvetica", Font.PLAIN, 10);
-  public SequenceI [] sequence;
+
+  public SequenceI[] sequence;
+
   final StringBuffer mappingDetails = new StringBuffer();
+
   String appletToolTip = null;
+
   int toolx, tooly;
+
   PDBChain mainchain;
-  Vector highlightRes;
+
+  Vector<String> highlightRes;
+
   boolean pdbAction = false;
+
   Bond highlightBond1, highlightBond2;
+
   boolean errorLoading = false;
+
   boolean seqColoursReady = false;
+
   FeatureRenderer fr;
+
   AlignmentPanel ap;
+
   StructureSelectionManager ssm;
 
-  public AppletPDBCanvas(PDBEntry pdbentry,
-                         SequenceI[] seq,
-                         String [] chains,
-                         AlignmentPanel ap,
-                         String protocol)
+  public AppletPDBCanvas(PDBEntry pdbentry, SequenceI[] seq,
+          String[] chains, AlignmentPanel ap, String protocol)
 
   {
     this.ap = ap;
     this.pdbentry = pdbentry;
     this.sequence = seq;
 
-    ssm = StructureSelectionManager.getStructureSelectionManager();
+    ssm = StructureSelectionManager
+            .getStructureSelectionManager(ap.av.applet);
 
-    try{
+    try
+    {
       pdb = ssm.setMapping(seq, chains, pdbentry.getFile(), protocol);
 
-      if(protocol.equals(jalview.io.AppletFormatAdapter.PASTE))
-       pdbentry.setFile("INLINE"+pdb.id);
+      if (protocol.equals(jalview.io.AppletFormatAdapter.PASTE))
+      {
+        pdbentry.setFile("INLINE" + pdb.getId());
+      }
 
-    }catch(Exception ex)
+    } catch (Exception ex)
     {
       ex.printStackTrace();
       return;
     }
 
-    pdbentry.setId(pdb.id);
+    pdbentry.setId(pdb.getId());
 
     ssm.addStructureViewerListener(this);
 
@@ -119,34 +183,37 @@ public class AppletPDBCanvas
     int seqstart = 0;
     int seqend = 0;
 
-    //JUST DEAL WITH ONE SEQUENCE FOR NOW
+    // JUST DEAL WITH ONE SEQUENCE FOR NOW
     SequenceI sequence = seq[0];
 
-    for (int i = 0; i < pdb.chains.size(); i++)
+    for (int i = 0; i < pdb.getChains().size(); i++)
     {
 
-      mappingDetails.append("\n\nPDB Sequence is :\nSequence = " +
-                            ( (PDBChain) pdb.chains.elementAt(i)).sequence.
-                            getSequenceAsString());
-      mappingDetails.append("\nNo of residues = " +
-                            ( (PDBChain) pdb.chains.elementAt(i)).residues.size() +
-                            "\n\n");
+      mappingDetails
+              .append("\n\nPDB Sequence is :\nSequence = "
+                      + pdb.getChains().elementAt(i).sequence
+                              .getSequenceAsString());
+      mappingDetails.append("\nNo of residues = "
+              + pdb.getChains().elementAt(i).residues.size() + "\n\n");
 
       // Now lets compare the sequences to get
       // the start and end points.
       // Align the sequence to the pdb
+      // TODO: DNa/Pep switch
       AlignSeq as = new AlignSeq(sequence,
-                                 ( (PDBChain) pdb.chains.elementAt(i)).sequence,
-                                 "pep");
+              pdb.getChains().elementAt(i).sequence, pdb.getChains()
+                      .elementAt(i).isNa ? AlignSeq.DNA : AlignSeq.PEP);
       as.calcScoreMatrix();
       as.traceAlignment();
       PrintStream ps = new PrintStream(System.out)
       {
+        @Override
         public void print(String x)
         {
           mappingDetails.append(x);
         }
 
+        @Override
         public void println()
         {
           mappingDetails.append("\n");
@@ -164,25 +231,25 @@ public class AppletPDBCanvas
         pdbend = as.seq2end;
         seqstart = as.seq1start + sequence.getStart() - 1;
         seqend = as.seq1end + sequence.getEnd() - 1;
-       }
+      }
 
       mappingDetails.append("\nPDB start/end " + pdbstart + " " + pdbend);
       mappingDetails.append("\nSEQ start/end " + seqstart + " " + seqend);
     }
 
-    mainchain = (PDBChain) pdb.chains.elementAt(maxchain);
+    mainchain = pdb.getChains().elementAt(maxchain);
 
     mainchain.pdbstart = pdbstart;
     mainchain.pdbend = pdbend;
     mainchain.seqstart = seqstart;
     mainchain.seqend = seqend;
     mainchain.isVisible = true;
-  //  mainchain.makeExactMapping(maxAlignseq, sequence);
-  //  mainchain.transferRESNUMFeatures(sequence, null);
+    // mainchain.makeExactMapping(maxAlignseq, sequence);
+    // mainchain.transferRESNUMFeatures(sequence, null);
     this.pdb = pdb;
     this.prefsize = new Dimension(getSize().width, getSize().height);
 
-    //Initialize the matrices to identity
+    // Initialize the matrices to identity
     for (int i = 0; i < 3; i++)
     {
       for (int j = 0; j < 3; j++)
@@ -205,6 +272,7 @@ public class AppletPDBCanvas
 
     addKeyListener(new KeyAdapter()
     {
+      @Override
       public void keyPressed(KeyEvent evt)
       {
         doKeyPressed(evt);
@@ -219,18 +287,19 @@ public class AppletPDBCanvas
     scale = findScale();
   }
 
-  Vector visiblebonds;
+  Vector<Bond> visiblebonds;
+
   void setupBonds()
   {
     seqColoursReady = false;
     // Sort the bonds by z coord
-    visiblebonds = new Vector();
+    visiblebonds = new Vector<Bond>();
 
-    for (int ii = 0; ii < pdb.chains.size(); ii++)
+    for (int ii = 0; ii < pdb.getChains().size(); ii++)
     {
-      if ( ( (PDBChain) pdb.chains.elementAt(ii)).isVisible)
+      if (pdb.getChains().elementAt(ii).isVisible)
       {
-        Vector tmp = ( (PDBChain) pdb.chains.elementAt(ii)).bonds;
+        Vector<Bond> tmp = pdb.getChains().elementAt(ii).bonds;
 
         for (int i = 0; i < tmp.size(); i++)
         {
@@ -249,24 +318,22 @@ public class AppletPDBCanvas
     float[] max = new float[3];
     float[] min = new float[3];
 
-    max[0] = (float) - 1e30;
-    max[1] = (float) - 1e30;
-    max[2] = (float) - 1e30;
+    max[0] = (float) -1e30;
+    max[1] = (float) -1e30;
+    max[2] = (float) -1e30;
 
     min[0] = (float) 1e30;
     min[1] = (float) 1e30;
     min[2] = (float) 1e30;
 
-    for (int ii = 0; ii < pdb.chains.size(); ii++)
+    for (int ii = 0; ii < pdb.getChains().size(); ii++)
     {
-      if ( ( (PDBChain) pdb.chains.elementAt(ii)).isVisible)
+      if (pdb.getChains().elementAt(ii).isVisible)
       {
-        Vector bonds = ( (PDBChain) pdb.chains.elementAt(ii)).bonds;
+        Vector<Bond> bonds = pdb.getChains().elementAt(ii).bonds;
 
-        for (int i = 0; i < bonds.size(); i++)
+        for (Bond tmp : bonds)
         {
-          Bond tmp = (Bond) bonds.elementAt(i);
-
           if (tmp.start[0] >= max[0])
           {
             max[0] = tmp.start[0];
@@ -330,9 +397,9 @@ public class AppletPDBCanvas
       }
     }
 
-    width[0] = (float) Math.abs(max[0] - min[0]);
-    width[1] = (float) Math.abs(max[1] - min[1]);
-    width[2] = (float) Math.abs(max[2] - min[2]);
+    width[0] = Math.abs(max[0] - min[0]);
+    width[1] = Math.abs(max[1] - min[1]);
+    width[2] = Math.abs(max[2] - min[2]);
 
     maxwidth = width[0];
 
@@ -386,25 +453,20 @@ public class AppletPDBCanvas
 
     int bsize = 0;
 
-    //Find centre coordinate
-    for (int ii = 0; ii < pdb.chains.size(); ii++)
+    // Find centre coordinate
+    for (int ii = 0; ii < pdb.getChains().size(); ii++)
     {
-      if ( ( (PDBChain) pdb.chains.elementAt(ii)).isVisible)
+      if (pdb.getChains().elementAt(ii).isVisible)
       {
-        Vector bonds = ( (PDBChain) pdb.chains.elementAt(ii)).bonds;
+        Vector<Bond> bonds = pdb.getChains().elementAt(ii).bonds;
 
         bsize += bonds.size();
 
-        for (int i = 0; i < bonds.size(); i++)
+        for (Bond b : bonds)
         {
-          xtot = xtot + ( (Bond) bonds.elementAt(i)).start[0] +
-              ( (Bond) bonds.elementAt(i)).end[0];
-
-          ytot = ytot + ( (Bond) bonds.elementAt(i)).start[1] +
-              ( (Bond) bonds.elementAt(i)).end[1];
-
-          ztot = ztot + ( (Bond) bonds.elementAt(i)).start[2] +
-              ( (Bond) bonds.elementAt(i)).end[2];
+          xtot = xtot + b.start[0] + b.end[0];
+          ytot = ytot + b.start[1] + b.end[1];
+          ztot = ztot + b.start[2] + b.end[2];
         }
       }
     }
@@ -414,6 +476,7 @@ public class AppletPDBCanvas
     centre[2] = ztot / (2 * (float) bsize);
   }
 
+  @Override
   public void paint(Graphics g)
   {
 
@@ -423,7 +486,9 @@ public class AppletPDBCanvas
       g.fillRect(0, 0, getSize().width, getSize().height);
       g.setColor(Color.black);
       g.setFont(new Font("Verdana", Font.BOLD, 14));
-      g.drawString("Error loading PDB data!!", 50, getSize().height / 2);
+      g.drawString(
+              MessageManager.getString("label.error_loading_pdb_data"), 50,
+              getSize().height / 2);
       return;
     }
 
@@ -431,14 +496,15 @@ public class AppletPDBCanvas
     {
       g.setColor(Color.black);
       g.setFont(new Font("Verdana", Font.BOLD, 14));
-      g.drawString("Fetching PDB data...", 50, getSize().height / 2);
+      g.drawString(MessageManager.getString("label.fetching_pdb_data"), 50,
+              getSize().height / 2);
       return;
     }
 
-    //Only create the image at the beginning -
-    //this saves much memory usage
-    if ( (img == null) || (prefsize.width != getSize().width) ||
-        (prefsize.height != getSize().height))
+    // Only create the image at the beginning -
+    // this saves much memory usage
+    if ((img == null) || (prefsize.width != getSize().width)
+            || (prefsize.height != getSize().height))
     {
 
       try
@@ -451,8 +517,7 @@ public class AppletPDBCanvas
         ig = img.getGraphics();
 
         redrawneeded = true;
-      }
-      catch (Exception ex)
+      } catch (Exception ex)
       {
         ex.printStackTrace();
       }
@@ -490,8 +555,6 @@ public class AppletPDBCanvas
     repaint();
   }
 
-
-
   // This method has been taken out of PDBChain to allow
   // Applet and Application specific sequence renderers to be used
   void colourBySequence()
@@ -501,7 +564,7 @@ public class AppletPDBCanvas
     StructureMapping[] mapping = ssm.getMapping(pdbentry.getFile());
 
     boolean showFeatures = false;
-    if (ap.av.getShowSequenceFeatures())
+    if (ap.av.isShowSequenceFeatures())
     {
       if (fr == null)
       {
@@ -516,13 +579,13 @@ public class AppletPDBCanvas
     PDBChain chain;
     if (bysequence && pdb != null)
     {
-      for (int ii = 0; ii < pdb.chains.size(); ii++)
+      for (int ii = 0; ii < pdb.getChains().size(); ii++)
       {
-        chain = (PDBChain) pdb.chains.elementAt(ii);
+        chain = pdb.getChains().elementAt(ii);
 
         for (int i = 0; i < chain.bonds.size(); i++)
         {
-          Bond tmp = (Bond) chain.bonds.elementAt(i);
+          Bond tmp = chain.bonds.elementAt(i);
           tmp.startCol = Color.lightGray;
           tmp.endCol = Color.lightGray;
           if (chain != mainchain)
@@ -536,7 +599,7 @@ public class AppletPDBCanvas
             {
               if (mapping[m].getSequence() == sequence[s])
               {
-                int pos = mapping[m].getSeqPos(tmp.at1.resNumber)-1;
+                int pos = mapping[m].getSeqPos(tmp.at1.resNumber) - 1;
                 if (pos > 0)
                 {
                   pos = sequence[s].findIndex(pos);
@@ -544,11 +607,10 @@ public class AppletPDBCanvas
                   if (showFeatures)
                   {
                     tmp.startCol = fr.findFeatureColour(tmp.startCol,
-                                                        sequence[s],
-                                                        pos);
+                            sequence[s], pos);
                   }
                 }
-                pos = mapping[m].getSeqPos(tmp.at2.resNumber)-1;
+                pos = mapping[m].getSeqPos(tmp.at2.resNumber) - 1;
                 if (pos > 0)
                 {
                   pos = sequence[s].findIndex(pos);
@@ -556,8 +618,7 @@ public class AppletPDBCanvas
                   if (showFeatures)
                   {
                     tmp.endCol = fr.findFeatureColour(tmp.endCol,
-                                                        sequence[s],
-                                                        pos);
+                            sequence[s], pos);
                   }
                 }
 
@@ -570,6 +631,7 @@ public class AppletPDBCanvas
   }
 
   Zsort zsort;
+
   public void drawScene(Graphics g)
   {
     if (zbuffer)
@@ -579,23 +641,19 @@ public class AppletPDBCanvas
         zsort = new Zsort();
       }
 
-      zsort.Zsort(visiblebonds);
+      zsort.sort(visiblebonds);
     }
 
     Bond tmpBond = null;
     for (int i = 0; i < visiblebonds.size(); i++)
     {
-      tmpBond = (Bond) visiblebonds.elementAt(i);
+      tmpBond = visiblebonds.elementAt(i);
 
-      xstart = (int) ( ( (tmpBond.start[0] - centre[0]) * scale) +
-                      (getSize().width / 2));
-      ystart = (int) ( ( (centre[1] - tmpBond.start[1] ) * scale) +
-                      (getSize().height / 2));
+      xstart = (int) (((tmpBond.start[0] - centre[0]) * scale) + (getSize().width / 2));
+      ystart = (int) (((centre[1] - tmpBond.start[1]) * scale) + (getSize().height / 2));
 
-      xend = (int) ( ( (tmpBond.end[0] - centre[0]) * scale) +
-                    (getSize().width / 2));
-      yend = (int) ( ( (centre[1] - tmpBond.end[1] ) * scale) +
-                    (getSize().height / 2));
+      xend = (int) (((tmpBond.end[0] - centre[0]) * scale) + (getSize().width / 2));
+      yend = (int) (((centre[1] - tmpBond.end[1]) * scale) + (getSize().height / 2));
 
       xmid = (xend + xstart) / 2;
       ymid = (yend + ystart) / 2;
@@ -677,7 +735,7 @@ public class AppletPDBCanvas
   {
     if (!wire)
     {
-      if ( ( (float) Math.abs(y2 - y1) / (float) Math.abs(x2 - x1)) < 0.5)
+      if (((float) Math.abs(y2 - y1) / (float) Math.abs(x2 - x1)) < 0.5)
       {
         g.drawLine(x1, y1, x2, y2);
         g.drawLine(x1 + 1, y1 + 1, x2 + 1, y2 + 1);
@@ -723,6 +781,7 @@ public class AppletPDBCanvas
     }
   }
 
+  @Override
   public void mousePressed(MouseEvent e)
   {
     pdbAction = true;
@@ -735,23 +794,25 @@ public class AppletPDBCanvas
       repaint();
       if (foundchain != -1)
       {
-        PDBChain chain = (PDBChain) pdb.chains.elementAt(foundchain);
+        PDBChain chain = pdb.getChains().elementAt(foundchain);
         if (chain == mainchain)
         {
           if (fatom.alignmentMapping != -1)
           {
             if (highlightRes == null)
             {
-              highlightRes = new Vector();
+              highlightRes = new Vector<String>();
             }
 
-            if (highlightRes.contains(fatom.alignmentMapping + "" + ""))
+            final String atomString = Integer
+                    .toString(fatom.alignmentMapping);
+            if (highlightRes.contains(atomString))
             {
-              highlightRes.removeElement(fatom.alignmentMapping + "");
+              highlightRes.removeElement(atomString);
             }
             else
             {
-              highlightRes.addElement(fatom.alignmentMapping + "");
+              highlightRes.addElement(atomString);
             }
           }
         }
@@ -765,6 +826,7 @@ public class AppletPDBCanvas
     dragging = false;
   }
 
+  @Override
   public void mouseMoved(MouseEvent e)
   {
     pdbAction = true;
@@ -781,7 +843,7 @@ public class AppletPDBCanvas
     PDBChain chain = null;
     if (foundchain != -1)
     {
-      chain = (PDBChain) pdb.chains.elementAt(foundchain);
+      chain = pdb.getChains().elementAt(foundchain);
       if (chain == mainchain)
       {
         mouseOverStructure(fatom.resNumber, chain.id);
@@ -793,31 +855,36 @@ public class AppletPDBCanvas
       toolx = e.getX();
       tooly = e.getY();
 
-      appletToolTip = chain.id + ":" + fatom.resNumber + " " + fatom.resName;
+      appletToolTip = chain.id + ":" + fatom.resNumber + " "
+              + fatom.resName;
       redrawneeded = true;
       repaint();
     }
     else
     {
-      mouseOverStructure(-1, chain!=null?chain.id:null);
+      mouseOverStructure(-1, chain != null ? chain.id : null);
       appletToolTip = null;
       redrawneeded = true;
       repaint();
     }
   }
 
+  @Override
   public void mouseClicked(MouseEvent e)
   {
   }
 
+  @Override
   public void mouseEntered(MouseEvent e)
   {
   }
 
+  @Override
   public void mouseExited(MouseEvent e)
   {
   }
 
+  @Override
   public void mouseDragged(MouseEvent evt)
   {
     int x = evt.getX();
@@ -828,33 +895,29 @@ public class AppletPDBCanvas
     MCMatrix objmat = new MCMatrix(3, 3);
     objmat.setIdentity();
 
-    if ( (evt.getModifiers() & Event.META_MASK) != 0)
+    if ((evt.getModifiers() & Event.META_MASK) != 0)
     {
-      objmat.rotatez( (float) ( (mx - omx)));
+      objmat.rotatez(((mx - omx)));
     }
     else
     {
-      objmat.rotatex( (float) ( (omy - my)));
-      objmat.rotatey( (float) ( (omx - mx)));
+      objmat.rotatex(((omy - my)));
+      objmat.rotatey(((omx - mx)));
     }
 
-    //Alter the bonds
-    for (int ii = 0; ii < pdb.chains.size(); ii++)
+    // Alter the bonds
+    for (PDBChain chain : pdb.getChains())
     {
-      Vector bonds = ( (PDBChain) pdb.chains.elementAt(ii)).bonds;
-
-      for (int i = 0; i < bonds.size(); i++)
+      for (Bond tmpBond : chain.bonds)
       {
-        Bond tmpBond = (Bond) bonds.elementAt(i);
-
-        //Translate the bond so the centre is 0,0,0
-        tmpBond.translate( -centre[0], -centre[1], -centre[2]);
+        // Translate the bond so the centre is 0,0,0
+        tmpBond.translate(-centre[0], -centre[1], -centre[2]);
 
-        //Now apply the rotation matrix
+        // Now apply the rotation matrix
         tmpBond.start = objmat.vectorMultiply(tmpBond.start);
         tmpBond.end = objmat.vectorMultiply(tmpBond.end);
 
-        //Now translate back again
+        // Now translate back again
         tmpBond.translate(centre[0], centre[1], centre[2]);
       }
     }
@@ -871,6 +934,7 @@ public class AppletPDBCanvas
     repaint();
   }
 
+  @Override
   public void mouseReleased(MouseEvent evt)
   {
     dragging = false;
@@ -880,18 +944,12 @@ public class AppletPDBCanvas
   void drawLabels(Graphics g)
   {
 
-    for (int ii = 0; ii < pdb.chains.size(); ii++)
+    for (PDBChain chain : pdb.getChains())
     {
-      PDBChain chain = (PDBChain) pdb.chains.elementAt(ii);
-
       if (chain.isVisible)
       {
-        Vector bonds = ( (PDBChain) pdb.chains.elementAt(ii)).bonds;
-
-        for (int i = 0; i < bonds.size(); i++)
+        for (Bond tmpBond : chain.bonds)
         {
-          Bond tmpBond = (Bond) bonds.elementAt(i);
-
           if (tmpBond.at1.isSelected)
           {
             labelAtom(g, tmpBond, 1);
@@ -899,7 +957,6 @@ public class AppletPDBCanvas
 
           if (tmpBond.at2.isSelected)
           {
-
             labelAtom(g, tmpBond, 2);
           }
         }
@@ -913,10 +970,8 @@ public class AppletPDBCanvas
 
     if (n == 1)
     {
-      int xstart = (int) ( ( (b.start[0] - centre[0]) * scale) +
-                          (getSize().width / 2));
-      int ystart = (int) ( ( (centre[1] - b.start[1]) * scale) +
-                          (getSize().height / 2));
+      int xstart = (int) (((b.start[0] - centre[0]) * scale) + (getSize().width / 2));
+      int ystart = (int) (((centre[1] - b.start[1]) * scale) + (getSize().height / 2));
 
       g.setColor(Color.red);
       g.drawString(b.at1.resName + "-" + b.at1.resNumber, xstart, ystart);
@@ -924,10 +979,8 @@ public class AppletPDBCanvas
 
     if (n == 2)
     {
-      int xstart = (int) ( ( (b.end[0] - centre[0]) * scale) +
-                          (getSize().width / 2));
-      int ystart = (int) ( ( (centre[1] - b.end[1]) * scale) +
-                          (getSize().height / 2));
+      int xstart = (int) (((b.end[0] - centre[0]) * scale) + (getSize().width / 2));
+      int ystart = (int) (((centre[1] - b.end[1]) * scale) + (getSize().height / 2));
 
       g.setColor(Color.red);
       g.drawString(b.at2.resName + "-" + b.at2.resNumber, xstart, ystart);
@@ -935,33 +988,32 @@ public class AppletPDBCanvas
   }
 
   int foundchain = -1;
+
   public Atom findAtom(int x, int y)
   {
     Atom fatom = null;
 
     foundchain = -1;
 
-    for (int ii = 0; ii < pdb.chains.size(); ii++)
+    for (int ii = 0; ii < pdb.getChains().size(); ii++)
     {
-      PDBChain chain = (PDBChain) pdb.chains.elementAt(ii);
+      PDBChain chain = pdb.getChains().elementAt(ii);
       int truex;
       Bond tmpBond = null;
 
       if (chain.isVisible)
       {
-        Vector bonds = ( (PDBChain) pdb.chains.elementAt(ii)).bonds;
+        Vector<Bond> bonds = pdb.getChains().elementAt(ii).bonds;
 
         for (int i = 0; i < bonds.size(); i++)
         {
-          tmpBond = (Bond) bonds.elementAt(i);
+          tmpBond = bonds.elementAt(i);
 
-          truex = (int) ( ( (tmpBond.start[0] - centre[0]) * scale) +
-                         (getSize().width / 2));
+          truex = (int) (((tmpBond.start[0] - centre[0]) * scale) + (getSize().width / 2));
 
           if (Math.abs(truex - x) <= 2)
           {
-            int truey = (int) ( ( (centre[1] - tmpBond.start[1] ) * scale) +
-                               (getSize().height / 2));
+            int truey = (int) (((centre[1] - tmpBond.start[1]) * scale) + (getSize().height / 2));
 
             if (Math.abs(truey - y) <= 2)
             {
@@ -974,13 +1026,11 @@ public class AppletPDBCanvas
 
         // Still here? Maybe its the last bond
 
-        truex = (int) ( ( (tmpBond.end[0] - centre[0]) * scale) +
-                       (getSize().width / 2));
+        truex = (int) (((tmpBond.end[0] - centre[0]) * scale) + (getSize().width / 2));
 
         if (Math.abs(truex - x) <= 2)
         {
-          int truey = (int) ( ( (tmpBond.end[1] - centre[1]) * scale) +
-                             (getSize().height / 2));
+          int truey = (int) (((tmpBond.end[1] - centre[1]) * scale) + (getSize().height / 2));
 
           if (Math.abs(truey - y) <= 2)
           {
@@ -992,15 +1042,16 @@ public class AppletPDBCanvas
 
       }
 
-      if (fatom != null) //)&& chain.ds != null)
+      if (fatom != null) // )&& chain.ds != null)
       {
-        chain = (PDBChain) pdb.chains.elementAt(foundchain);
+        chain = pdb.getChains().elementAt(foundchain);
       }
     }
 
     return fatom;
   }
 
+  @Override
   public void update(Graphics g)
   {
     paint(g);
@@ -1013,8 +1064,7 @@ public class AppletPDBCanvas
       return;
     }
 
-    if (highlightRes != null
-        && highlightRes.contains( (ii - 1) + ""))
+    if (highlightRes != null && highlightRes.contains((ii - 1) + ""))
     {
       return;
     }
@@ -1023,7 +1073,7 @@ public class AppletPDBCanvas
     Bond tmpBond;
     for (index = 0; index < mainchain.bonds.size(); index++)
     {
-      tmpBond = (Bond) mainchain.bonds.elementAt(index);
+      tmpBond = mainchain.bonds.elementAt(index);
       if (tmpBond.at1.alignmentMapping == ii - 1)
       {
         if (highlightBond1 != null)
@@ -1041,13 +1091,13 @@ public class AppletPDBCanvas
 
         if (index > 0)
         {
-          highlightBond1 = (Bond) mainchain.bonds.elementAt(index - 1);
+          highlightBond1 = mainchain.bonds.elementAt(index - 1);
           highlightBond1.at2.isSelected = true;
         }
 
         if (index != mainchain.bonds.size())
         {
-          highlightBond2 = (Bond) mainchain.bonds.elementAt(index);
+          highlightBond2 = mainchain.bonds.elementAt(index);
           highlightBond2.at1.isSelected = true;
         }
 
@@ -1061,9 +1111,9 @@ public class AppletPDBCanvas
 
   public void setAllchainsVisible(boolean b)
   {
-    for (int ii = 0; ii < pdb.chains.size(); ii++)
+    for (int ii = 0; ii < pdb.getChains().size(); ii++)
     {
-      PDBChain chain = (PDBChain) pdb.chains.elementAt(ii);
+      PDBChain chain = pdb.getChains().elementAt(ii);
       chain.isVisible = b;
     }
     mainchain.isVisible = true;
@@ -1071,45 +1121,69 @@ public class AppletPDBCanvas
     setupBonds();
   }
 
-
-  //////////////////////////////////
-  ///StructureListener
-  public String getPdbFile()
+  // ////////////////////////////////
+  // /StructureListener
+  @Override
+  public String[] getPdbFile()
   {
-    return pdbentry.getFile();
+    return new String[] { pdbentry.getFile() };
   }
 
-
   String lastMessage;
+
   public void mouseOverStructure(int pdbResNum, String chain)
   {
-      if (lastMessage == null || !lastMessage.equals(pdbResNum+chain))
-        ssm.mouseOverStructure(pdbResNum, chain, pdbentry.getFile());
+    if (lastMessage == null || !lastMessage.equals(pdbResNum + chain))
+    {
+      ssm.mouseOverStructure(pdbResNum, chain, pdbentry.getFile());
+    }
 
-      lastMessage = pdbResNum+chain;
+    lastMessage = pdbResNum + chain;
   }
 
   StringBuffer resetLastRes = new StringBuffer();
+
   StringBuffer eval = new StringBuffer();
 
-  public void highlightAtom(int atomIndex, int pdbResNum, String chain, String pdbfile)
+  /**
+   * Highlight the specified atoms in the structure.
+   * 
+   * @param atoms
+   */
+  @Override
+  public void highlightAtoms(List<AtomSpec> atoms)
   {
     if (!seqColoursReady)
     {
       return;
     }
-
-    if (highlightRes != null
-        && highlightRes.contains( (atomIndex - 1) + ""))
+    for (AtomSpec atom : atoms)
     {
-      return;
+      int atomIndex = atom.getAtomIndex();
+
+      if (highlightRes != null
+              && highlightRes.contains((atomIndex - 1) + ""))
+      {
+        continue;
+      }
+
+      highlightAtom(atomIndex);
     }
 
+    redrawneeded = true;
+    repaint();
+  }
+
+  /**
+   * @param atomIndex
+   */
+  protected void highlightAtom(int atomIndex)
+  {
     int index = -1;
     Bond tmpBond;
     for (index = 0; index < mainchain.bonds.size(); index++)
     {
-      tmpBond = (Bond) mainchain.bonds.elementAt(index);
+      tmpBond = mainchain.bonds.elementAt(index);
       if (tmpBond.at1.atomIndex == atomIndex)
       {
         if (highlightBond1 != null)
@@ -1127,34 +1201,32 @@ public class AppletPDBCanvas
 
         if (index > 0)
         {
-          highlightBond1 = (Bond) mainchain.bonds.elementAt(index - 1);
+          highlightBond1 = mainchain.bonds.elementAt(index - 1);
           highlightBond1.at2.isSelected = true;
         }
 
         if (index != mainchain.bonds.size())
         {
-          highlightBond2 = (Bond) mainchain.bonds.elementAt(index);
+          highlightBond2 = mainchain.bonds.elementAt(index);
           highlightBond2.at1.isSelected = true;
         }
 
         break;
       }
     }
-
-    redrawneeded = true;
-    repaint();
   }
 
-
-  public Color getColour(int atomIndex, int pdbResNum, String chain, String pdbfile)
+  public Color getColour(int atomIndex, int pdbResNum, String chain,
+          String pdbfile)
   {
     return Color.white;
-   // if (!pdbfile.equals(pdbentry.getFile()))
-   //   return null;
+    // if (!pdbfile.equals(pdbentry.getFile()))
+    // return null;
 
-    //return new Color(viewer.getAtomArgb(atomIndex));
+    // return new Color(viewer.getAtomArgb(atomIndex));
   }
 
+  @Override
   public void updateColours(Object source)
   {
     colourBySequence();
@@ -1162,5 +1234,26 @@ public class AppletPDBCanvas
     repaint();
   }
 
+  @Override
+  public void releaseReferences(Object svl)
+  {
+    // TODO Auto-generated method stub
+
+  }
 
+  @Override
+  public boolean isListeningFor(SequenceI seq)
+  {
+    if (sequence != null)
+    {
+      for (SequenceI s : sequence)
+      {
+        if (s == seq)
+        {
+          return true;
+        }
+      }
+    }
+    return false;
+  }
 }