String (@Nullable String text, @NotNull FontMetrics metrics, int availableWidth) { if (text == null || availableWidth <= 0) return ""; if (isFit(text, metrics, availableWidth)) return text; int ellipsisWidth = metrics.stringWidth(ellipsis); if (availableWidth <= ellipsisWidth) return ellipsis; int width = availableWidth - ellipsisWidth; if (alignment == SwingConstants.LEFT) return ellipsis + trimLeft(text, metrics, width); if (alignment == SwingConstants.RIGHT) return trimRight(text, metrics, width) + ellipsis; String postfix = trimRight(text, metrics, width / 2) + ellipsis; return postfix + trimLeft(text, metrics, availableWidth - metrics.stringWidth(postfix)); }