Merge branch 'bug/JAL-2621' into develop
[jalview.git] / src / jalview / gui / OverviewPanel.java
index d15ec40..9d0a55d 100755 (executable)
  */
 package jalview.gui;
 
-import jalview.renderer.AnnotationRenderer;
+import jalview.bin.Cache;
+import jalview.renderer.OverviewRenderer;
+import jalview.util.MessageManager;
+import jalview.util.Platform;
 import jalview.viewmodel.OverviewDimensions;
+import jalview.viewmodel.OverviewDimensionsHideHidden;
+import jalview.viewmodel.OverviewDimensionsShowHidden;
+import jalview.viewmodel.ViewportListenerI;
 
-import java.awt.Color;
+import java.awt.BorderLayout;
+import java.awt.Cursor;
 import java.awt.Dimension;
-import java.awt.Graphics;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
 import java.awt.event.ComponentAdapter;
 import java.awt.event.ComponentEvent;
 import java.awt.event.MouseAdapter;
 import java.awt.event.MouseEvent;
 import java.awt.event.MouseMotionAdapter;
-import java.awt.image.BufferedImage;
+import java.beans.PropertyChangeEvent;
+import java.beans.PropertyVetoException;
 
+import javax.swing.JCheckBoxMenuItem;
+import javax.swing.JInternalFrame;
 import javax.swing.JPanel;
+import javax.swing.JPopupMenu;
+import javax.swing.SwingUtilities;
 
 /**
  * Panel displaying an overview of the full alignment, with an interactive box
@@ -42,34 +55,24 @@ import javax.swing.JPanel;
  * @author $author$
  * @version $Revision$
  */
-public class OverviewPanel extends JPanel implements Runnable
+public class OverviewPanel extends JPanel
+        implements Runnable, ViewportListenerI
 {
-  private static final Color TRANS_GREY = new Color(100, 100, 100, 25);
-
-  private final AnnotationRenderer renderer = new AnnotationRenderer();
-
   private OverviewDimensions od;
 
-  private BufferedImage miniMe;
-
-  private BufferedImage lastMiniMe = null;
+  private OverviewCanvas oviewCanvas;
 
   private AlignViewport av;
 
   private AlignmentPanel ap;
 
-  //
-  private boolean resizing = false;
+  private JCheckBoxMenuItem displayToggle;
 
-  // This is set true if the user resizes whilst
-  // the overview is being calculated
-  private boolean resizeAgain = false;
+  private boolean showHidden = true;
 
-  // Can set different properties in this seqCanvas than
-  // main visible SeqCanvas
-  private SequenceRenderer sr;
+  private boolean draggingBox = false;
 
-  private jalview.renderer.seqfeatures.FeatureRenderer fr;
+  private ProgressPanel progressPanel;
 
   /**
    * Creates a new OverviewPanel object.
@@ -81,33 +84,63 @@ public class OverviewPanel extends JPanel implements Runnable
   {
     this.av = alPanel.av;
     this.ap = alPanel;
-    setLayout(null);
 
-    sr = new SequenceRenderer(av);
-    sr.renderGaps = false;
-    sr.forOverview = true;
-    fr = new FeatureRenderer(alPanel);
-
-    boolean showAnnotation = false;
-    // TODO: in applet this was getSequenceConsensusHash()
-    // check if it makes any functional difference: hconsensus or conservation
-    if (av.getAlignmentConservationAnnotation() == null)
+    showHidden = Cache.getDefault(Preferences.SHOW_OV_HIDDEN_AT_START,
+            false);
+    if (showHidden)
     {
-      showAnnotation = true;
+      od = new OverviewDimensionsShowHidden(av.getRanges(),
+            (av.isShowAnnotation()
+                    && av.getAlignmentConservationAnnotation() != null));
     }
-    od = new OverviewDimensions(av, showAnnotation);
+    else
+    {
+      od = new OverviewDimensionsHideHidden(av.getRanges(),
+              (av.isShowAnnotation()
+                      && av.getAlignmentConservationAnnotation() != null));
+    }
+
+    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();
         }
+        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()
@@ -115,14 +148,45 @@ public class OverviewPanel extends JPanel implements Runnable
       @Override
       public void mouseDragged(MouseEvent evt)
       {
-        if (!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(),
+                    av.getAlignment().getHiddenColumns());
+          }
+          else
+          {
+            od.updateViewportFromMouse(evt.getX(), evt.getY(),
+                    av.getAlignment().getHiddenSequences(),
+                    av.getAlignment().getHiddenColumns());
+          }
+        }
+      }
+
+      @Override
+      public void mouseMoved(MouseEvent evt)
+      {
+        if (od.isPositionInBox(evt.getX(), evt.getY()))
+        {
+          /*
+           * using HAND_CURSOR rather than DRAG_CURSOR 
+           * as the latter is not supported on Mac
+           */
+          getParent().setCursor(
+                  Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
+        }
+        else
         {
-          // TODO: feature: jv2.5 detect shift drag and update selection from
-          // it.
-          od.updateViewportFromMouse(evt.getX(), evt.getY());
-          ap.setScrollValues(od.getScrollCol(), od.getScrollRow());
+          // reset cursor
+          getParent().setCursor(
+                  Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
         }
       }
+
     });
 
     addMouseListener(new MouseAdapter()
@@ -130,15 +194,106 @@ public class OverviewPanel extends JPanel implements Runnable
       @Override
       public void mousePressed(MouseEvent evt)
       {
-        if (!av.getWrapAlignment())
+        if (SwingUtilities.isRightMouseButton(evt))
+        {
+          if (!Platform.isAMac())
+          {
+            showPopupMenu(evt);
+          }
+        }
+        else
         {
-          od.updateViewportFromMouse(evt.getX(), evt.getY());
-          ap.setScrollValues(od.getScrollCol(), od.getScrollRow());
+          // 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
+          {
+            draggingBox = true;
+            od.setDragPoint(evt.getX(), evt.getY(),
+                    av.getAlignment().getHiddenSequences(),
+                    av.getAlignment().getHiddenColumns());
+          }
         }
       }
+
+      @Override
+      public void mouseClicked(MouseEvent evt)
+      {
+        if (SwingUtilities.isRightMouseButton(evt))
+        {
+          showPopupMenu(evt);
+        }
+      }
+
+      @Override
+      public void mouseReleased(MouseEvent evt)
+      {
+        draggingBox = false;
+      }
+
     });
