Merge branch 'develop' into releases/Release_2_11_3_Branch
[jalview.git] / src / jalview / gui / ScalePanel.java
index b392c8f..a186ce6 100755 (executable)
  */
 package jalview.gui;
 
-import jalview.datamodel.ColumnSelection;
-import jalview.datamodel.HiddenColumns;
-import jalview.datamodel.SequenceGroup;
-import jalview.renderer.ScaleRenderer;
-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;
 import java.awt.Graphics;
@@ -48,9 +38,18 @@ import java.util.List;
 import javax.swing.JMenuItem;
 import javax.swing.JPanel;
 import javax.swing.JPopupMenu;
-import javax.swing.SwingUtilities;
 import javax.swing.ToolTipManager;
 
+import jalview.datamodel.ColumnSelection;
+import jalview.datamodel.HiddenColumns;
+import jalview.datamodel.SequenceGroup;
+import jalview.renderer.ScaleRenderer;
+import jalview.renderer.ScaleRenderer.ScaleMark;
+import jalview.util.MessageManager;
+import jalview.util.Platform;
+import jalview.viewmodel.ViewportListenerI;
+import jalview.viewmodel.ViewportRanges;
+
 /**
  * The panel containing the sequence ruler (when not in wrapped mode), and
  * supports a range of mouse operations to select, hide or reveal columns.
@@ -108,22 +107,7 @@ public class ScalePanel extends JPanel
   @Override
   public void mousePressed(MouseEvent evt)
   {
-    int x = (evt.getX() / av.getCharWidth()) + av.getRanges().getStartRes();
-    final int res;
-
-    if (av.hasHiddenColumns())
-    {
-      x = av.getAlignment().getHiddenColumns().visibleToAbsoluteColumn(x);
-    }
-
-    if (x >= av.getAlignment().getWidth())
-    {
-      res = av.getAlignment().getWidth() - 1;
-    }
-    else
-    {
-      res = x;
-    }
+    int res = ap.getSeqPanel().findAlignmentColumn(evt);
 
     min = res;
     max = res;
@@ -131,8 +115,9 @@ public class ScalePanel extends JPanel
     if (evt.isPopupTrigger()) // Mac: mousePressed
     {
       rightMouseButtonPressed(evt, res);
+      return;
     }
-    else if (SwingUtilities.isRightMouseButton(evt) && !Platform.isAMac())
+    if (Platform.isWinRightButton(evt))
     {
       /*
        * defer right-mouse click handling to mouse up on Windows
@@ -141,10 +126,7 @@ public class ScalePanel extends JPanel
        */
       return;
     }
-    else
-    {
-      leftMouseButtonPressed(evt, res);
-    }
+    leftMouseButtonPressed(evt, res);
   }
 
   /**
@@ -157,10 +139,27 @@ public class ScalePanel extends JPanel
    */
   protected void rightMouseButtonPressed(MouseEvent evt, final int res)
   {
+    JPopupMenu pop = buildPopupMenu(res);
+    if (pop.getSubElements().length > 0)
+    {
+      pop.show(this, evt.getX(), evt.getY());
+    }
+  }
+
+  /**
+   * Builds a popup menu with 'Hide' or 'Reveal' options, or both, or neither
+   * 
+   * @param res
+   *          column number (0..)
+   * @return
+   */
+  protected JPopupMenu buildPopupMenu(final int res)
+  {
     JPopupMenu pop = new JPopupMenu();
 
     /*
-     * grab the hidden range in case mouseMoved nulls it
+     * logic here depends on 'reveal', set in mouseMoved;
+     * grab the hidden range in case mouseMoved nulls it later
      */
     final int[] hiddenRange = reveal;
     if (hiddenRange != null)
@@ -181,7 +180,8 @@ public class ScalePanel extends JPanel
       });
       pop.add(item);
 
-      if (av.getAlignment().getHiddenColumns().hasMultiHiddenColumnRegions())
+      if (av.getAlignment().getHiddenColumns()
+              .hasMultiHiddenColumnRegions())
       {
         item = new JMenuItem(MessageManager.getString("action.reveal_all"));
         item.addActionListener(new ActionListener()
@@ -198,9 +198,9 @@ public class ScalePanel extends JPanel
         });
         pop.add(item);
       }
-      pop.show(this, evt.getX(), evt.getY());
     }
-    else if (av.getColumnSelection().contains(res))
+
+    if (av.getColumnSelection().contains(res))
     {
       JMenuItem item = new JMenuItem(
               MessageManager.getString("label.hide_columns"));
@@ -222,8 +222,8 @@ public class ScalePanel extends JPanel
         }
       });
       pop.add(item);
-      pop.show(this, evt.getX(), evt.getY());
     }
