From fc238ee902aa8604e5ed4af33a77ad3e1c1f42ae Mon Sep 17 00:00:00 2001 From: gmungoc Date: Thu, 19 Jan 2017 09:40:06 +0000 Subject: [PATCH] JAL-2370 corrected applet mouse drag, refactored into ColumnSelection --- src/jalview/appletgui/ScalePanel.java | 68 ++++++---------------------- src/jalview/datamodel/ColumnSelection.java | 51 +++++++++++++++++++++ src/jalview/gui/ScalePanel.java | 66 ++++----------------------- 3 files changed, 73 insertions(+), 112 deletions(-) diff --git a/src/jalview/appletgui/ScalePanel.java b/src/jalview/appletgui/ScalePanel.java index 5a156fa..ed07b63 100755 --- a/src/jalview/appletgui/ScalePanel.java +++ b/src/jalview/appletgui/ScalePanel.java @@ -264,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); } } diff --git a/src/jalview/datamodel/ColumnSelection.java b/src/jalview/datamodel/ColumnSelection.java index c467f4a..98a7fe2 100644 --- a/src/jalview/datamodel/ColumnSelection.java +++ b/src/jalview/datamodel/ColumnSelection.java @@ -1831,4 +1831,55 @@ public class ColumnSelection return changed; } + /** + * Adjusts column selections, and the given selection group, to match the + * range of a stretch (e.g. mouse drag) operation + *

+ * Method refactored from ScalePanel.mouseDragged + * + * @param res + * current column position, adjusted for hidden columns + * @param sg + * current selection group + * @param min + * start position of the stretch group + * @param max + * end position of the stretch group + */ + public void stretchGroup(int res, SequenceGroup sg, int min, int max) + { + if (!contains(res)) + { + addElement(res); + } + + if (res > sg.getStartRes()) + { + // expand selection group to the right + sg.setEndRes(res); + } + if (res < sg.getStartRes()) + { + // expand selection group to the left + sg.setStartRes(res); + } + + /* + * expand or shrink column selection to match the + * range of the drag operation + */ + for (int col = min; col <= max; col++) + { + if (col < sg.getStartRes() || col > sg.getEndRes()) + { + // shrinking drag - remove from selection + removeElement(col); + } + else + { + // expanding drag - add to selection + addElement(col); + } + } + } } diff --git a/src/jalview/gui/ScalePanel.java b/src/jalview/gui/ScalePanel.java index 00d465a..8961f21 100755 --- a/src/jalview/gui/ScalePanel.java +++ b/src/jalview/gui/ScalePanel.java @@ -326,77 +326,29 @@ public class ScalePanel extends JPanel implements MouseMotionListener, } /** - * DOCUMENT ME! + * 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 - * DOCUMENT ME! */ @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 = i; // 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); } } -- 1.7.10.2