JAL-3093 'read-only' annotations when in wrapped mode
[jalview.git] / src / jalview / gui / SeqPanel.java
index 14ec6e9..e678738 100644 (file)
@@ -287,6 +287,8 @@ public class SeqPanel extends JPanel
                 .getAlignmentAnnotation();
         int rowOffsetPx = yOffsetPx - alignmentHeightPixels;
         annIndex = AnnotationPanel.getRowIndex(rowOffsetPx, anns);
+        // also last sequence in alignment (for backwards compatible behaviour)
+        seq = alignmentHeight - 1;
       }
       else
       {
@@ -353,18 +355,13 @@ public class SeqPanel extends JPanel
     }
     else
     {
-      if (x > seqCanvas.getX() + seqCanvas.getWidth())
-      {
-        // make sure we calculate relative to visible alignment, rather than
-        // right-hand gutter
-        x = seqCanvas.getX() + seqCanvas.getWidth();
-      }
+      /*
+       * make sure we calculate relative to visible alignment, 
+       * rather than right-hand gutter
+       */
+      x = Math.min(x, seqCanvas.getX() + seqCanvas.getWidth());
       res = (x / av.getCharWidth()) + startRes;
-      if (res > av.getRanges().getEndRes())
-      {
-        // moused off right
-        res = av.getRanges().getEndRes();
-      }
+      res = Math.min(res, av.getRanges().getEndRes());
     }
 
     if (av.hasHiddenColumns())
@@ -374,55 +371,6 @@ public class SeqPanel extends JPanel
     }
 
     return res;
-
-  }
-
-  /**
-   * Answers the index in the alignment (0...) of the sequence under the mouse
-   * position. If the mouse is below the alignment (say, over annotations),
-   * answers the index of the last sequence.
-   * 
-   * @param evt
-   * @return
-   */
-  int findSeq(MouseEvent evt)
-  {
-    int seq = 0;
-    int y = evt.getY();
-
-    int charHeight = av.getCharHeight();
-    int alignmentHeight = av.getAlignment().getHeight();
-    if (av.getWrapAlignment())
-    {
-      int hgap = charHeight;
-      if (av.getScaleAboveWrapped())
-      {
-        hgap += charHeight;
-      }
-
-      int alignmentHeightPixels = alignmentHeight * charHeight;
-      int cHeight = alignmentHeightPixels + hgap
-              + seqCanvas.getAnnotationHeight();
-
-      y -= hgap;
-
-      int yOffsetPx = y % cHeight; // yPos below repeating width(s)
-//      if (yOffsetPx > alignmentHeightPixels)
-//      {
-//        seq = -1; // cursor is over annotation or below alignment entirely
-//      }
-//      else
-      // {
-        seq = Math.min(yOffsetPx / charHeight, alignmentHeight - 1);
-//      }
-    }
-    else
-    {
-      seq = Math.min((y / charHeight) + av.getRanges().getStartSeq(),
-              alignmentHeight - 1);
-    }
-
-    return seq;
   }
 
   /**
@@ -758,13 +706,20 @@ public class SeqPanel extends JPanel
   @Override
   public void mouseReleased(MouseEvent evt)
   {
+    MousePos pos = findMousePosition(evt);
+    if (pos.annotationIndex != -1)
+    {
+      // mouse is over annotation row in wrapped mode
+      return;
+    }
+
     boolean didDrag = mouseDragging; // did we come here after a drag
     mouseDragging = false;
     mouseWheelPressed = false;
 
     if (evt.isPopupTrigger()) // Windows: mouseReleased
     {
-      showPopupMenu(evt);
+      showPopupMenu(evt, pos);
       evt.consume();
       return;
     }
@@ -788,6 +743,12 @@ public class SeqPanel extends JPanel
   public void mousePressed(MouseEvent evt)
   {
     lastMousePress = evt.getPoint();
+    MousePos pos = findMousePosition(evt);
+    if (pos.annotationIndex != -1)
+    {
+      // mouse is over an annotation row in wrapped mode
+      return;
+    }
 
     if (SwingUtilities.isMiddleMouseButton(evt))
     {
@@ -806,12 +767,12 @@ public class SeqPanel extends JPanel
     }
     else
     {
-      doMousePressedDefineMode(evt);
+      doMousePressedDefineMode(evt, pos);
       return;
     }
 
-    int seq = findSeq(evt);
-    int res = findColumn(evt);
+    int seq = pos.seqIndex;
+    int res = pos.column;
 
     if (seq < 0 || res < 0)
     {
@@ -933,7 +894,7 @@ public class SeqPanel extends JPanel
       mouseMovedOverAnnotation(mousePos);
       return;
     }
-    final int seq = mousePos.seqIndex;// findSeq(evt);
+    final int seq = mousePos.seqIndex;
 
     final int column = mousePos.column;
     if (column < 0 || seq < 0 || seq >= av.getAlignment().getHeight())
@@ -1216,6 +1177,13 @@ public class SeqPanel extends JPanel
   @Override
   public void mouseDragged(MouseEvent evt)
   {
+    MousePos pos = findMousePosition(evt);
+    if (pos.annotationIndex != -1)
+    {
+      // mouse is over annotation row in wrapped mode
+      return;
+    }
+
     if (mouseWheelPressed)
     {
       boolean inSplitFrame = ap.av.getCodingComplement() != null;
@@ -1315,7 +1283,7 @@ public class SeqPanel extends JPanel
       return;
     }
 
-    int res = findColumn(evt);
+    int res = pos.column;
 
     if (res < 0)
     {
@@ -1801,7 +1769,13 @@ public class SeqPanel extends JPanel
   public void mouseClicked(MouseEvent evt)
   {
     SequenceGroup sg = null;
-    SequenceI sequence = av.getAlignment().getSequenceAt(findSeq(evt));
+    MousePos pos = findMousePosition(evt);
+    if (pos.annotationIndex != -1)
+    {
+      // mouse is over annotation label in wrapped mode
+      return;
+    }
+
     if (evt.getClickCount() > 1)
     {
       sg = av.getSelectionGroup();
@@ -1811,12 +1785,13 @@ public class SeqPanel extends JPanel
         av.setSelectionGroup(null);
       }
 
-      int column = findColumn(evt);
+      int column = pos.column;
 
       /*
        * find features at the position (if not gapped), or straddling
        * the position (if at a gap)
        */
