AdtUiUtils { public static String shrinkToFit(String text, FontMetrics metrics, float availableSpace) { return shrinkToFit(text, s -> metrics.stringWidth(s) <= availableSpace); } private  AdtUiUtils(); static String shrinkToFit(String text, FontMetrics metrics, float availableSpace); static String shrinkToFit(String text, Predicate<String> textFitPredicate); static int unscale(int i); static Color overlayColor(int backgroundRgb, int foregroundRbg, float foregroundOpacity); static boolean isActionKeyDown(@NotNull InputEvent event); @JdkConstants.InputEventMask static int getActionMask(); static String getActionKeyText(); static JComponent createHorizontalSeparator(); @NotNull static JBScrollPane createNestedVScrollPane(@NotNull JComponent component); static final JBFont DEFAULT_FONT; static final JBFont EMPTY_TOOL_WINDOW_FONT; static final Color DEFAULT_FONT_COLOR; static final Color DEFAULT_BORDER_COLOR; static final Border DEFAULT_TOP_BORDER; static final Border DEFAULT_LEFT_BORDER; static final Border DEFAULT_BOTTOM_BORDER; static final Border DEFAULT_RIGHT_BORDER; static final Border DEFAULT_HORIZONTAL_BORDERS; static final Border DEFAULT_VERTICAL_BORDERS; static final GridBagConstraints GBC_FULL; }
@Test public void testShrinkToFit() { JLabel testLabel = new JLabel("Test"); FontMetrics testMetrics = testLabel.getFontMetrics(AdtUiUtils.DEFAULT_FONT); String testString = StringUtil.repeat("A", 100); int stringWidth = testMetrics.stringWidth(testString); int ellipsisWidth = testMetrics.stringWidth(ELLIPSIS); int perCharacterWidth = testMetrics.stringWidth("A"); assertEquals(testString, AdtUiUtils.shrinkToFit(testString, testMetrics, stringWidth)); assertEquals("", AdtUiUtils.shrinkToFit(testString, testMetrics, ellipsisWidth - 1)); assertEquals("", AdtUiUtils.shrinkToFit(testString, testMetrics, ellipsisWidth)); for (int i = 5; i < 80; ++i) { int spaceAvailable = i * perCharacterWidth; String shrunk = AdtUiUtils.shrinkToFit(testString, s -> testMetrics.stringWidth(s) <= spaceAvailable + ellipsisWidth); assertEquals(StringUtil.repeat("A", i) + "...", shrunk); } }