JAL-3383 minor code tidying and commenting
[jalview.git] / src / jalview / gui / OverviewCanvas.java
index df831e1..308764b 100644 (file)
 package jalview.gui;
 
 import jalview.api.AlignViewportI;
-import jalview.datamodel.SequenceI;
-import jalview.renderer.AnnotationRenderer;
-import jalview.renderer.seqfeatures.FeatureColourFinder;
+import jalview.bin.Cache;
+import jalview.datamodel.AlignmentI;
+import jalview.renderer.OverviewRenderer;
+import jalview.renderer.OverviewResColourFinder;
 import jalview.viewmodel.OverviewDimensions;
 
 import java.awt.Color;
@@ -31,10 +32,10 @@ import java.awt.Dimension;
 import java.awt.Graphics;
 import java.awt.image.BufferedImage;
 
-import javax.swing.JComponent;
+import javax.swing.JPanel;
 
-
-public class OverviewCanvas extends JComponent
+@SuppressWarnings("serial")
+public class OverviewCanvas extends JPanel
 {
   private static final Color TRANS_GREY = new Color(100, 100, 100, 25);
 
@@ -42,217 +43,279 @@ public class OverviewCanvas extends JComponent
   // the overview is being calculated
   private volatile boolean restart = false;
 
-  private BufferedImage miniMe;
-
-  private BufferedImage lastMiniMe = null;
+  private volatile boolean updaterunning = false;
 
+  private boolean disposed = false;
 
+  BufferedImage lastMiniMe = null;
 
   // Can set different properties in this seqCanvas than
   // main visible SeqCanvas
-  private SequenceRenderer sr;
+  private final SequenceRenderer sr;
+
+  private final jalview.renderer.seqfeatures.FeatureRenderer fr;
+
+  private OverviewDimensions od;
+
+  private OverviewRenderer or = null;
 
-  private jalview.renderer.seqfeatures.FeatureRenderer fr;
+  private final AlignViewportI av;
 
-  private final AnnotationRenderer renderer = new AnnotationRenderer();
+  private final OverviewResColourFinder cf;
 
-  OverviewDimensions od;
+  private final ProgressPanel progressPanel;
 
-  AlignViewport av;
+  private boolean showSequenceFeatures;
 
-  AlignmentPanel ap;
+  private boolean showAnnotation;
 
-  public OverviewCanvas(OverviewDimensions overviewDims,
-          AlignViewportI alignvp, AlignmentPanel alignp)
+  private jalview.api.FeatureRenderer featureRenderer;
+
+  private final OverviewPanel panel;
+
+  private final boolean showProgress;
+
+  public OverviewCanvas(OverviewPanel panel,
+          OverviewDimensions overviewDims,
+          AlignViewportI alignvp, ProgressPanel pp)
   {
+    this.panel = panel;
     od = overviewDims;
-    av = alignp.av;
-    ap = alignp;
-
+    lastMiniMe = null;
+    av = alignvp;
+    progressPanel = pp;
+    showProgress = (pp != null);
     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,
+            jalview.renderer.OverviewResColourFinder.OVERVIEW_DEFAULT_GAP);
+    Color hiddenCol = Cache.getDefaultColour(Preferences.HIDDEN_COLOUR,
+            jalview.renderer.OverviewResColourFinder.OVERVIEW_DEFAULT_HIDDEN);
+    cf = new OverviewResColourFinder(useLegacy, gapCol, hiddenCol);
+
+    setSize(od.getWidth(), od.getHeight());
+    setPreferredSize(getSize()); // BH 2019.07.29 added
   }
 
-  /*
-   * Signals to drawing code that the associated alignment viewport
-   * has changed and a redraw will be required
+  /**
+   * 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 restartDraw()
+  public void resetOviewDims(OverviewDimensions overviewDims)
   {
-    restart = true;
+    od = overviewDims;
+    lastMiniMe = null;
   }
 
-  public void draw(boolean showSequenceFeatures, boolean showAnnotation)
+  /**
+   * Signals to drawing code that the associated alignment viewport has changed
+   * and a redraw will be required
+   */
+  public boolean restartDraw()
   {
-    miniMe = null;
+    synchronized (this)
+    {
+      if (updaterunning)
+      {
+        setRestart("restartDraw");
+      }
+      else
+      {
+        updaterunning = true;
+        restart = false;
+      }
+      return restart;
+    }
+  }
 
-    if (showSequenceFeatures)
+  private void setRestart(String why)
+  {
+    // System.out.println("OC restart true " + why);
+    restart = true;
+    if (or != null)
     {
-      fr.transferSettings(ap.getSeqPanel().seqCanvas.getFeatureRenderer());
+      or.setRedraw(true);
     }
+  }
 
-    // why do we need to set preferred size again? was set in
-    // updateOverviewImage
+  /**
+   * 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 featureRenderer
+   *          the renderer to transfer feature colouring from
+   */
+  public void draw(boolean showSequenceFeatures, boolean showAnnotation,
+          jalview.api.FeatureRenderer featureRenderer)
+  {
+    this.showSequenceFeatures = showSequenceFeatures;
+    this.showAnnotation = showAnnotation;
+    this.featureRenderer = featureRenderer;
+    if (showSequenceFeatures)
+    {
+      fr.transferSettings(featureRenderer);
+    }
     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());
