📄 synthtableui.java
字号:
synthG.drawLine(context, "Table.grid", g, x - 1, 0, x - 1, tableHeight - 1); } } else { x = damagedArea.x + damagedArea.width; for (int column = cMin; column < cMax; column++) { int w = cm.getColumn(column).getWidth(); x -= w; synthG.drawLine(context, "Table.grid", g, x - 1, 0, x - 1, tableHeight - 1); } x -= cm.getColumn(cMax).getWidth(); synthG.drawLine(context, "Table.grid", g, x, 0, x, tableHeight - 1); } } } private int viewIndexForColumn(TableColumn aColumn) { TableColumnModel cm = table.getColumnModel(); for (int column = 0; column < cm.getColumnCount(); column++) { if (cm.getColumn(column) == aColumn) { return column; } } return -1; } private void paintCells(SynthContext context, Graphics g, int rMin, int rMax, int cMin, int cMax) { JTableHeader header = table.getTableHeader(); TableColumn draggedColumn = (header == null) ? null : header.getDraggedColumn(); TableColumnModel cm = table.getColumnModel(); int columnMargin = cm.getColumnMargin(); Rectangle cellRect; TableColumn aColumn; int columnWidth; if (table.getComponentOrientation().isLeftToRight()) { for(int row = rMin; row <= rMax; row++) { cellRect = table.getCellRect(row, cMin, false); for(int column = cMin; column <= cMax; column++) { aColumn = cm.getColumn(column); columnWidth = aColumn.getWidth(); cellRect.width = columnWidth - columnMargin; if (aColumn != draggedColumn) { paintCell(context, g, cellRect, row, column); } cellRect.x += columnWidth; } } } else { for(int row = rMin; row <= rMax; row++) { cellRect = table.getCellRect(row, cMin, false); aColumn = cm.getColumn(cMin); if (aColumn != draggedColumn) { columnWidth = aColumn.getWidth(); cellRect.width = columnWidth - columnMargin; paintCell(context, g, cellRect, row, cMin); } for(int column = cMin+1; column <= cMax; column++) { aColumn = cm.getColumn(column); columnWidth = aColumn.getWidth(); cellRect.width = columnWidth - columnMargin; cellRect.x -= columnWidth; if (aColumn != draggedColumn) { paintCell(context, g, cellRect, row, column); } } } } // Paint the dragged column if we are dragging. if (draggedColumn != null) { paintDraggedArea(context, g, rMin, rMax, draggedColumn, header.getDraggedDistance()); } // Remove any renderers that may be left in the rendererPane. rendererPane.removeAll(); } private void paintDraggedArea(SynthContext context, Graphics g, int rMin, int rMax, TableColumn draggedColumn, int distance) { int draggedColumnIndex = viewIndexForColumn(draggedColumn); Rectangle minCell = table.getCellRect(rMin, draggedColumnIndex, true); Rectangle maxCell = table.getCellRect(rMax, draggedColumnIndex, true); Rectangle vacatedColumnRect = minCell.union(maxCell); // Paint a gray well in place of the moving column. g.setColor(table.getParent().getBackground()); g.fillRect(vacatedColumnRect.x, vacatedColumnRect.y, vacatedColumnRect.width, vacatedColumnRect.height); // Move to the where the cell has been dragged. vacatedColumnRect.x += distance; // Fill the background. g.setColor(context.getStyle().getColor(context, ColorType.BACKGROUND)); g.fillRect(vacatedColumnRect.x, vacatedColumnRect.y, vacatedColumnRect.width, vacatedColumnRect.height); SynthGraphicsUtils synthG = context.getStyle().getGraphicsUtils( context); // Paint the vertical grid lines if necessary. if (table.getShowVerticalLines()) { g.setColor(table.getGridColor()); int x1 = vacatedColumnRect.x; int y1 = vacatedColumnRect.y; int x2 = x1 + vacatedColumnRect.width - 1; int y2 = y1 + vacatedColumnRect.height - 1; // Left synthG.drawLine(context, "Table.grid", g, x1-1, y1, x1-1, y2); // Right synthG.drawLine(context, "Table.grid", g, x2, y1, x2, y2); } for(int row = rMin; row <= rMax; row++) { // Render the cell value Rectangle r = table.getCellRect(row, draggedColumnIndex, false); r.x += distance; paintCell(context, g, r, row, draggedColumnIndex); // Paint the (lower) horizontal grid line if necessary. if (table.getShowHorizontalLines()) { g.setColor(table.getGridColor()); Rectangle rcr = table.getCellRect(row, draggedColumnIndex, true); rcr.x += distance; int x1 = rcr.x; int y1 = rcr.y; int x2 = x1 + rcr.width - 1; int y2 = y1 + rcr.height - 1; synthG.drawLine(context, "Table.grid", g, x1, y2, x2, y2); } } } private void paintCell(SynthContext context, Graphics g, Rectangle cellRect, int row, int column) { if (table.isEditing() && table.getEditingRow()==row && table.getEditingColumn()==column) { Component component = table.getEditorComponent(); component.setBounds(cellRect); component.validate(); } else { TableCellRenderer renderer = table.getCellRenderer(row, column); Component component = table.prepareRenderer(renderer, row, column); rendererPane.paintComponent(g, component, table, cellRect.x, cellRect.y, cellRect.width, cellRect.height, true); } } public void propertyChange(PropertyChangeEvent event) { if (SynthLookAndFeel.shouldUpdateStyle(event)) { updateStyle((JTable)event.getSource()); } } private class SynthBooleanTableCellRenderer extends JCheckBox implements TableCellRenderer { public SynthBooleanTableCellRenderer() { super(); setHorizontalAlignment(JLabel.CENTER); } public String getName() { String name = super.getName(); if (name == null) { return "Table.cellRenderer"; } return name; } public Component getTableCellRendererComponent( JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { if (isSelected) { setForeground(table.getSelectionForeground()); setBackground(table.getSelectionBackground()); } else { setForeground(table.getForeground()); setBackground(table.getBackground()); } setSelected((value != null && ((Boolean)value).booleanValue())); // NOTE: We don't do this as otherwise the the JCheckBox will // think it is selected when it may not be. This means JCheckBox // renderers don't render the selection correctly./* if (!useTableColors && (isSelected || hasFocus)) { SynthLookAndFeel.setSelectedUI((SynthButtonUI)SynthLookAndFeel. getUIOfType(getUI(), SynthButtonUI.class), isSelected, hasFocus, table.isEnabled()); } else { SynthLookAndFeel.resetSelectedUI(); }*/ return this; } public void paint(Graphics g) { super.paint(g); // Refer to comment above for why this is commented out. // SynthLookAndFeel.resetSelectedUI(); } } private class SynthTableCellRenderer extends DefaultTableCellRenderer { private Object numberFormat; private Object dateFormat; private boolean opaque; public void setOpaque(boolean isOpaque) { opaque = isOpaque; } public boolean isOpaque() { return opaque; } public String getName() { String name = super.getName(); if (name == null) { return "Table.cellRenderer"; } return name; } public void setBorder(Border b) { if (useUIBorder || b instanceof SynthBorder) { super.setBorder(b); } } public Component getTableCellRendererComponent( JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { if (!useTableColors && (isSelected || hasFocus)) { SynthLookAndFeel.setSelectedUI((SynthLabelUI)SynthLookAndFeel. getUIOfType(getUI(), SynthLabelUI.class), isSelected, hasFocus, table.isEnabled()); } else { SynthLookAndFeel.resetSelectedUI(); } super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); setIcon(null); Class columnClass = table.getColumnClass(column); configureValue(value, columnClass); return this; } private void configureValue(Object value, Class columnClass) { if (columnClass == Object.class || columnClass == null) { setHorizontalAlignment(JLabel.LEADING); } else if (columnClass == Float.class || columnClass == Double.class) { if (numberFormat == null) { numberFormat = NumberFormat.getInstance(); } setHorizontalAlignment(JLabel.TRAILING); setText((value == null) ? "" : ((NumberFormat)numberFormat).format(value)); } else if (columnClass == Number.class) { setHorizontalAlignment(JLabel.TRAILING); // Super will have set value. } else if (columnClass == Icon.class || columnClass == ImageIcon.class) { setHorizontalAlignment(JLabel.CENTER); setIcon((Icon)value); setText(""); } else if (columnClass == Date.class) { if (dateFormat == null) { dateFormat = DateFormat.getDateInstance(); } setHorizontalAlignment(JLabel.LEADING); setText((value == null) ? "" : ((Format)dateFormat).format(value)); } else { configureValue(value, columnClass.getSuperclass()); } } public void paint(Graphics g) { super.paint(g); SynthLookAndFeel.resetSelectedUI(); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -