Merge branch 'bug/JAL-247pngShowHiddenSeqMarks' into trialMerge
[jalview.git] / src / jalview / gui / SeqCanvas.java
index 1753a64..b6b0d29 100755 (executable)
@@ -46,7 +46,7 @@ import java.beans.PropertyChangeEvent;
 import java.util.Iterator;
 import java.util.List;
 
-import javax.swing.JComponent;
+import javax.swing.JPanel;
 
 /**
  * The Swing component on which the alignment sequences, and annotations (if
@@ -54,7 +54,7 @@ import javax.swing.JComponent;
  * Wrapped mode, but not the scale above in Unwrapped mode.
  * 
  */
-public class SeqCanvas extends JComponent implements ViewportListenerI
+public class SeqCanvas extends JPanel implements ViewportListenerI
 {
   private static final String ZEROS = "0000000000";
 
@@ -89,7 +89,8 @@ public class SeqCanvas extends JComponent implements ViewportListenerI
 
   private int wrappedVisibleWidths; // number of wrapped widths displayed
 
-  private Graphics2D gg;
+  // Don't do this! Graphics handles are supposed to be transient
+  //private Graphics2D gg;
 
   /**
    * Creates a new SeqCanvas object.
@@ -196,7 +197,7 @@ public class SeqCanvas extends JComponent implements ViewportListenerI
     int yPos = ypos + charHeight;
     int startX = startx;
     int endX = endx;
-
+    
     if (av.hasHiddenColumns())
     {
       HiddenColumns hiddenColumns = av.getAlignment().getHiddenColumns();
@@ -232,6 +233,7 @@ public class SeqCanvas extends JComponent implements ViewportListenerI
         }
       }
 
+      
       /*
        * white fill the space for the scale
        */
@@ -256,6 +258,7 @@ public class SeqCanvas extends JComponent implements ViewportListenerI
         g.drawString(valueAsString, xOffset, y);
       }
     }
+
   }
 
   /**
@@ -278,7 +281,7 @@ public class SeqCanvas extends JComponent implements ViewportListenerI
    */
   public void fastPaint(int horizontal, int vertical)
   {
-    if (fastpainting || gg == null || img == null)
+    if (fastpainting  || img == null)
     {
       return;
     }
@@ -298,6 +301,7 @@ public class SeqCanvas extends JComponent implements ViewportListenerI
       int transX = 0;
       int transY = 0;
       
+      Graphics gg = img.getGraphics();
       gg.copyArea(horizontal * charWidth, vertical * charHeight,
               img.getWidth(), img.getHeight(), -horizontal * charWidth,
               -vertical * charHeight);
@@ -338,7 +342,8 @@ public class SeqCanvas extends JComponent implements ViewportListenerI
       gg.translate(transX, transY);
       drawPanel(gg, startRes, endRes, startSeq, endSeq, 0);
       gg.translate(-transX, -transY);
-
+      gg.dispose();
+      
       // Call repaint on alignment panel so that repaints from other alignment
       // panel components can be aggregated. Otherwise performance of the
       // overview window and others may be adversely affected.
@@ -352,24 +357,23 @@ public class SeqCanvas extends JComponent implements ViewportListenerI
   @Override
   public void paintComponent(Graphics g)
   {
-    super.paintComponent(g);    
-    
+    super.paintComponent(g);
+
     int charHeight = av.getCharHeight();
     int charWidth = av.getCharWidth();
-    
+
     ViewportRanges ranges = av.getRanges();
-    
+
     int width = getWidth();
     int height = getHeight();
-    
+
     width -= (width % charWidth);
     height -= (height % charHeight);
-    
+
     // selectImage is the selection group outline image
-    BufferedImage selectImage = drawSelectionGroup(
-            ranges.getStartRes(), ranges.getEndRes(),
-            ranges.getStartSeq(), ranges.getEndSeq());
-    
+    BufferedImage selectImage = drawSelectionGroup(ranges.getStartRes(),
+            ranges.getEndRes(), ranges.getStartSeq(), ranges.getEndSeq());
+
     if ((img != null) && (fastPaint
             || (getVisibleRect().width != g.getClipBounds().width)
             || (getVisibleRect().height != g.getClipBounds().height)))
@@ -390,19 +394,21 @@ public class SeqCanvas extends JComponent implements ViewportListenerI
         {
           return;
         }
-        gg = (Graphics2D) img.getGraphics();
-        gg.setFont(av.getFont());
       }
-    
+
+      
+      Graphics2D gg = (Graphics2D) img.getGraphics();
+      gg.setFont(av.getFont());
+
       if (av.antiAlias)
       {
         gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                 RenderingHints.VALUE_ANTIALIAS_ON);
       }
-    
+
       gg.setColor(Color.white);
       gg.fillRect(0, 0, img.getWidth(), img.getHeight());
-    
+
       if (av.getWrapAlignment())
       {
         drawWrappedPanel(gg, getWidth(), getHeight(), ranges.getStartRes());
@@ -412,7 +418,10 @@ public class SeqCanvas extends JComponent implements ViewportListenerI
         drawPanel(gg, ranges.getStartRes(), ranges.getEndRes(),
                 ranges.getStartSeq(), ranges.getEndSeq(), 0);
       }
-    
+      
+      
+      gg.dispose();
+
       // lcimg is a local *copy* of img which we'll draw selectImage on top of
       BufferedImage lcimg = buildLocalImage(selectImage);
       g.drawImage(lcimg, 0, 0, this);
@@ -773,12 +782,25 @@ public class SeqCanvas extends JComponent implements ViewportListenerI
     int xOffset = labelWidthWest
             + ((startColumn - ranges.getStartRes()) % viewportWidth)
             * charWidth;
+
+    // BH 2018 note: I have switched to using Graphics.create() here because it is 
+    // more reliable (and simpler) to reset. The difference seems to be that SwingJS 
+    // automatically sets a clipping region on an image to be the image dimensions, whereas
+    // Java sets no clip for an image. (A bug? Go figure!)
+    // Since we are using an off-screen BufferedImage here, the result is that g.getClip()
+    // returns non-null in JavaScript but not Java. 
+    //
+    // Anyway, this works and, I suggest, is better design anyway. 
+    // 
+    // Graphics g = gg.create();
+    // mc 30/08/18 undone because gnu.jpdf.PDFGraphics doesn't handle
+    // get/setTransform
+
     g.translate(xOffset, 0);
 
     // When printing we have an extra clipped region,
     // the Printable page which we need to account for here
     Shape clip = g.getClip();
-
     if (clip == null)
     {
       g.setClip(0, 0, viewportWidth * charWidth, canvasHeight);
@@ -789,6 +811,7 @@ public class SeqCanvas extends JComponent implements ViewportListenerI
               viewportWidth * charWidth, (int) clip.getBounds().getHeight());
     }
 
+
     /*
      * white fill the region to be drawn (so incremental fast paint doesn't
      * scribble over an existing image)
@@ -814,8 +837,9 @@ public class SeqCanvas extends JComponent implements ViewportListenerI
               startColumn, endx + 1);
       g.translate(0, -cHeight - ypos - 3);
     }
-    g.setClip(clip);
+    // g.dispose();
     g.translate(-xOffset, 0);
+//    g.setClip(clip);
   }
 
   /**
@@ -830,6 +854,7 @@ public class SeqCanvas extends JComponent implements ViewportListenerI
     int charWidth = av.getCharWidth();
 
     g.setFont(av.getFont());
+
     g.setColor(Color.black);
 
     int ypos = wrappedSpaceAboveAlignment;
@@ -851,6 +876,7 @@ public class SeqCanvas extends JComponent implements ViewportListenerI
       if (av.getScaleRightWrapped())
       {
         int x = labelWidthWest + viewportWidth * charWidth;
+        
         g.translate(x, 0);
         drawVerticalScale(g, startCol, endColumn, ypos, false);
         g.translate(-x, 0);
@@ -1175,10 +1201,10 @@ public class SeqCanvas extends JComponent implements ViewportListenerI
     if (group != null)
     {
       g.setStroke(new BasicStroke());
-      g.setColor(group.getOutlineColour());
       
       do
       {
+        g.setColor(group.getOutlineColour());
         drawPartialGroupOutline(g, group, startRes, endRes, startSeq,
                 endSeq, offset);
 
@@ -1551,20 +1577,42 @@ public class SeqCanvas extends JComponent implements ViewportListenerI
    * Highlights search results in the visible region by rendering as white text
    * on a black background. Any previous highlighting is removed. Answers true
    * if any highlight was left on the visible alignment (so status bar should be
+   * set to match), else false. This method does _not_ set the 'fastPaint' flag,
+   * so allows the next repaint to update the whole display.
+   * 
+   * @param results
+   * @return
+   */
+  public boolean highlightSearchResults(SearchResultsI results)
+  {
+    return highlightSearchResults(results, false);
+
+  }
+  
+  /**
+   * Highlights search results in the visible region by rendering as white text
+   * on a black background. Any previous highlighting is removed. Answers true
+   * if any highlight was left on the visible alignment (so status bar should be
    * set to match), else false.
    * <p>
-   * Currently fastPaint is not implemented for wrapped alignments. If a wrapped
-   * alignment had to be scrolled to show the highlighted region, then it should
-   * be fully redrawn, otherwise a fast paint can be performed. This argument
-   * could be removed if fast paint of scrolled wrapped alignment is coded in
-   * future (JAL-2609).
+   * Optionally, set the 'fastPaint' flag for a faster redraw if only the
+   * highlighted regions are modified. This speeds up highlighting across linked
+   * alignments.
+   * <p>
+   * Currently fastPaint is not implemented for scrolled wrapped alignments. If
+   * a wrapped alignment had to be scrolled to show the highlighted region, then
+   * it should be fully redrawn, otherwise a fast paint can be performed. This
+   * argument could be removed if fast paint of scrolled wrapped alignment is
+   * coded in future (JAL-2609).
    * 
    * @param results
-   * @param noFastPaint
+   * @param doFastPaint
+   *          if true, sets a flag so the next repaint only redraws the modified
+   *          image
    * @return
    */
   public boolean highlightSearchResults(SearchResultsI results,
-          boolean noFastPaint)
+          boolean doFastPaint)
   {
     if (fastpainting)
     {
@@ -1573,7 +1621,7 @@ public class SeqCanvas extends JComponent implements ViewportListenerI
     boolean wrapped = av.getWrapAlignment();
     try
     {
-      fastPaint = !noFastPaint;
+      fastPaint = doFastPaint;
       fastpainting = fastPaint;
 
       /*
@@ -1630,7 +1678,7 @@ public class SeqCanvas extends JComponent implements ViewportListenerI
    */
   protected boolean drawMappedPositions(SearchResultsI results)
   {
-    if ((results == null) || (gg == null)) // JAL-2784 check gg is not null
+    if ((results == null) || (img == null)) // JAL-2784 check gg is not null
     {
       return false;
     }
@@ -1702,9 +1750,11 @@ public class SeqCanvas extends JComponent implements ViewportListenerI
       }
       int transX = (firstCol - ranges.getStartRes()) * av.getCharWidth();
       int transY = (firstSeq - ranges.getStartSeq()) * av.getCharHeight();
+      Graphics gg = img.getGraphics();
       gg.translate(transX, transY);
       drawPanel(gg, firstCol, lastCol, firstSeq, lastSeq, 0);
       gg.translate(-transX, -transY);
+      gg.dispose();
     }
 
     return matchFound;
@@ -1825,7 +1875,7 @@ public class SeqCanvas extends JComponent implements ViewportListenerI
       return;
     }
 
-    if (fastpainting || gg == null)
+    if (fastpainting || img == null)
     {
       return;
     }
@@ -1835,6 +1885,9 @@ public class SeqCanvas extends JComponent implements ViewportListenerI
 
     try
     {
+      
+      Graphics gg = img.getGraphics();
+      
       calculateWrappedGeometry(getWidth(), getHeight());
 
       /*
@@ -1863,6 +1916,8 @@ public class SeqCanvas extends JComponent implements ViewportListenerI
        */
       drawWrappedDecorators(gg, ranges.getStartRes());
 
+      gg.dispose();
+      
       repaint();
     } finally
     {
@@ -1886,6 +1941,8 @@ public class SeqCanvas extends JComponent implements ViewportListenerI
       return;
     }
 
+    Graphics gg = img.getGraphics();
+    
     ViewportRanges ranges = av.getRanges();
     int viewportWidth = ranges.getViewportWidth();
     int charWidth = av.getCharWidth();
@@ -1912,6 +1969,8 @@ public class SeqCanvas extends JComponent implements ViewportListenerI
       /*
        * white fill first to erase annotations
        */
+      
+      
       gg.translate(xOffset, 0);
       gg.setColor(Color.white);
       gg.fillRect(labelWidthWest, ypos,
@@ -1919,6 +1978,7 @@ public class SeqCanvas extends JComponent implements ViewportListenerI
       gg.translate(-xOffset, 0);
 
       drawWrappedWidth(gg, ypos, startRes, endRes, canvasHeight);
+      
     }
 
     /*
@@ -1962,7 +2022,8 @@ public class SeqCanvas extends JComponent implements ViewportListenerI
       gg.setColor(Color.white);
       gg.fillRect(0, canvasHeight - heightBelow, getWidth(), heightBelow);
     }
-  }
+    gg.dispose();
+ }
 
   /**
    * Shifts the visible alignment by the specified number of columns - left if
@@ -1978,6 +2039,9 @@ public class SeqCanvas extends JComponent implements ViewportListenerI
     {
       return;
     }
+
+    Graphics gg = img.getGraphics();
+
     int charWidth = av.getCharWidth();
 
     int canvasHeight = getHeight();
@@ -2044,11 +2108,11 @@ public class SeqCanvas extends JComponent implements ViewportListenerI
                   * charWidth, heightToCopy, widthToCopy,
                   -wrappedRepeatHeightPx);
         }
-
         y += wrappedRepeatHeightPx;
         xpos += viewportWidth;
       }
     }
+    gg.dispose();
   }
 
   
@@ -2064,7 +2128,7 @@ public class SeqCanvas extends JComponent implements ViewportListenerI
    */
   protected boolean drawMappedPositionsWrapped(SearchResultsI results)
   {
-    if ((results == null) || (gg == null)) // JAL-2784 check gg is not null
+    if ((results == null) || (img == null)) // JAL-2784 check gg is not null
     {
       return false;
     }
@@ -2099,6 +2163,9 @@ public class SeqCanvas extends JComponent implements ViewportListenerI
 
     int gapHeight = charHeight * (av.getScaleAboveWrapped() ? 2 : 1);
 
+    
+    Graphics gg = img.getGraphics();
+
     for (int seqNo = ranges.getStartSeq(); seqNo <= ranges
             .getEndSeq(); seqNo++)
     {
@@ -2169,6 +2236,8 @@ public class SeqCanvas extends JComponent implements ViewportListenerI
       }
     }
   
+    gg.dispose();
+
     return matchFound;
   }