X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fgui%2FOverviewCanvas.java;h=99dba3d97e39ce7b32c741d0895439dc3092eb2c;hb=a5928e2c1b9e2cea8d9c43f3d03f9cce2604f3b2;hp=df831e1ee80aeda455a0d1fe3bc81d6c9ac1d005;hpb=fd45447c22c4bb229e2383ab8010d3a60a48f86d;p=jalview.git diff --git a/src/jalview/gui/OverviewCanvas.java b/src/jalview/gui/OverviewCanvas.java index df831e1..99dba3d 100644 --- a/src/jalview/gui/OverviewCanvas.java +++ b/src/jalview/gui/OverviewCanvas.java @@ -20,33 +20,48 @@ */ package jalview.gui; -import jalview.api.AlignViewportI; -import jalview.datamodel.SequenceI; -import jalview.renderer.AnnotationRenderer; -import jalview.renderer.seqfeatures.FeatureColourFinder; -import jalview.viewmodel.OverviewDimensions; - import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.image.BufferedImage; +import java.awt.image.RasterFormatException; -import javax.swing.JComponent; +import javax.swing.JPanel; +import jalview.api.AlignViewportI; +import jalview.bin.Cache; +import jalview.bin.Console; +import jalview.renderer.OverviewRenderer; +import jalview.renderer.OverviewResColourFinder; +import jalview.viewmodel.OverviewDimensions; +import jalview.viewmodel.seqfeatures.FeatureRendererModel; -public class OverviewCanvas extends JComponent +public class OverviewCanvas extends JPanel { + public static final Color OVERVIEW_DEFAULT_GAP = Color.lightGray; + + public static final Color OVERVIEW_DEFAULT_LEGACY_GAP = Color.white; + + public static final Color OVERVIEW_DEFAULT_RESIDUE = Color.white; + + public static final Color OVERVIEW_DEFAULT_LEGACY_RESIDUE = Color.lightGray; + + public static final Color OVERVIEW_DEFAULT_HIDDEN = Color.darkGray + .darker(); + private static final Color TRANS_GREY = new Color(100, 100, 100, 25); // This is set true if the alignment view changes whilst // the overview is being calculated private volatile boolean restart = false; - private BufferedImage miniMe; + private volatile boolean updaterunning = false; - private BufferedImage lastMiniMe = null; + private boolean dispose = false; + private BufferedImage miniMe; + private BufferedImage lastMiniMe = null; // Can set different properties in this seqCanvas than // main visible SeqCanvas @@ -54,95 +69,127 @@ public class OverviewCanvas extends JComponent private jalview.renderer.seqfeatures.FeatureRenderer fr; - private final AnnotationRenderer renderer = new AnnotationRenderer(); + private OverviewDimensions od; - OverviewDimensions od; + private OverviewRenderer or = null; - AlignViewport av; + private AlignViewportI av; - AlignmentPanel ap; + private OverviewResColourFinder cf; + + private ProgressPanel progressPanel; public OverviewCanvas(OverviewDimensions overviewDims, - AlignViewportI alignvp, AlignmentPanel alignp) + AlignViewportI alignvp, ProgressPanel pp) { od = overviewDims; - av = alignp.av; - ap = alignp; + av = alignvp; + progressPanel = pp; sr = new SequenceRenderer(av); sr.renderGaps = false; - sr.forOverview = true; - fr = new FeatureRenderer(ap); + fr = new jalview.renderer.seqfeatures.FeatureRenderer(av); + + boolean useLegacy = Cache.getDefault(Preferences.USE_LEGACY_GAP, false); + Color gapCol = Cache.getDefaultColour(Preferences.GAP_COLOUR, + OVERVIEW_DEFAULT_GAP); + Color hiddenCol = Cache.getDefaultColour(Preferences.HIDDEN_COLOUR, + OVERVIEW_DEFAULT_HIDDEN); + Color residueCol = useLegacy ? OVERVIEW_DEFAULT_LEGACY_RESIDUE : OVERVIEW_DEFAULT_RESIDUE; + + cf = new OverviewResColourFinder(gapCol, residueCol, hiddenCol); + + setSize(od.getWidth(), od.getHeight()); + } + + /** + * Update the overview dimensions object used by the canvas (e.g. if we change + * from showing hidden columns to hiding them or vice versa) + * + * @param overviewDims + */ + public void resetOviewDims(OverviewDimensions overviewDims) + { + od = overviewDims; } - /* - * Signals to drawing code that the associated alignment viewport - * has changed and a redraw will be required + /** + * Signals to drawing code that the associated alignment viewport has changed + * and a redraw will be required */ - public void restartDraw() + public boolean restartDraw() { - restart = true; + synchronized (this) + { + if (updaterunning) + { + restart = true; + if (or != null) + { + or.setRedraw(true); + } + } + else + { + updaterunning = true; + } + return restart; + } } - public void draw(boolean showSequenceFeatures, boolean showAnnotation) + /** + * Draw the overview sequences + * + * @param showSequenceFeatures + * true if sequence features are to be shown + * @param showAnnotation + * true if the annotation is to be shown + * @param transferRenderer + * the renderer to transfer feature colouring from + */ + public void draw(boolean showSequenceFeatures, boolean showAnnotation, + FeatureRendererModel transferRenderer) { miniMe = null; if (showSequenceFeatures) { - fr.transferSettings(ap.getSeqPanel().seqCanvas.getFeatureRenderer()); + fr.transferSettings(transferRenderer); } - // why do we need to set preferred size again? was set in - // updateOverviewImage setPreferredSize(new Dimension(od.getWidth(), od.getHeight())); - miniMe = new BufferedImage(od.getWidth(), od.getHeight(), - BufferedImage.TYPE_INT_RGB); - - Graphics mg = miniMe.getGraphics(); - mg.setColor(Color.orange); - mg.fillRect(0, 0, od.getWidth(), miniMe.getHeight()); + or = new OverviewRenderer(fr, od, av.getAlignment(), + av.getResidueShading(), cf); - // calculate sampleCol and sampleRow - // alignment width is max number of residues/bases - // alignment height is number of sequences - int alwidth = av.getAlignment().getWidth(); - int alheight = av.getAlignment().getAbsoluteHeight(); + or.addPropertyChangeListener(progressPanel); - // sampleCol or sampleRow is the width/height allocated to each residue - // in particular, sometimes we may need more than one row/col of the - // BufferedImage allocated - // sampleCol is how much of a residue to assign to each pixel - // sampleRow is how many sequences to assign to each pixel - float sampleCol = alwidth / (float) od.getWidth(); - float sampleRow = alheight / (float) od.getSequencesHeight(); + miniMe = or.draw(od.getRows(av.getAlignment()), + od.getColumns(av.getAlignment())); - buildImage(sampleRow, sampleCol); + Graphics mg = miniMe.getGraphics(); if (showAnnotation) { - renderer.updateFromAlignViewport(av); - for (int col = 0; col < od.getWidth() && !restart; col++) - { - mg.translate(col, od.getSequencesHeight()); - renderer.drawGraph(mg, av.getAlignmentConservationAnnotation(), - av.getAlignmentConservationAnnotation().annotations, - (int) (sampleCol) + 1, od.getGraphHeight(), - (int) (col * sampleCol), (int) (col * sampleCol) + 1); - mg.translate(-col, -od.getSequencesHeight()); - - } + mg.translate(0, od.getSequencesHeight()); + or.drawGraph(mg, av.getAlignmentConservationAnnotation(), + od.getGraphHeight(), od.getColumns(av.getAlignment())); + mg.translate(0, -od.getSequencesHeight()); } - System.gc(); + or.removePropertyChangeListener(progressPanel); + or = null; if (restart) { restart = false; - draw(showSequenceFeatures, showAnnotation); + if (!dispose) + { + draw(showSequenceFeatures, showAnnotation, transferRenderer); + } } else { + updaterunning = false; lastMiniMe = miniMe; } } @@ -150,6 +197,8 @@ public class OverviewCanvas extends JComponent @Override public void paintComponent(Graphics g) { + // super.paintComponent(g); + if (restart) { if (lastMiniMe == null) @@ -166,93 +215,108 @@ public class OverviewCanvas extends JComponent } else if (lastMiniMe != null) { - g.drawImage(lastMiniMe, 0, 0, this); - if (lastMiniMe != miniMe) + // is this a resize? + if ((getWidth() > 0) && (getHeight() > 0) + && ((getWidth() != od.getWidth()) + || (getHeight() != od.getHeight()))) { - g.setColor(TRANS_GREY); - g.fillRect(0, 0, getWidth(), getHeight()); + // if there is annotation, scale the alignment and annotation + // separately + if (od.getGraphHeight() > 0 && od.getSequencesHeight() > 0 // BH 2019 + ) + { + try + { + BufferedImage topImage = lastMiniMe.getSubimage(0, 0, + od.getWidth(), od.getSequencesHeight()); + BufferedImage bottomImage = lastMiniMe.getSubimage(0, + od.getSequencesHeight(), od.getWidth(), + od.getGraphHeight()); + + // must be done at this point as we rely on using old width/height + // above, and new width/height below + od.setWidth(getWidth()); + od.setHeight(getHeight()); + + // stick the images back together so lastMiniMe is consistent in the + // event of a repaint - BUT probably not thread safe + lastMiniMe = new BufferedImage(od.getWidth(), od.getHeight(), + BufferedImage.TYPE_INT_RGB); + Graphics lg = lastMiniMe.getGraphics(); + lg.drawImage(topImage, 0, 0, od.getWidth(), + od.getSequencesHeight(), null); + lg.drawImage(bottomImage, 0, od.getSequencesHeight(), + od.getWidth(), od.getGraphHeight(), this); + lg.dispose(); + } catch (RasterFormatException e) + { + Console.debug( + "Scaling miscalculation resizing Overview window"); + od.setWidth(getWidth()); + od.setHeight(getHeight()); + } + } + else + { + od.setWidth(getWidth()); + od.setHeight(getHeight()); + } + + // make sure the box is in the right place + od.setBoxPosition(av.getAlignment().getHiddenSequences(), + av.getAlignment().getHiddenColumns()); } + // fall back to normal behaviour + g.drawImage(lastMiniMe, 0, 0, getWidth(), getHeight(), this); + } + else + { + g.drawImage(lastMiniMe, 0, 0, getWidth(), getHeight(), this); } + // draw the box g.setColor(Color.red); od.drawBox(g); } - /* - * Build the overview panel image - */ - private void buildImage(float sampleRow, float sampleCol) + public void dispose() { - int lastcol = -1; - int lastrow = -1; - int rgbcolor = Color.white.getRGB(); - - SequenceI seq = null; - FeatureColourFinder finder = new FeatureColourFinder(fr); - - final boolean hasHiddenCols = av.hasHiddenColumns(); - boolean hiddenRow = false; - // get hidden row and hidden column map once at beginning. - // clone featureRenderer settings to avoid race conditions... if state is - // updated just need to refresh again - for (int row = 0; row < od.getSequencesHeight() && !restart; row++) + dispose = true; + od = null; + synchronized (this) { - boolean doCopy = true; - int currentrow = (int) (row * sampleRow); - if (currentrow != lastrow) - { - doCopy = false; - - lastrow = currentrow; - - // get the sequence which would be at alignment index 'lastrow' if no - // rows were hidden, and determine whether it is hidden or not - hiddenRow = av.getAlignment().isHidden(lastrow); - seq = av.getAlignment().getSequenceAtAbsoluteIndex(lastrow); - } - - for (int col = 0; col < od.getWidth() && !restart; col++) + restart = true; + if (or != null) { - if (doCopy) - { - rgbcolor = miniMe.getRGB(col, row - 1); - } - else if ((int) (col * sampleCol) != lastcol - || (int) (row * sampleRow) != lastrow) - { - lastcol = (int) (col * sampleCol); - rgbcolor = getColumnColourFromSequence(seq, hiddenRow, - hasHiddenCols, lastcol, finder); - } - // else we just use the color we already have , so don't need to set it - - miniMe.setRGB(col, row, rgbcolor); + or.setRedraw(true); } } } - /* - * Find the colour of a sequence at a specified column position - */ - private int getColumnColourFromSequence(jalview.datamodel.SequenceI seq, - boolean hiddenRow, boolean hasHiddenCols, int lastcol, - FeatureColourFinder finder) + public Color getGapColour() { - Color color = Color.white; - - if ((seq != null) && (seq.getLength() > lastcol)) - { - color = sr.getResidueColour(seq, lastcol, finder); - } + return cf.getGapColour(); + } - if (hiddenRow - || (hasHiddenCols && !av.getColumnSelection() - .isVisible(lastcol))) - { - color = color.darker().darker(); - } + public Color getHiddenColour() + { + return cf.getHiddenColour(); + } - return color.getRGB(); + public Color getResidueColour() + { + return cf.getResidueColour(); } + /** + * Sets the colours to use for gaps, residues and hidden regions + * + * @param gaps + * @param residues + * @param hidden + */ + public void setColours(Color gaps, Color residues, Color hidden) + { + cf = new OverviewResColourFinder(gaps, residues, hidden); + } }