1 7 8 package javax.swing.plaf.synth; 9 10 import javax.swing.table.*; 11 import javax.swing.*; 12 import javax.swing.event.*; 13 import java.util.Enumeration ; 14 import java.util.Date ; 15 import java.awt.event.*; 16 import java.awt.*; 17 import java.beans.*; 18 import javax.swing.border.*; 19 import javax.swing.plaf.*; 20 import javax.swing.plaf.basic.BasicTableHeaderUI ; 21 import sun.swing.plaf.synth.SynthUI; 22 23 30 class SynthTableHeaderUI extends BasicTableHeaderUI implements 31 PropertyChangeListener, SynthUI { 32 33 37 private TableCellRenderer prevRenderer = null; 38 39 private SynthStyle style; 40 41 public static ComponentUI createUI(JComponent h) { 42 return new SynthTableHeaderUI (); 43 } 44 45 protected void installDefaults() { 46 prevRenderer = header.getDefaultRenderer(); 47 if (prevRenderer instanceof UIResource) { 48 header.setDefaultRenderer(new HeaderRenderer()); 49 } 50 updateStyle(header); 51 } 52 53 private void updateStyle(JTableHeader c) { 54 SynthContext context = getContext(c, ENABLED); 55 SynthStyle oldStyle = style; 56 style = SynthLookAndFeel.updateStyle(context, this); 57 if (style != oldStyle) { 58 if (oldStyle != null) { 59 uninstallKeyboardActions(); 60 installKeyboardActions(); 61 } 62 } 63 context.dispose(); 64 } 65 66 protected void installListeners() { 67 super.installListeners(); 68 header.addPropertyChangeListener(this); 69 } 70 71 protected void uninstallDefaults() { 72 if (header.getDefaultRenderer() instanceof HeaderRenderer) { 73 header.setDefaultRenderer(prevRenderer); 74 } 75 76 SynthContext context = getContext(header, ENABLED); 77 78 style.uninstallDefaults(context); 79 context.dispose(); 80 style = null; 81 } 82 83 protected void uninstallListeners() { 84 header.removePropertyChangeListener(this); 85 super.uninstallListeners(); 86 } 87 88 public void update(Graphics g, JComponent c) { 89 SynthContext context = getContext(c); 90 91 SynthLookAndFeel.update(context, g); 92 context.getPainter().paintTableHeaderBackground(context, 93 g, 0, 0, c.getWidth(), c.getHeight()); 94 paint(context, g); 95 context.dispose(); 96 } 97 98 public void paint(Graphics g, JComponent c) { 99 SynthContext context = getContext(c); 100 101 paint(context, g); 102 context.dispose(); 103 } 104 105 protected void paint(SynthContext context, Graphics g) { 106 super.paint(g, context.getComponent()); 107 } 108 109 public void paintBorder(SynthContext context, Graphics g, int x, 110 int y, int w, int h) { 111 context.getPainter().paintTableHeaderBorder(context, g, x, y, w, h); 112 } 113 public SynthContext getContext(JComponent c) { 117 return getContext(c, getComponentState(c)); 118 } 119 120 private SynthContext getContext(JComponent c, int state) { 121 return SynthContext.getContext(SynthContext .class, c, 122 SynthLookAndFeel.getRegion(c), style, state); 123 } 124 125 private Region getRegion(JComponent c) { 126 return SynthLookAndFeel.getRegion(c); 127 } 128 129 private int getComponentState(JComponent c) { 130 return SynthLookAndFeel.getComponentState(c); 131 } 132 133 public void propertyChange(PropertyChangeEvent evt) { 134 if (SynthLookAndFeel.shouldUpdateStyle(evt)) { 135 updateStyle((JTableHeader)evt.getSource()); 136 } 137 } 138 139 140 private static class HeaderRenderer extends DefaultTableCellRenderer 141 implements UIResource { 142 HeaderRenderer() { 143 setHorizontalAlignment(JLabel.LEADING); 144 } 145 146 public void setBorder(Border border) { 147 if (border instanceof SynthBorder ) { 148 super.setBorder(border); 149 } 150 } 151 152 public String getName() { 153 String name = super.getName(); 154 if (name == null) { 155 return "TableHeader.renderer"; 156 } 157 return name; 158 } 159 160 public Component getTableCellRendererComponent( 161 JTable table, Object value, boolean isSelected, 162 boolean hasFocus, int row, int column) { 163 setText((value == null) ? "" : value.toString()); 164 return this; 165 } 166 } 167 } 168 | Popular Tags |