Coverage Report - org.homeunix.thecave.buddi.view.swing.MaxLengthListCellRenderer
 
Classes in this File Line Coverage Branch Coverage Complexity
MaxLengthListCellRenderer
92%
12/13
70%
7/10
3.5
 
 1  
 /*
 2  
  * Created on Aug 7, 2007 by wyatt
 3  
  */
 4  
 package org.homeunix.thecave.buddi.view.swing;
 5  
 
 6  
 import java.awt.Component;
 7  
 
 8  
 import javax.swing.DefaultListCellRenderer;
 9  
 import javax.swing.JComponent;
 10  
 import javax.swing.JList;
 11  
 
 12  
 import org.homeunix.thecave.buddi.util.Formatter;
 13  
 
 14  
 public class MaxLengthListCellRenderer extends DefaultListCellRenderer {
 15  
         public static final long serialVersionUID = 0;
 16  
 
 17  
         private final JComponent component;
 18  
 
 19  3598
         private int computedLength = -1;
 20  
 
 21  3598
         public MaxLengthListCellRenderer(JComponent component) {
 22  3598
                 this.component = component;
 23  3598
         }
 24  
 
 25  
         @Override
 26  
         public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
 27  32298
                 super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
 28  
 
 29  32298
                 if (value != null){
 30  32298
                         String s = value.toString();
 31  32298
                         if (index == -1 && s.length() > computedLength && component != null && component.getGraphics() != null)
 32  
                                 //The width - 55 is obtained by trial and error on the Mac.  In general, the Mac LnF
 33  
                                 // has more padding than most others, so this should be more than enough for others.
 34  
                                 // This offset prevents the combo box button from interfering with the display
 35  
                                 // of oversized values, by making the value wrap at the word level, and remove the
 36  
                                 // "..." at the end of long values.
 37  4988
                                 this.setText(Formatter.getStringToLength(s, component.getWidth() - 55, component.getGraphics().getFontMetrics()));
 38  
                         else
 39  27310
                                 this.setText(s);
 40  32298
                 }
 41  
                 else {
 42  0
                         this.setText(" ");
 43  
                 }
 44  
 
 45  32298
                 return this;
 46  
         }
 47  
 }