JAL-4047 - proof of concept for JAL-4048 - display columns of info in sequence ID...
[jalview.git] / src / jalview / gui / IdCanvas.java
index 4057cef..e284bbe 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.Font;
@@ -37,6 +33,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 +58,7 @@ public class IdCanvas extends JPanel implements ViewportListenerI
 
   BufferedImage image;
 
-//  Graphics2D gg;
+  // Graphics2D gg;
 
   int imgHeight = 0;
 
@@ -79,7 +82,7 @@ public class IdCanvas extends JPanel implements ViewportListenerI
     this.av = av;
     PaintRefresher.Register(this, av.getSequenceSetId());
     av.getRanges().addPropertyChangeListener(this);
-    }
+  }
 
   /**
    * DOCUMENT ME!
@@ -137,7 +140,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);
     }
@@ -203,7 +206,7 @@ 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
@@ -223,41 +226,42 @@ public class IdCanvas extends JPanel implements ViewportListenerI
   {
     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);
     }
-    
+
     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);
 
     gg.dispose();
-    
+
     g.drawImage(image, 0, 0, this);
   }
 
@@ -273,14 +277,13 @@ public class IdCanvas extends JPanel implements ViewportListenerI
    * @param endSeq
    * @param selection
    */
-  void drawIds(Graphics2D g, AlignViewport alignViewport, final int startSeq,
-          final int endSeq, 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())
     {
-      setIdfont(new Font(font.getName(), Font.ITALIC,
-              font.getSize()));
+      setIdfont(new Font(font.getName(), Font.ITALIC, font.getSize()));
     }
     else
     {
@@ -308,7 +311,16 @@ public class IdCanvas extends JPanel implements ViewportListenerI
     }
 
     // Now draw the id strings
-    int panelWidth = getWidth();
+    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()));
     int xPos = 0;
 
     // Now draw the id strings
@@ -320,10 +332,10 @@ public class IdCanvas extends JPanel implements ViewportListenerI
       {
         continue;
       }
-
       if (hasHiddenRows || alignViewport.isDisplayReferenceSeq())
       {
         g.setFont(getHiddenFont(sequence, alignViewport));
+        fm = g.getFontMetrics();
       }
 
       // Selected sequence colours
@@ -347,8 +359,7 @@ public class IdCanvas extends JPanel implements ViewportListenerI
       g.setColor(currentColor);
 
       int charHeight = alignViewport.getCharHeight();
-      g.fillRect(0, (i - startSeq) * charHeight,
-              getWidth(), charHeight);
+      g.fillRect(0, (i - startSeq) * charHeight, getWidth(), charHeight);
 
       g.setColor(currentTextColor);
 
@@ -360,10 +371,37 @@ public class IdCanvas extends JPanel implements ViewportListenerI
         xPos = panelWidth - fm.stringWidth(string) - 4;
       }
 
-      g.drawString(string, xPos, (((i - startSeq) * charHeight) + charHeight)
-              - (charHeight / 5));
+      g.drawString(string, xPos,
+              (((i - startSeq) * charHeight) + charHeight)
+                      - (charHeight / 5));
 
-      if (hasHiddenRows)
+      if (visible != null && visible.size() > 0)
+      {
+        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, (i - startSeq) * charHeight, xPos + colWid - 4,
+                    charHeight);
+          }
+          else
+          {
+            g.setColor(col_cell.bg);
+            g.fillRect(xPos, (i - startSeq) * charHeight, xPos + colWid - 4,
+                    charHeight);
+            g.setColor(col_cell.fg);
+            g.drawString(col_cell.label, xPos,
+                    (((i - startSeq) * charHeight) + charHeight)
+                            - (charHeight / 5));
+          }
+          xPos += colWid;
+          g.setColor(currentTextColor);
+        }
+      }
+      if (hasHiddenRows && av.getShowHiddenMarkers())
       {
         drawMarker(g, alignViewport, i, startSeq, 0);
       }
@@ -384,32 +422,20 @@ public class IdCanvas extends JPanel implements ViewportListenerI
     int alignmentWidth = alignViewport.getAlignment().getWidth();
     final int alheight = alignViewport.getAlignment().getHeight();
 
-    int annotationHeight = 0;
+    /*
+     * assumption: SeqCanvas.calculateWrappedGeometry has been called
+     */
+    SeqCanvas seqCanvas = alignViewport.getAlignPanel()
+            .getSeqPanel().seqCanvas;
+
+    final int charHeight = alignViewport.getCharHeight();
 
     AnnotationLabels labels = null;
     if (alignViewport.isShowAnnotation())
     {
-      if (ap == null)
-      {
-        ap = new AnnotationPanel(alignViewport);
-      }
-      annotationHeight = ap.adjustPanelHeight();
       labels = new AnnotationLabels(alignViewport);
     }
 
-    final int charHeight = alignViewport.getCharHeight();
-    int hgap = charHeight;
-    if (alignViewport.getScaleAboveWrapped())
-    {
-      hgap += charHeight;
-    }
-
-    /*
-     * height of alignment + gap + annotations (if shown)
-     */
-    int cHeight = alheight * charHeight + hgap
-            + annotationHeight;
-
     ViewportRanges ranges = alignViewport.getRanges();
 
     int rowSize = ranges.getViewportWidth();
@@ -419,7 +445,7 @@ public class IdCanvas extends JPanel implements ViewportListenerI
      * out of visible space, whichever comes first
      */
     boolean hasHiddenRows = alignViewport.hasHiddenRows();
-    int ypos = hgap;
+    int ypos = seqCanvas.wrappedSpaceAboveAlignment;
     int rowStartRes = ranges.getStartRes();
     while ((ypos <= pageHeight) && (rowStartRes < alignmentWidth))
     {
@@ -444,7 +470,7 @@ public class IdCanvas extends JPanel implements ViewportListenerI
         g.translate(0, -ypos - (alheight * charHeight));
       }
 
-      ypos += cHeight;
+      ypos += seqCanvas.wrappedRepeatHeightPx;
       rowStartRes += rowSize;
     }
   }
@@ -459,7 +485,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;
@@ -502,8 +529,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;