1 package net.sf.jga.swing; 33 34 import java.awt.Color ; 35 import java.awt.Component ; 36 import javax.swing.DefaultListCellRenderer ; 37 import javax.swing.Icon ; 38 import javax.swing.JLabel ; 39 import javax.swing.JList ; 40 import net.sf.jga.fn.UnaryFunctor; 41 import net.sf.jga.fn.string.DefaultFormat; 42 import net.sf.jga.util.Formattable; 43 44 53 54 public class GenericListCellRenderer<T> extends DefaultListCellRenderer implements Formattable<T> { 55 56 static final long serialVersionUID = -8781266653605837205L; 57 58 private UnaryFunctor<T,String > _uf; 59 60 public GenericListCellRenderer() { 61 this(new DefaultFormat<T>()); 62 } 63 64 public GenericListCellRenderer (UnaryFunctor<T,String > uf) { 65 if (uf == null) { 66 throw new IllegalArgumentException ("Non-null functor required"); 67 } 68 69 _uf = uf; 70 } 71 72 73 public Component getListCellRendererComponent(JList list, Object value, int idx, 74 boolean sel, boolean focus) 75 { 76 Component comp = super.getListCellRendererComponent(list, value, idx, sel, focus); 77 if (comp instanceof JLabel && ! (value instanceof Icon )) { 78 ((JLabel ) comp).setText(_uf.fn((T)value)); 81 } 82 83 return comp; 84 } 85 86 90 93 public void setFormat(UnaryFunctor<T,String > formatter) { 94 if (formatter == null) { 95 throw new IllegalArgumentException ("Non-null functor required"); 96 } 97 98 _uf = formatter; 99 } 100 } 101 102 103 | Popular Tags |