JAL-2089 patch broken merge to master for Release 2.10.0b1
[jalview.git] / src / MCview / AppletPDBCanvas.java
index 212f0f3..ad5837e 100644 (file)
@@ -1,36 +1,54 @@
 /*
- * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.0b1)
- * Copyright (C) 2014 The Jalview Authors
+ * 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.
+ * as published by the Free Software Foundation, either version 3
+ * of the License, or (at your option) any later version.
  *  
  * 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 Jalview.  If not, see <http://www.gnu.org/licenses/>.
+ * You should have received a copy of the GNU General Public License
+ * 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.*;
-import jalview.util.MessageManager;
+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
@@ -50,7 +68,7 @@ public class AppletPDBCanvas extends Panel implements MouseListener,
 
   int my = 0;
 
-  public PDBfile pdb;
+  public StructureFile pdb;
 
   PDBEntry pdbentry;
 
@@ -110,7 +128,7 @@ public class AppletPDBCanvas extends Panel implements MouseListener,
 
   PDBChain mainchain;
 
-  Vector highlightRes;
+  Vector<String> highlightRes;
 
   boolean pdbAction = false;
 
@@ -142,7 +160,9 @@ public class AppletPDBCanvas extends Panel implements MouseListener,
       pdb = ssm.setMapping(seq, chains, pdbentry.getFile(), protocol);
 
       if (protocol.equals(jalview.io.AppletFormatAdapter.PASTE))
-        pdbentry.setFile("INLINE" + pdb.id);
+      {
+        pdbentry.setFile("INLINE" + pdb.getId());
+      }
 
     } catch (Exception ex)
     {
@@ -150,7 +170,7 @@ public class AppletPDBCanvas extends Panel implements MouseListener,
       return;
     }
 
-    pdbentry.setId(pdb.id);
+    pdbentry.setId(pdb.getId());
 
     ssm.addStructureViewerListener(this);
 
@@ -166,33 +186,34 @@ public class AppletPDBCanvas extends Panel implements MouseListener,
     // 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("\n\nPDB Sequence is :\nSequence = "
+                      + pdb.getChains().elementAt(i).sequence
+                              .getSequenceAsString());
       mappingDetails.append("\nNo of residues = "
-              + ((PDBChain) pdb.chains.elementAt(i)).residues.size()
-              + "\n\n");
+              + 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,
-              ((PDBChain) pdb.chains.elementAt(i)).isNa ? AlignSeq.DNA
-                      : AlignSeq.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");
@@ -216,7 +237,7 @@ public class AppletPDBCanvas extends Panel implements MouseListener,
       mappingDetails.append("\nSEQ start/end " + seqstart + " " + seqend);
     }
 
-    mainchain = (PDBChain) pdb.chains.elementAt(maxchain);
+    mainchain = pdb.getChains().elementAt(maxchain);
 
     mainchain.pdbstart = pdbstart;
     mainchain.pdbend = pdbend;
@@ -251,6 +272,7 @@ public class AppletPDBCanvas extends Panel implements MouseListener,
 
     addKeyListener(new KeyAdapter()
     {
+      @Override
       public void keyPressed(KeyEvent evt)
       {
         doKeyPressed(evt);
@@ -265,19 +287,19 @@ public class AppletPDBCanvas extends Panel implements MouseListener,
     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++)
         {
@@ -304,16 +326,14 @@ public class AppletPDBCanvas extends Panel implements MouseListener,
     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];
@@ -377,9 +397,9 @@ public class AppletPDBCanvas extends Panel implements MouseListener,
       }
     }
 
-    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];
 
@@ -434,24 +454,19 @@ public class AppletPDBCanvas extends Panel implements MouseListener,
     int bsize = 0;
 
     // Find centre coordinate
-    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;
 
         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];
         }
       }
     }
@@ -461,6 +476,7 @@ public class AppletPDBCanvas extends Panel implements MouseListener,
     centre[2] = ztot / (2 * (float) bsize);
   }
 
+  @Override
   public void paint(Graphics g)
   {
 
@@ -470,7 +486,9 @@ public class AppletPDBCanvas extends Panel implements MouseListener,
       g.fillRect(0, 0, getSize().width, getSize().height);
       g.setColor(Color.black);
       g.setFont(new Font("Verdana", Font.BOLD, 14));
-      g.drawString(MessageManager.getString("label.error_loading_pdb_data"), 50, getSize().height / 2);
+      g.drawString(
+              MessageManager.getString("label.error_loading_pdb_data"), 50,
+              getSize().height / 2);
       return;
     }
 
@@ -478,7 +496,8 @@ public class AppletPDBCanvas extends Panel implements MouseListener,
     {
       g.setColor(Color.black);
       g.setFont(new Font("Verdana", Font.BOLD, 14));
-      g.drawString(MessageManager.getString("label.fetching_pdb_data"), 50, getSize().height / 2);
+      g.drawString(MessageManager.getString("label.fetching_pdb_data"), 50,
+              getSize().height / 2);
       return;
     }
 
@@ -545,7 +564,7 @@ public class AppletPDBCanvas extends Panel implements MouseListener,
     StructureMapping[] mapping = ssm.getMapping(pdbentry.getFile());
 
     boolean showFeatures = false;
-    if (ap.av.getShowSequenceFeatures())
+    if (ap.av.isShowSequenceFeatures())
     {
       if (fr == null)
       {
@@ -560,13 +579,13 @@ public class AppletPDBCanvas extends Panel implements MouseListener,
     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)
@@ -622,13 +641,13 @@ public class AppletPDBCanvas extends Panel implements MouseListener,
         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));
@@ -762,6 +781,7 @@ public class AppletPDBCanvas extends Panel implements MouseListener,
     }
   }
 
+  @Override
   public void mousePressed(MouseEvent e)
   {
     pdbAction = true;
@@ -774,23 +794,25 @@ public class AppletPDBCanvas extends Panel implements MouseListener,
       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);
             }
           }
         }
@@ -804,6 +826,7 @@ public class AppletPDBCanvas extends Panel implements MouseListener,
     dragging = false;
   }
 
+  @Override
   public void mouseMoved(MouseEvent e)
   {
     pdbAction = true;
@@ -820,7 +843,7 @@ public class AppletPDBCanvas extends Panel implements MouseListener,
     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);
@@ -846,18 +869,22 @@ public class AppletPDBCanvas extends Panel implements MouseListener,
     }
   }
 
+  @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();
@@ -870,23 +897,19 @@ public class AppletPDBCanvas extends Panel implements MouseListener,
 
     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++)
+    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]);
 
@@ -911,6 +934,7 @@ public class AppletPDBCanvas extends Panel implements MouseListener,
     repaint();
   }
 
+  @Override
   public void mouseReleased(MouseEvent evt)
   {
     dragging = false;
@@ -920,18 +944,12 @@ public class AppletPDBCanvas extends Panel implements MouseListener,
   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);
@@ -939,7 +957,6 @@ public class AppletPDBCanvas extends Panel implements MouseListener,
 
           if (tmpBond.at2.isSelected)
           {
-
             labelAtom(g, tmpBond, 2);
           }
         }
@@ -978,19 +995,19 @@ public class AppletPDBCanvas extends Panel implements MouseListener,
 
     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));
 
@@ -1027,13 +1044,14 @@ public class AppletPDBCanvas extends Panel implements MouseListener,
 
       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);
@@ -1055,7 +1073,7 @@ public class AppletPDBCanvas extends Panel implements MouseListener,
     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)
@@ -1073,13 +1091,13 @@ public class AppletPDBCanvas extends Panel implements MouseListener,
 
         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;
         }
 
@@ -1093,9 +1111,9 @@ public class AppletPDBCanvas extends Panel implements MouseListener,
 
   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;
@@ -1105,10 +1123,10 @@ public class AppletPDBCanvas extends Panel implements MouseListener,
 
   // ////////////////////////////////
   // /StructureListener
+  @Override
   public String[] getPdbFile()
   {
-    return new String[]
-    { pdbentry.getFile() };
+    return new String[] { pdbentry.getFile() };
   }
 
   String lastMessage;
@@ -1116,7 +1134,9 @@ public class AppletPDBCanvas extends Panel implements MouseListener,
   public void mouseOverStructure(int pdbResNum, String chain)
   {
     if (lastMessage == null || !lastMessage.equals(pdbResNum + chain))
+    {
       ssm.mouseOverStructure(pdbResNum, chain, pdbentry.getFile());
+    }
 
     lastMessage = pdbResNum + chain;
   }
@@ -1125,24 +1145,45 @@ public class AppletPDBCanvas extends Panel implements MouseListener,
 
   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)
@@ -1160,22 +1201,19 @@ public class AppletPDBCanvas extends Panel implements MouseListener,
 
         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,
@@ -1188,6 +1226,7 @@ public class AppletPDBCanvas extends Panel implements MouseListener,
     // return new Color(viewer.getAtomArgb(atomIndex));
   }
 
+  @Override
   public void updateColours(Object source)
   {
     colourBySequence();
@@ -1202,4 +1241,19 @@ public class AppletPDBCanvas extends Panel implements MouseListener,
 
   }
 
+  @Override
+  public boolean isListeningFor(SequenceI seq)
+  {
+    if (sequence != null)
+    {
+      for (SequenceI s : sequence)
+      {
+        if (s == seq)
+        {
+          return true;
+        }
+      }
+    }
+    return false;
+  }
 }