JAL-3383 JAL-3253-applet Java8 switches to clarify ViewportRanges
[jalview.git] / src / jalview / gui / IdCanvas.java
index 484173c..e5a5946 100755 (executable)
@@ -55,7 +55,7 @@ public class IdCanvas extends JPanel implements ViewportListenerI
 
   BufferedImage image;
 
-  Graphics2D gg;
+//  Graphics2D gg;
 
   int imgHeight = 0;
 
@@ -152,10 +152,11 @@ public class IdCanvas extends JPanel implements ViewportListenerI
    */
   public void fastPaint(int vertical)
   {
+
     /*
      * for now, not attempting fast paint of wrapped ids...
      */
-    if (gg == null || av.getWrapAlignment())
+    if (image == null || av.getWrapAlignment())
     {
       repaint();
 
@@ -164,6 +165,7 @@ public class IdCanvas extends JPanel implements ViewportListenerI
 
     ViewportRanges ranges = av.getRanges();
 
+    Graphics2D gg = image.createGraphics();
     gg.copyArea(0, 0, getWidth(), imgHeight, 0,
             -vertical * av.getCharHeight());
 
@@ -200,6 +202,8 @@ public class IdCanvas extends JPanel implements ViewportListenerI
 
     gg.translate(0, -transY);
 
+    gg.dispose();
+    
     fastPaint = true;
 
     // Call repaint on alignment panel so that repaints from other alignment
@@ -217,8 +221,6 @@ public class IdCanvas extends JPanel implements ViewportListenerI
   @Override
   public void paintComponent(Graphics g)
   {
-    super.paintComponent(g);
-
     g.setColor(Color.white);
     g.fillRect(0, 0, getWidth(), getHeight());
     
@@ -246,31 +248,33 @@ public class IdCanvas extends JPanel implements ViewportListenerI
                 BufferedImage.TYPE_INT_RGB);
     }
     
-    gg = (Graphics2D) image.getGraphics();
+    Graphics2D gg = image.createGraphics();
     
     // Fill in the background
     gg.setColor(Color.white);
     gg.fillRect(0, 0, getWidth(), imgHeight);
     
     drawIds(gg, av, av.getRanges().getStartSeq(), av.getRanges().getEndSeq(), searchResults);
+
+    gg.dispose();
     
     g.drawImage(image, 0, 0, this);
   }
 
   /**
-   * Draws sequence ids from sequence index starty to endy (inclusive), with the
-   * font and other display settings configured on the viewport. Ids of
+   * Draws sequence ids from sequence index startSeq to endSeq (inclusive), with
+   * the font and other display settings configured on the viewport. Ids of
    * sequences included in the selection are coloured grey, otherwise the
    * current id colour for the sequence id is used.
    * 
    * @param g
    * @param alignViewport
-   * @param starty
-   * @param endy
+   * @param startSeq
+   * @param endSeq
    * @param selection
    */