+      SequenceI sequence = av.getAlignment().getSequenceAt(pos.seqIndex);// findSeq(evt));
       List<SequenceFeature> features = seqCanvas.getFeatureRenderer()
               .findFeaturesAtColumn(sequence, column + 1);
 
@@ -1883,28 +1858,28 @@ public class SeqPanel extends JPanel
   /**
    * DOCUMENT ME!
    * 
-   * @param evt
+   * @param pos
    *          DOCUMENT ME!
    */
-  public void doMousePressedDefineMode(MouseEvent evt)
+  protected void doMousePressedDefineMode(MouseEvent evt, MousePos pos)
   {
-    final int res = findColumn(evt);
-    final int seq = findSeq(evt);
+    if (pos.annotationIndex != -1)
+    {
+      // JvOptionPane.showInternalMessageDialog(Desktop.desktop,
+      // MessageManager.getString(
+      // "label.cannot_edit_annotations_in_wrapped_view"),
+      // MessageManager.getString("label.wrapped_view_no_edit"),
+      // JvOptionPane.WARNING_MESSAGE);
+      return;
+    }
+
+    final int res = pos.column;
+    final int seq = pos.seqIndex;
     oldSeq = seq;
     updateOverviewAndStructs = false;
 
     startWrapBlock = wrappedBlock;
 
-    if (av.getWrapAlignment() && seq > av.getAlignment().getHeight())
-    {
-      JvOptionPane.showInternalMessageDialog(Desktop.desktop,
-              MessageManager.getString(
-                      "label.cannot_edit_annotations_in_wrapped_view"),
-              MessageManager.getString("label.wrapped_view_no_edit"),
-              JvOptionPane.WARNING_MESSAGE);
-      return;
-    }
-
     if (seq < 0 || res < 0)
     {
       return;
@@ -1932,7 +1907,7 @@ public class SeqPanel extends JPanel
 
     if (evt.isPopupTrigger()) // Mac: mousePressed
     {
-      showPopupMenu(evt);
+      showPopupMenu(evt, pos);
       return;
     }
 
@@ -1948,8 +1923,8 @@ public class SeqPanel extends JPanel
 
     if (av.cursorMode)
     {
-      seqCanvas.cursorX = findColumn(evt);
-      seqCanvas.cursorY = findSeq(evt);
+      seqCanvas.cursorX = res;
+      seqCanvas.cursorY = seq;
       seqCanvas.repaint();
       return;
     }
@@ -2007,15 +1982,14 @@ public class SeqPanel extends JPanel
 
   /**
    * Build and show a pop-up menu at the right-click mouse position
-   * 
+   *
    * @param evt
-   * @param res
-   * @param sequences
+   * @param pos
    */
-  void showPopupMenu(MouseEvent evt)
+  void showPopupMenu(MouseEvent evt, MousePos pos)
   {
-    final int column = findColumn(evt);
-    final int seq = findSeq(evt);
+    final int column = pos.column;
+    final int seq = pos.seqIndex;
     SequenceI sequence = av.getAlignment().getSequenceAt(seq);
     List<SequenceFeature> features = ap.getFeatureRenderer()
             .findFeaturesAtColumn(sequence, column + 1);
@@ -2083,8 +2057,15 @@ public class SeqPanel extends JPanel
    */
   public void doMouseDraggedDefineMode(MouseEvent evt)
   {
-    int res = findColumn(evt);
-    int y = findSeq(evt);
+    MousePos pos = findMousePosition(evt);
+    if (pos.annotationIndex != -1)
+    {
+      // mouse is over annotation in wrapped mode
+      return;
+    }
+
+    int res = pos.column;
+    int y = pos.seqIndex;
 
     if (wrappedBlock != startWrapBlock)
     {