JAL-2587 Update overview progress bar while drawing rows too
[jalview.git] / src / jalview / renderer / OverviewRenderer.java
index b9d57a4..416defb 100644 (file)
@@ -39,7 +39,9 @@ public class OverviewRenderer
 {
   public static final String UPDATE = "OverviewUpdate";
 
-  protected PropertyChangeSupport changeSupport = new PropertyChangeSupport(
+  private static final int MAX_PROGRESS = 100;
+
+  private PropertyChangeSupport changeSupport = new PropertyChangeSupport(
           this);
 
   private FeatureColourFinder finder;
@@ -90,7 +92,9 @@ public class OverviewRenderer
     int seqIndex = 0;
     int pixelRow = 0;
     int alignmentHeight = miniMe.getHeight() - graphHeight;
+    int totalPixels = miniMe.getWidth() * alignmentHeight;
 
+    int lastRowUpdate = 0;
     changeSupport.firePropertyChange(UPDATE, -1, 0);
 
     for (int alignmentRow : rows)
@@ -141,6 +145,8 @@ public class OverviewRenderer
             }
           }
 
+          sendProgressUpdate((pixelCol + 1) * (endRow - pixelRow),
+                  totalPixels, lastRowUpdate);
           pixelCol = endCol + 1;
         }
         colIndex++;
@@ -148,10 +154,7 @@ public class OverviewRenderer
 
       if (pixelRow != endRow + 1)
       {
-        changeSupport.firePropertyChange(UPDATE,
-                Math.round(100 * (float) pixelRow / alignmentHeight),
-                Math.round(
-                        100 * ((float) (endRow + 1) / alignmentHeight)));
+        lastRowUpdate = sendProgressUpdate(endRow + 1, alignmentHeight, 0);
         pixelRow = endRow + 1;
       }
       seqIndex++;
@@ -160,20 +163,31 @@ public class OverviewRenderer
     // final update to progress bar if present
     if (redraw)
     {
-      changeSupport.firePropertyChange(UPDATE,
-              Math.round(100 * (float) (pixelRow - 1) / alignmentHeight),
-              0);
+      sendProgressUpdate(pixelRow - 1, alignmentHeight, 0);
     }
     else
     {
-      changeSupport.firePropertyChange(UPDATE,
-              Math.round(100 * alignmentHeight / miniMe.getHeight() - 1),
-              Math.round(100 * alignmentHeight / miniMe.getHeight()));
+      sendProgressUpdate(alignmentHeight, miniMe.getHeight(), 0);
     }
     return miniMe;
   }
 
   /*
+   * Calculate progress update value and fire event
+   */
+  private int sendProgressUpdate(int position, int maximum, int offset)
+  {
+    int newUpdate = offset
+            + Math.round(MAX_PROGRESS * ((float) position / maximum));
+    if (newUpdate > offset)
+    {
+      changeSupport.firePropertyChange(UPDATE, offset, newUpdate);
+      return newUpdate;
+    }
+    return offset;
+  }
+
+  /*
    * Find the colour of a sequence at a specified column position
    */
   private int getColumnColourFromSequence(jalview.datamodel.SequenceI seq,
@@ -222,7 +236,7 @@ public class OverviewRenderer
     {
       if (redraw)
       {
-        changeSupport.firePropertyChange(UPDATE, 99, 0);
+        changeSupport.firePropertyChange(UPDATE, MAX_PROGRESS - 1, 0);
         break;
       }
 
@@ -255,11 +269,13 @@ public class OverviewRenderer
 
           g.fillRect(pixelCol, y - height, endCol - pixelCol + 1, height);
         }
+
         pixelCol = endCol + 1;
         colIndex++;
       }
     }
-    changeSupport.firePropertyChange(UPDATE, 99, 100);
+    changeSupport.firePropertyChange(UPDATE, MAX_PROGRESS - 1,
+            MAX_PROGRESS);
   }
 
   public void setRedraw(boolean b)