Merge branch 'releases/Release_2_10_2b1_Branch' of http://source.jalview.org/git...
[jalview.git] / src / jalview / gui / OverviewPanel.java
index 1c01108..51d7a84 100755 (executable)
@@ -20,6 +20,8 @@
  */
 package jalview.gui;
 
+import jalview.bin.Cache;
+import jalview.renderer.OverviewRenderer;
 import jalview.util.MessageManager;
 import jalview.util.Platform;
 import jalview.viewmodel.OverviewDimensions;
@@ -51,8 +53,8 @@ import javax.swing.SwingUtilities;
  * @author $author$
  * @version $Revision$
  */
-public class OverviewPanel extends JPanel implements Runnable,
-        ViewportListenerI
+public class OverviewPanel extends JPanel
+        implements Runnable, ViewportListenerI
 {
   private OverviewDimensions od;
 
@@ -68,6 +70,8 @@ public class OverviewPanel extends JPanel implements Runnable,
 
   private boolean draggingBox = false;
 
+  private ProgressPanel progressPanel;
+
   /**
    * Creates a new OverviewPanel object.
    * 
@@ -79,30 +83,62 @@ public class OverviewPanel extends JPanel implements Runnable,
     this.av = alPanel.av;
     this.ap = alPanel;
 
-    od = new OverviewDimensionsShowHidden(av.getRanges(),
-            (av.isShowAnnotation() && av
-                    .getAlignmentConservationAnnotation() != null));
-
-    setSize(od.getWidth(), od.getHeight());
+    showHidden = Cache.getDefault(Preferences.SHOW_OV_HIDDEN_AT_START,
+            true);
+    if (showHidden)
+    {
+      od = new OverviewDimensionsShowHidden(av.getRanges(),
+            (av.isShowAnnotation()
+                    && av.getAlignmentConservationAnnotation() != null));
+    }
+    else
+    {
+      od = new OverviewDimensionsHideHidden(av.getRanges(),
+              (av.isShowAnnotation()
+                      && av.getAlignmentConservationAnnotation() != null));
+    }
 
-    oviewCanvas = new OverviewCanvas(od, av);
     setLayout(new BorderLayout());
+    progressPanel = new ProgressPanel(OverviewRenderer.UPDATE,
+            MessageManager.getString("label.oview_calc"), getWidth());
+    this.add(progressPanel, BorderLayout.SOUTH);
+    oviewCanvas = new OverviewCanvas(od, av, progressPanel);
+
     add(oviewCanvas, BorderLayout.CENTER);
 
     av.getRanges().addPropertyChangeListener(this);
 
+    // without this the overview window does not size to fit the overview canvas
+    setPreferredSize(new Dimension(od.getWidth(), od.getHeight()));
+
     addComponentListener(new ComponentAdapter()
     {
       @Override
       public void componentResized(ComponentEvent evt)
       {
-        if ((getWidth() != od.getWidth())
-                || (getHeight() != (od.getHeight())))
+        // Resize is called on the initial display of the overview.
+        // This code adjusts sizes to account for the progress bar if it has not
+        // already been accounted for, which triggers another resize call for
+        // the correct sizing, at which point the overview image is updated.
+        // (This avoids a double recalculation of the image.)
+        if (getWidth() == od.getWidth() && getHeight() == od.getHeight()
+                + progressPanel.getHeight())
         {
           updateOverviewImage();
-          setBoxPosition();
+        }
+        else
+        {
+          if ((getWidth() > 0) && (getHeight() > 0))
+          {
+            od.setWidth(getWidth());
+            od.setHeight(getHeight() - progressPanel.getHeight());
+          }
+
+          setPreferredSize(new Dimension(od.getWidth(),
+                  od.getHeight() + progressPanel.getHeight()));
         }
       }
+
     });
 
     addMouseMotionListener(new MouseMotionAdapter()
@@ -110,24 +146,22 @@ public class OverviewPanel extends JPanel implements Runnable,
       @Override
       public void mouseDragged(MouseEvent evt)
       {
-        if (!SwingUtilities.isRightMouseButton(evt)
-                && !av.getWrapAlignment())
+        if (!SwingUtilities.isRightMouseButton(evt))
         {
           if (draggingBox)
           {
             // set the mouse position as a fixed point in the box
             // and drag relative to that position
-            od.adjustViewportFromMouse(evt.getX(),
-                    evt.getY(), av.getAlignment().getHiddenSequences(),
+            od.adjustViewportFromMouse(evt.getX(), evt.getY(),
+                    av.getAlignment().getHiddenSequences(),
                     av.getAlignment().getHiddenColumns());
           }
           else
           {
-            od.updateViewportFromMouse(evt.getX(), evt.getY(), av
-                  .getAlignment().getHiddenSequences(), av.getAlignment()
-                  .getHiddenColumns());
+            od.updateViewportFromMouse(evt.getX(), evt.getY(),
+                    av.getAlignment().getHiddenSequences(),
+                    av.getAlignment().getHiddenColumns());
           }
-
         }
       }
 
@@ -159,13 +193,14 @@ public class OverviewPanel extends JPanel implements Runnable,
             showPopupMenu(evt);
           }
         }
-        else if (!av.getWrapAlignment())
+        else
         {
+          // don't do anything if the mouse press is in the overview's box
+          // (wait to see if it's a drag instead)
+          // otherwise update the viewport
           if (!od.isPositionInBox(evt.getX(), evt.getY()))
           {
-            // don't do anything if the mouse press is in the overview's box
-            // (wait to see if it's a drag instead)
-            // otherwise update the viewport
+            draggingBox = false;
             od.updateViewportFromMouse(evt.getX(), evt.getY(),
                     av.getAlignment().getHiddenSequences(),
                     av.getAlignment().getHiddenColumns());
@@ -181,15 +216,6 @@ public class OverviewPanel extends JPanel implements Runnable,
       }
 
       @Override
-      public void mouseReleased(MouseEvent evt)
-      {
-        if (draggingBox)
-        {
-          draggingBox = false;
-        }
-      }
-
-      @Override
       public void mouseClicked(MouseEvent evt)
       {
         if (SwingUtilities.isRightMouseButton(evt))
@@ -197,11 +223,7 @@ public class OverviewPanel extends JPanel implements Runnable,
           showPopupMenu(evt);
         }
       }
-
-
     });
-
-    updateOverviewImage();
   }
 
   /*
@@ -238,15 +260,15 @@ public class OverviewPanel extends JPanel implements Runnable,
     {
       showHidden = false;
       od = new OverviewDimensionsHideHidden(av.getRanges(),
-              (av.isShowAnnotation() && av
-                      .getAlignmentConservationAnnotation() != null));
+              (av.isShowAnnotation()
+                      && av.getAlignmentConservationAnnotation() != null));
     }
     else
     {
       showHidden = true;
       od = new OverviewDimensionsShowHidden(av.getRanges(),
-              (av.isShowAnnotation() && av
-                      .getAlignmentConservationAnnotation() != null));
+              (av.isShowAnnotation()
+                      && av.getAlignmentConservationAnnotation() != null));
     }
     oviewCanvas.resetOviewDims(od);
     updateOverviewImage();
@@ -258,13 +280,22 @@ public class OverviewPanel extends JPanel implements Runnable,
    */
   public void updateOverviewImage()
   {
+    if (oviewCanvas == null)
+    {
+      /*
+       * panel has been disposed
+       */
+      return;
+    }
+
     if ((getWidth() > 0) && (getHeight() > 0))
     {
       od.setWidth(getWidth());
-      od.setHeight(getHeight());
+      od.setHeight(getHeight() - progressPanel.getHeight());
     }
     
-    setPreferredSize(new Dimension(od.getWidth(), od.getHeight()));
+    setPreferredSize(new Dimension(od.getWidth(),
+            od.getHeight() + progressPanel.getHeight()));
 
     if (oviewCanvas.restartDraw())
     {
@@ -275,16 +306,20 @@ public class OverviewPanel extends JPanel implements Runnable,
     thread.start();
     repaint();
 
+    
   }
 
   @Override
   public void run()
   {
-    oviewCanvas.draw(av.isShowSequenceFeatures(),
-            (av.isShowAnnotation() && av
-                    .getAlignmentConservationAnnotation() != null), ap
-                    .getSeqPanel().seqCanvas.getFeatureRenderer());
-    setBoxPosition();
+    if (oviewCanvas != null)
+    {
+      oviewCanvas.draw(av.isShowSequenceFeatures(),
+              (av.isShowAnnotation()
+                      && av.getAlignmentConservationAnnotation() != null),
+              ap.getSeqPanel().seqCanvas.getFeatureRenderer());
+      setBoxPosition();
+    }
   }
 
   /**
@@ -294,9 +329,12 @@ public class OverviewPanel extends JPanel implements Runnable,
    */
   private void setBoxPosition()
   {
-    od.setBoxPosition(av.getAlignment().getHiddenSequences(), av
-            .getAlignment().getHiddenColumns());
-    repaint();
+    if (od != null)
+    {
+      od.setBoxPosition(av.getAlignment().getHiddenSequences(),
+              av.getAlignment().getHiddenColumns());
+      repaint();
+    }
   }
 
   @Override
@@ -304,4 +342,23 @@ public class OverviewPanel extends JPanel implements Runnable,
   {
     setBoxPosition();
   }
+
+  /**
+   * Removes this object as a property change listener, and nulls references
+   */
+  protected void dispose()
+  {
+    try
+    {
+      av.getRanges().removePropertyChangeListener(this);
+      oviewCanvas.dispose();
+    } finally
+    {
+      progressPanel = null;
+      av = null;
+      oviewCanvas = null;
+      ap = null;
+      od = null;
+    }
+  }
 }