Merge branch 'bug/JAL-247autoIdWidthSeqMarker' into develop trial_merge/develop/bug/JAL-247autoIdWidthSeqMarker
authorJim Procter <jprocter@issues.jalview.org>
Tue, 7 Apr 2020 11:48:59 +0000 (12:48 +0100)
committerJim Procter <jprocter@issues.jalview.org>
Tue, 7 Apr 2020 11:48:59 +0000 (12:48 +0100)
1  2 
src/jalview/gui/AlignmentPanel.java
src/jalview/gui/IdCanvas.java

@@@ -73,11 -73,6 +73,11 @@@ import javax.swing.SwingUtilities
  public class AlignmentPanel extends GAlignmentPanel implements
          AdjustmentListener, Printable, AlignmentViewPanel, ViewportListenerI
  {
 +  /*
 +   * spare space in pixels between sequence id and alignment panel
 +   */
 +  private static final int ID_WIDTH_PADDING = 4;
 +
    public AlignViewport av;
  
    OverviewPanel overviewPanel;
      getAnnotationPanel().adjustPanelHeight();
  
      Dimension d = calculateIdWidth();
 -
 -    d.setSize(d.width + 4, d.height);
      getIdPanel().getIdCanvas().setPreferredSize(d);
      hscrollFillerPanel.setPreferredSize(d);
  
    }
  
    /**
 -   * Calculate the width of the alignment labels based on the displayed names
 -   * and any bounds on label width set in preferences.
 +   * Calculates the width of the alignment labels based on the displayed names
 +   * and any bounds on label width set in preferences. The calculated width is
 +   * also set as a property of the viewport.
     * 
     * @return Dimension giving the maximum width of the alignment label panel
     *         that should be used.
     */
    public Dimension calculateIdWidth()
    {
 +    int oldWidth = av.getIdWidth();
 +
      // calculate sensible default width when no preference is available
      Dimension r = null;
      if (av.getIdWidth() < 0)
      {
        int afwidth = (alignFrame != null ? alignFrame.getWidth() : 300);
 -      int maxwidth = Math.max(20, Math.min(afwidth - 200, 2 * afwidth / 3));
 +      int idWidth = Math.min(afwidth - 200, 2 * afwidth / 3);
 +      int maxwidth = Math.max(IdwidthAdjuster.MIN_ID_WIDTH, idWidth);
        r = calculateIdWidth(maxwidth);
        av.setIdWidth(r.width);
      }
        r.width = av.getIdWidth();
        r.height = 0;
      }
 +
 +    /*
 +     * fudge: if desired width has changed, update layout
 +     * (see also paintComponent - updates layout on a repaint)
 +     */
 +    if (r.width != oldWidth)
 +    {
 +      idPanelHolder.setPreferredSize(r);
 +      validate();
 +    }
      return r;
    }
  
     * @return Dimension giving the maximum width of the alignment label panel
     *         that should be used.
     */
 -  public Dimension calculateIdWidth(int maxwidth)
 +  protected Dimension calculateIdWidth(int maxwidth)
    {
      Container c = new Container();
  
      AlignmentI al = av.getAlignment();
      int i = 0;
      int idWidth = 0;
 -    String id;
  
      while ((i < al.getHeight()) && (al.getSequenceAt(i) != null))
      {
        SequenceI s = al.getSequenceAt(i);
 -
 -      id = s.getDisplayId(av.getShowJVSuffix());
 -
 -      if (fm.stringWidth(id) > idWidth)
 -      {
 -        idWidth = fm.stringWidth(id);
 -      }
 -
 +      String id = s.getDisplayId(av.getShowJVSuffix());
 +      int stringWidth = fm.stringWidth(id);
 +      idWidth = Math.max(idWidth, stringWidth);
        i++;
      }
  
        while (i < al.getAlignmentAnnotation().length)
        {
          String label = al.getAlignmentAnnotation()[i].label;
 -
 -        if (fm.stringWidth(label) > idWidth)
 -        {
 -          idWidth = fm.stringWidth(label);
 -        }
 -
 +        int stringWidth = fm.stringWidth(label);
 +        idWidth = Math.max(idWidth, stringWidth);
          i++;
        }
      }
  
 -    return new Dimension(
 -            maxwidth < 0 ? idWidth : Math.min(maxwidth, idWidth), 12);
 +    int w = maxwidth < 0 ? idWidth : Math.min(maxwidth, idWidth);
 +    w += ID_WIDTH_PADDING;
 +
 +    return new Dimension(w, 12);
    }
  
    /**
      }
    }
  
 -  /**
 -   * DOCUMENT ME!
 -   * 
 -   * @param g
 -   *          DOCUMENT ME!
 -   */
    @Override
    public void paintComponent(Graphics g)
    {
      List<SequenceI> selection = av.getSelectionGroup() == null ? null
              : av.getSelectionGroup().getSequences(null);
      idCanvas.drawIds((Graphics2D) idGraphics, av, startSeq, endSeq - 1,
-             selection);
+             selection, idWidth);
  
      idGraphics.setFont(av.getFont());
      idGraphics.translate(0, -scaleHeight);
      int cHeight = av.getAlignment().getHeight() * av.getCharHeight() + hgap
              + annotationHeight;
  
