JAL-3383 minor code tidying and commenting
[jalview.git] / src / jalview / gui / OverviewPanel.java
index 43b4310..9245104 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;
@@ -55,24 +57,29 @@ import javax.swing.SwingUtilities;
  * @author $author$
  * @version $Revision$
  */
+@SuppressWarnings("serial")
 public class OverviewPanel extends JPanel
         implements Runnable, ViewportListenerI
 {
-  private OverviewDimensions od;
+  protected OverviewDimensions od;
 
-  private OverviewCanvas oviewCanvas;
+  OverviewCanvas canvas;
 
-  private AlignViewport av;
+  protected AlignViewportI av;
 
-  private AlignmentPanel ap;
+  AlignmentViewPanel ap;
 
-  private JCheckBoxMenuItem displayToggle;
+  protected JCheckBoxMenuItem displayToggle;
 
-  private boolean showHidden = true;
+  protected boolean showHidden = true;
 
-  private boolean draggingBox = false;
+  protected boolean draggingBox = false;
 
-  private ProgressPanel progressPanel;
+  protected ProgressPanel progressPanel;
+
+  private Dimension dim;
+
+  private boolean showProgress = !Platform.isJS(); // Jalview.getInstance().getShowStatus()
 
   /**
    * Creates a new OverviewPanel object.
@@ -80,67 +87,36 @@ public class OverviewPanel extends JPanel
    * @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,
-            true);
-    if (showHidden)
-    {
-      od = new OverviewDimensionsShowHidden(av.getRanges(),
-            (av.isShowAnnotation()
-                    && av.getAlignmentConservationAnnotation() != null));
-    }
-    else
-    {
-      od = new OverviewDimensionsHideHidden(av.getRanges(),
-              (av.isShowAnnotation()
-                      && av.getAlignmentConservationAnnotation() != null));
-    }
-
+            false);
+    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()));
-
     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();
       }
-
     });
 
     addMouseMotionListener(new MouseMotionAdapter()
@@ -172,13 +148,18 @@ public class OverviewPanel extends JPanel
       {
         if (od.isPositionInBox(evt.getX(), evt.getY()))
         {
-          // display drag cursor at mouse position
-          setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
+          /*
+           * using HAND_CURSOR rather than DRAG_CURSOR 
+           * as the latter is not supported on Mac
+           */
+          getParent().setCursor(
+                  Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
         }
         else
         {
           // reset cursor
-          setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
+          getParent().setCursor(
+                  Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
         }
       }
     });
@@ -188,32 +169,37 @@ public class OverviewPanel extends JPanel
       @Override
       public void mousePressed(MouseEvent evt)
       {
+        if (Platform.isWinRightButton(evt))
+        {
+          showPopupMenu(evt);
+          return;
+        }
         if (SwingUtilities.isRightMouseButton(evt))
         {
-          if (!Platform.isAMac())
-          {
-            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;
+
+          // 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
         {
-          // 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;
-            od.updateViewportFromMouse(evt.getX(), evt.getY(),
-                    av.getAlignment().getHiddenSequences(),
-                    av.getAlignment().getHiddenColumns());
-          }
-          else
-          {
-            draggingBox = true;
-            od.setDragPoint(evt.getX(), evt.getY(),
-                    av.getAlignment().getHiddenSequences(),
-                    av.getAlignment().getHiddenColumns());
-          }
+          draggingBox = true;
+          od.setDragPoint(evt.getX(), evt.getY(),
+                  av.getAlignment().getHiddenSequences(),
+                  av.getAlignment().getHiddenColumns());
         }
       }
 
@@ -225,13 +211,72 @@ public class OverviewPanel extends JPanel
           showPopupMenu(evt);
         }
       }
+
+      @Override
+      public void mouseReleased(MouseEvent evt)
+      {
+        draggingBox = false;
+      }
     });
   }
 
-  /*
+  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();
+      }
+    }
+  }
+
+  /**
+   * 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
+    {
+      od = new OverviewDimensionsHideHidden(av.getRanges(), showAnnotation,
+              dim);
+    }
+
+  }
+
+  /**
    * Displays the popup menu and acts on user input
    */
-  private void showPopupMenu(MouseEvent e)
+  protected void showPopupMenu(MouseEvent e)
   {
     JPopupMenu popup = new JPopupMenu();
     ActionListener menuListener = new ActionListener()
@@ -253,36 +298,38 @@ public class OverviewPanel extends JPanel
     popup.show(this, e.getX(), e.getY());
   }
 
-  /*
-   * Toggle overview display between showing hidden columns and hiding hidden columns
+  /**
+   * Toggle overview display between showing hidden columns and hiding hidden
+   * columns
    */
-  private void toggleHiddenColumns()
+  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
@@ -290,37 +337,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());
     }
   }
 
@@ -333,6 +379,7 @@ public class OverviewPanel extends JPanel
   {
     if (od != null)
     {
+      od.updateBox();
       int oldX = od.getBoxX();
       int oldY = od.getBoxY();
       int oldWidth = od.getBoxWidth();
@@ -373,14 +420,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
@@ -388,7 +436,7 @@ public class OverviewPanel extends JPanel
     {
       progressPanel = null;
       av = null;
-      oviewCanvas = null;
+      canvas = null;
       ap = null;
       od = null;
     }