-  void drawIds(Graphics2D g, AlignViewport alignViewport, final int starty,
-          final int endy, List<SequenceI> selection)
+  void drawIds(Graphics2D g, AlignViewport alignViewport, final int startSeq,
+          final int endSeq, List<SequenceI> selection)
   {
     Font font = alignViewport.getFont();
     if (alignViewport.isSeqNameItalics())
@@ -295,11 +299,11 @@ public class IdCanvas extends JPanel implements ViewportListenerI
     Color currentColor = Color.white;
     Color currentTextColor = Color.black;
 
-    boolean hasHiddenRows = av.hasHiddenRows();
+    boolean hasHiddenRows = alignViewport.hasHiddenRows();
 
     if (alignViewport.getWrapAlignment())
     {
-      drawIdsWrapped(starty, hasHiddenRows);
+      drawIdsWrapped(g, alignViewport, startSeq, getHeight());
       return;
     }
 
@@ -308,7 +312,7 @@ public class IdCanvas extends JPanel implements ViewportListenerI
     int xPos = 0;
 
     // Now draw the id strings
-    for (int i = starty; i <= endy; i++)
+    for (int i = startSeq; i <= endSeq; i++)
     {
       SequenceI sequence = alignViewport.getAlignment().getSequenceAt(i);
 
@@ -328,8 +332,8 @@ public class IdCanvas extends JPanel implements ViewportListenerI
         currentColor = Color.black;
         currentTextColor = Color.white;
       }
-      else if ((av.getSelectionGroup() != null) && av.getSelectionGroup()
-              .getSequences(null).contains(sequence))
+      else if ((alignViewport.getSelectionGroup() != null) && alignViewport
+              .getSelectionGroup().getSequences(null).contains(sequence))
       {
         currentColor = Color.lightGray;
         currentTextColor = Color.black;
@@ -343,7 +347,7 @@ public class IdCanvas extends JPanel implements ViewportListenerI
       g.setColor(currentColor);
 
       int charHeight = alignViewport.getCharHeight();
-      g.fillRect(0, (i - starty) * charHeight,
+      g.fillRect(0, (i - startSeq) * charHeight,
               getWidth(), charHeight);
 
       g.setColor(currentTextColor);
@@ -356,56 +360,57 @@ public class IdCanvas extends JPanel implements ViewportListenerI
         xPos = panelWidth - fm.stringWidth(string) - 4;
       }
 
-      g.drawString(string, xPos, (((i - starty) * charHeight) + charHeight)
+      g.drawString(string, xPos, (((i - startSeq) * charHeight) + charHeight)
               - (charHeight / 5));
 
       if (hasHiddenRows)
       {
-        drawMarker(g, alignViewport, i, starty, 0);
+        drawMarker(g, alignViewport, i, startSeq, 0);
       }
     }
   }
 
   /**
-   * Draws sequence ids in wrapped mode
+   * Draws sequence ids, and annotation labels if annotations are shown, in
+   * wrapped mode
    * 
-   * @param starty
-   * @param hasHiddenRows
+   * @param g
+   * @param alignViewport
+   * @param startSeq
    */
-  protected void drawIdsWrapped(int starty, boolean hasHiddenRows)
+  void drawIdsWrapped(Graphics2D g, AlignViewport alignViewport,
+          int startSeq, int pageHeight)
   {
-    int maxwidth = av.getAlignment().getWidth();
-    int alheight = av.getAlignment().getHeight();
-
-    if (av.hasHiddenColumns())
-    {
-      maxwidth = av.getAlignment().getHiddenColumns()
-              .absoluteToVisibleColumn(maxwidth) - 1;
-    }
+    int alignmentWidth = alignViewport.getAlignment().getWidth();
+    final int alheight = alignViewport.getAlignment().getHeight();
 
     int annotationHeight = 0;
 
     AnnotationLabels labels = null;
-    if (av.isShowAnnotation())
+    if (alignViewport.isShowAnnotation())
     {
       if (ap == null)
       {
-        ap = new AnnotationPanel(av);
+        ap = new AnnotationPanel(alignViewport);
       }
-
       annotationHeight = ap.adjustPanelHeight();
-      labels = new AnnotationLabels(av);
+      labels = new AnnotationLabels(alignViewport);
     }
 
-    int hgap = av.getCharHeight();
-    if (av.getScaleAboveWrapped())
+    final int charHeight = alignViewport.getCharHeight();
+    int hgap = charHeight;
+    if (alignViewport.getScaleAboveWrapped())
     {
-      hgap += av.getCharHeight();
+      hgap += charHeight;
     }
 
-    int cHeight = alheight * av.getCharHeight() + hgap + annotationHeight;
+    /*
+     * height of alignment + gap + annotations (if shown)
+     */
+    int cHeight = alheight * charHeight + hgap
+            + annotationHeight;
 
-    ViewportRanges ranges = av.getRanges();
+    ViewportRanges ranges = alignViewport.getRanges();
 
     int rowSize = ranges.getViewportWidth();
 
@@ -413,34 +418,34 @@ public class IdCanvas extends JPanel implements ViewportListenerI
      * draw repeating sequence ids until out of sequence data or
      * out of visible space, whichever comes first
      */
+    boolean hasHiddenRows = alignViewport.hasHiddenRows();
     int ypos = hgap;
-    int row = ranges.getStartRes();
-    while ((ypos <= getHeight()) && (row < maxwidth))
+    int rowStartRes = ranges.getStartRes();
+    while ((ypos <= pageHeight) && (rowStartRes < alignmentWidth))
     {
-      for (int i = starty; i < alheight; i++)
+      for (int i = startSeq; i < alheight; i++)
       {
-        SequenceI s = av.getAlignment().getSequenceAt(i);
-        if (hasHiddenRows || av.isDisplayReferenceSeq())
+        SequenceI s = alignViewport.getAlignment().getSequenceAt(i);
+        if (hasHiddenRows || alignViewport.isDisplayReferenceSeq())
         {
-          gg.setFont(getHiddenFont(s, av));
+          g.setFont(getHiddenFont(s, alignViewport));
         }
         else
         {
-          gg.setFont(getIdfont());
+          g.setFont(getIdfont());
         }
-
-        drawIdString(gg, hasHiddenRows, s, i, 0, ypos);
+        drawIdString(g, hasHiddenRows, s, i, 0, ypos);
       }
 
-      if (labels != null && av.isShowAnnotation())
+      if (labels != null && alignViewport.isShowAnnotation())
       {
-        gg.translate(0, ypos + (alheight * av.getCharHeight()));
-        labels.drawComponent(gg, getWidth());
-        gg.translate(0, -ypos - (alheight * av.getCharHeight()));
+        g.translate(0, ypos + (alheight * charHeight));
+        labels.drawComponent(g, getWidth());
+        g.translate(0, -ypos - (alheight * charHeight));
       }
 
       ypos += cHeight;
-      row += rowSize;
+      rowStartRes += rowSize;
     }
   }
 
@@ -574,21 +579,45 @@ public class IdCanvas extends JPanel implements ViewportListenerI
   @Override
   public void propertyChange(PropertyChangeEvent evt)
   {
+    // BH just clarifying logic
     String propertyName = evt.getPropertyName();
-    if (propertyName.equals(ViewportRanges.STARTSEQ)
-            || (av.getWrapAlignment()
-                    && propertyName.equals(ViewportRanges.STARTRES)))
-    {
+    switch (propertyName) {
+    case ViewportRanges.STARTSEQ:
       fastPaint((int) evt.getNewValue() - (int) evt.getOldValue());
-    }
-    else if (propertyName.equals(ViewportRanges.STARTRESANDSEQ))
-    {
+      return;
+    case ViewportRanges.STARTRES:
+      if (av.getWrapAlignment())
+      {
+        fastPaint((int) evt.getNewValue() - (int) evt.getOldValue());
+      }
+      return;
+    case ViewportRanges.STARTRESANDSEQ:
       fastPaint(((int[]) evt.getNewValue())[1]
               - ((int[]) evt.getOldValue())[1]);
-    }
-    else if (propertyName.equals(ViewportRanges.MOVE_VIEWPORT))
-    {
+      return;
+    case ViewportRanges.MOVE_VIEWPORT:
       repaint();
+      return;
+    case ViewportRanges.ENDRES:
+    case ViewportRanges.ENDSEQ:
+      // ignore ??
+      return;
     }
+// BH 2019.07.27 was:
+//    if (propertyName.equals(ViewportRanges.STARTSEQ)
+//            || (av.getWrapAlignment()
+//                    && propertyName.equals(ViewportRanges.STARTRES)))
+//    {
+//      fastPaint((int) evt.getNewValue() - (int) evt.getOldValue());
+//    }
+//    else if (propertyName.equals(ViewportRanges.STARTRESANDSEQ))
+//    {
+//      fastPaint(((int[]) evt.getNewValue())[1]
+//              - ((int[]) evt.getOldValue())[1]);
+//    }
+//    else if (propertyName.equals(ViewportRanges.MOVE_VIEWPORT))
+//    {
+//      repaint();
+    // }
   }
 }