📄 tableprinter.java
字号:
// draw the header for (int col = firstColumn; col < columnCount; col++) { TableColumn tk = columnModel.getColumn(col); int width = tk.getWidth(); rect.x = x[col]; rect.width = (int)(width * colScaleFactor);// + 1; if ((rect.x + rect.width) > printWidth) { lastColumn = col; // update/add the segment TablePrintSegment nextSegment = getPrintSegment(pageIndex + 1); if (nextSegment == null) { addPrintSegment(pageIndex + 1, lastRow, lastColumn); } else { nextSegment.lastRow = lastRow; nextSegment.lastColumn = lastColumn; } break; } lastColumn = -1; rect.x--; g2d.setClip(rect); totalWidth += rect.width; // fill the background g2d.setColor(headerBackground); g2d.fill(rect); g2d.setColor(Color.BLACK); // top line g2d.setClip(0, 0, printWidth, printHeight); g2d.drawLine(0, rect.y, totalWidth - 2, rect.y); // reset the clip region g2d.setClip(rect); // draw the left border if (col == firstColumn) { g2d.drawLine(rect.x + 1, rect.y, rect.x + 1, rect.y + rect.height); } // draw the right border g2d.drawLine(rect.x + rect.width - 1, rect.y, rect.x + rect.width - 1, rect.y + rect.height); colWidths[col] = rect.width; if (col+1 < columnCount) { x[col+1] = x[col] + rect.width; } y = ((rect.y + rect.height) / 2) + rect.y - h; String title = (String)tk.getIdentifier(); BasicGraphicsUtils.drawStringUnderlineCharAt(g2d, title, -1, rect.x + 2, y); } // draw the bottom border g2d.setClip(0, 0, printWidth, printHeight); g2d.drawLine(0, rect.y + rect.height, totalWidth - 2, rect.y + rect.height); g2d.setFont(plainFont); fm = g2d.getFontMetrics(); h = fm.getHeight(); boolean usingPrintableModel = false; TableModel model = table.getModel(); PrintableTableModel printModel = null; if (model instanceof PrintableTableModel) { usingPrintableModel = true; printModel = (PrintableTableModel)model; } g2d.setColor(Color.BLACK); /* Log.debug("pageIndex: " + pageIndex); Log.debug("rowCount: " + table.getRowCount()); Log.debug("hPage: " + printHeight); Log.debug("lastRow: " + lastRow); */ for (int row = lastRow; row < rowCount; row++) { y += rowHeight; rect.y = y - h; if (row > lastRow) { rect.height = rowHeight; } else { rect.y -= 2; rect.height += 2; } //Log.debug("index: "+nRow+ " rect: " + rect); if ((rect.y + rect.height) > printHeight) { if (lastColumn == -1) { lastRow = row; } TablePrintSegment nextSegment = getPrintSegment(pageIndex + 1); if (nextSegment == null) { addPrintSegment(pageIndex + 1, lastRow, lastColumn); } else { nextSegment.lastRow = lastRow; nextSegment.lastColumn = lastColumn; } return PAGE_EXISTS; } g2d.setClip(0, rect.y - 2, totalWidth + 1, rect.y + rect.height + 2); // bottom line g2d.drawLine(0, rect.y + rect.height - 1, totalWidth - 2, rect.y + rect.height - 1); for (int col = firstColumn; col < columnCount; col++) { rect.x = x[col]; rect.width = colWidths[col]; rect.x--; g2d.setClip(rect.x, rect.y - 1, rect.width, rect.height); // draw the left border if (col == firstColumn) { g2d.drawLine(rect.x + 1, rect.y - 1, rect.x + 1, rect.y + rect.height); } // draw the right border g2d.drawLine(rect.x + rect.width - 1, rect.y - 1, rect.x + rect.width - 1, rect.y + rect.height); //int col = columnModel.getColumn(nCol).getModelIndex(); //Log.debug("col: "+col+" row: " + row); String value = null; if (usingPrintableModel) { value = printModel.getPrintValueAt(row, col); } else { Object obj = model.getValueAt(row, col); value = (obj == null ? Constants.EMPTY : obj.toString()); } if (col > firstColumn) { g2d.drawString(value, rect.x + 2, y); } else { g2d.drawString(value, rect.x + 4, y); } } // if we don't have to add more columns do the row check if (lastColumn == -1) { if (row == rowCount - 1) { lastRow = -1; TablePrintSegment nextSegment = getPrintSegment(pageIndex + 1); if (nextSegment == null) { addPrintSegment(pageIndex + 1, lastRow, lastColumn); } else { nextSegment.lastRow = lastRow; nextSegment.lastColumn = lastColumn; } } } } if (pageIndex == 0) { addPrintSegment(pageIndex, 0, lastColumn); } return PAGE_EXISTS; } private void addPrintSegment(int pageIndex, int lastRow, int lastColumn) { if (pages == null) { pages = new Vector<TablePrintSegment>(); } TablePrintSegment segment = new TablePrintSegment(); segment.pageIndex = pageIndex; segment.lastRow = lastRow; segment.lastColumn = lastColumn; pages.add(segment); } private TablePrintSegment getPrintSegment(int pageIndex) { if (pages == null || pages.isEmpty()) { return null; } for (int i = 0, n = pages.size(); i < n; i++) { TablePrintSegment segment = pages.get(i); if (segment.pageIndex == pageIndex) { return segment; } } return null; } /** * Calculate how much of the table will fit on a page without * causing a row or column to be split across two pages */ protected Dimension getPrintSize(int positionX, int positionY) { Rectangle rect; int printWidth; int printHeight; int firstCol = table.columnAtPoint(new Point(positionX, positionY)); int firstRow = table.rowAtPoint(new Point(positionX, positionY)); int maxWidth = (int)(pageFormat.getImageableWidth()); int maxHeight = (int)(pageFormat.getImageableHeight()); if (displayHeaderOnPage(positionY)) maxHeight -= table.getTableHeader().getHeight(); int lastCol = table.columnAtPoint(new Point(positionX + maxWidth, positionY)); if (lastCol == -1) { printWidth = table.getWidth() - positionX; } else { rect = table.getCellRect(0, lastCol - 1, true); printWidth = rect.x + rect.width - positionX; } int lastRow = table.rowAtPoint(new Point(positionX, positionY + maxHeight)); if (lastRow == -1) { printHeight = table.getHeight() - positionY; } else { rect = table.getCellRect(lastRow - 1, 0, true); printHeight = rect.y + rect.height - positionY; } return new Dimension(printWidth, printHeight); } /** * Paint / print a portion of the table */ /* protected void paintTable(Graphics g, int positionX, int positionY, Dimension size) { int offsetX = (int)(pageFormat.getImageableX()); int offsetY = (int)(pageFormat.getImageableY()); if (displayHeaderOnPage(positionY)) { JTableHeader header = table.getTableHeader(); if ((header.getWidth() == 0) || (header.getHeight() == 0)) header.setSize(header.getPreferredSize()); int headerHeight = header.getHeight(); g.translate(offsetX - positionX, offsetY); g.clipRect(positionX, 0, size.width, size.height + headerHeight); header.paint(g); g.translate(0, headerHeight - positionY); g.clipRect(positionX, positionY, size.width, size.height); } else { g.translate(offsetX - positionX, offsetY - positionY); g.clipRect(positionX, positionY, size.width, size.height); } table.paint(g); } */ /** * Determine whether or not to paint the headers on the current page */ protected boolean displayHeaderOnPage(int positionY) { return ((headerStatus == ALL_PAGES) || ((headerStatus == FIRST_PAGE_ONLY) && positionY == 0)); } /** * Calculate the number of pages it will take to print the entire table */ public int getNumberOfPages() { //Log.debug("getNumberOfPages"); Dimension size = null; int tableWidth = table.getWidth(); int tableHeight = table.getHeight(); int positionX = 0; int positionY = 0; int pageIndex = 0; while (positionY < tableHeight) { positionX = 0; while (positionX < tableWidth) { size = getPrintSize(positionX, positionY); positionX += size.width; pageIndex++; } positionY += size.height; } return pageIndex; } public Printable getPrintable(int index) { return this; } public PageFormat getPageFormat(int index) { return pageFormat; } class TablePrintSegment { int pageIndex; int lastRow; int lastColumn; public TablePrintSegment() {} } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -