1 7 8 package org.jdesktop.swing.table; 9 10 import java.awt.BorderLayout ; 11 import java.awt.Color ; 12 import java.awt.Component ; 13 import java.awt.Font ; 14 import java.awt.Graphics ; 15 import java.awt.Graphics2D ; 16 import java.awt.Insets ; 17 import java.awt.Rectangle ; 18 import java.awt.RenderingHints ; 19 20 import javax.swing.Icon ; 21 import javax.swing.JLabel ; 22 import javax.swing.JPanel ; 23 import javax.swing.JTable ; 24 import javax.swing.UIManager ; 25 import javax.swing.border.Border ; 26 import javax.swing.border.CompoundBorder ; 27 import javax.swing.border.EmptyBorder ; 28 import javax.swing.table.JTableHeader ; 29 import javax.swing.table.TableCellRenderer ; 30 31 import org.jdesktop.swing.JXTable; 32 import org.jdesktop.swing.LabelProperties; 33 import org.jdesktop.swing.decorator.Sorter; 34 import org.jdesktop.swing.icon.SortArrowIcon; 35 36 42 public class ColumnHeaderRenderer extends JPanel implements TableCellRenderer { 43 44 private static TableCellRenderer sharedInstance = null; 45 private static Icon defaultDownIcon = new SortArrowIcon(false); 46 private static Icon defaultUpIcon = new SortArrowIcon(true); 47 private static Border defaultMarginBorder = new EmptyBorder (2,2,2,2); 48 49 private Icon downIcon = defaultDownIcon; 50 private Icon upIcon = defaultUpIcon; 51 private JLabel label = new JLabel ("", JLabel.CENTER); 52 private JLabel arrow = new JLabel ((Icon ) null, JLabel.CENTER); 53 private boolean antiAliasedText = false; 54 55 private boolean backgroundSet = false; 56 private boolean foregroundSet = false; 57 private boolean fontSet = false; 58 59 public static TableCellRenderer getSharedInstance() { 60 if (sharedInstance == null) { 61 sharedInstance = new ColumnHeaderRenderer(); 62 } 63 return sharedInstance; 64 } 65 66 public ColumnHeaderRenderer() { 67 setLayout(new BorderLayout ()); 68 Font boldFont = label.getFont().deriveFont(Font.BOLD); 70 label.setFont(boldFont); 71 label.setOpaque(false); 72 add(label); 73 add(arrow, BorderLayout.EAST); 74 } 75 76 public Component getTableCellRendererComponent(JTable table, Object value, 77 boolean isSelected, boolean hasFocus, int rowIndex, int columnIndex) { 78 label.setText(value == null ? "" : value.toString()); 79 JTableHeader header = table.getTableHeader(); 80 if (header != null) { 81 if (!foregroundSet) { 84 setForeground(header.getForeground()); 85 foregroundSet = false; 86 } 87 if (!backgroundSet) { 88 setBackground(header.getBackground()); 89 backgroundSet = false; 90 } 91 if (!fontSet) { 92 setFont(header.getFont()); 93 fontSet = false; 94 } 95 } 96 if (table instanceof JXTable) { 97 Sorter sorter = ((JXTable) table).getSorter(columnIndex); 99 100 if (sorter == null) { 101 arrow.setIcon(null); 102 } 103 else { 104 arrow.setIcon(sorter.isAscending() ? upIcon : downIcon); 105 } 106 } 107 108 setBorder(new CompoundBorder ( 109 UIManager.getBorder("TableHeader.cellBorder"), 110 defaultMarginBorder)); 111 return this; 112 } 113 114 public void setAntiAliasedText(boolean antiAlias) { 115 this.antiAliasedText = antiAlias; 116 } 117 118 public boolean getAntiAliasedText() { 119 return antiAliasedText; 120 } 121 122 public void setBackground(Color background) { 123 backgroundSet = true; 124 super.setBackground(background); 125 } 126 127 public void setForeground(Color foreground) { 128 foregroundSet = true; 129 super.setForeground(foreground); 130 if (label != null) { 131 label.setForeground(foreground); 132 } 133 } 134 135 public void setFont(Font font) { 136 fontSet = true; 137 super.setFont(font); 138 if (label != null) { 139 label.setFont(font); 140 } 141 } 142 143 public void setDownIcon(Icon icon) { 144 this.downIcon = icon; 145 } 146 147 public Icon getDownIcon() { 148 return downIcon; 149 } 150 151 public void setUpIcon(Icon icon) { 152 this.upIcon = icon; 153 } 154 155 public Icon getUpIcon() { 156 return upIcon; 157 } 158 159 public void setHorizontalAlignment(int alignment) { 160 label.setHorizontalAlignment(alignment); 161 } 162 163 public int getHorizontalAlignment() { 164 return label.getHorizontalAlignment(); 165 } 166 167 public void setHorizontalTextPosition(int textPosition) { 168 label.setHorizontalTextPosition(textPosition); 169 } 170 171 public int getHorizontalTextPosition() { 172 return label.getHorizontalTextPosition(); 173 } 174 175 public void setIcon(Icon icon) { 176 label.setIcon(icon); 177 } 178 179 public Icon getIcon() { 180 return label.getIcon(); 181 } 182 183 public void setIconTextGap(int iconTextGap) { 184 label.setIconTextGap(iconTextGap); 185 } 186 187 public int getIconTextGap() { 188 return label.getIconTextGap(); 189 } 190 191 public void setVerticalAlignment(int alignment) { 192 label.setVerticalAlignment(alignment); 193 } 194 195 public int getVerticalAlignment() { 196 return label.getVerticalAlignment(); 197 } 198 199 public void setVerticalTextPosition(int textPosition) { 200 label.setVerticalTextPosition(textPosition); 201 } 202 203 public int getVerticalTextPosition() { 204 return label.getVerticalTextPosition(); 205 } 206 207 public void paint(Graphics g) { 208 if (antiAliasedText) { 209 Graphics2D g2 = (Graphics2D ) g; 210 Object save = g2.getRenderingHint(RenderingHints. 211 KEY_TEXT_ANTIALIASING); 212 g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, 213 RenderingHints.VALUE_TEXT_ANTIALIAS_ON); 214 215 super.paint(g2); 216 217 g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, save); 218 } else { 219 super.paint(g); 220 } 221 } 222 } 223 | Popular Tags |