JAL-3954 Register AlignFrame as web service change listener and add menus
[jalview.git] / src / jalview / gui / SeqCanvas.java
index f62507a..459f004 100755 (executable)
@@ -99,6 +99,7 @@ public class SeqCanvas extends JPanel implements ViewportListenerI
 
   private int availHeight;
 
+  private boolean allowFastPaint;
   // Don't do this! Graphics handles are supposed to be transient
   // private Graphics2D gg;
 
@@ -207,7 +208,7 @@ public class SeqCanvas extends JPanel implements ViewportListenerI
     int yPos = ypos + charHeight;
     int startX = startx;
     int endX = endx;
-
+    
     if (av.hasHiddenColumns())
     {
       HiddenColumns hiddenColumns = av.getAlignment().getHiddenColumns();
@@ -347,6 +348,7 @@ public class SeqCanvas extends JPanel implements ViewportListenerI
         }
       }
 
+
       // System.err.println(">>> FastPaint to " + transX + " " + transY + " "
       // + horizontal + " " + vertical + " " + startRes + " " + endRes
       // + " " + startSeq + " " + endSeq);
@@ -355,6 +357,8 @@ public class SeqCanvas extends JPanel implements ViewportListenerI
       gg.copyArea(horizontal * charWidth, vertical * charHeight,
               img.getWidth(), img.getHeight(), -horizontal * charWidth,
               -vertical * charHeight);
+
+
       gg.translate(transX, transY);
       drawPanel(gg, startRes, endRes, startSeq, endSeq, 0);
       gg.translate(-transX, -transY);
@@ -363,7 +367,6 @@ public class SeqCanvas extends JPanel implements ViewportListenerI
       // 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.
-      // System.out.println("SeqCanvas fastPaint() repaint() request...");
       av.getAlignPanel().repaint();
     } finally
     {
@@ -374,7 +377,7 @@ public class SeqCanvas extends JPanel implements ViewportListenerI
   @Override
   public void paintComponent(Graphics g)
   {
-    if (av.getAlignPanel().getHoldRepaint())
+    if (av==null || av.getAlignPanel()==null || av.getAlignPanel().getHoldRepaint())
     {
       return;
     }
@@ -415,11 +418,9 @@ public class SeqCanvas extends JPanel implements ViewportListenerI
     // }
 
     Rectangle vis, clip;
-    if (img != null
-            && (fastPaint
-                    || (vis = getVisibleRect()).width != (clip = g
-                            .getClipBounds()).width
-                    || vis.height != clip.height))
+    if (allowFastPaint  && img != null
+            && (fastPaint || (vis = getVisibleRect()).width != (clip = g.getClipBounds()).width
+                          || vis.height != clip.height))
     {
       g.drawImage(img, 0, 0, this);
       drawSelectionGroup((Graphics2D) g, startRes, endRes, startSeq,
@@ -428,6 +429,7 @@ public class SeqCanvas extends JPanel implements ViewportListenerI
     }
     else
     {
+      allowFastPaint = true;
       // img is a cached version of the last view we drew.
       // If we have no img or the size has changed, make a new one.
       //
@@ -470,7 +472,6 @@ public class SeqCanvas extends JPanel implements ViewportListenerI
       drawCursor(g, startRes, endRes, startSeq, endSeq);
     }
   }
-
   /**
    * Draw an alignment panel for printing
    * 
@@ -519,31 +520,32 @@ public class SeqCanvas extends JPanel implements ViewportListenerI
   }
 
   /**
-   * Using the current font, determine fields labelWidthEast and labelWidthWest,
-   * and return the number of residues that can fill the remaining width
+   * Returns the visible width of the canvas in residues, after allowing for
+   * East or West scales (if shown)
    * 
-   * @param w
+   * @param canvasWidth
    *          the width in pixels (possibly including scales)
    * 
-   * @return the visible width in residues, after allowing for East or West
-   *         scales (if shown)
-   * 
+   * @return
    */
-  public int getWrappedCanvasWidth(int w)
+  public int getWrappedCanvasWidth(int canvasWidth)
   {
     int charWidth = av.getCharWidth();
 
     FontMetrics fm = getFontMetrics(av.getFont());
 
-    int labelWidth = (av.getScaleRightWrapped() || av.getScaleLeftWrapped()
-            ? getLabelWidth(fm)
-            : 0);
+    int labelWidth = 0;
+    
+    if (av.getScaleRightWrapped() || av.getScaleLeftWrapped())
+    {
+      labelWidth = getLabelWidth(fm);
+    }
 
     labelWidthEast = av.getScaleRightWrapped() ? labelWidth : 0;
 
     labelWidthWest = av.getScaleLeftWrapped() ? labelWidth : 0;
 
-    return (w - labelWidthEast - labelWidthWest) / charWidth;
+    return (canvasWidth - labelWidthEast - labelWidthWest) / charWidth;
   }
 
   /**
@@ -595,7 +597,9 @@ public class SeqCanvas extends JPanel implements ViewportListenerI
           final int startColumn)
   {
     int wrappedWidthInResidues = calculateWrappedGeometry();
+
     av.setWrappedWidth(wrappedWidthInResidues);
+
     ViewportRanges ranges = av.getRanges();
     ranges.setViewportStartAndWidth(startColumn, wrappedWidthInResidues);
 
@@ -635,7 +639,6 @@ public class SeqCanvas extends JPanel implements ViewportListenerI
     availWidth -= (availWidth % charWidth);
     availHeight -= (availHeight % charHeight);
   }
-
   /**
    * Calculates and saves values needed when rendering a wrapped alignment.
    * These depend on many factors, including
@@ -665,7 +668,6 @@ public class SeqCanvas extends JPanel implements ViewportListenerI
    */
   public int calculateWrappedGeometry(int canvasWidth, int canvasHeight)
   {
-
     int charHeight = av.getCharHeight();
 
     /*
@@ -818,7 +820,6 @@ public class SeqCanvas extends JPanel implements ViewportListenerI
       if (av.getScaleRightWrapped())
       {
         int x = labelWidthWest + viewportWidth * charWidth;
-
         g.translate(x, 0);
         drawVerticalScale(g, startCol, endColumn, ypos, false);
         g.translate(-x, 0);
@@ -904,7 +905,6 @@ public class SeqCanvas extends JPanel implements ViewportListenerI
           { 5f, 3f }, 0f);
 
   private final static BasicStroke basicStroke = new BasicStroke();
-
   /*
    * Draw a selection group over a wrapped alignment
    */
@@ -921,7 +921,6 @@ public class SeqCanvas extends JPanel implements ViewportListenerI
             / charWidth;
     int startx = startRes;
     int maxwidth = av.getAlignment().getVisibleWidth();
-
     // JAL-3253-applet had this:
     // // height gap above each panel
     // int charHeight = av.getCharHeight();
@@ -949,10 +948,8 @@ public class SeqCanvas extends JPanel implements ViewportListenerI
       }
 
       g.translate(labelWidthWest, 0);
-
       drawUnwrappedSelection(g, group, startx, endx, 0,
               av.getAlignment().getHeight() - 1, ypos);
-
       g.translate(-labelWidthWest, 0);
 
       // update vertical offset
@@ -1298,6 +1295,7 @@ public class SeqCanvas extends JPanel implements ViewportListenerI
     }
   }
 
+
   /**
    * Draw a selection group over an unwrapped alignment
    * 
@@ -1320,7 +1318,6 @@ public class SeqCanvas extends JPanel implements ViewportListenerI
           int startRes, int endRes, int startSeq, int endSeq, int offset)
   {
     int charWidth = av.getCharWidth();
-
     if (!av.hasHiddenColumns())
     {
       drawPartialGroupOutline(g, group, startRes, endRes, startSeq, endSeq,
@@ -1513,7 +1510,6 @@ public class SeqCanvas extends JPanel implements ViewportListenerI
       g.drawLine(sx + xwidth, oldY, sx + xwidth, sy);
     }
   }
-
   /**
    * Highlights search results in the visible region by rendering as white text
    * on a black background. Any previous highlighting is removed. Answers true
@@ -1529,7 +1525,6 @@ public class SeqCanvas extends JPanel implements ViewportListenerI
     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
@@ -1706,7 +1701,6 @@ public class SeqCanvas extends JPanel implements ViewportListenerI
   public void propertyChange(PropertyChangeEvent evt)
   {
     String eventName = evt.getPropertyName();
-
     // BH 2019.07.27 removes dead code introduced in aad3650 and simplifies
     // logic, emphasizing no check for ENDRES or ENDSEQ
 
@@ -1744,9 +1738,6 @@ public class SeqCanvas extends JPanel implements ViewportListenerI
               - ((int[]) evt.getOldValue())[0];
       scrollY = ((int[]) evt.getNewValue())[1]
               - ((int[]) evt.getOldValue())[1];
-
-      // System.out.println("SC dx dy " + scrollX + " " + scrollY);
-
       if (scrollX != 0 && scrollY != 0)
       {
         // all sorts of problems in JavaScript if this is commented out.
@@ -1897,9 +1888,7 @@ public class SeqCanvas extends JPanel implements ViewportListenerI
 
     try
     {
-
       Graphics gg = img.getGraphics();
-
       calculateWrappedGeometry();
 
       /*
@@ -1929,7 +1918,6 @@ public class SeqCanvas extends JPanel implements ViewportListenerI
       drawWrappedDecorators(gg, ranges.getStartRes());
 
       gg.dispose();
-
       repaint();
     } finally
     {
@@ -1954,7 +1942,6 @@ public class SeqCanvas extends JPanel implements ViewportListenerI
     }
 
     Graphics gg = img.getGraphics();
-
     ViewportRanges ranges = av.getRanges();
     int viewportWidth = ranges.getViewportWidth();
     int charWidth = av.getCharWidth();
@@ -1982,7 +1969,6 @@ public class SeqCanvas extends JPanel implements ViewportListenerI
       /*
        * white fill first to erase annotations
        */
-
       gg.translate(xOffset, 0);
       gg.setColor(Color.white);
       gg.fillRect(labelWidthWest, ypos, (endRes - startRes + 1) * charWidth,
@@ -1990,7 +1976,6 @@ public class SeqCanvas extends JPanel implements ViewportListenerI
       gg.translate(-xOffset, 0);
 
       drawWrappedWidth(gg, ypos, startRes, endRes, canvasHeight);
-
     }
 
     /*
@@ -2252,7 +2237,6 @@ public class SeqCanvas extends JPanel implements ViewportListenerI
         }
       }
     }
-
     gg.dispose();
 
     return matchFound;
@@ -2274,7 +2258,7 @@ public class SeqCanvas extends JPanel implements ViewportListenerI
    */
   public void setNoFastPaint()
   {
-    fastPaint = false;
+    allowFastPaint = false;
   }
 
 }