JAL-3210 Improvements to eclipse detection. New src tree and SwingJS updated from...
[jalview.git] / src / jalview / gui / OverviewPanel.java
index a5aaf7c..5694c3d 100755 (executable)
@@ -20,6 +20,8 @@
  */
 package jalview.gui;
 
+import jalview.api.AlignViewportI;
+import jalview.api.AlignmentViewPanel;
 import jalview.bin.Cache;
 import jalview.renderer.OverviewRenderer;
 import jalview.util.MessageManager;
@@ -61,11 +63,11 @@ public class OverviewPanel extends JPanel
 {
   protected OverviewDimensions od;
 
-  private OverviewCanvas oviewCanvas;
+  OverviewCanvas canvas;
 
-  protected AlignViewport av;
+  protected AlignViewportI av;
 
-  private AlignmentPanel ap;
+  AlignmentViewPanel ap;
 
   protected JCheckBoxMenuItem displayToggle;
 
@@ -75,71 +77,50 @@ public class OverviewPanel extends JPanel
 
   protected ProgressPanel progressPanel;
 
+  private Dimension dim;
+
+  private boolean showProgress = !Platform.isJS(); // Jalview.getInstance().getShowStatus()
+
   /**
    * Creates a new OverviewPanel object.
    * 
    * @param alPanel
    *          The alignment panel which is shown in the overview panel
    */
-  public OverviewPanel(AlignmentPanel alPanel)
+  public OverviewPanel(AlignmentViewPanel alPanel, Dimension dim)
   {
-    this.av = alPanel.av;
+    this.av = alPanel.getAlignViewport();
     this.ap = alPanel;
+    this.dim = dim;
 
     showHidden = Cache.getDefault(Preferences.SHOW_OV_HIDDEN_AT_START,
             false);
-    if (showHidden)
-    {
-      od = new OverviewDimensionsShowHidden(av.getRanges(),
-            (av.isShowAnnotation()
-                    && av.getAlignmentConservationAnnotation() != null));
-    }
-    else
-    {
-      od = new OverviewDimensionsHideHidden(av.getRanges(),
-              (av.isShowAnnotation()
-                      && av.getAlignmentConservationAnnotation() != null));
-    }
-
+    createOverviewDimensions();
     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);
+    if (showProgress) // BH 2019
+    {
+      add(progressPanel, BorderLayout.SOUTH);
+    }
+    canvas = new OverviewCanvas(this, od, av,
+            showProgress ? progressPanel : null);
+    canvas.setPreferredSize(canvas.getSize());
+    add(canvas, 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()));
-    
+    // BH - no,no! - This does not include the progressPanel!
+    // BH the problem was that OverviewCanvas.setPreferredSize() had not been set.
+    // setPreferredSize(new Dimension(od.getWidth(), od.getHeight()));
+
     addComponentListener(new ComponentAdapter()
     {
       @Override
       public void componentResized(ComponentEvent evt)
       {
-        // 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();
-        }
-        else
-        {
-          if ((getWidth() > 0) && (getHeight() > 0))
-          {
-            od.setWidth(getWidth());
-            od.setHeight(getHeight() - progressPanel.getHeight());
-          }
-
-          setPreferredSize(new Dimension(od.getWidth(),
-                  od.getHeight() + progressPanel.getHeight()));
-        }
+        resizePanel();
       }
 
     });
