From 9a72e919f5f39f03f62d384a907ade3e279d884e Mon Sep 17 00:00:00 2001 From: kiramt Date: Wed, 16 Aug 2017 11:09:39 +0100 Subject: [PATCH] JAL-2665 Added sequence group update event. --- src/jalview/datamodel/SequenceGroup.java | 35 ++++++++++++++ src/jalview/gui/SeqCanvas.java | 76 +++++++++++++++++------------- src/jalview/gui/SeqPanel.java | 8 ++++ 3 files changed, 86 insertions(+), 33 deletions(-) diff --git a/src/jalview/datamodel/SequenceGroup.java b/src/jalview/datamodel/SequenceGroup.java index 46c802f..06cdec4 100755 --- a/src/jalview/datamodel/SequenceGroup.java +++ b/src/jalview/datamodel/SequenceGroup.java @@ -27,6 +27,8 @@ import jalview.renderer.ResidueShaderI; import jalview.schemes.ColourSchemeI; import java.awt.Color; +import java.beans.PropertyChangeListener; +import java.beans.PropertyChangeSupport; import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -39,6 +41,26 @@ import java.util.Map; */ public class SequenceGroup implements AnnotatedCollectionI { + // TODO ideally this event notification functionality should be separated into + // a + // subclass of ViewportProperties similarly to ViewportRanges. Done here as + // quick fix for JAL-2665 + public static final String SEQ_GROUP_CHANGED = "Sequence group changed"; + + protected PropertyChangeSupport changeSupport = new PropertyChangeSupport( + this); + + public void addPropertyChangeListener(PropertyChangeListener listener) + { + changeSupport.addPropertyChangeListener(listener); + } + + public void removePropertyChangeListener(PropertyChangeListener listener) + { + changeSupport.removePropertyChangeListener(listener); + } + // end of event notification functionality initialisation + String groupName; String description; @@ -495,7 +517,10 @@ public class SequenceGroup implements AnnotatedCollectionI { if (s != null && !sequences.contains(s)) { + List before = sequences; sequences.add(s); + changeSupport.firePropertyChange(SEQ_GROUP_CHANGED, before, + sequences); } if (recalc) @@ -688,7 +713,10 @@ public class SequenceGroup implements AnnotatedCollectionI { synchronized (sequences) { + List before = sequences; sequences.remove(s); + changeSupport.firePropertyChange(SEQ_GROUP_CHANGED, before, + sequences); if (recalc) { @@ -725,7 +753,9 @@ public class SequenceGroup implements AnnotatedCollectionI */ public void setStartRes(int i) { + int before = startRes; startRes = i; + changeSupport.firePropertyChange(SEQ_GROUP_CHANGED, before, startRes); } /** @@ -735,7 +765,9 @@ public class SequenceGroup implements AnnotatedCollectionI */ public void setEndRes(int i) { + int before = endRes; endRes = i; + changeSupport.firePropertyChange(SEQ_GROUP_CHANGED, before, endRes); } /** @@ -1344,7 +1376,10 @@ public class SequenceGroup implements AnnotatedCollectionI { synchronized (sequences) { + List before = sequences; sequences.clear(); + changeSupport.firePropertyChange(SEQ_GROUP_CHANGED, before, + sequences); } } diff --git a/src/jalview/gui/SeqCanvas.java b/src/jalview/gui/SeqCanvas.java index a289dba..abd6325 100755 --- a/src/jalview/gui/SeqCanvas.java +++ b/src/jalview/gui/SeqCanvas.java @@ -353,7 +353,27 @@ public class SeqCanvas extends JComponent implements ViewportListenerI public void paintComponent(Graphics g) { updateViewport(); - BufferedImage lcimg = img; // take reference since other threads may null + + // img is a cached version of the last view we drew + // selectImage will hold any selection we have + // lcimg is a local *copy* of img which we'll draw selectImage on top of + + if (img == null) + { + setupImage(); + } + if (img == null) + { + return; + } + BufferedImage lcimg = new BufferedImage(img.getWidth(), img.getHeight(), + img.getType()); + Graphics2D g2d = lcimg.createGraphics(); + g2d.drawImage(img, 0, 0, null); + g2d.dispose(); + + + // BufferedImage lcimg = img; // take reference since other threads may null // img and call later. super.paintComponent(g); @@ -366,7 +386,6 @@ public class SeqCanvas extends JComponent implements ViewportListenerI { Graphics2D g2 = (Graphics2D) lcimg.getGraphics(); g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC)); - // g.drawImage(lcimg, 0, 0, this); // overlay selection group on lcimg if (selectImage != null) @@ -381,12 +400,11 @@ public class SeqCanvas extends JComponent implements ViewportListenerI return; } - lcimg = setupImage(lcimg); - // img = lcimg; + /* lcimg = setupImage(lcimg); if (lcimg == null) { return; - } + }*/ if (av.antiAlias) { @@ -409,8 +427,6 @@ public class SeqCanvas extends JComponent implements ViewportListenerI } Graphics2D g2 = (Graphics2D) lcimg.getGraphics(); - // g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC)); - // g.drawImage(lcimg, 0, 0, this); // scrolling only works with g not g2??? // overlay selection group on lcimg if (selectImage != null) @@ -420,10 +436,15 @@ public class SeqCanvas extends JComponent implements ViewportListenerI g2.drawImage(selectImage, 0, 0, this); } g.drawImage(lcimg, 0, 0, this); - // g.drawImage(selectImage, 0, 0, this); } - private BufferedImage setupImage(BufferedImage lcimg) + private void paintSeqGroup() + { + fastPaint = true; + repaint(); + } + + private void setupImage() { // this draws the whole of the alignment imgWidth = getWidth(); @@ -434,15 +455,13 @@ public class SeqCanvas extends JComponent implements ViewportListenerI if ((imgWidth < 1) || (imgHeight < 1)) { - return null; + return; } - if (lcimg == null || imgWidth != lcimg.getWidth() - || imgHeight != lcimg.getHeight()) - { + try { - lcimg = img = new BufferedImage(imgWidth, imgHeight, + img = new BufferedImage(imgWidth, imgHeight, BufferedImage.TYPE_INT_ARGB); // ARGB so alpha compositing works gg = (Graphics2D) img.getGraphics(); gg.setFont(av.getFont()); @@ -452,10 +471,9 @@ public class SeqCanvas extends JComponent implements ViewportListenerI System.err.println("SeqCanvas OutOfMemory Redraw Error.\n" + er); new OOMWarning("Creating alignment image for display", er); - return null; + return; } - } - return lcimg; + } private BufferedImage setupSelectionImage() @@ -475,7 +493,7 @@ public class SeqCanvas extends JComponent implements ViewportListenerI try { - lcimg = new BufferedImage(imgWidth, imgHeight, + lcimg = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); // ARGB so alpha compositing works } catch (OutOfMemoryError er) { @@ -863,7 +881,6 @@ public class SeqCanvas extends JComponent implements ViewportListenerI // /////////////////////////////////// // Now outline any areas if necessary // /////////////////////////////////// - // SequenceGroup group = av.getSelectionGroup(); SequenceGroup group = null; @@ -873,7 +890,6 @@ public class SeqCanvas extends JComponent implements ViewportListenerI int groupIndex = -1; int visWidth = (endRes - startRes + 1) * charWidth; - // if ((group == null) && (av.getAlignment().getGroups().size() > 0)) if (av.getAlignment().getGroups().size() > 0) { group = av.getAlignment().getGroups().get(0); @@ -926,18 +942,8 @@ public class SeqCanvas extends JComponent implements ViewportListenerI oldY = sy; inGroup = true; - /*if (group == av.getSelectionGroup()) - { - g.setStroke(new BasicStroke(1, BasicStroke.CAP_BUTT, - BasicStroke.JOIN_ROUND, 3f, new float[] { 5f, 3f }, - 0f)); - g.setColor(Color.RED); - } - else - {*/ - g.setStroke(new BasicStroke()); - g.setColor(group.getOutlineColour()); - // } + g.setStroke(new BasicStroke()); + g.setColor(group.getOutlineColour()); } } else @@ -1223,7 +1229,11 @@ public class SeqCanvas extends JComponent implements ViewportListenerI { String eventName = evt.getPropertyName(); - if (av.getWrapAlignment()) + if (eventName.equals(SequenceGroup.SEQ_GROUP_CHANGED)) + { + paintSeqGroup(); + } + else if (av.getWrapAlignment()) { if (eventName.equals(ViewportRanges.STARTRES)) { diff --git a/src/jalview/gui/SeqPanel.java b/src/jalview/gui/SeqPanel.java index 2daa157..25fce62 100644 --- a/src/jalview/gui/SeqPanel.java +++ b/src/jalview/gui/SeqPanel.java @@ -1746,6 +1746,11 @@ public class SeqPanel extends JPanel implements MouseListener, stretchGroup.getWidth(); } + if (stretchGroup != null) + { + stretchGroup.addPropertyChangeListener(seqCanvas); + } + seqCanvas.repaint(); } @@ -1795,6 +1800,9 @@ public class SeqPanel extends JPanel implements MouseListener, { return; } + + stretchGroup.removePropertyChangeListener(seqCanvas); + // always do this - annotation has own state // but defer colourscheme update until hidden sequences are passed in boolean vischange = stretchGroup.recalcConservation(true); -- 1.7.10.2