Merge branch 'bug/JAL-2750' into develop
[jalview.git] / src / jalview / gui / ScalePanel.java
index dcaafcd..6abee08 100755 (executable)
@@ -29,6 +29,7 @@ import jalview.renderer.ScaleRenderer.ScaleMark;
 import jalview.util.MessageManager;
 import jalview.util.Platform;
 import jalview.viewmodel.ViewportListenerI;
+import jalview.viewmodel.ViewportRanges;
 
 import java.awt.Color;
 import java.awt.FontMetrics;
@@ -41,6 +42,7 @@ import java.awt.event.MouseEvent;
 import java.awt.event.MouseListener;
 import java.awt.event.MouseMotionListener;
 import java.beans.PropertyChangeEvent;
+import java.util.Iterator;
 import java.util.List;
 
 import javax.swing.JMenuItem;
@@ -53,8 +55,8 @@ import javax.swing.ToolTipManager;
  * The panel containing the sequence ruler (when not in wrapped mode), and
  * supports a range of mouse operations to select, hide or reveal columns.
  */
-public class ScalePanel extends JPanel implements MouseMotionListener,
-        MouseListener, ViewportListenerI
+public class ScalePanel extends JPanel
+        implements MouseMotionListener, MouseListener, ViewportListenerI
 {
   protected int offy = 4;
 
@@ -111,7 +113,7 @@ public class ScalePanel extends JPanel implements MouseMotionListener,
 
     if (av.hasHiddenColumns())
     {
-      x = av.getAlignment().getHiddenColumns().adjustForHiddenColumns(x);
+      x = av.getAlignment().getHiddenColumns().visibleToAbsoluteColumn(x);
     }
 
     if (x >= av.getAlignment().getWidth())
@@ -167,17 +169,13 @@ public class ScalePanel extends JPanel implements MouseMotionListener,
         {
           av.showColumn(reveal[0]);
           reveal = null;
-          ap.paintAlignment(true);
-          if (ap.overviewPanel != null)
-          {
-            ap.overviewPanel.updateOverviewImage();
-          }
+          ap.paintAlignment(true, true);
           av.sendSelection();
         }
       });
       pop.add(item);
 
-      if (av.getAlignment().getHiddenColumns().hasHiddenColumns())
+      if (av.getAlignment().getHiddenColumns().hasMultiHiddenColumnRegions())
       {
         item = new JMenuItem(MessageManager.getString("action.reveal_all"));
         item.addActionListener(new ActionListener()
@@ -187,11 +185,7 @@ public class ScalePanel extends JPanel implements MouseMotionListener,
           {
             av.showAllHiddenColumns();
             reveal = null;
-            ap.paintAlignment(true);
-            if (ap.overviewPanel != null)
-            {
-              ap.overviewPanel.updateOverviewImage();
-            }
+            ap.paintAlignment(true, true);
             av.sendSelection();
           }
         });
@@ -209,18 +203,13 @@ public class ScalePanel extends JPanel implements MouseMotionListener,
         public void actionPerformed(ActionEvent e)
         {
           av.hideColumns(res, res);
-          if (av.getSelectionGroup() != null
-                  && av.getSelectionGroup().getSize() == av.getAlignment()
-                          .getHeight())
+          if (av.getSelectionGroup() != null && av.getSelectionGroup()
+                  .getSize() == av.getAlignment().getHeight())
           {
             av.setSelectionGroup(null);
           }
 
-          ap.paintAlignment(true);
-          if (ap.overviewPanel != null)
-          {
-            ap.overviewPanel.updateOverviewImage();
-          }
+          ap.paintAlignment(true, true);
           av.sendSelection();
         }
       });
@@ -272,7 +261,7 @@ public class ScalePanel extends JPanel implements MouseMotionListener,
       sg.setEndRes(max);
     }
     av.setSelectionGroup(sg);
-    ap.paintAlignment(false);
+    ap.paintAlignment(false, false);
     av.sendSelection();
   }
 
@@ -287,13 +276,15 @@ public class ScalePanel extends JPanel implements MouseMotionListener,
   {
     mouseDragging = false;
 
-    int res = (evt.getX() / av.getCharWidth())
+    int xCords = Math.max(0, evt.getX()); // prevent negative X coordinates
+
+    int res = (xCords / av.getCharWidth())
             + av.getRanges().getStartRes();
 
     if (av.hasHiddenColumns())
     {
       res = av.getAlignment().getHiddenColumns()
-              .adjustForHiddenColumns(res);
+              .visibleToAbsoluteColumn(res);
     }
 
     if (res >= av.getAlignment().getWidth())
@@ -309,7 +300,7 @@ public class ScalePanel extends JPanel implements MouseMotionListener,
       }
       else
       {
-        ap.paintAlignment(false);
+        ap.paintAlignment(false, false);
       }
       return;
     }
@@ -328,7 +319,7 @@ public class ScalePanel extends JPanel implements MouseMotionListener,
       }
     }
     stretchingGroup = false;
-    ap.paintAlignment(false);
+    ap.paintAlignment(false, false);
     av.sendSelection();
   }
 