@@ -195,37 +176,39 @@ public class OverviewPanel extends JPanel
       @Override
       public void mousePressed(MouseEvent evt)
       {
-         
-       if (Platform.isWinRightButton(evt)) {
-               showPopupMenu(evt);
-               return;
-       }
-        if (SwingUtilities.isRightMouseButton(evt)) {
-               return;
+
+        if (Platform.isWinRightButton(evt))
+        {
+          showPopupMenu(evt);
+          return;
         }
-          // 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()))
-          {
-            draggingBox = false;
+        if (SwingUtilities.isRightMouseButton(evt))
+        {
+          return;
+        }
+        // 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()))
+        {
+          draggingBox = false;
 
-            // display drag cursor at mouse position
-            setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
+          // display drag cursor at mouse position
+          setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
 
-            od.updateViewportFromMouse(evt.getX(), evt.getY(),
-                    av.getAlignment().getHiddenSequences(),
-                    av.getAlignment().getHiddenColumns());
-            getParent().setCursor(
-                    Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
-          }
-          else
-          {
-            draggingBox = true;
-            od.setDragPoint(evt.getX(), evt.getY(),
-                    av.getAlignment().getHiddenSequences(),
-                    av.getAlignment().getHiddenColumns());
-          }
+          od.updateViewportFromMouse(evt.getX(), evt.getY(),
+                  av.getAlignment().getHiddenSequences(),
+                  av.getAlignment().getHiddenColumns());
+          getParent().setCursor(
+                  Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
+        }
+        else
+        {
+          draggingBox = true;
+          od.setDragPoint(evt.getX(), evt.getY(),
+                  av.getAlignment().getHiddenSequences(),
+                  av.getAlignment().getHiddenColumns());
+        }
       }
 
       @Override
@@ -245,14 +228,71 @@ public class OverviewPanel extends JPanel
 
     });
 
-    /*
-     * Javascript does not call componentResized on initial display,
-     * so do the update here
-     */
-    if (Platform.isJS())
+    // /*
+    // * Javascript does not call componentResized on initial display,
+    // * so do the update here
+    // */
+    // if (Platform.isJS())
+    // {
+    // updateOverviewImage();
+    // }
+  }
+
+  protected void resizePanel()
+  {
+    int ph = (progressPanel.getParent() == null ? 0
+            : progressPanel.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() + ph)
+    {
+      if (canvas.lastMiniMe == null)
+      {
+        updateOverviewImage();
+      }
+    }
+    else
+    {
+      int w = getWidth();
+      int h = getHeight();
+      if ((w > 0) && (h > 0))
+      {
+        if (dim != null)
+        {
+          dim.setSize(w, h - ph);
+        }
+        od.setWidth(w);
+        od.setHeight(h - ph);
+        updateOverviewImage();
+      }
+      // BH 2019.07.29 this is unnecessary -- it is what layout managers are
+      // for:
+      // setPreferredSize(new Dimension(od.getWidth(), od.getHeight() +
+      // ph));
+    }
+  }
+
+  /**
+   * Create the appropriate type of OverViewDimensions, with the desired size.
+   */
+  private void createOverviewDimensions()
+  {
+    boolean showAnnotation = (av.isShowAnnotation()
+            && av.getAlignmentConservationAnnotation() != null);
+    if (showHidden)
+    {
+      od = new OverviewDimensionsShowHidden(av.getRanges(), showAnnotation,
+              dim);
+    }
+    else
     {
-      updateOverviewImage();
+      od = new OverviewDimensionsHideHidden(av.getRanges(), showAnnotation,
+              dim);
     }
+
   }
 
   /*
@@ -285,31 +325,32 @@ public class OverviewPanel extends JPanel
    */
   protected void toggleHiddenColumns()
   {
-    if (showHidden)
-    {
-      showHidden = false;
-      od = new OverviewDimensionsHideHidden(av.getRanges(),
-              (av.isShowAnnotation()
-                      && av.getAlignmentConservationAnnotation() != null));
-    }
-    else
-    {
-      showHidden = true;
-      od = new OverviewDimensionsShowHidden(av.getRanges(),
-              (av.isShowAnnotation()
-                      && av.getAlignmentConservationAnnotation() != null));
-    }
-    oviewCanvas.resetOviewDims(od);
+    showHidden = !showHidden;
+    createOverviewDimensions();
+    canvas.resetOviewDims(od);
     updateOverviewImage();
     setBoxPosition();
   }
 
   /**
-   * Updates the overview image when the related alignment panel is updated
+   * Updates the overview image when the related alignment panel is updated.
+   * 
+   * Cases:
+   * 
+   * AlignFrame.setFeatureGroupState
+   * 
+   * AlignmentPanel.paintAlignment(true,...) (117 references)
+   * 
+   * OverviewPanel..componentResized() OverviewPanel.toggleHiddenColumns()
+   * 
+   * PopupMenu for action.reveal_sequences, action.reveal_all
+   * 
+   * SliderPanel.mouseReleased()
+   * 
    */
   public void updateOverviewImage()
   {
-    if (oviewCanvas == null)
+    if (canvas == null)
     {
       /*
        * panel has been disposed
@@ -317,37 +358,36 @@ public class OverviewPanel extends JPanel
       return;
     }
 
+    int ph = (progressPanel.getParent() == null ? 0
+            : progressPanel.getHeight());
+
     if ((getWidth() > 0) && (getHeight() > 0))
     {
       od.setWidth(getWidth());
-      od.setHeight(getHeight() - progressPanel.getHeight());
+      od.setHeight(getHeight() - ph);
     }
-    
-    setPreferredSize(new Dimension(od.getWidth(),
-            od.getHeight() + progressPanel.getHeight()));
 
-    if (oviewCanvas.restartDraw())
+    setPreferredSize(new Dimension(od.getWidth(), od.getHeight() + ph));
+
+    if (canvas.restartDraw())
     {
       return;
     }
 
     Thread thread = new Thread(this);
     thread.start();
-    repaint();
-
-    
   }
 
   @Override
   public void run()
   {
-    if (oviewCanvas != null)
+    if (canvas != null)
     {
-      oviewCanvas.draw(av.isShowSequenceFeatures(),
+      setBoxPosition();
+      canvas.draw(av.isShowSequenceFeatures(),
               (av.isShowAnnotation()
                       && av.getAlignmentConservationAnnotation() != null),
-              ap.getSeqPanel().seqCanvas.getFeatureRenderer());
-      setBoxPosition();
+              ap.getFeatureRenderer());
     }
   }
 
@@ -360,6 +400,7 @@ public class OverviewPanel extends JPanel
   {
     if (od != null)
     {
+      od.updateBox();
       int oldX = od.getBoxX();
       int oldY = od.getBoxY();
       int oldWidth = od.getBoxWidth();
@@ -400,14 +441,15 @@ public class OverviewPanel extends JPanel
         av.getRanges().removePropertyChangeListener(this);
       }
 
-      oviewCanvas.dispose();
+      canvas.dispose();
 
       /*
        * close the parent frame (which also removes it from the
        * Desktop Windows menu)
        */
-      ((JInternalFrame) SwingUtilities.getAncestorOfClass(
-              JInternalFrame.class, (this))).setClosed(true);
+      ((JInternalFrame) SwingUtilities
+              .getAncestorOfClass(JInternalFrame.class, (this)))
+                      .setClosed(true);
     } catch (PropertyVetoException e)
     {
       // ignore
@@ -415,7 +457,7 @@ public class OverviewPanel extends JPanel
     {
       progressPanel = null;
       av = null;
-      oviewCanvas = null;
+      canvas = null;
       ap = null;
       od = null;
     }