private void computePreferredSize() { Model model = getModel(); Selection sel = model.getSelection(); int columns = sel.size(); if (columns == 0) { setPreferredSize(new Dimension(0, 0)); return; } Graphics g = getGraphics(); if (g == null) { cellHeight = 16; cellWidth = 24; } else { FontMetrics fm = g.getFontMetrics(HEAD_FONT); cellHeight = fm.getHeight(); cellWidth = 24; for (int i = 0; i < columns; i++) { String header = sel.get(i).toShortString(); cellWidth = Math.max(cellWidth, fm.stringWidth(header)); } } tableWidth = (cellWidth + COLUMN_SEP) * columns - COLUMN_SEP; tableHeight = cellHeight * (1 + rowCount) + HEADER_SEP; setPreferredSize(new Dimension(tableWidth, tableHeight)); revalidate(); repaint(); }
@Override public void paintComponent(Graphics g) { super.paintComponent(g); Dimension sz = getSize(); int top = Math.max(0, (sz.height - tableHeight) / 2); int left = Math.max(0, (sz.width - tableWidth) / 2); Model model = getModel(); if (model == null) return; Selection sel = model.getSelection(); int columns = sel.size(); if (columns == 0) { g.setFont(BODY_FONT); GraphicsUtil.drawCenteredText( g, Strings.get("tableEmptyMessage"), sz.width / 2, sz.height / 2); return; } g.setColor(Color.GRAY); int lineY = top + cellHeight + HEADER_SEP / 2; g.drawLine(left, lineY, left + tableWidth, lineY); g.setColor(Color.BLACK); g.setFont(HEAD_FONT); FontMetrics headerMetric = g.getFontMetrics(); int x = left; int y = top + headerMetric.getAscent() + 1; for (int i = 0; i < columns; i++) { x = paintHeader(sel.get(i).toShortString(), x, y, g, headerMetric); } g.setFont(BODY_FONT); FontMetrics bodyMetric = g.getFontMetrics(); Rectangle clip = g.getClipBounds(); int firstRow = Math.max(0, (clip.y - y) / cellHeight - 1); int lastRow = Math.min(rowCount, 2 + (clip.y + clip.height - y) / cellHeight); int y0 = top + cellHeight + HEADER_SEP; x = left; for (int col = 0; col < columns; col++) { SelectionItem item = sel.get(col); ValueLog log = model.getValueLog(item); int radix = item.getRadix(); int offs = rowCount - log.size(); y = y0 + Math.max(offs, firstRow) * cellHeight; for (int row = Math.max(offs, firstRow); row < lastRow; row++) { Value val = log.get(row - offs); String label = val.toDisplayString(radix); int width = bodyMetric.stringWidth(label); g.drawString(label, x + (cellWidth - width) / 2, y + bodyMetric.getAscent()); y += cellHeight; } x += cellWidth + COLUMN_SEP; } }
private void computeRowCount() { Model model = getModel(); Selection sel = model.getSelection(); int rows = 0; for (int i = sel.size() - 1; i >= 0; i--) { int x = model.getValueLog(sel.get(i)).size(); if (x > rows) rows = x; } if (rowCount != rows) { rowCount = rows; computePreferredSize(); } }