From 961e285f3c6769a3d68152a7784d85f6e814cafb Mon Sep 17 00:00:00 2001 From: gmungoc Date: Tue, 2 Apr 2019 14:10:45 +0100 Subject: [PATCH] JAL-3218 allow both Hide and Reveal in scale panel popup menu --- src/jalview/gui/ScalePanel.java | 35 +++++---- test/jalview/gui/ScalePanelTest.java | 130 ++++++++++++++++++++++++++++++++++ 2 files changed, 152 insertions(+), 13 deletions(-) diff --git a/src/jalview/gui/ScalePanel.java b/src/jalview/gui/ScalePanel.java index b392c8f..8f49c6c 100755 --- a/src/jalview/gui/ScalePanel.java +++ b/src/jalview/gui/ScalePanel.java @@ -115,15 +115,7 @@ public class ScalePanel extends JPanel { x = av.getAlignment().getHiddenColumns().visibleToAbsoluteColumn(x); } - - if (x >= av.getAlignment().getWidth()) - { - res = av.getAlignment().getWidth() - 1; - } - else - { - res = x; - } + res = Math.min(x, av.getAlignment().getWidth() - 1); min = res; max = res; @@ -157,10 +149,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) @@ -198,9 +207,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 +231,8 @@ public class ScalePanel extends JPanel } }); pop.add(item); - pop.show(this, evt.getX(), evt.getY()); } + return pop; } /** diff --git a/test/jalview/gui/ScalePanelTest.java b/test/jalview/gui/ScalePanelTest.java index 0ec1c1f..7d39ea9 100644 --- a/test/jalview/gui/ScalePanelTest.java +++ b/test/jalview/gui/ScalePanelTest.java @@ -12,10 +12,14 @@ import jalview.datamodel.SequenceGroup; import jalview.datamodel.SequenceI; import jalview.io.DataSourceType; import jalview.io.FileLoader; +import jalview.util.MessageManager; import jalview.viewmodel.ViewportRanges; import java.awt.event.MouseEvent; +import javax.swing.JMenuItem; +import javax.swing.JPopupMenu; + import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; @@ -95,6 +99,132 @@ public class ScalePanelTest assertEquals(sg.getEndRes(), 30); } + @Test(groups = "Functional") + public void testBuildPopupMenu() + { + final String hide = MessageManager.getString("label.hide_columns"); + final String reveal = MessageManager.getString("label.reveal"); + final String revealAll = MessageManager.getString("action.reveal_all"); + + String seq1 = ">Seq1\nANTOFAGASTA"; + AlignFrame alignFrame = new FileLoader().LoadFileWaitTillLoaded(seq1, + DataSourceType.PASTE); + ScalePanel scalePanel = alignFrame.alignPanel.getScalePanel(); + AlignViewport viewport = alignFrame.getViewport(); + int cw = viewport.getCharWidth(); + + /* + * hide columns 3-4 (counting from 0) + */ + viewport.hideColumns(3, 4); + alignFrame.alignPanel.updateLayout(); + + /* + * verify popup menu left/right of hidden column marker + * NB need to call mouseMoved first as this sets field 'reveal' + */ + int xPos = 1 * cw + 2; // 2 columns left of hidden marker + MouseEvent mouse = new MouseEvent(scalePanel, 0, 1, 0, xPos, 0, 1, + false); + scalePanel.mouseMoved(mouse); + JPopupMenu popup = scalePanel.buildPopupMenu(1); + assertEquals(popup.getSubElements().length, 0); + + /* + * popup just left of hidden marker has 'Reveal' + */ + xPos = 2 * cw + 2; + mouse = new MouseEvent(scalePanel, 0, 1, 0, xPos, 0, 1, false); + scalePanel.mouseMoved(mouse); + popup = scalePanel.buildPopupMenu(2); + assertEquals(popup.getSubElements().length, 1); + assertEquals(((JMenuItem) popup.getSubElements()[0]).getText(), reveal); + + /* + * popup just right of hidden marker has 'Reveal' + */ + xPos = 3 * cw + 2; + mouse = new MouseEvent(scalePanel, 0, 1, 0, xPos, 0, 1, false); + scalePanel.mouseMoved(mouse); + popup = scalePanel.buildPopupMenu(5); // allowing for 2 hidden columns + assertEquals(popup.getSubElements().length, 1); + assertEquals(((JMenuItem) popup.getSubElements()[0]).getText(), reveal); + + /* + * popup further right is empty + */ + xPos = 4 * cw + 2; + mouse = new MouseEvent(scalePanel, 0, 1, 0, xPos, 0, 1, false); + scalePanel.mouseMoved(mouse); + popup = scalePanel.buildPopupMenu(6); // allowing for 2 hidden columns + assertEquals(popup.getSubElements().length, 0); + + /* + * 'drag' to select columns around the hidden column marker + */ + xPos = 1 * cw + 2; + mouse = new MouseEvent(scalePanel, 0, 1, 0, xPos, 0, 1, false); + scalePanel.mousePressed(mouse); + scalePanel.mouseDragged(mouse); + xPos = 5 * cw + 2; + mouse = new MouseEvent(scalePanel, 0, 1, 0, xPos, 0, 1, false); + scalePanel.mouseReleased(mouse); + + /* + * popup 2 columns left of marker: 'Hide' only + */ + xPos = 1 * cw + 2; + mouse = new MouseEvent(scalePanel, 0, 1, 0, xPos, 0, 1, false); + scalePanel.mouseMoved(mouse); + popup = scalePanel.buildPopupMenu(1); + assertEquals(popup.getSubElements().length, 1); + assertEquals(((JMenuItem) popup.getSubElements()[0]).getText(), hide); + + /* + * popup just left of marker: 'Reveal' and 'Hide' + */ + xPos = 2 * cw + 2; + mouse = new MouseEvent(scalePanel, 0, 1, 0, xPos, 0, 1, false); + scalePanel.mouseMoved(mouse); + popup = scalePanel.buildPopupMenu(1); + assertEquals(popup.getSubElements().length, 2); + assertEquals(((JMenuItem) popup.getSubElements()[0]).getText(), reveal); + assertEquals(((JMenuItem) popup.getSubElements()[1]).getText(), hide); + + /* + * popup just right of marker: 'Reveal' and 'Hide' + */ + xPos = 3 * cw + 2; + mouse = new MouseEvent(scalePanel, 0, 1, 0, xPos, 0, 1, false); + scalePanel.mouseMoved(mouse); + popup = scalePanel.buildPopupMenu(5); + assertEquals(popup.getSubElements().length, 2); + assertEquals(((JMenuItem) popup.getSubElements()[0]).getText(), reveal); + assertEquals(((JMenuItem) popup.getSubElements()[1]).getText(), hide); + + /* + * hiding a second region adds option 'Reveal all' to 'Reveal' + */ + viewport.hideColumns(6, 7); + alignFrame.alignPanel.updateLayout(); + xPos = 3 * cw + 2; + mouse = new MouseEvent(scalePanel, 0, 1, 0, xPos, 0, 1, false); + scalePanel.mouseMoved(mouse); + popup = scalePanel.buildPopupMenu(5); + assertEquals(popup.getSubElements().length, 3); + assertEquals(((JMenuItem) popup.getSubElements()[0]).getText(), reveal); + assertEquals(((JMenuItem) popup.getSubElements()[1]).getText(), + revealAll); + assertEquals(((JMenuItem) popup.getSubElements()[2]).getText(), hide); + + alignFrame.deselectAllSequenceMenuItem_actionPerformed(null); + popup = scalePanel.buildPopupMenu(5); + assertEquals(popup.getSubElements().length, 2); + assertEquals(((JMenuItem) popup.getSubElements()[0]).getText(), reveal); + assertEquals(((JMenuItem) popup.getSubElements()[1]).getText(), + revealAll); + } + @BeforeClass(alwaysRun = true) public static void setUpBeforeClass() throws Exception { -- 1.7.10.2