1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
| private final class InternalGridUi extends ComponentUI {
private final InternalRowNameComponent rowNameComp = new InternalRowNameComponent();
private final Rectangle rowNameBounds = new Rectangle(0, 0, 0, getRowHeight());
private final Rectangle colGroupBounds = new Rectangle(0, 0, 0, getRowHeight());
@Override
public void paint(Graphics g, JComponent c) {
// Row names take some percent of the width
rowNameBounds.width = (int) (getSize().width*NAME_CELLS_WIDTH_WEIGHT);
// Cell start just after the names and share the left space
colGroupBounds.x = rowNameBounds.width;
colGroupBounds.width =
((getSize().width-rowNameBounds.width)/model.getGroupCount());
for (int row=viewPort.getViewPosition().y/getRowHeight(); row<model.getRowCount(); row++) {
drawRow(g, row);
}
}
private void drawRow(Graphics g, int row) {
Graphics drawingGrahpics = null;
rowNameComp.setBounds(0, 0, rowNameBounds.width, getRowHeight());
rowNameBounds.y = row*getRowHeight();
// Avoid drawing rows that are outside the visible area
if ( !rowNameBounds.intersects(g.getClipBounds()) )
return;
rowNameComp.setName(model.getRowName(row));
drawingGrahpics = g.create(rowNameBounds.x, rowNameBounds.y,
rowNameBounds.width, rowNameBounds.height);
try {
rowNameComp.paint(drawingGrahpics);
} finally {
drawingGrahpics.dispose();
}
for (int group=0; group<model.getGroupCount(); group++) {
drawGroup(g, group, row);
}
} // drawRow
private void drawGroup(Graphics g, int group, int row) {
Graphics drawingGrahpics = null;
Component cellComponent = null;
final int cellCount = model.getGroupSize(group);
final int cellWidth = colGroupBounds.width/cellCount;
// Place the cell to the corresponding row
colGroupBounds.y = row*getRowHeight();
// Start at the begining of the previous group
colGroupBounds.x = rowNameBounds.width+(group*colGroupBounds.width)
+(group*getSpacerWidth());
// Fill a rectangle with the cell background to avoid drawing many
// tiny rectangles behind each cells.
g.setColor(getCellBackground());
g.fillRect(colGroupBounds.x, colGroupBounds.y,
colGroupBounds.width, getHeight());
for (int cell=0; cell<cellCount; cell++) {
// Avoid drawing cells that are outside the visible area
if ( !g.getClipBounds().intersects(colGroupBounds) )
continue;
cellComponent = renderer.getGridCellRendererComponent(
Grid.this, model.getValueAt(row, group, cell), row, group, cell);
cellComponent.setBounds(0, 0, cellWidth-2, getRowHeight()-2);
// cellWidth*cell to compute the start point for the cell
drawingGrahpics = g.create(colGroupBounds.x+1+(cellWidth*cell),
colGroupBounds.y+1, cellWidth, colGroupBounds.height-1);
try {
cellComponent.paint(drawingGrahpics);
// Draw the grid border around the cell.
drawingGrahpics.setColor(UIManager.getColor("Table.gridColor"));
drawingGrahpics.drawLine(0, 0, 0, colGroupBounds.height);
drawingGrahpics.drawLine(0, colGroupBounds.height-2, cellWidth, colGroupBounds.height-2);
if ( cell==cellCount-1 ) {
drawingGrahpics.drawLine(cellWidth-2, 0, cellWidth-2, colGroupBounds.height);
}
} finally {
drawingGrahpics.dispose();
}
}
} // drawGroup
} |
Partager