+    return pop;
   }
 
   /**
@@ -262,6 +262,7 @@ public class ScalePanel extends JPanel
     }
     av.setSelectionGroup(sg);
     ap.paintAlignment(false, false);
+    PaintRefresher.Refresh(this,av.getSequenceSetId());
     av.sendSelection();
   }
 
@@ -278,18 +279,8 @@ public class ScalePanel extends JPanel
     mouseDragging = false;
     ap.getSeqPanel().stopScrolling();
 
-    int xCords = Math.max(0, evt.getX()); // prevent negative X coordinates
-    ViewportRanges ranges = av.getRanges();
-    int res = (xCords / av.getCharWidth())
-            + ranges.getStartRes();
-    res = Math.min(res, ranges.getEndRes());
-    if (av.hasHiddenColumns())
-    {
-      res = av.getAlignment().getHiddenColumns()
-              .visibleToAbsoluteColumn(res);
-    }
-    res = Math.max(0, res);
-
+    int res = ap.getSeqPanel().findAlignmentColumn(evt);
+    
     if (!stretchingGroup)
     {
       if (evt.isPopupTrigger()) // Windows: mouseReleased
@@ -326,6 +317,7 @@ public class ScalePanel extends JPanel
     ap.paintAlignment(false, false);
     av.isSelectionGroupChanged(true);
     av.isColSelChanged(true);
+    PaintRefresher.Refresh(ap, av.getSequenceSetId());
     av.sendSelection();
   }
 
@@ -341,16 +333,12 @@ public class ScalePanel extends JPanel
   @Override
   public void mouseDragged(MouseEvent evt)
   {
-    mouseDragging = true;
+    mouseDragging = true;  
+    int res = ap.getSeqPanel().findAlignmentColumn(evt);
+    
     ColumnSelection cs = av.getColumnSelection();
-    HiddenColumns hidden = av.getAlignment().getHiddenColumns();
-
-    int res = (evt.getX() / av.getCharWidth())
-            + av.getRanges().getStartRes();
-    res = Math.max(0, res);
-    res = hidden.visibleToAbsoluteColumn(res);
-    res = Math.min(res, av.getAlignment().getWidth() - 1);
-    min = Math.min(res, min);
+    
+    min = Math.min(res,min);
     max = Math.max(res, max);
 
     SequenceGroup sg = av.getSelectionGroup();
@@ -359,6 +347,7 @@ public class ScalePanel extends JPanel
       stretchingGroup = true;
       cs.stretchGroup(res, sg, min, max);
       ap.paintAlignment(false, false);
+      PaintRefresher.Refresh(ap, av.getSequenceSetId());
     }
   }
 
@@ -367,6 +356,7 @@ public class ScalePanel extends JPanel
   {
     if (mouseDragging)
     {
+      mouseDragging = false;
       ap.getSeqPanel().stopScrolling();
     }
   }
@@ -399,25 +389,32 @@ public class ScalePanel extends JPanel
   {
     this.setToolTipText(null);
     reveal = null;
-    if (!av.hasHiddenColumns())
-    {
+    final int res = ap.getSeqPanel().findAlignmentColumn(evt);
+    
+    highlightAllStructPos(res);
+    if (!av.hasHiddenColumns()) {
       return;
     }
-
-    int res = (evt.getX() / av.getCharWidth())
-            + av.getRanges().getStartRes();
-
     reveal = av.getAlignment().getHiddenColumns()
-            .getRegionWithEdgeAtRes(res);
-
-    res = av.getAlignment().getHiddenColumns().visibleToAbsoluteColumn(res);
-
+            .getRegionWithEdgeAtRes(av.getAlignment().getHiddenColumns().absoluteToVisibleColumn(res));
+    if (reveal==null)
+    {
+      return;
+    }
     ToolTipManager.sharedInstance().registerComponent(this);
     this.setToolTipText(
             MessageManager.getString("label.reveal_hidden_columns"));
     repaint();
   }
 
+  public void highlightAllStructPos(int col)
+  {
+    ap.getStructureSelectionManager().highlightPositionsOnMany(
+            ap.av.getAlignment().getSequencesArray(), new int[]
+            { col, col }, ap);
+
+  }
+
   /**
    * DOCUMENT ME!
    * 
@@ -427,7 +424,7 @@ public class ScalePanel extends JPanel
   @Override
   public void paintComponent(Graphics g)
   {
-    super.paintComponent(g);
+    // super.paintComponent(g); // BH 2019
 
     /*
      * shouldn't get called in wrapped mode as the scale above is
@@ -515,10 +512,11 @@ public class ScalePanel extends JPanel
 
           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 - avCharHeight / 4,
+                      -1 + res * avCharWidth + avCharHeight / 4,
+                      -1 + res * avCharWidth },
+                  new int[]
+                  { y, y, y + 2 * yOf }, 3);
         }
       }
     }
@@ -569,10 +567,11 @@ public class ScalePanel extends JPanel
             || 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.
+
+      // 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();
     }
   }