Merge branch 'develop' into spike/JAL-4047/JAL-4048_columns_in_sequenceID
[jalview.git] / src / jalview / gui / IdCanvas.java
index 484173c..2faea01 100755 (executable)
  */
 package jalview.gui;
 
-import jalview.datamodel.SequenceI;
-import jalview.viewmodel.ViewportListenerI;
-import jalview.viewmodel.ViewportRanges;
-
 import java.awt.BorderLayout;
 import java.awt.Color;
+import java.awt.Dimension;
 import java.awt.Font;
 import java.awt.FontMetrics;
 import java.awt.Graphics;
@@ -37,6 +34,13 @@ import java.util.List;
 
 import javax.swing.JPanel;
 
+import jalview.datamodel.SequenceI;
+import jalview.viewmodel.ViewportListenerI;
+import jalview.viewmodel.ViewportRanges;
+import jalview.viewmodel.seqfeatures.IdColumn;
+import jalview.viewmodel.seqfeatures.IdColumns;
+import jalview.viewmodel.seqfeatures.IdColumns.ColumnCell;
+
 /**
  * DOCUMENT ME!
  * 
@@ -55,7 +59,7 @@ public class IdCanvas extends JPanel implements ViewportListenerI
 
   BufferedImage image;
 
-  Graphics2D gg;
+  // Graphics2D gg;
 
   int imgHeight = 0;
 
@@ -79,7 +83,7 @@ public class IdCanvas extends JPanel implements ViewportListenerI
     this.av = av;
     PaintRefresher.Register(this, av.getSequenceSetId());
     av.getRanges().addPropertyChangeListener(this);
-    }
+  }
 
   /**
    * DOCUMENT ME!
@@ -137,7 +141,7 @@ public class IdCanvas extends JPanel implements ViewportListenerI
     g.drawString(s.getDisplayId(av.getShowJVSuffix()), xPos,
             (((i - starty + 1) * charHeight) + ypos) - (charHeight / 5));
 
-    if (hiddenRows)
+    if (hiddenRows && av.getShowHiddenMarkers())
     {
       drawMarker(g, av, i, starty, ypos);
     }
@@ -152,10 +156,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 +169,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());
 
@@ -196,10 +202,12 @@ public class IdCanvas extends JPanel implements ViewportListenerI
 
     gg.translate(0, transY);
 
-    drawIds(gg, av, ss, es, searchResults);
+    drawIds(gg, av, ss, es, searchResults,true);
 
     gg.translate(0, -transY);
 
+    gg.dispose();
+
     fastPaint = true;
 
     // Call repaint on alignment panel so that repaints from other alignment
@@ -217,66 +225,66 @@ 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());
-    
+
     if (fastPaint)
     {
       fastPaint = false;
       g.drawImage(image, 0, 0, this);
-    
+
       return;
     }
-    
+
     int oldHeight = imgHeight;
-    
+
     imgHeight = getHeight();
     imgHeight -= (imgHeight % av.getCharHeight());
-    
+
     if (imgHeight < 1)
     {
       return;
     }
-    
+
     if (oldHeight != imgHeight || image.getWidth(this) != getWidth())
     {
-       image = new BufferedImage(getWidth(), imgHeight,
-                BufferedImage.TYPE_INT_RGB);
+      image = new BufferedImage(getWidth(), imgHeight,
+              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);
-    
+
+    drawIds(gg, av, av.getRanges().getStartSeq(),
+            av.getRanges().getEndSeq(), searchResults,true);
+
+    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, boolean forGUI)
   {
     Font font = alignViewport.getFont();
     if (alignViewport.isSeqNameItalics())
     {
-      setIdfont(new Font(font.getName(), Font.ITALIC,
-              font.getSize()));
+      setIdfont(new Font(font.getName(), Font.ITALIC, font.getSize()));
     }
     else
     {
@@ -295,31 +303,40 @@ 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;
     }
 
     // Now draw the id strings
-    int panelWidth = getWidth();
-    int xPos = 0;
+    int fullPanelWidth = getWidth();
+
+    IdColumns id_cols = alignViewport.getIdColumns();
+    List<IdColumn> visible = id_cols.getVisible();
+    /**
+     * width of an idColumn
+     */
+    int colWid = 20;
+    int panelWidth = Math.max(fullPanelWidth / 2,
+            fullPanelWidth - (colWid * visible.size()));
 
     // Now draw the id strings