-     int idWidth = getVisibleIdWidth(false);
+     final int idWidth = getVisibleIdWidth(false);
  
      int maxwidth = av.getAlignment().getVisibleWidth();
  
       * draw sequence ids and annotation labels (if shown)
       */
      IdCanvas idCanvas = getIdPanel().getIdCanvas();
-     idCanvas.drawIdsWrapped((Graphics2D) g, av, 0, totalHeight);
+     idCanvas.drawIdsWrapped((Graphics2D) g, av, 0, totalHeight, idWidth);
  
      g.translate(idWidth, 0);
  
     *          be returned
     * @return
     */
 -  public int getVisibleIdWidth(boolean onscreen)
 +  protected int getVisibleIdWidth(boolean onscreen)
    {
      // see if rendering offscreen - check preferences and calc width accordingly
      if (!onscreen && Cache.getDefault("FIGURE_AUTOIDWIDTH", false))
      {
 -      return calculateIdWidth(-1).width + 4;
 +      return calculateIdWidth(-1).width;
      }
 -    Integer idwidth = null;
 -    if (onscreen || (idwidth = Cache
 -            .getIntegerProperty("FIGURE_FIXEDIDWIDTH")) == null)
 +    Integer idwidth = onscreen ? null
 +            : Cache.getIntegerProperty("FIGURE_FIXEDIDWIDTH");
 +    if (idwidth != null)
      {
 -      int w = getIdPanel().getWidth();
 -      return (w > 0 ? w : calculateIdWidth().width + 4);
 +      return idwidth.intValue() + ID_WIDTH_PADDING;
      }
 -    return idwidth.intValue() + 4;
 +
 +    int w = getIdPanel().getWidth();
 +    return (w > 0 ? w : calculateIdWidth().width);
    }
  
    void makeAlignmentImage(jalview.util.ImageMaker.TYPE type, File file)
   */
  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;
@@@ -33,10 -37,6 +33,10 @@@ import java.util.List
  
  import javax.swing.JPanel;
  
 +import jalview.datamodel.SequenceI;
 +import jalview.viewmodel.ViewportListenerI;
 +import jalview.viewmodel.ViewportRanges;
 +
  /**
   * DOCUMENT ME!
   * 
@@@ -85,20 -85,21 +85,21 @@@ public class IdCanvas extends JPanel im
     * DOCUMENT ME!
     * 
     * @param g
-    *          DOCUMENT ME!
+    *                     DOCUMENT ME!
     * @param hiddenRows
-    *          true - check and display hidden row marker if need be
+    *                     true - check and display hidden row marker if need be
     * @param s
-    *          DOCUMENT ME!
+    *                     DOCUMENT ME!
     * @param i
-    *          DOCUMENT ME!
+    *                     DOCUMENT ME!
     * @param starty
-    *          DOCUMENT ME!
+    *                     DOCUMENT ME!
     * @param ypos
-    *          DOCUMENT ME!
+    *                     DOCUMENT ME!
+    * @param idWidth
     */
    public void drawIdString(Graphics2D g, boolean hiddenRows, SequenceI s,
-           int i, int starty, int ypos)
+           int i, int starty, int ypos, int idWidth)
    {
      int xPos = 0;
      int panelWidth = getWidth();
      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);
