From bf840600fcefa0ef93aa186f3ed023873c1f583c Mon Sep 17 00:00:00 2001 From: RequestPrivacy Date: Mon, 14 Nov 2022 11:03:05 +0100 Subject: [PATCH] Add uniform string length formatting to MixStatusCell text --- .../sparrow/control/MixStatusCell.java | 64 ++++++++++++++++++- 1 file changed, 63 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/sparrowwallet/sparrow/control/MixStatusCell.java b/src/main/java/com/sparrowwallet/sparrow/control/MixStatusCell.java index 79271a2e..b958a203 100644 --- a/src/main/java/com/sparrowwallet/sparrow/control/MixStatusCell.java +++ b/src/main/java/com/sparrowwallet/sparrow/control/MixStatusCell.java @@ -21,6 +21,7 @@ import java.util.Locale; public class MixStatusCell extends TreeTableCell { private static final int ERROR_DISPLAY_MILLIS = 5 * 60 * 1000; + private static int maxMixesDoneDigitCount = 0; public MixStatusCell() { super(); @@ -40,7 +41,7 @@ public class MixStatusCell extends TreeTableCell { setText(null); setGraphic(null); } else { - setText(Integer.toString(mixStatus.getMixesDone())); + setText(formatMixesDone(mixStatus.getMixesDone())); if(mixStatus.getNextMixUtxo() == null) { setContextMenu(new MixStatusContextMenu(mixStatus.getUtxoEntry(), mixStatus.getMixProgress() != null && mixStatus.getMixProgress().getMixStep() != MixStep.FAIL)); } else { @@ -211,4 +212,65 @@ public class MixStatusCell extends TreeTableCell { } } } + + /** + * Provides an equal length for all displayed mixesDone cell texts by reserving enough space to hold the largest + * mix count. + * @param mixesDone + * @return A string representation of mixesDone but of the same string length as the greatest mix + * count in the column. + */ + private String formatMixesDone(int mixesDone) { + int mixesDoneDigits = calcDigitCount(mixesDone); + if (mixesDoneDigits > maxMixesDoneDigitCount) maxMixesDoneDigitCount = mixesDoneDigits; + String formattingString = "%1$" + maxMixesDoneDigitCount + "s"; + + return String.format(formattingString, mixesDone); + } + + /** + * Util method to count the digits of a number excluding its sign by a divide and conquer algorithm. + * @param number + * @return An integer representing the number of digits in the provided integer. + */ + private int calcDigitCount(int number) { + number = Math.abs(number); + if (number < 100000) { + if (number < 100) { + if (number < 10) { + return 1; + } else { + return 2; + } + } else { + if (number < 1000) { + return 3; + } else { + if (number < 10000) { + return 4; + } else { + return 5; + } + } + } + } else { + if (number < 10000000) { + if (number < 1000000) { + return 6; + } else { + return 7; + } + } else { + if (number < 100000000) { + return 8; + } else { + if (number < 1000000000) { + return 9; + } else { + return 10; + } + } + } + } + } }