-    for (int i = starty; i <= endy; i++)
+    for (int i = startSeq; i <= endSeq; i++)
     {
+      int xPos = 0;
       SequenceI sequence = alignViewport.getAlignment().getSequenceAt(i);
 
       if (sequence == null)
       {
         continue;
       }
-
       if (hasHiddenRows || alignViewport.isDisplayReferenceSeq())
       {
         g.setFont(getHiddenFont(sequence, alignViewport));
+        fm = g.getFontMetrics();
       }
 
       // Selected sequence colours
@@ -328,8 +345,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,8 +360,7 @@ public class IdCanvas extends JPanel implements ViewportListenerI
       g.setColor(currentColor);
 
       int charHeight = alignViewport.getCharHeight();
-      g.fillRect(0, (i - starty) * charHeight,
-              getWidth(), charHeight);
+      g.fillRect(0, (i - startSeq) * charHeight, getWidth(), charHeight);
 
       g.setColor(currentTextColor);
 
@@ -356,56 +372,96 @@ public class IdCanvas extends JPanel implements ViewportListenerI
         xPos = panelWidth - fm.stringWidth(string) - 4;
       }
 
-      g.drawString(string, xPos, (((i - starty) * charHeight) + charHeight)
-              - (charHeight / 5));
+      g.drawString(string, xPos,
+              (((i - startSeq) * charHeight) + charHeight)
+                      - (charHeight / 5));
 
-      if (hasHiddenRows)
+      if (visible != null && visible.size() > 0)
+      {
+        try
+        {
+          xPos = panelWidth + 2;
+          for (IdColumn col : visible)
+          {
+            ColumnCell col_cell = id_cols.getCellFor(sequence, col);
+            if (col_cell == null)
+            {
+              g.setColor(Color.gray);
+              g.fillRect(xPos + 1, (i - startSeq) * charHeight,
+                      xPos + colWid - 3, charHeight);
+            }
+            else
+            {
+              g.setColor(col_cell.bg);
+              g.fillRect(xPos + 1, (i - startSeq) * charHeight,
+                      xPos + colWid - 3, charHeight);
+              g.setColor(col_cell.fg);
+              g.drawString(col_cell.label, xPos,
+                      (((i - startSeq) * charHeight) + charHeight)
+                              - (charHeight / 5));
+            }
+            xPos += colWid;
+            g.setColor(currentTextColor);
+          }
+        } catch (Exception q)
+        {
+        }
+      }
+      if (hasHiddenRows && av.getShowHiddenMarkers())
       {
-        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();
+    drawIdsWrapped(g, alignViewport, startSeq, pageHeight, -1, true);
+  }
 
-    if (av.hasHiddenColumns())
-    {
-      maxwidth = av.getAlignment().getHiddenColumns()
-              .absoluteToVisibleColumn(maxwidth) - 1;
-    }
+  /**
+   * render sequence IDs and annotation labels when wrapped - without GUI junk
+   * @param g
+   * @param av2
+   * @param i
+   * @param totalHeight
+   */
+  public void drawIdsWrappedNoGUI(Graphics2D g, AlignViewport av2, int i,
+          int totalHeight)
+  {
+    drawIdsWrapped(g, av2, totalHeight, totalHeight, i,false);
+  }
 