@@ -348,7 +339,7 @@ public class ScalePanel extends JPanel implements MouseMotionListener,
     int res = (evt.getX() / av.getCharWidth())
             + av.getRanges().getStartRes();
     res = Math.max(0, res);
-    res = hidden.adjustForHiddenColumns(res);
+    res = hidden.visibleToAbsoluteColumn(res);
     res = Math.min(res, av.getAlignment().getWidth() - 1);
     min = Math.min(res, min);
     max = Math.max(res, max);
@@ -358,7 +349,7 @@ public class ScalePanel extends JPanel implements MouseMotionListener,
     {
       stretchingGroup = true;
       cs.stretchGroup(res, sg, min, max);
-      ap.paintAlignment(false);
+      ap.paintAlignment(false, false);
     }
   }
 
@@ -401,20 +392,15 @@ public class ScalePanel extends JPanel implements MouseMotionListener,
     int res = (evt.getX() / av.getCharWidth())
             + av.getRanges().getStartRes();
 
-    res = av.getAlignment().getHiddenColumns().adjustForHiddenColumns(res);
+    reveal = av.getAlignment().getHiddenColumns()
+            .getRegionWithEdgeAtRes(res);
 
-    for (int[] region : av.getAlignment().getHiddenColumns())
-    {
-      if (res + 1 == region[0] || res - 1 == region[1])
-      {
-        reveal = region;
-        ToolTipManager.sharedInstance().registerComponent(this);
-        this.setToolTipText(
-                MessageManager.getString("label.reveal_hidden_columns"));
-        repaint();
-        return;
-      }
-    }
+    res = av.getAlignment().getHiddenColumns().visibleToAbsoluteColumn(res);
+
+    ToolTipManager.sharedInstance().registerComponent(this);
+    this.setToolTipText(
+            MessageManager.getString("label.reveal_hidden_columns"));
+    repaint();
   }
 
   /**
@@ -426,8 +412,17 @@ public class ScalePanel extends JPanel implements MouseMotionListener,
   @Override
   public void paintComponent(Graphics g)
   {
-    drawScale(g, av.getRanges().getStartRes(), av.getRanges().getEndRes(),
-            getWidth(), getHeight());
+    super.paintComponent(g);
+
+    /*
+     * shouldn't get called in wrapped mode as the scale above is
+     * drawn instead by SeqCanvas.drawNorthScale
+     */
+    if (!av.getWrapAlignment())
+    {
+      drawScale(g, av.getRanges().getStartRes(), av.getRanges().getEndRes(),
+              getWidth(), getHeight());
+    }
   }
 
   // scalewidth will normally be screenwidth,
@@ -467,7 +462,7 @@ public class ScalePanel extends JPanel implements MouseMotionListener,
         {
           if (hidden.isVisible(sel))
           {
-            sel = hidden.findColumnPosition(sel);
+            sel = hidden.absoluteToVisibleColumn(sel);
           }
           else
           {
@@ -497,20 +492,18 @@ public class ScalePanel extends JPanel implements MouseMotionListener,
 
       if (av.getShowHiddenMarkers())
       {
-        List<Integer> positions = hidden.findHiddenRegionPositions();
-        for (int pos : positions)
+        Iterator<Integer> it = hidden.getStartRegionIterator(startx,
+                startx + widthx + 1);
+        while (it.hasNext())
         {
-          res = pos - startx;
-
-          if (res < 0 || res > widthx)
-          {
-            continue;
-          }
+          res = it.next() - startx;
 
-          gg.fillPolygon(new int[] {
-              -1 + res * avCharWidth - avCharHeight / 4,
+          gg.fillPolygon(
+                  new int[]
+          { -1 + res * avCharWidth - avCharHeight / 4,
               -1 + res * avCharWidth + avCharHeight / 4,
-              -1 + res * avCharWidth }, new int[] { y, y, y + 2 * yOf }, 3);
+              -1 + res * avCharWidth }, new int[]
+          { y, y, y + 2 * yOf }, 3);
         }
       }
     }
@@ -551,7 +544,22 @@ public class ScalePanel extends JPanel implements MouseMotionListener,
   public void propertyChange(PropertyChangeEvent evt)
   {
     // Respond to viewport change events (e.g. alignment panel was scrolled)
-    repaint();
+    // Both scrolling and resizing change viewport ranges: scrolling changes
+    // both start and end points, but resize only changes end values.
+    // Here we only want to fastpaint on a scroll, with resize using a normal
+    // paint, so scroll events are identified as changes to the horizontal or
+    // vertical start value.
+    if (evt.getPropertyName().equals(ViewportRanges.STARTRES)
+            || evt.getPropertyName().equals(ViewportRanges.STARTRESANDSEQ)
+            || evt.getPropertyName().equals(ViewportRanges.MOVE_VIEWPORT))
+    {
+      // scroll event, repaint panel
+       
+       // Call repaint on alignment panel so that repaints from other alignment
+    // panel components can be aggregated. Otherwise performance of the overview
+    // window and others may be adversely affected.
+      av.getAlignPanel().repaint();
+    }
   }
 
 }