+       drawMarker(g, av, i, starty, ypos, idWidth);
      }
  
    }
  
      gg.translate(0, transY);
  
-     drawIds(gg, av, ss, es, searchResults);
+     drawIds(gg, av, ss, es, searchResults, getWidth());
  
      gg.translate(0, -transY);
  
      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, getWidth());
      
      g.drawImage(image, 0, 0, this);
    }
    /**
     * 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.
+    * sequences included in the selection are coloured grey, otherwise the current
+    * id colour for the sequence id is used.
     * 
     * @param g
     * @param alignViewport
     * @param startSeq
     * @param endSeq
     * @param selection
+    * @param idWidth
     */
    void drawIds(Graphics2D g, AlignViewport alignViewport, final int startSeq,
-           final int endSeq, List<SequenceI> selection)
+           final int endSeq, List<SequenceI> selection, int idWidth)
    {
      Font font = alignViewport.getFont();
      if (alignViewport.isSeqNameItalics())
  
      if (alignViewport.getWrapAlignment())
      {
-       drawIdsWrapped(g, alignViewport, startSeq, getHeight());
+       drawIdsWrapped(g, alignViewport, startSeq, getHeight(), getWidth());
        return;
      }
  
        g.drawString(string, xPos, (((i - startSeq) * charHeight) + charHeight)
                - (charHeight / 5));
  
 -      if (hasHiddenRows)
 +      if (hasHiddenRows && av.getShowHiddenMarkers())
        {
-         drawMarker(g, alignViewport, i, startSeq, 0);
+         drawMarker(g, alignViewport, i, startSeq, 0, idWidth);
        }
      }
    }
     * @param g
     * @param alignViewport
     * @param startSeq
+    * @param idWidth
     */
    void drawIdsWrapped(Graphics2D g, AlignViewport alignViewport,
-           int startSeq, int pageHeight)
+           int startSeq, int pageHeight, int idWidth)
    {
      int alignmentWidth = alignViewport.getAlignment().getWidth();
      final int alheight = alignViewport.getAlignment().getHeight();
          {
            g.setFont(getIdfont());
          }
-         drawIdString(g, hasHiddenRows, s, i, 0, ypos);
+         drawIdString(g, hasHiddenRows, s, i, 0, ypos, idWidth);
        }
  
        if (labels != null && alignViewport.isShowAnnotation())
    }
  
    /**
-    * Draws a marker (a blue right-pointing triangle) between sequences to
-    * indicate hidden sequences.
+    * Draws a marker (a blue right-pointing triangle) between sequences to indicate
+    * hidden sequences.
     * 
     * @param g
     * @param alignViewport
     * @param seqIndex
     * @param starty
     * @param yoffset
+    * @param idWidth
     */
-   void drawMarker(Graphics2D g, AlignViewport alignViewport, int seqIndex, int starty, int yoffset)
+   void drawMarker(Graphics2D g, AlignViewport alignViewport, int seqIndex, int starty, int yoffset, int idWidth)
    {
      SequenceI[] hseqs = alignViewport.getAlignment()
              .getHiddenSequences().hiddenSequences;
      /*
       * vertices of the triangle, below or above hidden seqs
       */
-     int[] xPoints = new int[]
-     { getWidth() - charHeight,
-         getWidth() - charHeight, getWidth() };
+     int[] xPoints = new int[] { idWidth - charHeight, idWidth - charHeight,
+         idWidth };
      int yShift = seqIndex - starty;
  
      if (below)