+  }
 
+  /*
+   * Displays the popup menu and acts on user input
+   */
+  private void showPopupMenu(MouseEvent e)
+  {
+    JPopupMenu popup = new JPopupMenu();
+    ActionListener menuListener = new ActionListener()
+    {
+      @Override
+      public void actionPerformed(ActionEvent event)
+      {
+        // switch on/off the hidden columns view
+        toggleHiddenColumns();
+        displayToggle.setSelected(showHidden);
+      }
+    };
+    displayToggle = new JCheckBoxMenuItem(
+            MessageManager.getString("label.togglehidden"));
+    displayToggle.setEnabled(true);
+    displayToggle.setSelected(showHidden);
+    popup.add(displayToggle);
+    displayToggle.addActionListener(menuListener);
+    popup.show(this, e.getX(), e.getY());
+  }
+
+  /*
+   * Toggle overview display between showing hidden columns and hiding hidden columns
+   */
+  private 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);
     updateOverviewImage();
+    setBoxPosition();
   }
 
   /**
@@ -146,224 +301,115 @@ public class OverviewPanel extends JPanel implements Runnable
    */
   public void updateOverviewImage()
   {
-    if (resizing)
+    if (oviewCanvas == null)
     {
-      resizeAgain = true;
+      /*
+       * panel has been disposed
+       */
       return;
     }
 
-    resizing = true;
-
     if ((getWidth() > 0) && (getHeight() > 0))
     {
-      od.setWidth(getWidth()); // width = getWidth();
-      od.setHeight(getHeight()); // sequencesHeight = getHeight() - graphHeight;
+      od.setWidth(getWidth());
+      od.setHeight(getHeight() - progressPanel.getHeight());
     }
+    
+    setPreferredSize(new Dimension(od.getWidth(),
+            od.getHeight() + progressPanel.getHeight()));
 
-    setPreferredSize(new Dimension(od.getWidth(), od.getHeight()));
+    if (oviewCanvas.restartDraw())
+    {
+      return;
+    }
 
     Thread thread = new Thread(this);
     thread.start();
     repaint();
+
+    
   }
 
   @Override
   public void run()
   {
-    miniMe = null;
-
-    if (av.isShowSequenceFeatures())
-    {
-      fr.transferSettings(ap.getSeqPanel().seqCanvas.getFeatureRenderer());
-    }
-
-    // why do we need to set preferred size again? was set in
-    // updateOverviewImage
-    setPreferredSize(new Dimension(od.getWidth(), od.getHeight()));
-
-    miniMe = new BufferedImage(od.getWidth(), od.getHeight(),
-            BufferedImage.TYPE_INT_RGB);
-
-    Graphics mg = miniMe.getGraphics();
-    mg.setColor(Color.orange);
-    mg.fillRect(0, 0, od.getWidth(), miniMe.getHeight());
-
-    // calculate sampleCol and sampleRow
-    // alignment width is max number of residues/bases
-    // alignment height is number of sequences
-    int alwidth = av.getAlignment().getWidth();
-    int alheight = av.getAlignment().getAbsoluteHeight();
-
-    // sampleCol or sampleRow is the width/height allocated to each residue
-    // in particular, sometimes we may need more than one row/col of the
-    // BufferedImage allocated
-    // sampleCol is how much of a residue to assign to each pixel
-    // sampleRow is how many sequences to assign to each pixel
-    float sampleCol = alwidth / (float) od.getWidth();
-    float sampleRow = alheight / (float) od.getSequencesHeight();
-
-    buildImage(sampleRow, sampleCol);
-
-    if (av.getAlignmentConservationAnnotation() != null)
+    if (oviewCanvas != null)
     {
-      renderer.updateFromAlignViewport(av);
-      for (int col = 0; col < od.getWidth() && !resizeAgain; col++)
-      {
-        mg.translate(col, od.getSequencesHeight());
-        renderer.drawGraph(mg, av.getAlignmentConservationAnnotation(),
-                av.getAlignmentConservationAnnotation().annotations,
-                (int) (sampleCol) + 1, od.getGraphHeight(),
-                (int) (col * sampleCol), (int) (col * sampleCol) + 1);
-        mg.translate(-col, -od.getSequencesHeight());
-
-      }
+      oviewCanvas.draw(av.isShowSequenceFeatures(),
+              (av.isShowAnnotation()
+                      && av.getAlignmentConservationAnnotation() != null),
+              ap.getSeqPanel().seqCanvas.getFeatureRenderer());
+      setBoxPosition();
     }
-    System.gc();
-
-    resizing = false;
-
-    if (resizeAgain)
-    {
-      resizeAgain = false;
-      updateOverviewImage();
-    }
-    else
-    {
-      lastMiniMe = miniMe;
-    }
-
-    setBoxPosition();
   }
 
