1 19 20 package org.netbeans.modules.versioning.util; 21 22 import javax.swing.table.DefaultTableCellRenderer ; 23 import java.awt.FontMetrics ; 24 import java.awt.Graphics ; 25 26 31 public class FilePathCellRenderer extends DefaultTableCellRenderer { 32 33 private static final int VISIBLE_START_CHARS = 0; 34 35 private String computeFitText(String text) { 36 if (text.length() <= VISIBLE_START_CHARS + 3) return text; 37 38 FontMetrics fm = getFontMetrics(getFont()); 39 int width = getSize().width; 40 41 String prefix = text.substring(0, VISIBLE_START_CHARS) + "..."; 42 int prefixLength = fm.stringWidth(prefix); 43 int desired = width - prefixLength - 2; 44 if (desired <= 0) return text; 45 46 for (int i = text.length() - 1; i >= 0; i--) { 47 String suffix = text.substring(i); 48 int swidth = fm.stringWidth(suffix); 49 if (swidth >= desired) { 50 return suffix.length() > 0 ? prefix + suffix.substring(1) : text; 51 } 52 } 53 return text; 54 } 55 56 protected void paintComponent(Graphics g) { 57 setText(computeFitText(getText())); 58 super.paintComponent(g); 59 } 60 } 61 | Popular Tags |