Merge branch 'develop' into features/JAL-2360colourSchemeApplicability
[jalview.git] / src / jalview / appletgui / ScalePanel.java
index 0f71818..ed07b63 100755 (executable)
@@ -152,8 +152,7 @@ public class ScalePanel extends Panel implements MouseMotionListener,
     PopupMenu pop = new PopupMenu();
     if (reveal != null)
     {
-      MenuItem item = new MenuItem(
-              MessageManager.getString("label.reveal"));
+      MenuItem item = new MenuItem(MessageManager.getString("label.reveal"));
       item.addActionListener(new ActionListener()
       {
         @Override
@@ -205,8 +204,8 @@ public class ScalePanel extends Panel implements MouseMotionListener,
         {
           av.hideColumns(res, res);
           if (av.getSelectionGroup() != null
-                  && av.getSelectionGroup().getSize() == av
-                          .getAlignment().getHeight())
+                  && av.getSelectionGroup().getSize() == av.getAlignment()
+                          .getHeight())
           {
             av.setSelectionGroup(null);
           }
@@ -265,72 +264,30 @@ public class ScalePanel extends Panel implements MouseMotionListener,
     av.sendSelection();
   }
 
+  /**
+   * Action on dragging the mouse in the scale panel is to expand or shrink the
+   * selection group range (including any hidden columns that it spans)
+   * 
+   * @param evt
+   */
   @Override
   public void mouseDragged(MouseEvent evt)
   {
     mouseDragging = true;
+    ColumnSelection cs = av.getColumnSelection();
 
     int res = (evt.getX() / av.getCharWidth()) + av.getStartRes();
-    if (res < 0)
-    {
-      res = 0;
-    }
-
-    if (av.hasHiddenColumns())
-    {
-      res = av.getColumnSelection().adjustForHiddenColumns(res);
-    }
-
-    if (res > av.getAlignment().getWidth())
-    {
-      res = av.getAlignment().getWidth() - 1;
-    }
-
-    if (res < min)
-    {
-      min = res;
-    }
-
-    if (res > max)
-    {
-      max = res;
-    }
+    res = Math.max(0, res);
+    res = cs.adjustForHiddenColumns(res);
+    res = Math.min(res, av.getAlignment().getWidth() - 1);
+    min = Math.min(res, min);
+    max = Math.max(res, max);
 
     SequenceGroup sg = av.getSelectionGroup();
-
     if (sg != null)
     {
       stretchingGroup = true;
-
-      if (!av.getColumnSelection().contains(res))
-      {
-        av.getColumnSelection().addElement(res);
-      }
-
-      if (res > sg.getStartRes())
-      {
-        sg.setEndRes(res);
-      }
-      if (res < sg.getStartRes())
-      {
-        sg.setStartRes(res);
-      }
-
-      int col;
-      for (int i = min; i <= max; i++)
-      {
-        col = av.getColumnSelection().adjustForHiddenColumns(i);
-
-        if ((col < sg.getStartRes()) || (col > sg.getEndRes()))
-        {
-          av.getColumnSelection().removeElement(col);
-        }
-        else
-        {
-          av.getColumnSelection().addElement(col);
-        }
-      }
-
+      cs.stretchGroup(res, sg, min, max);
       ap.paintAlignment(false);
     }
   }
@@ -409,22 +366,33 @@ public class ScalePanel extends Panel implements MouseMotionListener,
 
     // Fill the selected columns
     ColumnSelection cs = av.getColumnSelection();
-    gg.setColor(new Color(220, 0, 0));
     int avCharWidth = av.getCharWidth();
     int avcharHeight = av.getCharHeight();
-    for (int sel : cs.getSelected())
+    if (cs != null)
     {
-      // TODO: JAL-2001 - provide a fast method to list visible selected in a
-      // given range
-      if (av.hasHiddenColumns())
+      gg.setColor(new Color(220, 0, 0));
+      boolean hasHiddenColumns = cs.hasHiddenColumns();
+      for (int sel : cs.getSelected())
       {
-        sel = av.getColumnSelection().findColumnPosition(sel);
-      }
+        // TODO: JAL-2001 - provide a fast method to list visible selected in a
+        // given range
+        if (hasHiddenColumns)
+        {
+          if (cs.isVisible(sel))
+          {
+            sel = cs.findColumnPosition(sel);
+          }
+          else
+          {
+            continue;
+          }
+        }
 
-      if ((sel >= startx) && (sel <= endx))
-      {
-        gg.fillRect((sel - startx) * avCharWidth, 0, avCharWidth,
-                getSize().height);
+        if ((sel >= startx) && (sel <= endx))
+        {
+          gg.fillRect((sel - startx) * avCharWidth, 0, avCharWidth,
+                  getSize().height);
+        }
       }
     }
 
@@ -471,12 +439,10 @@ public class ScalePanel extends Panel implements MouseMotionListener,
       if (av.getShowHiddenMarkers())
       {
         int widthx = 1 + endx - startx;
-        for (int i = 0; i < av.getColumnSelection().getHiddenColumns()
-                .size(); i++)
+        for (int i = 0; i < cs.getHiddenColumns().size(); i++)
         {
 
-          res = av.getColumnSelection().findHiddenRegionPosition(i)
-                  - startx;
+          res = cs.findHiddenRegionPosition(i) - startx;
 
           if (res < 0 || res > widthx)
           {
@@ -486,8 +452,7 @@ public class ScalePanel extends Panel implements MouseMotionListener,
           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);
         }
       }
     }