From 50fbfb08041ca4ac18e92b82e44446fa72a1f311 Mon Sep 17 00:00:00 2001 From: James Procter Date: Fri, 8 Sep 2023 16:01:29 +0100 Subject: [PATCH] JAL-4281 reinstate access to legacy id width calculation so we can import old files, and make new calculation default for both GUI and Headless modes --- src/jalview/gui/AlignmentPanel.java | 55 +++++++++++++++++++++++++++------- src/jalview/gui/IdCanvas.java | 4 +-- src/jalview/gui/IdwidthAdjuster.java | 4 +-- 3 files changed, 49 insertions(+), 14 deletions(-) diff --git a/src/jalview/gui/AlignmentPanel.java b/src/jalview/gui/AlignmentPanel.java index ce87c01..c5c7ae9 100644 --- a/src/jalview/gui/AlignmentPanel.java +++ b/src/jalview/gui/AlignmentPanel.java @@ -256,8 +256,10 @@ public class AlignmentPanel extends GAlignmentPanel implements /** * 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. + * and any bounds on label width set in preferences. + * + * The calculated width is set as a property of the viewport and the layout is + * updated. * * @return Dimension giving the maximum width of the alignment label panel * that should be used. @@ -294,10 +296,19 @@ public class AlignmentPanel extends GAlignmentPanel implements public Dimension calculateDefaultAlignmentIdWidth() { + return calculateIdWidth(-1, false, false); + } + /** + * pre 2.11.3 Id width calculation - used when importing old projects only + * @return + */ + public int getLegacyIdWidth() + { int afwidth = (alignFrame != null ? alignFrame.getWidth() : 300); int idWidth = Math.min(afwidth - 200, 2 * afwidth / 3); int maxwidth = Math.max(IdwidthAdjuster.MIN_ID_WIDTH, idWidth); - return calculateIdWidth(-1, false, false); + Dimension w = calculateIdWidthOrLegacy(true, maxwidth, false, false); + return w.width; } /** @@ -314,9 +325,34 @@ public class AlignmentPanel extends GAlignmentPanel implements return calculateIdWidth(maxwidth, true, false); } + /** + * Calculate the width of the alignment labels based on the displayed names + * and any bounds on label width set in preferences. + * + * @param maxwidth + * -1 or maximum width allowed for IdWidth + * @param includeAnnotations - when true, annotation label widths are also accounted for + * @param visibleOnly - when true, ignore label widths for hidden annotation rows + * @return Dimension giving the maximum width of the alignment label panel + * that should be used. + */ public Dimension calculateIdWidth(int maxwidth, boolean includeAnnotations, boolean visibleOnly) { + return calculateIdWidthOrLegacy(false, maxwidth, includeAnnotations, visibleOnly); + } + + /** + * legacy mode or post 2.11.3 ID width calculation + * @param legacy - uses annotation labels, not rendered label width (excludes additional decorators) + * @param maxwidth + * @param includeAnnotations + * @param visibleOnly + * @return + */ + private Dimension calculateIdWidthOrLegacy(boolean legacy, int maxwidth, + boolean includeAnnotations, boolean visibleOnly) + { Container c = new Container(); FontMetrics fm = c.getFontMetrics( @@ -338,7 +374,9 @@ public class AlignmentPanel extends GAlignmentPanel implements // Also check annotation label widths if (includeAnnotations && al.getAlignmentAnnotation() != null) { - if (Jalview.isHeadlessMode()) + fm = c.getFontMetrics(getAlabels().getFont()); + + if (!legacy || Jalview.isHeadlessMode()) { AnnotationLabels aal = getAlabels(); int stringWidth = aal.drawLabels(null, false, idWidth, false, false, fm); @@ -346,8 +384,6 @@ public class AlignmentPanel extends GAlignmentPanel implements } else { - fm = c.getFontMetrics(getAlabels().getFont()); - for (i = 0; i < al.getAlignmentAnnotation().length; i++) { AlignmentAnnotation aa = al.getAlignmentAnnotation()[i]; @@ -566,7 +602,7 @@ public class AlignmentPanel extends GAlignmentPanel implements // this is called after loading new annotation onto alignment if (alignFrame.getHeight() == 0) { - jalview.bin.Console.outPrintln("NEEDS FIXING"); + jalview.bin.Console.error("adjustAnnotationHeight called with zero height alignment window"); } validateAnnotationDimensions(true); addNotify(); @@ -599,7 +635,7 @@ public class AlignmentPanel extends GAlignmentPanel implements Dimension e = idPanel.getSize(); int idWidth = e.width; boolean manuallyAdjusted = this.getIdPanel().getIdCanvas() - .manuallyAdjusted(); + .isManuallyAdjusted(); annotationScroller.setPreferredSize(new Dimension( manuallyAdjusted ? idWidth : annotationScroller.getWidth(), annotationHeight)); @@ -1221,7 +1257,7 @@ public class AlignmentPanel extends GAlignmentPanel implements } int w = getIdPanel().getWidth(); - w = this.calculateIdWidth(-1, true, true).width; + w = calculateIdWidth(-1, true, true).width; return (w > 0 ? w : calculateIdWidth().width); } @@ -1849,5 +1885,4 @@ public class AlignmentPanel extends GAlignmentPanel implements overviewPanel = null; } } - } diff --git a/src/jalview/gui/IdCanvas.java b/src/jalview/gui/IdCanvas.java index aaded9e..c1751d2 100755 --- a/src/jalview/gui/IdCanvas.java +++ b/src/jalview/gui/IdCanvas.java @@ -451,7 +451,7 @@ public class IdCanvas extends JPanel implements ViewportListenerI int getWidth = getWidth(); int thisIdWidth = getWidth; g.translate(0, ypos + (alheight * charHeight)); - if (!manuallyAdjusted()) + if (!isManuallyAdjusted()) { int getAnnotationsIdWidth = labels.drawLabels(g, false, -1, false,forGUI, null); @@ -623,7 +623,7 @@ public class IdCanvas extends JPanel implements ViewportListenerI private boolean manuallyAdjusted = false; - public boolean manuallyAdjusted() + public boolean isManuallyAdjusted() { return manuallyAdjusted; } diff --git a/src/jalview/gui/IdwidthAdjuster.java b/src/jalview/gui/IdwidthAdjuster.java index 762612e..f1c4c83 100755 --- a/src/jalview/gui/IdwidthAdjuster.java +++ b/src/jalview/gui/IdwidthAdjuster.java @@ -179,7 +179,7 @@ public class IdwidthAdjuster extends JPanel public void setWidth(int newWidth) { if (newWidth < MIN_ID_WIDTH - || ap.getIdPanel().getIdCanvas().manuallyAdjusted()) + || ap.getIdPanel().getIdCanvas().isManuallyAdjusted()) { return; } @@ -190,7 +190,7 @@ public class IdwidthAdjuster extends JPanel public boolean manuallyAdjusted() { - return ap.getIdPanel().getIdCanvas().manuallyAdjusted(); + return ap.getIdPanel().getIdCanvas().isManuallyAdjusted(); } @Override -- 1.7.10.2