From aaead18c6acb21da4d63f3ad9f454d9eb410152f Mon Sep 17 00:00:00 2001 From: gmungoc Date: Thu, 26 Jul 2018 13:38:12 +0100 Subject: [PATCH] JAL-3071 IdPanel.ScrollThread for JalviewJS, Javadoc --- src/jalview/gui/AnnotationPanel.java | 7 +- src/jalview/gui/IdPanel.java | 129 +++++++++++++++++++++++++--------- src/jalview/gui/SeqPanel.java | 2 +- 3 files changed, 100 insertions(+), 38 deletions(-) diff --git a/src/jalview/gui/AnnotationPanel.java b/src/jalview/gui/AnnotationPanel.java index 5d8540c..822c6d2 100755 --- a/src/jalview/gui/AnnotationPanel.java +++ b/src/jalview/gui/AnnotationPanel.java @@ -28,7 +28,6 @@ import jalview.datamodel.SequenceI; import jalview.gui.JalviewColourChooser.ColourChooserListener; import jalview.renderer.AnnotationRenderer; import jalview.renderer.AwtRenderPanelI; -import jalview.schemes.FeatureColour; import jalview.schemes.ResidueProperties; import jalview.util.Comparison; import jalview.util.MessageManager; @@ -59,8 +58,6 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; -import javax.swing.JColorChooser; -import javax.swing.JDialog; import javax.swing.JMenuItem; import javax.swing.JPanel; import javax.swing.JPopupMenu; @@ -661,10 +658,10 @@ public class AnnotationPanel extends JPanel implements AwtRenderPanelI, } /** - * DOCUMENT ME! + * On leaving the panel, calls ScalePanel.mouseExited to deal with scrolling + * with column selection on a mouse drag * * @param evt - * DOCUMENT ME! */ @Override public void mouseExited(MouseEvent evt) diff --git a/src/jalview/gui/IdPanel.java b/src/jalview/gui/IdPanel.java index 3f43008..8290dcf 100755 --- a/src/jalview/gui/IdPanel.java +++ b/src/jalview/gui/IdPanel.java @@ -20,6 +20,7 @@ */ package jalview.gui; +import jalview.bin.Jalview; import jalview.datamodel.Sequence; import jalview.datamodel.SequenceFeature; import jalview.datamodel.SequenceGroup; @@ -31,6 +32,8 @@ import jalview.viewmodel.AlignmentViewport; import jalview.viewmodel.ViewportRanges; import java.awt.BorderLayout; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; @@ -40,6 +43,7 @@ import java.util.List; import javax.swing.JPanel; import javax.swing.SwingUtilities; +import javax.swing.Timer; import javax.swing.ToolTipManager; /** @@ -215,14 +219,21 @@ public class IdPanel extends JPanel } /** - * DOCUMENT ME! + * On (re-)entering the panel, stop any scrolling * * @param e - * DOCUMENT ME! */ @Override public void mouseEntered(MouseEvent e) { + stopScrolling(); + } + + /** + * Interrupts the scroll thread if one is running + */ + void stopScrolling() + { if (scrollThread != null) { scrollThread.stopScrolling(); @@ -243,16 +254,70 @@ public class IdPanel extends JPanel return; } - if (mouseDragging && (e.getY() < 0) - && (av.getRanges().getStartSeq() > 0)) + if (mouseDragging) { - scrollThread = new ScrollThread(true); + /* + * on mouse drag above or below the panel, start + * scrolling if there are more sequences to show + */ + ViewportRanges ranges = av.getRanges(); + if (e.getY() < 0 && ranges.getStartSeq() > 0) + { + startScrolling(true); + } + else if (e.getY() >= getHeight() + && ranges.getEndSeq() <= av.getAlignment().getHeight()) + { + startScrolling(false); + } } + } - if (mouseDragging && (e.getY() >= getHeight()) - && (av.getAlignment().getHeight() > av.getRanges().getEndSeq())) + /** + * Starts scrolling either up or down + * + * @param up + */ + void startScrolling(boolean up) + { + scrollThread = new ScrollThread(up); + if (!Jalview.isJS()) { - scrollThread = new ScrollThread(false); + /* + * Java - run in a new thread + */ + scrollThread.start(); + } + else + { + /* + * for JalviewJS using Swing Timer + */ + Timer t = new Timer(20, new ActionListener() + { + @Override + public void actionPerformed(ActionEvent e) + { + if (scrollThread != null) + { + // if (!scrollOnce() {t.stop();}) gives compiler error :-( + scrollThread.scrollOnce(); + } + } + }); + t.addActionListener(new ActionListener() + { + @Override + public void actionPerformed(ActionEvent e) + { + if (scrollThread == null) + { + // finished and nulled itself + t.stop(); + } + } + }); + t.start(); } } @@ -407,10 +472,7 @@ public class IdPanel extends JPanel @Override public void mouseReleased(MouseEvent e) { - if (scrollThread != null) - { - scrollThread.stopScrolling(); - } + stopScrolling(); mouseDragging = false; PaintRefresher.Refresh(this, av.getSequenceSetId()); @@ -492,7 +554,7 @@ public class IdPanel extends JPanel * Scrolls the alignment either up or down, one row at a time, adding newly * visible sequences to the current selection. Speed is limited to a maximum * of ten rows per second. The thread exits when the end of the alignment is - * reached or a flag is set to stop it. + * reached or a flag is set to stop it by a call to stopScrolling. */ @Override public void run() @@ -501,25 +563,7 @@ public class IdPanel extends JPanel while (running) { - ViewportRanges ranges = IdPanel.this.av.getRanges(); - if (ranges.scrollUp(up)) - { - int toSeq = up ? ranges.getStartSeq() : ranges.getEndSeq(); - int fromSeq = toSeq < lastid ? lastid - 1 : lastid + 1; - IdPanel.this.selectSeqs(fromSeq, toSeq); - - lastid = toSeq; - } - else - { - /* - * scroll did nothing - reached limit of visible alignment - */ - running = false; - } - - alignPanel.paintAlignment(false, false); - + running = scrollOnce(); try { Thread.sleep(100); @@ -527,6 +571,27 @@ public class IdPanel extends JPanel { } } + IdPanel.this.scrollThread = null; + } + + /** + * Scrolls one row up or down. Answers true if a scroll could be done, false + * if not (top or bottom of alignment reached). + */ + boolean scrollOnce() + { + ViewportRanges ranges = IdPanel.this.av.getRanges(); + if (ranges.scrollUp(up)) + { + int toSeq = up ? ranges.getStartSeq() : ranges.getEndSeq(); + int fromSeq = toSeq < lastid ? lastid - 1 : lastid + 1; + IdPanel.this.selectSeqs(fromSeq, toSeq); + lastid = toSeq; + alignPanel.paintAlignment(false, false); + return true; + } + + return false; } } } diff --git a/src/jalview/gui/SeqPanel.java b/src/jalview/gui/SeqPanel.java index 330a962..1d943f0 100644 --- a/src/jalview/gui/SeqPanel.java +++ b/src/jalview/gui/SeqPanel.java @@ -2051,7 +2051,7 @@ public class SeqPanel extends JPanel * a drag in ScalePanel or AnnotationPanel */ mouseDragging = true; - if (!av.getWrapAlignment() && mouseDragging && scrollThread == null) + if (!av.getWrapAlignment() && scrollThread == null) { scrollThread = new ScrollThread(); scrollThread.setMousePosition(mousePos); -- 1.7.10.2