-  private void buildImage(float sampleRow, float sampleCol)
+  /**
+   * Update the overview panel box when the associated alignment panel is
+   * changed
+   * 
+   */
+  private void setBoxPositionOnly()
   {
-    int lastcol = -1;
-    int lastrow = -1;
-    int color = Color.white.getRGB();
-
-    jalview.datamodel.SequenceI seq = null;
-
-    final boolean hasHiddenCols = av.hasHiddenColumns();
-    boolean hiddenRow = false;
-    // get hidden row and hidden column map once at beginning.
-    // clone featureRenderer settings to avoid race conditions... if state is
-    // updated just need to refresh again
-    for (int row = 0; row < od.getSequencesHeight() && !resizeAgain; row++)
+    if (od != null)
     {
-      boolean doCopy = true;
-      int currentrow = (int) (row * sampleRow);
-      if (currentrow != lastrow)
-      {
-        doCopy = false;
-
-        lastrow = currentrow;
-
-        // get the sequence which would be at alignment index 'lastrow' if no
-        // columns were hidden, and determine whether it is hidden or not
-        hiddenRow = av.getAlignment().isHidden(lastrow);
-        seq = av.getAlignment().getSequenceAtAbsoluteIndex(lastrow);
-      }
-
-      if (seq == null)
-      {
-        System.out.println(lastrow + " null");
-        continue;
-      }
-
-      for (int col = 0; col < od.getWidth() && !resizeAgain; col++)
-      {
-        if (doCopy)
-        {
-          color = miniMe.getRGB(col, row - 1);
-        }
-        else if ((int) (col * sampleCol) != lastcol
-                || (int) (row * sampleRow) != lastrow)
-        {
-          lastcol = (int) (col * sampleCol);
-          color = getColumnColourFromSequence(seq, hiddenRow, hasHiddenCols,
-                  lastcol);
-        }
-        // else if ((int) (col * sampleCol) == lastcol && (int) (row *
-        // sampleRow) == lastrow))
-        // we just use the color we already have , so don't need to set it
-
-        miniMe.setRGB(col, row, color);
-      }
+      int oldX = od.getBoxX();
+      int oldY = od.getBoxY();
+      int oldWidth = od.getBoxWidth();
+      int oldHeight = od.getBoxHeight();
+      od.setBoxPosition(av.getAlignment().getHiddenSequences(),
+              av.getAlignment().getHiddenColumns());
+      repaint(oldX - 1, oldY - 1, oldWidth + 2, oldHeight + 2);
+      repaint(od.getBoxX(), od.getBoxY(), od.getBoxWidth(),
+              od.getBoxHeight());
     }
   }
 
