apply version 2.7 copyright
[jalview.git] / src / jalview / appletgui / AnnotationLabels.java
index 2344719..5140024 100755 (executable)
@@ -1,6 +1,6 @@
 /*
- * Jalview - A Sequence Alignment Editor and Viewer (Version 2.6)
- * Copyright (C) 2010 J Procter, AM Waterhouse, G Barton, M Clamp, S Searle
+ * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
+ * Copyright (C) 2011 J Procter, AM Waterhouse, G Barton, M Clamp, S Searle
  * 
  * This file is part of Jalview.
  * 
@@ -23,8 +23,8 @@ import java.awt.*;
 import java.awt.event.*;
 import java.awt.image.BufferedImage;
 
-
 import jalview.datamodel.*;
+import jalview.util.ParseHtmlBodyAndLinks;
 
 public class AnnotationLabels extends Panel implements ActionListener,
         MouseListener, MouseMotionListener
@@ -224,35 +224,52 @@ public class AnnotationLabels extends Panel implements ActionListener,
   public void mouseMoved(MouseEvent evt)
   {
     resizePanel = evt.getY() < 10 && evt.getX() < 14;
-
     int row = getSelectedRow(evt.getY() + scrollOffset);
 
     if (row > -1)
     {
+      ParseHtmlBodyAndLinks phb = new ParseHtmlBodyAndLinks(av.alignment.getAlignmentAnnotation()[row].getDescription(true), true, "\n");
       if (tooltip == null)
       {
-        tooltip = new Tooltip(
-                ap.av.alignment.getAlignmentAnnotation()[row]
-                        .getDescription(true),
-                this);
+        tooltip = new Tooltip(phb.getNonHtmlContent(), this);
       }
       else
       {
-        tooltip.setTip(ap.av.alignment.getAlignmentAnnotation()[row]
-                .getDescription(true));
+        tooltip.setTip(phb.getNonHtmlContent());
       }
     }
     else if (tooltip != null)
     {
       tooltip.setTip("");
     }
-
   }
 
+  /**
+   * curent drag position
+   */
   MouseEvent dragEvent = null;
 
+  /**
+   * flag to indicate drag events should be ignored
+   */
+  private boolean dragCancelled = false;
+
+  /**
+   * clear any drag events in progress
+   */
+  public void cancelDrag()
+  {
+    dragEvent = null;
+    dragCancelled = true;
+  }
+
   public void mouseDragged(MouseEvent evt)
   {
+    if (dragCancelled)
+    {
+      return;
+    }
+    ;
     dragEvent = evt;
 
     if (resizePanel)
@@ -285,6 +302,22 @@ public class AnnotationLabels extends Panel implements ActionListener,
     }
     else
     {
+      int diff;
+      if ((diff = 6 - evt.getY()) > 0)
+      {
+        // nudge scroll up
+        ap.apvscroll.setValue(ap.apvscroll.getValue() - diff);
+        ap.adjustmentValueChanged(null);
+
+      }
+      else if ((0 < (diff = 6
+              - ap.annotationSpaceFillerHolder.getSize().height
+              + evt.getY())))
+      {
+        // nudge scroll down
+        ap.apvscroll.setValue(ap.apvscroll.getValue() + diff);
+        ap.adjustmentValueChanged(null);
+      }
       repaint();
     }
   }
@@ -295,8 +328,31 @@ public class AnnotationLabels extends Panel implements ActionListener,
 
   public void mouseReleased(MouseEvent evt)
   {
+    if (!resizePanel && !dragCancelled)
+    {
+      int start = selectedRow;
+
+      int end = getSelectedRow(evt.getY() + scrollOffset);
+
+      if (start>-1 && start != end)
+      {
+        // Swap these annotations
+        AlignmentAnnotation startAA = ap.av.alignment
+                .getAlignmentAnnotation()[start];
+        if (end == -1)
+        {
+          end = ap.av.alignment.getAlignmentAnnotation().length - 1;
+        }
+        AlignmentAnnotation endAA = ap.av.alignment
+                .getAlignmentAnnotation()[end];
+
+        ap.av.alignment.getAlignmentAnnotation()[end] = startAA;
+        ap.av.alignment.getAlignmentAnnotation()[start] = endAA;
+      }
+    }
     resizePanel = false;
     dragEvent = null;
+    dragCancelled = false;
     repaint();
     ap.annotationPanel.repaint();
   }
@@ -312,6 +368,7 @@ public class AnnotationLabels extends Panel implements ActionListener,
 
   public void mouseExited(MouseEvent evt)
   {
+    dragCancelled = false;
 
     if (dragEvent == null)
     {
@@ -334,6 +391,7 @@ public class AnnotationLabels extends Panel implements ActionListener,
     {
       return;
     }
+    dragCancelled=false;
     // todo: move below to mouseClicked ?
     selectedRow = getSelectedRow(evt.getY() + scrollOffset);
 
@@ -587,7 +645,8 @@ public class AnnotationLabels extends Panel implements ActionListener,
   public void paint(Graphics g)
   {
     int w = getSize().width;
-    if (image == null || w != image.getWidth(this))
+    int h = getSize().height;
+    if (image == null || w != image.getWidth(this) || h!=image.getHeight(this) )
     {
       image = createImage(w, ap.annotationPanel.getSize().height);
     }
@@ -637,7 +696,7 @@ public class AnnotationLabels extends Panel implements ActionListener,
       g.drawLine(2, 8, 5, 2);
       g.drawLine(5, 2, 8, 8);
     }
-    else if (dragEvent != null && aa != null)
+    else if (!dragCancelled && dragEvent != null && aa != null)
     {
       g.setColor(Color.lightGray);
       g.drawString(aa[selectedRow].label, dragEvent.getX(),
@@ -651,5 +710,4 @@ public class AnnotationLabels extends Panel implements ActionListener,
       g.drawString("to add annotation", 2, 18);
     }
   }
-
 }