1 30 31 package com.jgoodies.looks.windows; 32 33 import java.awt.Component ; 34 import java.awt.Graphics ; 35 import java.awt.Point ; 36 import java.awt.Rectangle ; 37 38 import javax.swing.JComponent ; 39 import javax.swing.JTable ; 40 import javax.swing.plaf.ComponentUI ; 41 import javax.swing.table.TableCellRenderer ; 42 import javax.swing.table.TableColumn ; 43 import javax.swing.table.TableColumnModel ; 44 45 import com.sun.java.swing.plaf.windows.WindowsTableHeaderUI; 46 47 48 58 public final class WindowsXPTableHeaderUI extends WindowsTableHeaderUI { 59 60 private TableCellRenderer xpRenderer; 61 62 public static ComponentUI createUI(JComponent h) { 63 return new WindowsXPTableHeaderUI(); 64 } 65 66 public void installUI(JComponent c) { 67 super.installUI(c); 68 xpRenderer = header.getDefaultRenderer(); 69 } 70 71 public void uninstallUI(JComponent c) { 72 xpRenderer = null; 73 super.uninstallUI(c); 74 } 75 76 public void paint(Graphics g, JComponent c) { 77 TableColumnModel cm = header.getColumnModel(); 78 if (cm.getColumnCount() <= 0) { 79 return; 80 } 81 boolean ltr = header.getComponentOrientation().isLeftToRight(); 82 83 Rectangle clip = g.getClipBounds(); 84 Point left = clip.getLocation(); 85 Point right = new Point (clip.x + clip.width - 1, clip.y); 86 int cMin = header.columnAtPoint(ltr ? left : right); 87 int cMax = header.columnAtPoint(ltr ? right : left); 88 if (cMin == -1) { 90 cMin = 0; 91 } 92 if (cMax == -1) { 96 cMax = cm.getColumnCount() - 1; 97 } 98 99 TableColumn draggedColumn = header.getDraggedColumn(); 100 int columnWidth; 101 int columnMargin = cm.getColumnMargin(); 102 Rectangle cellRect = header.getHeaderRect(cMin); 103 TableColumn aColumn; 104 if (ltr) { 105 for (int column = cMin; column <= cMax; column++) { 106 aColumn = cm.getColumn(column); 107 columnWidth = aColumn.getWidth(); 108 cellRect.width = columnWidth - columnMargin; 109 if (aColumn != draggedColumn) { 110 paintCell(g, cellRect, column); 111 } 112 cellRect.x += columnWidth; 113 } 114 } else { 115 aColumn = cm.getColumn(cMin); 116 if (aColumn != draggedColumn) { 117 columnWidth = aColumn.getWidth(); 118 cellRect.width = columnWidth - columnMargin; 119 cellRect.x += columnMargin; 120 paintCell(g, cellRect, cMin); 121 } 122 for (int column = cMin + 1; column <= cMax; column++) { 123 aColumn = cm.getColumn(column); 124 columnWidth = aColumn.getWidth(); 125 cellRect.width = columnWidth - columnMargin; 126 cellRect.x -= columnWidth; 127 if (aColumn != draggedColumn) { 128 paintCell(g, cellRect, column); 129 } 130 } 131 } 132 133 if (draggedColumn != null) { 135 int draggedColumnIndex = viewIndexForColumn(draggedColumn); 136 Rectangle draggedCellRect = header 137 .getHeaderRect(draggedColumnIndex); 138 139 g.setColor(header.getParent().getBackground()); 141 g.fillRect(draggedCellRect.x, draggedCellRect.y, 142 draggedCellRect.width, draggedCellRect.height); 143 144 draggedCellRect.x += header.getDraggedDistance(); 145 146 g.setColor(header.getBackground()); 148 g.fillRect(draggedCellRect.x, draggedCellRect.y, 149 draggedCellRect.width, draggedCellRect.height); 150 151 paintCell(g, draggedCellRect, draggedColumnIndex); 152 } 153 154 rendererPane.removeAll(); 156 } 157 158 private void paintCell(Graphics g, Rectangle cellRect, int columnIndex) { 159 TableColumn aColumn = header.getColumnModel().getColumn(columnIndex); 160 TableCellRenderer renderer = aColumn.getHeaderRenderer(); 161 if (renderer == null) { 162 renderer = header.getDefaultRenderer(); 163 } 164 165 JTable table = header.getTable(); 166 Component c = renderer.getTableCellRendererComponent(table, 167 aColumn.getHeaderValue(), false, false, -1, columnIndex); 168 169 if (renderer != xpRenderer) { 170 rendererPane.add(c); 176 boolean nonOpaque = !c.isOpaque(); 177 if (nonOpaque) { 178 Component background = xpRenderer 179 .getTableCellRendererComponent(table, null, false, 180 false, -1, columnIndex); 181 rendererPane.paintComponent(g, background, header, cellRect.x, 182 cellRect.y, cellRect.width, cellRect.height, true); 183 184 if ((c instanceof JComponent ) 190 && (background instanceof JComponent )) { 191 ((JComponent ) c).setBorder( 192 ((JComponent ) background).getBorder()); 193 } 194 } 195 } 196 197 rendererPane.paintComponent(g, c, header, cellRect.x, cellRect.y, 198 cellRect.width, cellRect.height, true); 199 } 200 201 private int viewIndexForColumn(TableColumn aColumn) { 202 TableColumnModel cm = header.getColumnModel(); 203 for (int column = cm.getColumnCount() - 1; column >= 0; column--) { 204 if (cm.getColumn(column) == aColumn) { 205 return column; 206 } 207 } 208 return -1; 209 } 210 211 } 212 | Popular Tags |