-
-    // 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();
-
-    // 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();
-
-    buildImage(sampleRow, sampleCol);
-
-    if (showAnnotation)
+    AlignmentI al = av.getAlignment();
+    or = new OverviewRenderer(panel.ap, fr, od, al, av.getResidueShading(),
+            cf, showProgress);
+    if (showProgress)
     {
-      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());
-
-      }
+      or.addPropertyChangeListener(progressPanel);
     }
-    System.gc();
+    or.drawMiniMe();
+  }
 
+  synchronized void finalizeDraw(BufferedImage miniMe)
+  {
+    if (showProgress && or != null)
+    {
+      or.removePropertyChangeListener(progressPanel);
+    }
     if (restart)
     {
+      or = null;
       restart = false;
-      draw(showSequenceFeatures, showAnnotation);
+      if (!disposed)
+      {
+        draw(showSequenceFeatures, showAnnotation, featureRenderer);
+      }
     }
     else
     {
+      if (showAnnotation && or != null)
+      {
+        or.drawGraph(av.getAlignmentConservationAnnotation());
+      }
+      or = null;
+      updaterunning = false;
       lastMiniMe = miniMe;
+      repaint();
     }
   }
 
   @Override
   public void paintComponent(Graphics g)
   {
+
+    int w = getWidth();
+    int h = getHeight();
+    if (w == 0 || od.getBoxWidth() <= 0)
+    {
+      // BH 2019.07.27 removes two unnecessary paints, since boxwidth can be -1
+      // or 0 during early-stage painting
+      return;
+    }
+
+    boolean drawMe = (lastMiniMe != null);
     if (restart)
     {
-      if (lastMiniMe == null)
+      if (drawMe)
       {
-        g.setColor(Color.white);
-        g.fillRect(0, 0, getWidth(), getHeight());
+        g.drawImage(lastMiniMe, 0, 0, w, h, this);
       }
       else
       {
-        g.drawImage(lastMiniMe, 0, 0, getWidth(), getHeight(), this);
+        g.setColor(Color.white);
+        g.fillRect(0, 0, w, h);
       }
       g.setColor(TRANS_GREY);
-      g.fillRect(0, 0, getWidth(), getHeight());
+      g.fillRect(0, 0, w, h);
+      drawMe = false;
     }
-    else if (lastMiniMe != null)
+    else if (drawMe)
     {
-      g.drawImage(lastMiniMe, 0, 0, this);
-      if (lastMiniMe != miniMe)
+      // is this a resize?
+      if (w != od.getWidth() || h != od.getHeight())
       {
-        g.setColor(TRANS_GREY);
-        g.fillRect(0, 0, getWidth(), getHeight());
+
+        lastMiniMe = null;
+        return;
+        // // if there is annotation, scale the alignment and annotation
+        // // separately
+        // if (od.getGraphHeight() <= 0 && od.getSequencesHeight() <= 0)
+        // {
+        // od.setWidth(w);
+        // od.setHeight(h);
+        // return;
+        // }
+        // 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(w);
+        // od.setHeight(h);
+        //
+        // // stick the images back together so lastMiniMe is consistent in the
+        // // event of a repaint - BUT probably not thread safe
+        //
+        // // right -- this fails with fast user action.
+        //
+        // lastMiniMe = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
+        // Graphics lg = lastMiniMe.getGraphics();
+        // lg.drawImage(topImage, 0, 0, w, od.getSequencesHeight(), null);
+        // lg.drawImage(bottomImage, 0, od.getSequencesHeight(), w,
+        // od.getGraphHeight(), this);
+        // lg.dispose();
+        //
+        // } catch (RasterFormatException e)
+        // {
+        // System.out.println("OC Raster Exception " + lastMiniMe.getWidth()
+        // + "/" + w + "," + lastMiniMe.getHeight() + "/" + h + " "
+        // + od.getSequencesHeight() + " " + od.getGraphHeight());
+        // }
+        // BH 2019: removed -- this is now taken care of using vpbox in
+        // OverviewDimension
+        // // make sure the box is in the right place
+        // od.setBoxPosition(av.getAlignment().getHiddenSequences(),
+        // av.getAlignment().getHiddenColumns());
       }
     }
 
+    if (drawMe)
+    {
+      g.drawImage(lastMiniMe, 0, 0, w, h, this);
+    }
+    // draw the box
     g.setColor(Color.red);
+    // System.out.println("OC paintComponent nd=" + ndraw + " nr=" + nrepaint
+    // + " np=" + ++npaint);
     od.drawBox(g);
   }
 
-  /*
-   * Build the overview panel image
-   */
-  private void buildImage(float sampleRow, float sampleCol)
-  {
-    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++)
-    {
-      boolean doCopy = true;
-      int currentrow = (int) (row * sampleRow);
-      if (currentrow != lastrow)
-      {
-        doCopy = false;
+  // private int ndraw, npaint, nrepaint;
 
-        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++)
-      {
-        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);
-      }
-    }
-  }
+  // @Override
+  // public void repaint()
+  // {
+  // System.out.println("OC repaint " + (++nrepaint));
+  // super.repaint();
+  // }
 
-  /*
-   * 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 void dispose()
   {
-    Color color = Color.white;
-
-    if ((seq != null) && (seq.getLength() > lastcol))
+    disposed = true;
+    od = null;
+    lastMiniMe = null;
+    synchronized (this)
     {
-      color = sr.getResidueColour(seq, lastcol, finder);
+      setRestart("dispose");
     }
-
-    if (hiddenRow
-            || (hasHiddenCols && !av.getColumnSelection()
-                    .isVisible(lastcol)))
-    {
-      color = color.darker().darker();
-    }
-
-    return color.getRGB();
   }
 
 }