ClassWrapperRenderer implements TableCellRenderer { public String abbreviatePackagesToSingleLetter(String abbreviatePackage, int availableWidth, FontMetrics fm) { String result = abbreviatePackage; if (fm.stringWidth(result) > availableWidth) { final java.util.List<String> split = Splitter.on('.').splitToList(result); int index = 0; while (fm.stringWidth(result) > availableWidth && index < split.size() - 1) { java.util.List<String> list = new ArrayList<>(split.size()); for (int i = 0; i < split.size(); i++) { final String s = split.get(i); list.add(i <= index && s.length() > 0 ? s.substring(0, 1) : s); } result = Joiner.on(".").join(list); index++; } } return result; } ClassWrapperRenderer(); @Override Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column); String abbreviatePackagesToSingleLetter(String abbreviatePackage, int availableWidth, FontMetrics fm); void reloadConfiguration(String propertyConfiguration);  }
@Test(dataProvider= "abbreviations") public void testAbbreviatePackagesToSingleLetter(String clazz,int width, String expected) { final FontMetrics fontMetrics = Mockito.mock(FontMetrics.class); Mockito.when(fontMetrics.stringWidth(Matchers.anyString())).thenAnswer(invocation -> ((String)invocation.getArguments()[0]).length()); final String actual = underTest.abbreviatePackagesToSingleLetter(clazz, width, fontMetrics); assertEquals(actual,expected); }