JAL-2349 JAL-4033 refactored contact matrix geom calc to allow reporting of mapped...
[jalview.git] / src / jalview / gui / AnnotationPanel.java
index 756e544..d80c749 100755 (executable)
@@ -50,15 +50,18 @@ import javax.swing.JPopupMenu;
 import javax.swing.Scrollable;
 import javax.swing.ToolTipManager;
 
+import jalview.api.AlignViewportI;
 import jalview.datamodel.AlignmentAnnotation;
 import jalview.datamodel.AlignmentI;
 import jalview.datamodel.Annotation;
 import jalview.datamodel.ColumnSelection;
+import jalview.datamodel.ContactListI;
 import jalview.datamodel.HiddenColumns;
 import jalview.datamodel.SequenceI;
 import jalview.gui.JalviewColourChooser.ColourChooserListener;
 import jalview.renderer.AnnotationRenderer;
 import jalview.renderer.AwtRenderPanelI;
+import jalview.renderer.ContactGeometry;
 import jalview.schemes.ResidueProperties;
 import jalview.util.Comparison;
 import jalview.util.MessageManager;
@@ -551,7 +554,7 @@ public class AnnotationPanel extends JPanel implements AwtRenderPanelI,
         {
           activeRow = i;
         }
-        else if (aa[i].graph > 0)
+        else if (aa[i].graph != 0)
         {
           /*
            * we have clicked on a resizable graph annotation
@@ -778,8 +781,8 @@ public class AnnotationPanel extends JPanel implements AwtRenderPanelI,
   {
     int yPos = evt.getY();
     AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
-
-    int row = getRowIndex(yPos, aa);
+    int rowAndOffset[] = getRowIndexAndOffset(yPos, aa);
+    int row = rowAndOffset[0];
 
     if (row == -1)
     {
@@ -801,10 +804,11 @@ public class AnnotationPanel extends JPanel implements AwtRenderPanelI,
     if (row > -1 && ann.annotations != null
             && column < ann.annotations.length)
     {
-      String toolTip = buildToolTip(ann, column, aa);
+      String toolTip = buildToolTip(ann, column, aa, rowAndOffset[1], av);
       setToolTipText(toolTip == null ? null
               : JvSwingUtils.wrapTooltip(true, toolTip));
-      String msg = getStatusMessage(av.getAlignment(), column, ann);
+      String msg = getStatusMessage(av.getAlignment(), column, ann,
+              rowAndOffset[1], av);
       ap.alignFrame.setStatus(msg);
     }
     else
@@ -830,23 +834,38 @@ public class AnnotationPanel extends JPanel implements AwtRenderPanelI,
     {
       return -1;
     }
+    return getRowIndexAndOffset(yPos, aa)[0];
+  }
+
+  static int[] getRowIndexAndOffset(int yPos, AlignmentAnnotation[] aa)
+  {
+    int[] res = new int[2];
+    if (aa == null)
+    {
+      res[0] = -1;
+      res[1] = 0;
+      return res;
+    }
     int row = -1;
-    int height = 0;
+    int height = 0, lheight = 0;
 
     for (int i = 0; i < aa.length; i++)
     {
       if (aa[i].visible)
       {
+        lheight = height;
         height += aa[i].height;
       }
 
       if (height > yPos)
       {
         row = i;
+        res[0] = row;
+        res[1] = yPos - lheight;
         break;
       }
     }
-    return row;
+    return res;
   }
 
   /**
@@ -857,9 +876,10 @@ public class AnnotationPanel extends JPanel implements AwtRenderPanelI,
    * @param ann
    * @param column
    * @param anns
+   * @param rowAndOffset
    */
   static String buildToolTip(AlignmentAnnotation ann, int column,
-          AlignmentAnnotation[] anns)
+          AlignmentAnnotation[] anns, int rowAndOffset, AlignViewportI av)
   {
     String tooltip = null;
     if (ann.graphGroup > -1)
@@ -891,7 +911,18 @@ public class AnnotationPanel extends JPanel implements AwtRenderPanelI,
     {
       tooltip = ann.annotations[column].description;
     }
-
+    // TODO abstract tooltip generator so different implementations can be built
+    if (ann.graph == AlignmentAnnotation.CUSTOMRENDERER)
+    {
+      ContactListI clist = av.getContactList(ann, column);
+      if (clist != null)
+      {
+        ContactGeometry cgeom = new ContactGeometry(clist, ann.graphHeight);
+        ContactGeometry.contactInterval ci = cgeom.mapFor(rowAndOffset,
+                rowAndOffset);
+        tooltip += "Contact from " + ci.cStart + " to " + ci.cEnd;
+      }
+    }
     return tooltip;
   }
 
@@ -901,9 +932,10 @@ public class AnnotationPanel extends JPanel implements AwtRenderPanelI,
    * @param al
    * @param column
    * @param ann
+   * @param rowAndOffset
    */
   static String getStatusMessage(AlignmentI al, int column,
-          AlignmentAnnotation ann)
+          AlignmentAnnotation ann, int rowAndOffset, AlignViewportI av)
   {
     /*
      * show alignment column and annotation description if any
@@ -1124,19 +1156,22 @@ public class AnnotationPanel extends JPanel implements AwtRenderPanelI,
 
     Graphics2D gg = (Graphics2D) image.getGraphics();
 
-    gg.copyArea(0, 0, imgWidth, getHeight(),
-            -horizontal * av.getCharWidth(), 0);
-
-    if (horizontal > 0) // scrollbar pulled right, image to the left
+    if (imgWidth > Math.abs(horizontal * av.getCharWidth()))
     {
-      transX = (er - sr - horizontal) * av.getCharWidth();
-      sr = er - horizontal;
-    }
-    else if (horizontal < 0)
-    {
-      er = sr - horizontal;
-    }
+      // scroll is less than imgWidth away so can re-use buffered graphics
+      gg.copyArea(0, 0, imgWidth, getHeight(),
+              -horizontal * av.getCharWidth(), 0);
 
+      if (horizontal > 0) // scrollbar pulled right, image to the left
+      {
+        transX = (er - sr - horizontal) * av.getCharWidth();
+        sr = er - horizontal;
+      }
+      else if (horizontal < 0)
+      {
+        er = sr - horizontal;
+      }
+    }
     gg.translate(transX, 0);
 
     drawComponent(gg, sr, er);