JAL-2527 move default colours for applet / application out of gui.Preferences so...
[jalview.git] / src / jalview / gui / OverviewCanvas.java
index 3ced9ee..64bf15c 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.renderer.OverviewRenderer;
+import jalview.renderer.OverviewResColourFinder;
 import jalview.viewmodel.OverviewDimensions;
 
+import java.awt.AlphaComposite;
 import java.awt.Color;
 import java.awt.Dimension;
 import java.awt.Graphics;
+import java.awt.Graphics2D;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
 import java.awt.image.BufferedImage;
 
 import javax.swing.JComponent;
-
+import javax.swing.Timer;
 
 public class OverviewCanvas extends JComponent
 {
+  private static final long RUNNING_TIME = 1000;
+
+  private static final int SPEED = 40;
+
   private static final Color TRANS_GREY = new Color(100, 100, 100, 25);
 
   // This is set true if the alignment view changes whilst
@@ -44,37 +52,103 @@ public class OverviewCanvas extends JComponent
 
   private volatile boolean updaterunning = false;
 
+  private boolean dispose = false;
+
   private BufferedImage miniMe;
 
   private BufferedImage lastMiniMe = null;
 
+  private BufferedImage veryLastMiniMe = null;
+
   // Can set different properties in this seqCanvas than
   // main visible SeqCanvas
   private SequenceRenderer sr;
 
   private jalview.renderer.seqfeatures.FeatureRenderer fr;
 
-  private final AnnotationRenderer renderer = new AnnotationRenderer();
+  private OverviewDimensions od;
+
+  private OverviewRenderer or = null;
+
+  private AlignViewportI av;
+
+
+  private OverviewResColourFinder cf;
+
+  private float alpha = 0f;
 
-  OverviewDimensions od;
+  private long startTime = -1;
 
-  AlignViewportI av;
+  private final Timer timer;
+
+  private ProgressPanel progressPanel;
 
   public OverviewCanvas(OverviewDimensions overviewDims,
-          AlignViewportI alignvp)
+          AlignViewportI alignvp, ProgressPanel pp)
   {
     od = overviewDims;
     av = alignvp;
+    progressPanel = pp;
 
     sr = new SequenceRenderer(av);
     sr.renderGaps = false;
-    sr.forOverview = true;
     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());
+
+    timer = new Timer(SPEED, new ActionListener()
+    {
+
+      @Override
+      public void actionPerformed(ActionEvent e)
+      {
+        if (startTime < 0)
+        {
+          startTime = System.currentTimeMillis();
+        }
+        else
+        {
+
+          long time = System.currentTimeMillis();
+          long duration = time - startTime;
+          if (duration >= RUNNING_TIME)
+          {
+            startTime = -1;
+            ((Timer) e.getSource()).stop();
+            alpha = 0f;
+          }
+          else
+          {
+            alpha = 1f - ((float) duration / (float) RUNNING_TIME);
+          }
+          repaint();
+        }
+      }
+    });
+
+  }
+
+  /**
+   * 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 boolean restartDraw()
   {
@@ -83,6 +157,10 @@ public class OverviewCanvas extends JComponent
       if (updaterunning)
       {
         restart = true;
+        if (or != null)
+        {
+          or.setRedraw(true);
+        }
       }
       else
       {
@@ -92,68 +170,65 @@ public class OverviewCanvas extends JComponent
     }
   }
 
+  /**
+   * 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,
           FeatureRenderer transferRenderer)
   {
     miniMe = null;
+    veryLastMiniMe = lastMiniMe;
 
     if (showSequenceFeatures)
     {
       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);
+    or = new OverviewRenderer(fr, od, av.getAlignment(),
+            av.getResidueShading(), cf);
 
-    Graphics mg = miniMe.getGraphics();
-    mg.setColor(Color.orange);
-    mg.fillRect(0, 0, od.getWidth(), miniMe.getHeight());
+    or.addPropertyChangeListener(progressPanel);
 
-    // 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();
+    miniMe = or.draw(od.getRows(av.getAlignment()),
+            od.getColumns(av.getAlignment()));
 
-    // 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);
+    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(),
+              av.getCharWidth(), 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, transferRenderer);
+      if (!dispose)
+      {
+        draw(showSequenceFeatures, showAnnotation, transferRenderer);
+      }
     }
     else
     {
       updaterunning = false;
       lastMiniMe = miniMe;
+      alpha = 1f;
+      timer.start();
     }
   }
 
@@ -176,93 +251,88 @@ 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());
-      }
-    }
-
-    g.setColor(Color.red);
-    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;
+        // if there is annotation, scale the alignment and annotation separately
+        if (od.getGraphHeight() > 0)
+        {
+          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();
+        }
+        else
+        {
+          od.setWidth(getWidth());
+          od.setHeight(getHeight());
+        }
 
-        lastrow = currentrow;
+        // scale lastMiniMe to the new size
+        g.drawImage(lastMiniMe, 0, 0, getWidth(), getHeight(), this);
 
-        // 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);
+        // make sure the box is in the right place
+        od.setBoxPosition(av.getAlignment().getHiddenSequences(),
+                av.getAlignment().getHiddenColumns());
       }
-
-      for (int col = 0; col < od.getWidth() && !restart; col++)
+      else // not a resize
       {
-        if (doCopy)
+        if (alpha != 0) // this is a timer triggered dissolve
         {
-          rgbcolor = miniMe.getRGB(col, row - 1);
+          Graphics2D g2d = (Graphics2D) g.create();
+          
+          // draw the original image
+          g2d.drawImage(veryLastMiniMe, 0, 0, getWidth(), getHeight(),
+                  this);
+
+          // draw the new image on top with varying degrees of transparency
+          g2d.setComposite(AlphaComposite.SrcOver.derive(1f - alpha));
+          g2d.drawImage(lastMiniMe, 0, 0, getWidth(), getHeight(), this);
+
+          g2d.dispose();
         }
-        else if ((int) (col * sampleCol) != lastcol
-                || (int) (row * sampleRow) != lastrow)
+        else
         {
-          lastcol = (int) (col * sampleCol);
-          rgbcolor = getColumnColourFromSequence(seq, hiddenRow,
-                  hasHiddenCols, lastcol, finder);
+          // fall back to normal behaviour
+          g.drawImage(lastMiniMe, 0, 0, getWidth(), getHeight(), this);
         }
-        // else we just use the color we already have , so don't need to set it
-
-        miniMe.setRGB(col, row, rgbcolor);
       }
     }
+
+    // draw the box
+    g.setColor(Color.red);
+    od.drawBox(g);
   }
 
-  /*
-   * 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))
-    {
-      color = sr.getResidueColour(seq, lastcol, finder);
-    }
-
-    if (hiddenRow
-            || (hasHiddenCols && !av.getColumnSelection()
-                    .isVisible(lastcol)))
+    dispose = true;
+    synchronized (this)
     {
-      color = color.darker().darker();
+      restart = true;
+      if (or != null)
+      {
+        or.setRedraw(true);
+      }
     }
-
-    return color.getRGB();
   }
-
 }