JAL-4128 Catch RasterFormatException caused whilst resizing Overview window, recalcul...
[jalview.git] / src / jalview / gui / OverviewCanvas.java
index ed1497a..99dba3d 100644 (file)
  */
 package jalview.gui;
 
+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.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;
 
-import java.awt.Color;
-import java.awt.Dimension;
-import java.awt.Graphics;
-import java.awt.image.BufferedImage;
+public class OverviewCanvas extends JPanel
+{
+  public static final Color OVERVIEW_DEFAULT_GAP = Color.lightGray;
 
-import javax.swing.JComponent;
+  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();
 
-public class OverviewCanvas extends JComponent
-{
   private static final Color TRANS_GREY = new Color(100, 100, 100, 25);
 
   // This is set true if the alignment view changes whilst
@@ -43,6 +57,8 @@ public class OverviewCanvas extends JComponent
 
   private volatile boolean updaterunning = false;
 
+  private boolean dispose = false;
+
   private BufferedImage miniMe;
 
   private BufferedImage lastMiniMe = null;
@@ -61,11 +77,14 @@ public class OverviewCanvas extends JComponent
 
   private OverviewResColourFinder cf;
 
+  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;
@@ -73,10 +92,14 @@ public class OverviewCanvas extends JComponent
 
     boolean useLegacy = Cache.getDefault(Preferences.USE_LEGACY_GAP, false);
     Color gapCol = Cache.getDefaultColour(Preferences.GAP_COLOUR,
-            Preferences.OVERVIEW_DEFAULT_GAP);
+            OVERVIEW_DEFAULT_GAP);
     Color hiddenCol = Cache.getDefaultColour(Preferences.HIDDEN_COLOUR,
-            Preferences.OVERVIEW_DEFAULT_HIDDEN);
-    cf = new OverviewResColourFinder(useLegacy, gapCol, hiddenCol);
+            OVERVIEW_DEFAULT_HIDDEN);
+    Color residueCol = useLegacy ? OVERVIEW_DEFAULT_LEGACY_RESIDUE : OVERVIEW_DEFAULT_RESIDUE;
+    
+    cf = new OverviewResColourFinder(gapCol, residueCol, hiddenCol);
+
+    setSize(od.getWidth(), od.getHeight());
   }
 
   /**
@@ -125,7 +148,7 @@ public class OverviewCanvas extends JComponent
    *          the renderer to transfer feature colouring from
    */
   public void draw(boolean showSequenceFeatures, boolean showAnnotation,
-          FeatureRenderer transferRenderer)
+          FeatureRendererModel transferRenderer)
   {
     miniMe = null;
 
@@ -138,6 +161,9 @@ public class OverviewCanvas extends JComponent
 
     or = new OverviewRenderer(fr, od, av.getAlignment(),
             av.getResidueShading(), cf);
+
+    or.addPropertyChangeListener(progressPanel);
+
     miniMe = or.draw(od.getRows(av.getAlignment()),
             od.getColumns(av.getAlignment()));
 
@@ -147,16 +173,19 @@ public class OverviewCanvas extends JComponent
     {
       mg.translate(0, od.getSequencesHeight());
       or.drawGraph(mg, av.getAlignmentConservationAnnotation(),
-              av.getCharWidth(), od.getGraphHeight(),
-              od.getColumns(av.getAlignment()));
+              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
     {
@@ -168,6 +197,8 @@ public class OverviewCanvas extends JComponent
   @Override
   public void paintComponent(Graphics g)
   {
+    // super.paintComponent(g);
+
     if (restart)
     {
       if (lastMiniMe == null)
@@ -184,16 +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);
   }
 
+  public void dispose()
+  {
+    dispose = true;
+    od = null;
+    synchronized (this)
+    {
+      restart = true;
+      if (or != null)
+      {
+        or.setRedraw(true);
+      }
+    }
+  }
+
+  public Color getGapColour()
+  {
+    return cf.getGapColour();
+  }
+
+  public Color getHiddenColour()
+  {
+    return cf.getHiddenColour();
+  }
+
+  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);
+  }
 }