String (@Nullable String text, int maxWidth, FontMetrics metrics) { if (text == null || StringUtil.isEmpty(text)) { return ""; } final int strWidth = getStringWidth(text, metrics); if (strWidth <= maxWidth || myIsSelected) { return text; } if (getStringWidth(ELLIPSIS, metrics) > maxWidth) { return ""; } int insIndex = ObjectUtils.binarySearch(0, text.length(), mid ->{ final String candidate = text.substring(0, mid) + ELLIPSIS; final int width = getStringWidth(candidate, metrics); return width <= maxWidth ? -1 : 1; }); int i = Math.max(0,-insIndex-2); return text.substring(0, i) + ELLIPSIS; }