1 package com.calipso.reportgenerator.userinterface; 2 import javax.swing.table.TableCellRenderer ; 3 import javax.swing.*; 4 import javax.swing.border.LineBorder ; 5 import java.awt.*; 6 import java.net.URL ; 7 8 11 public class HeaderTableRenderer extends JPanel implements TableCellRenderer { 12 private JLabel cellLabel; 13 private Icon collapsedIcon; 14 private Icon expandedIcon; 15 18 public HeaderTableRenderer() { 19 cellLabel = new JLabel(); 20 setLayout(new BorderLayout()); 21 add(cellLabel, BorderLayout.NORTH); 22 setBackground(new Color(204, 204, 204)); 23 setBorder(new LineBorder (Color.GRAY, 1)); 24 } 25 26 30 protected void setValue(Object value) { 31 cellLabel.setText((value == null) ? "" : value.toString()); 32 } 33 41 public Component getCellComponent(JTable table, Object value, boolean isCollapsable, boolean isCollapsed){ 42 cellLabel.setFont(table.getFont()); 43 if (isCollapsable){ 44 cellLabel.setIcon(getIcon(isCollapsed)); 45 } 46 else { 47 cellLabel.setIcon(null); 48 } 49 setValue(value); 50 return this; 51 } 52 53 58 private Icon getIcon(boolean collapsed) { 59 if (collapsed){ 60 return getCollapsedIcon(); 61 } 62 else { 63 return getExpandedIcon(); 64 } 65 } 66 67 71 public int getIconWidth(){ 72 return getCollapsedIcon().getIconWidth(); 73 } 74 75 79 public int getIconHeight(){ 80 return getCollapsedIcon().getIconHeight(); 81 } 82 83 84 88 private Icon getExpandedIcon() { 89 if (expandedIcon == null){ 90 ClassLoader loader = this.getClass().getClassLoader(); 91 URL url = loader.getResource("com/calipso/reportgenerator/userinterface/images/collapse.gif"); 92 expandedIcon = new ImageIcon(url); 93 } 94 return expandedIcon; 95 } 96 97 101 private Icon getCollapsedIcon() { 102 if (collapsedIcon == null){ 103 ClassLoader loader = this.getClass().getClassLoader(); 104 URL url = loader.getResource("com/calipso/reportgenerator/userinterface/images/expand.gif"); 105 collapsedIcon = new ImageIcon(url); 106 } 107 return collapsedIcon; 108 } 109 110 120 public Component getTableCellRendererComponent(JTable table, Object value, 121 boolean isSelected, boolean hasFocus, 122 int row, int column) { 123 return null; 124 } 125 126 127 } | Popular Tags |