From af891ca5a64368cff67d242bbac4eda9dd43a42a Mon Sep 17 00:00:00 2001 From: gmungoc Date: Tue, 1 Aug 2017 14:30:33 +0200 Subject: [PATCH] JAL-2636 draw scale marks up to second last column --- src/jalview/renderer/ScaleRenderer.java | 53 +++++++++++++++++--------- test/jalview/renderer/ScaleRendererTest.java | 13 +++++-- 2 files changed, 46 insertions(+), 20 deletions(-) diff --git a/src/jalview/renderer/ScaleRenderer.java b/src/jalview/renderer/ScaleRenderer.java index 9fec256..d92608c 100644 --- a/src/jalview/renderer/ScaleRenderer.java +++ b/src/jalview/renderer/ScaleRenderer.java @@ -34,12 +34,24 @@ import java.util.List; */ public class ScaleRenderer { + /** + * Represents one major or minor scale mark + */ public final class ScaleMark { + /** + * true for a major scale mark, false for minor + */ public final boolean major; + /** + * visible column position (0..) e.g. 19 + */ public final int column; + /** + * text (if any) to show e.g. "20" + */ public final String text; ScaleMark(boolean isMajor, int col, String txt) @@ -48,19 +60,27 @@ public class ScaleRenderer column = col; text = txt; } + + /** + * String representation for inspection when debugging only + */ + @Override + public String toString() + { + return String.format("%s:%d:%s", major ? "major" : "minor", column, + text); + } } /** - * calculate positions markers on the alignment ruler + * Calculates position markers on the alignment ruler * * @param av * @param startx - * left-most column in visible view + * left-most column in visible view (0..) * @param endx - * - right-most column in visible view - * @return List of ScaleMark holding boolean: true/false for major/minor mark, - * marker position in alignment column coords, a String to be rendered - * at the position (or null) + * - right-most column in visible view (0..) + * @return */ public List calculateMarks(AlignViewportI av, int startx, int endx) @@ -87,41 +107,40 @@ public class ScaleRenderer scalestartx += 5; } List marks = new ArrayList(); - String string; - int refN, iadj; // todo: add a 'reference origin column' to set column number relative to - for (int i = scalestartx; i < endx; i += 5) + for (int i = scalestartx; i <= endx; i += 5) { if (((i - refSp) % 10) == 0) { + String text; if (refSeq == null) { - iadj = av.getAlignment().getHiddenColumns() + int iadj = av.getAlignment().getHiddenColumns() .adjustForHiddenColumns(i - 1) + 1; - string = String.valueOf(iadj); + text = String.valueOf(iadj); } else { - iadj = av.getAlignment().getHiddenColumns() + int iadj = av.getAlignment().getHiddenColumns() .adjustForHiddenColumns(i - 1); - refN = refSeq.findPosition(iadj); + int refN = refSeq.findPosition(iadj); // TODO show bounds if position is a gap // - ie L--R -> "1L|2R" for // marker if (iadj < refStartI) { - string = String.valueOf(iadj - refStartI); + text = String.valueOf(iadj - refStartI); } else if (iadj > refEndI) { - string = "+" + String.valueOf(iadj - refEndI); + text = "+" + String.valueOf(iadj - refEndI); } else { - string = String.valueOf(refN) + refSeq.getCharAt(iadj); + text = String.valueOf(refN) + refSeq.getCharAt(iadj); } } - marks.add(new ScaleMark(true, i - startx - 1, string)); + marks.add(new ScaleMark(true, i - startx - 1, text)); } else { diff --git a/test/jalview/renderer/ScaleRendererTest.java b/test/jalview/renderer/ScaleRendererTest.java index cf1039f..0af67cd 100644 --- a/test/jalview/renderer/ScaleRendererTest.java +++ b/test/jalview/renderer/ScaleRendererTest.java @@ -26,11 +26,11 @@ public class ScaleRendererTest AlignViewport av = af.getViewport(); /* - * scale has minor ticks at 5 and 15, major at 10 and 20 + * scale has minor ticks at 5, 15, 25, major at 10 and 20 * (these are base 1, ScaleMark holds base 0 values) */ List marks = new ScaleRenderer().calculateMarks(av, 0, 25); - assertEquals(marks.size(), 4); + assertEquals(marks.size(), 5); assertFalse(marks.get(0).major); assertEquals(marks.get(0).column, 4); @@ -48,6 +48,10 @@ public class ScaleRendererTest assertEquals(marks.get(3).column, 19); assertEquals(marks.get(3).text, "20"); + assertFalse(marks.get(4).major); + assertEquals(marks.get(4).column, 24); + assertNull(marks.get(4).text); + /* * now hide columns 9-11 and 18-20 (base 1) * scale marks are now in the same columns as before, but @@ -56,7 +60,7 @@ public class ScaleRendererTest av.hideColumns(8, 10); av.hideColumns(17, 19); marks = new ScaleRenderer().calculateMarks(av, 0, 25); - assertEquals(marks.size(), 4); + assertEquals(marks.size(), 5); assertFalse(marks.get(0).major); assertEquals(marks.get(0).column, 4); assertNull(marks.get(0).text); @@ -69,5 +73,8 @@ public class ScaleRendererTest assertTrue(marks.get(3).major); assertEquals(marks.get(3).column, 19); assertEquals(marks.get(3).text, "26"); // +6 hidden columns + assertFalse(marks.get(4).major); + assertEquals(marks.get(4).column, 24); + assertNull(marks.get(4).text); } } -- 1.7.10.2