001: /*
002: * PgsTableHeaderUI.java
003: *
004: * Created on 17. April 2005, 09:40
005: */
006:
007: package com.pagosoft.plaf;
008:
009: import javax.swing.*;
010: import javax.swing.plaf.*;
011: import javax.swing.plaf.basic.*;
012: import javax.swing.table.*;
013: import java.awt.*;
014:
015: /**
016: * @author pago
017: */
018: public class PgsTableHeaderUI extends BasicTableHeaderUI {
019: public static ComponentUI createUI(JComponent h) {
020: return new PgsTableHeaderUI();
021: }
022:
023: /**
024: * Creates a new instance of PgsTableHeaderUI
025: */
026: public PgsTableHeaderUI() {
027: super ();
028: }
029:
030: public void paint(Graphics g, JComponent c) {
031: if (header.getColumnModel().getColumnCount() <= 0) {
032: return;
033: }
034: boolean ltr = header.getComponentOrientation().isLeftToRight();
035:
036: Rectangle clip = g.getClipBounds();
037: Point left = clip.getLocation();
038: Point right = new Point(clip.x + clip.width - 1, clip.y);
039: TableColumnModel cm = header.getColumnModel();
040: int cMin = header.columnAtPoint(ltr ? left : right);
041: int cMax = header.columnAtPoint(ltr ? right : left);
042: // This should never happen.
043: if (cMin == -1) {
044: cMin = 0;
045: }
046: // If the table does not have enough columns to fill the view we'll get -1.
047: // Replace this with the index of the last column.
048: if (cMax == -1) {
049: cMax = cm.getColumnCount() - 1;
050: }
051:
052: TableColumn draggedColumn = header.getDraggedColumn();
053: int columnWidth;
054: Rectangle cellRect = header.getHeaderRect(ltr ? cMin : cMax);
055: TableColumn aColumn;
056: if (ltr) {
057: for (int column = cMin; column <= cMax; column++) {
058: aColumn = cm.getColumn(column);
059: columnWidth = aColumn.getWidth();
060: cellRect.width = columnWidth;
061: if (aColumn != draggedColumn) {
062: paintCell(g, cellRect, column);
063: }
064: cellRect.x += columnWidth;
065: }
066: } else {
067: for (int column = cMax; column >= cMin; column--) {
068: aColumn = cm.getColumn(column);
069: columnWidth = aColumn.getWidth();
070: cellRect.width = columnWidth;
071: if (aColumn != draggedColumn) {
072: paintCell(g, cellRect, column);
073: }
074: cellRect.x += columnWidth;
075: }
076: }
077:
078: // Paint the dragged column if we are dragging.
079: if (draggedColumn != null) {
080: int draggedColumnIndex = viewIndexForColumn(draggedColumn);
081: Rectangle draggedCellRect = header
082: .getHeaderRect(draggedColumnIndex);
083:
084: // Draw a gray well in place of the moving column.
085: g.setColor(header.getParent().getBackground());
086: g.fillRect(draggedCellRect.x, draggedCellRect.y,
087: draggedCellRect.width, draggedCellRect.height);
088:
089: draggedCellRect.x += header.getDraggedDistance();
090:
091: // Fill the background.
092: g.setColor(header.getBackground());
093: g.fillRect(draggedCellRect.x, draggedCellRect.y,
094: draggedCellRect.width, draggedCellRect.height);
095:
096: paintCell(g, draggedCellRect, draggedColumnIndex);
097: }
098:
099: // Remove all components in the rendererPane.
100: rendererPane.removeAll();
101: }
102:
103: private Component getHeaderRenderer(int columnIndex) {
104: TableColumn aColumn = header.getColumnModel().getColumn(
105: columnIndex);
106: TableCellRenderer renderer = aColumn.getHeaderRenderer();
107: if (renderer == null) {
108: renderer = header.getDefaultRenderer();
109: }
110: return renderer.getTableCellRendererComponent(
111: header.getTable(), aColumn.getHeaderValue(), false,
112: false, -1, columnIndex);
113: }
114:
115: private int viewIndexForColumn(TableColumn aColumn) {
116: TableColumnModel cm = header.getColumnModel();
117: for (int column = 0; column < cm.getColumnCount(); column++) {
118: if (cm.getColumn(column) == aColumn) {
119: return column;
120: }
121: }
122: return -1;
123: }
124:
125: private void paintCell(Graphics g, Rectangle cellRect,
126: int columnIndex) {
127: Component component = getHeaderRenderer(columnIndex);
128: rendererPane.paintComponent(g, component, header, cellRect.x,
129: cellRect.y, cellRect.width, cellRect.height, true);
130: }
131: }
|