-  /*
-   * Find the colour of a sequence at a specified column position
-   */
-  private int getColumnColourFromSequence(jalview.datamodel.SequenceI seq,
-          boolean hiddenRow, boolean hasHiddenCols, int lastcol)
+  private void setBoxPosition()
   {
-    int color;
-
-    if (seq.getLength() > lastcol)
+    if (od != null)
     {
-      color = sr.getResidueBoxColour(seq, lastcol).getRGB();
-
-      if (av.isShowSequenceFeatures())
-      {
-        color = fr.findFeatureColour(color, seq, lastcol);
-      }
+      od.setBoxPosition(av.getAlignment().getHiddenSequences(),
+              av.getAlignment().getHiddenColumns());
+      repaint();
     }
-    else
-    {
-      color = Color.white.getRGB(); // White
-    }
-
-    if (hiddenRow
-            || (hasHiddenCols && !av.getColumnSelection()
-                    .isVisible(lastcol)))
-    {
-      color = new Color(color).darker().darker().getRGB();
-    }
-
-    return color;
   }
 
-  /**
-   * Update the overview panel box when the associated alignment panel is
-   * changed
-   * 
-   */
-  public void setBoxPosition()
+  @Override
+  public void propertyChange(PropertyChangeEvent evt)
   {
-    od.setBoxPosition();
-    repaint();
+    setBoxPositionOnly();
   }
 
-
-  @Override
-  public void paintComponent(Graphics g)
+  /**
+   * Removes this object as a property change listener, and nulls references
+   */
+  protected void dispose()
   {
-    if (resizing || resizeAgain)
+    try
     {
-      if (lastMiniMe == null)
+      if (av != null)
       {
-        g.setColor(Color.white);
-        g.fillRect(0, 0, getWidth(), getHeight());
+        av.getRanges().removePropertyChangeListener(this);
       }
-      else
-      {
-        g.drawImage(lastMiniMe, 0, 0, getWidth(), getHeight(), this);
-      }
-      g.setColor(TRANS_GREY);
-      g.fillRect(0, 0, getWidth(), getHeight());
-    }
-    else if (lastMiniMe != null)
+
+      oviewCanvas.dispose();
+
+      /*
+       * close the parent frame (which also removes it from the
+       * Desktop Windows menu)
+       */
+      ((JInternalFrame) SwingUtilities.getAncestorOfClass(
+              JInternalFrame.class, (this))).setClosed(true);
+    } catch (PropertyVetoException e)
     {
-      g.drawImage(lastMiniMe, 0, 0, this);
-      if (lastMiniMe != miniMe)
-      {
-        g.setColor(TRANS_GREY);
-        g.fillRect(0, 0, getWidth(), getHeight());
-      }
+      // ignore
+    } finally
+    {
+      progressPanel = null;
+      av = null;
+      oviewCanvas = null;
+      ap = null;
+      od = null;
     }
-    // TODO: render selected regions
-    g.setColor(Color.red);
-    od.drawBox(g);
   }
 }