-    int annotationHeight = 0;
+  void drawIdsWrapped(Graphics2D g, AlignViewport alignViewport,
+          int startSeq, int pageHeight, int idWidth, boolean forGUI)
+  {
+    int alignmentWidth = alignViewport.getAlignment().getWidth();
+    final int alheight = alignViewport.getAlignment().getHeight();
 
-    AnnotationLabels labels = null;
-    if (av.isShowAnnotation())
-    {
-      if (ap == null)
-      {
-        ap = new AnnotationPanel(av);
-      }
+    /*
+     * assumption: SeqCanvas.calculateWrappedGeometry has been called
+     */
+    SeqCanvas seqCanvas = alignViewport.getAlignPanel()
+            .getSeqPanel().seqCanvas;
 
-      annotationHeight = ap.adjustPanelHeight();
-      labels = new AnnotationLabels(av);
-    }
+    final int charHeight = alignViewport.getCharHeight();
 
-    int hgap = av.getCharHeight();
-    if (av.getScaleAboveWrapped())
+    AnnotationLabels labels = null;
+    if (alignViewport.isShowAnnotation())
     {
-      hgap += av.getCharHeight();
+      labels = new AnnotationLabels(alignViewport);
     }
 
-    int cHeight = alheight * av.getCharHeight() + hgap + annotationHeight;
-
-    ViewportRanges ranges = av.getRanges();
+    ViewportRanges ranges = alignViewport.getRanges();
 
     int rowSize = ranges.getViewportWidth();
 
@@ -413,34 +469,49 @@ public class IdCanvas extends JPanel implements ViewportListenerI
      * draw repeating sequence ids until out of sequence data or
      * out of visible space, whichever comes first
      */
-    int ypos = hgap;
-    int row = ranges.getStartRes();
-    while ((ypos <= getHeight()) && (row < maxwidth))
+    boolean hasHiddenRows = alignViewport.hasHiddenRows();
+    int ypos = seqCanvas.wrappedSpaceAboveAlignment;
+    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()));
+        int getWidth = getWidth();
+        int thisIdWidth = getWidth;
+        g.translate(0, ypos + (alheight * charHeight));
+        if (!manuallyAdjusted())
+        {
+          int getAnnotationsIdWidth = labels.drawLabels(g, false, -1, false,forGUI,
+                  null);
+          thisIdWidth = idWidth < 0 ? getAnnotationsIdWidth : idWidth;
+          if (thisIdWidth > getWidth)
+          {
+            this.setPreferredSize(
+                    new Dimension(thisIdWidth, this.getHeight()));
+            this.repaint();
+            alignViewport.setIdWidth(thisIdWidth);
+          }
+        }
+        labels.drawComponent(g, false, thisIdWidth, forGUI);
+        g.translate(0, -ypos - (alheight * charHeight));
       }
 
-      ypos += cHeight;
-      row += rowSize;
+      ypos += seqCanvas.wrappedRepeatHeightPx;
+      rowStartRes += rowSize;
     }
   }
 
@@ -454,7 +525,8 @@ public class IdCanvas extends JPanel implements ViewportListenerI
    * @param starty
    * @param yoffset
    */
-  void drawMarker(Graphics2D g, AlignViewport alignViewport, int seqIndex, int starty, int yoffset)
+  void drawMarker(Graphics2D g, AlignViewport alignViewport, int seqIndex,
+          int starty, int yoffset)
   {
     SequenceI[] hseqs = alignViewport.getAlignment()
             .getHiddenSequences().hiddenSequences;
@@ -497,8 +569,7 @@ public class IdCanvas extends JPanel implements ViewportListenerI
     /*
      * vertices of the triangle, below or above hidden seqs
      */
-    int[] xPoints = new int[]
-    { getWidth() - charHeight,
+    int[] xPoints = new int[] { getWidth() - charHeight,
         getWidth() - charHeight, getWidth() };
     int yShift = seqIndex - starty;
 
@@ -591,4 +662,17 @@ public class IdCanvas extends JPanel implements ViewportListenerI
       repaint();
     }
   }
+
+  private boolean manuallyAdjusted = false;
+
+  public boolean manuallyAdjusted()
+  {
+    return manuallyAdjusted;
+  }
+
+  public void setManuallyAdjusted(boolean b)
+  {
+    manuallyAdjusted = b;
+  }
+
 }