From 3115ddc974d7aae39e7c072ea9d451b4788dbe13 Mon Sep 17 00:00:00 2001 From: gmungoc Date: Fri, 24 Aug 2018 16:16:08 +0100 Subject: [PATCH] JAL-247 print/export wrapped uses common code to draw seq ids and annotation labels --- src/jalview/gui/AlignmentPanel.java | 87 +++++++++--------------------- src/jalview/gui/IdCanvas.java | 99 +++++++++++++++++++---------------- 2 files changed, 79 insertions(+), 107 deletions(-) diff --git a/src/jalview/gui/AlignmentPanel.java b/src/jalview/gui/AlignmentPanel.java index 1ac5536..9cf01fa 100644 --- a/src/jalview/gui/AlignmentPanel.java +++ b/src/jalview/gui/AlignmentPanel.java @@ -1073,31 +1073,27 @@ public class AlignmentPanel extends GAlignmentPanel implements } /** - * DOCUMENT ME! + * Prints one page of an alignment in wrapped mode. Returns + * Printable.PAGE_EXISTS (0) if a page was drawn, or Printable.NO_SUCH_PAGE if + * no page could be drawn (page number out of range). * - * @param pg - * DOCUMENT ME! - * @param pwidth - * DOCUMENT ME! - * @param pheight - * DOCUMENT ME! - * @param pi - * DOCUMENT ME! + * @param pageWidth + * @param pageHeight + * @param pageNumber + * (0, 1, ...) + * @param g * - * @return DOCUMENT ME! + * @return * * @throws PrinterException - * DOCUMENT ME! */ - public int printWrappedAlignment(int pwidth, int pheight, int pi, - Graphics pg) throws PrinterException + public int printWrappedAlignment(int pageWidth, int pageHeight, int pageNumber, + Graphics g) throws PrinterException { int annotationHeight = 0; - AnnotationLabels labels = null; if (av.isShowAnnotation()) { annotationHeight = getAnnotationPanel().adjustPanelHeight(); - labels = new AnnotationLabels(av); } int hgap = av.getCharHeight(); @@ -1119,64 +1115,33 @@ public class AlignmentPanel extends GAlignmentPanel implements } int resWidth = getSeqPanel().seqCanvas - .getWrappedCanvasWidth(pwidth - idWidth); + .getWrappedCanvasWidth(pageWidth - idWidth); int totalHeight = cHeight * (maxwidth / resWidth + 1); - pg.setColor(Color.white); - pg.fillRect(0, 0, pwidth, pheight); - pg.setFont(av.getFont()); - - // ////////////// - // Draw the ids - pg.setColor(Color.black); + g.setColor(Color.white); + g.fillRect(0, 0, pageWidth, pageHeight); + g.setFont(av.getFont()); + g.setColor(Color.black); - pg.translate(0, -pi * pheight); + g.translate(0, -pageNumber * pageHeight); - pg.setClip(0, pi * pheight, pwidth, pheight); + g.setClip(0, pageNumber * pageHeight, pageWidth, pageHeight); - int ypos = hgap; - - do - { - for (int i = 0; i < av.getAlignment().getHeight(); i++) - { - pg.setFont(getIdPanel().getIdCanvas().getIdfont()); - SequenceI s = av.getAlignment().getSequenceAt(i); - String string = s.getDisplayId(av.getShowJVSuffix()); - int xPos = 0; - if (av.isRightAlignIds()) - { - FontMetrics fm = pg.getFontMetrics(); - xPos = idWidth - fm.stringWidth(string) - 4; - } - pg.drawString(string, xPos, - ((i * av.getCharHeight()) + ypos + av.getCharHeight()) - - (av.getCharHeight() / 5)); - } - if (labels != null) - { - pg.translate(-3, ypos - + (av.getAlignment().getHeight() * av.getCharHeight())); - - pg.setFont(av.getFont()); - labels.drawComponent(pg, idWidth); - pg.translate(+3, -ypos - - (av.getAlignment().getHeight() * av.getCharHeight())); - } - - ypos += cHeight; - } while (ypos < totalHeight); + /* + * draw sequence ids and annotation labels (if shown) + */ + IdCanvas idCanvas = getIdPanel().getIdCanvas(); + idCanvas.drawIdsWrapped((Graphics2D) g, av, 0, totalHeight); - pg.translate(idWidth, 0); + g.translate(idWidth, 0); - getSeqPanel().seqCanvas.drawWrappedPanelForPrinting(pg, pwidth - idWidth, + getSeqPanel().seqCanvas.drawWrappedPanelForPrinting(g, pageWidth - idWidth, totalHeight, 0); - if ((pi * pheight) < totalHeight) + if ((pageNumber * pageHeight) < totalHeight) { return Printable.PAGE_EXISTS; - } else { diff --git a/src/jalview/gui/IdCanvas.java b/src/jalview/gui/IdCanvas.java index 484173c..cf88c90 100755 --- a/src/jalview/gui/IdCanvas.java +++ b/src/jalview/gui/IdCanvas.java @@ -258,19 +258,19 @@ public class IdCanvas extends JPanel implements ViewportListenerI } /** - * 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 selection) + void drawIds(Graphics2D g, AlignViewport alignViewport, final int startSeq, + final int endSeq, List selection) { Font font = alignViewport.getFont(); if (alignViewport.isSeqNameItalics()) @@ -295,11 +295,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 +308,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 +328,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 +343,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 +356,63 @@ 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(); + int alignmentWidth = alignViewport.getAlignment().getWidth(); + final int alheight = alignViewport.getAlignment().getHeight(); - if (av.hasHiddenColumns()) + if (alignViewport.hasHiddenColumns()) { - maxwidth = av.getAlignment().getHiddenColumns() - .absoluteToVisibleColumn(maxwidth) - 1; + alignmentWidth = alignViewport.getAlignment().getHiddenColumns() + .absoluteToVisibleColumn(alignmentWidth) - 1; } 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 +420,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; } } -- 1.7.10.2