From d488108227f80bedaf367d4e0376c4ea90382f6b Mon Sep 17 00:00:00 2001 From: hansonr Date: Tue, 2 Jul 2019 14:32:19 +0200 Subject: [PATCH] JAL-3253 simpler coding using MouseAdapter. Using JLabel.setOpaque(true) to minimize unnecessary layout of status label. --- src/jalview/gui/IdwidthAdjuster.java | 196 ++++++++++------------------------ src/jalview/jbgui/GAlignFrame.java | 6 +- 2 files changed, 62 insertions(+), 140 deletions(-) diff --git a/src/jalview/gui/IdwidthAdjuster.java b/src/jalview/gui/IdwidthAdjuster.java index 0cffc3b..2c273ad 100755 --- a/src/jalview/gui/IdwidthAdjuster.java +++ b/src/jalview/gui/IdwidthAdjuster.java @@ -24,10 +24,8 @@ import jalview.api.AlignViewportI; import java.awt.Color; import java.awt.Cursor; -import java.awt.Graphics; +import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; -import java.awt.event.MouseListener; -import java.awt.event.MouseMotionListener; import javax.swing.JPanel; @@ -37,157 +35,77 @@ import javax.swing.JPanel; * @author $author$ * @version $Revision$ */ +@SuppressWarnings("serial") public class IdwidthAdjuster extends JPanel - implements MouseListener, MouseMotionListener { - boolean active = false; int oldX = 0; - AlignmentPanel ap; - /** - * Creates a new IdwidthAdjuster object. + * Creates a new IdwidthAdjuster object above the id panel that can be dragged + * to change the panel's width * * @param ap - * DOCUMENT ME! - */ - public IdwidthAdjuster(AlignmentPanel ap) - { - this.ap = ap; - setBackground(Color.white); - addMouseListener(this); - addMouseMotionListener(this); - } - - /** - * DOCUMENT ME! - * - * @param evt - * DOCUMENT ME! - */ - @Override - public void mousePressed(MouseEvent evt) - { - oldX = evt.getX(); - } - - /** - * DOCUMENT ME! - * - * @param evt - * DOCUMENT ME! - */ - @Override - public void mouseReleased(MouseEvent evt) - { - active = false; - repaint(); - - /* - * If in a SplitFrame with co-scaled alignments, set the other's id width to - * match - */ - final AlignViewportI viewport = ap.getAlignViewport(); - if (viewport.getCodingComplement() != null - && viewport.isScaleProteinAsCdna()) - { - viewport.getCodingComplement().setIdWidth(viewport.getIdWidth()); - SplitFrame sf = (SplitFrame) ap.alignFrame.getSplitViewContainer(); - sf.repaint(); - } - - } - - /** - * DOCUMENT ME! - * - * @param evt - * DOCUMENT ME! - */ - @Override - public void mouseEntered(MouseEvent evt) - { - active = true; - repaint(); - } - - /** - * DOCUMENT ME! + * The associated AlignmentPanel * - * @param evt - * DOCUMENT ME! */ - @Override - public void mouseExited(MouseEvent evt) - { - active = false; - repaint(); - } - - /** - * DOCUMENT ME! - * - * @param evt - * DOCUMENT ME! - */ - @Override - public void mouseDragged(MouseEvent evt) + public IdwidthAdjuster(AlignmentPanel ap) { - active = true; + // BH 2019.07.01 no need for overridden paintComponent, especially as it was + // overriding paint(g) so + // allowing strange component painting in its place. - final AlignViewportI viewport = ap.getAlignViewport(); - int curwidth = viewport.getIdWidth(); - int dif = evt.getX() - oldX; - - final int newWidth = curwidth + dif; - if ((newWidth > 20) || (dif > 0)) - { - viewport.setIdWidth(newWidth); - - ap.paintAlignment(true, false); - } - - oldX = evt.getX(); - } - - /** - * DOCUMENT ME! - * - * @param evt - * DOCUMENT ME! - */ - @Override - public void mouseMoved(MouseEvent evt) - { - } + setBackground(Color.white); + setOpaque(true); - /** - * DOCUMENT ME! - * - * @param evt - * DOCUMENT ME! - */ - @Override - public void mouseClicked(MouseEvent evt) - { - } + setCursor(Cursor.getPredefinedCursor(Cursor.W_RESIZE_CURSOR)); - /** - * DOCUMENT ME! - * - * @param g - * DOCUMENT ME! - */ - @Override - public void paintComponent(Graphics g) - { - g.setColor(Color.white); - g.fillRect(0, 0, getWidth(), getHeight()); + // BH 2019.07.01 MouseAdapter is cleaner - no need for fields active or ap - if (active) + MouseAdapter ma = (new MouseAdapter() { - setCursor(Cursor.getPredefinedCursor(Cursor.W_RESIZE_CURSOR)); - } + @Override + public void mousePressed(MouseEvent evt) + { + oldX = evt.getX(); + } + + @Override + public void mouseDragged(MouseEvent evt) + { + AlignViewportI viewport = ap.getAlignViewport(); + int curwidth = viewport.getIdWidth(); + int dif = evt.getX() - oldX; + int newWidth = curwidth + dif; + if ((newWidth > 20) || (dif > 0)) + { + viewport.setIdWidth(newWidth); + ap.paintAlignment(true, false); + } + oldX = evt.getX(); + } + + @Override + public void mouseReleased(MouseEvent evt) + { + + // If in a SplitFrame with co-scaled alignments, set the other's id + // width to match + + AlignViewportI viewport = ap.getAlignViewport(); + if (viewport.getCodingComplement() != null + && viewport.isScaleProteinAsCdna()) + { + viewport.getCodingComplement().setIdWidth(viewport.getIdWidth()); + SplitFrame sf = (SplitFrame) ap.alignFrame + .getSplitViewContainer(); + sf.repaint(); + } + + } + + }); + addMouseListener(ma); + addMouseMotionListener(ma); } } diff --git a/src/jalview/jbgui/GAlignFrame.java b/src/jalview/jbgui/GAlignFrame.java index 1263adb..9de31ab 100755 --- a/src/jalview/jbgui/GAlignFrame.java +++ b/src/jalview/jbgui/GAlignFrame.java @@ -545,7 +545,11 @@ public class GAlignFrame extends JInternalFrame this.getContentPane().setLayout(new BorderLayout()); alignFrameMenuBar.setFont(new java.awt.Font("Verdana", 0, 11)); - statusBar.setBackground(Color.white); + // statusBar.setBackground(Color.white); BH 2019.08.01 -- this does nothing, + // as the label is not opaque + statusBar.setOpaque(true);// BH 2019.07.01 -- setting a label opaque avoids + // frame repaint in SwingJS and has no effect in + // Java statusBar.setFont(new java.awt.Font("Verdana", 0, 11)); statusBar.setBorder(BorderFactory.createLineBorder(Color.black)); statusBar.setText(MessageManager.getString("label.status_bar")); -- 1.7.10.2