You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
198 lines
6.4 KiB
198 lines
6.4 KiB
package com.fr.design.components.table; |
|
|
|
import com.fr.design.gui.ilable.UILabel; |
|
import com.fr.design.layout.FRGUIPaneFactory; |
|
import com.fr.design.utils.ColorUtils; |
|
import com.fr.third.org.apache.commons.lang3.ArrayUtils; |
|
|
|
import javax.swing.BorderFactory; |
|
import javax.swing.JPanel; |
|
import javax.swing.JSeparator; |
|
import java.awt.BorderLayout; |
|
import java.awt.Color; |
|
import java.awt.Component; |
|
import java.awt.Dimension; |
|
import java.awt.GridLayout; |
|
|
|
/** |
|
* 表头 |
|
* 内容 |
|
* |
|
* 适用于需要一个表格的 Panel |
|
* |
|
* created by Harrison on 2022/05/26 |
|
**/ |
|
public class TablePanel extends JPanel { |
|
|
|
private static final Color DEFAULT_HEADER_COLOR = new Color(232, 232, 233); |
|
|
|
private static final Color DEFAULT_ODD_ROW_COLOR = new Color(245, 245, 247); |
|
|
|
private static final Color DEFAULT_EVEN_ROW_COLOR = Color.WHITE; |
|
|
|
private JPanel headerPanel; |
|
|
|
private JPanel[] headerItemPanels; |
|
|
|
private JPanel contentPanel; |
|
|
|
private JPanel[][] cellPanels; |
|
|
|
public TablePanel(int row, int column) { |
|
|
|
setLayout(FRGUIPaneFactory.createBorderLayout()); |
|
|
|
/* header 部分 */ |
|
|
|
this.headerPanel = new JPanel(); |
|
headerPanel.setLayout(FRGUIPaneFactory.createNColumnGridLayout(column)); |
|
headerPanel.setName("header-panel"); |
|
headerPanel.setPreferredSize(new Dimension(640, 24)); |
|
|
|
// border |
|
headerPanel.setBorder(BorderFactory.createLineBorder(new Color(218, 218, 221))); |
|
syncHeaderColor(headerPanel); |
|
|
|
headerItemPanels = new JPanel[column]; |
|
for (int i = 0; i < column; i++) { |
|
JPanel headerItemWrapper = FRGUIPaneFactory.createBorderLayout_S_Pane(); |
|
syncHeaderColor(headerItemWrapper); |
|
headerItemWrapper.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 10)); |
|
|
|
JPanel headerItemPanel = FRGUIPaneFactory.createBorderLayout_S_Pane(); |
|
headerItemPanel.setBorder(BorderFactory.createEmptyBorder(0, 10, 0, 0)); |
|
headerItemPanels[i] = headerItemPanel; |
|
|
|
UILabel label = new UILabel(); |
|
syncHeaderColor(label); |
|
|
|
headerItemPanel.add(new UILabel(), BorderLayout.CENTER); |
|
|
|
headerItemWrapper.add(headerItemPanel, BorderLayout.LINE_START); |
|
if (i != column - 1) { |
|
JSeparator separator = new JSeparator(JSeparator.VERTICAL); |
|
separator.setBackground(new Color(218, 218, 221)); |
|
headerItemWrapper.add(separator, BorderLayout.LINE_END); |
|
} |
|
headerPanel.add(headerItemWrapper); |
|
} |
|
|
|
/* content 部分 */ |
|
|
|
contentPanel = new JPanel(); |
|
|
|
contentPanel.setLayout(new GridLayout(row, 1)); |
|
contentPanel.setBorder(BorderFactory.createLineBorder(new Color(218, 218, 221))); |
|
|
|
cellPanels = new JPanel[row][column]; |
|
for (int i = 0; i < row; i++) { |
|
|
|
JPanel rowPanel = new JPanel(); |
|
// 获取行号 |
|
Color rowColor = getRowColorByRowNumber(i + 1); |
|
rowPanel.setBackground(rowColor); |
|
rowPanel.setLayout(FRGUIPaneFactory.createNColumnGridLayout(column)); |
|
rowPanel.setName("row-" + i); |
|
rowPanel.setBorder(BorderFactory.createEmptyBorder()); |
|
rowPanel.setPreferredSize(new Dimension(640, 24)); |
|
|
|
for (int j = 0; j < column; j++) { |
|
|
|
JPanel rowItemPanel = FRGUIPaneFactory.createBorderLayout_S_Pane(); |
|
rowItemPanel.setBorder(BorderFactory.createEmptyBorder(0, 10, 0, 0)); |
|
rowItemPanel.setName("rowItemPanel-"+ i + "-" + j); |
|
final UILabel empty = new UILabel(); |
|
empty.setPreferredSize(new Dimension(210, 24)); |
|
rowItemPanel.setBackground(rowPanel.getBackground()); |
|
rowItemPanel.add(empty, BorderLayout.CENTER); |
|
|
|
rowPanel.add(rowItemPanel); |
|
cellPanels[i][j] = rowItemPanel; |
|
} |
|
|
|
contentPanel.add(rowPanel); |
|
} |
|
|
|
add(headerPanel, BorderLayout.NORTH); |
|
add(contentPanel, BorderLayout.SOUTH); |
|
} |
|
|
|
/** |
|
* 获取行的颜色 |
|
* |
|
* @param row 行号 |
|
* @return 颜色 |
|
*/ |
|
private Color getRowColorByRowNumber(int row) { |
|
|
|
Color rowColor; |
|
if (row % 2 != 0) { |
|
rowColor = DEFAULT_EVEN_ROW_COLOR; |
|
} else { |
|
rowColor = DEFAULT_ODD_ROW_COLOR; |
|
} |
|
return rowColor; |
|
} |
|
|
|
public void updateHeaders(String[] headers) { |
|
|
|
for (int i = 0; i < headers.length; i++) { |
|
String header = headers[i]; |
|
UILabel headerContent = new UILabel(header); |
|
JPanel headerItemPanel = headerItemPanels[i]; |
|
if (ArrayUtils.getLength(headerItemPanel.getComponents()) == 1) { |
|
headerItemPanel.remove(0); |
|
} |
|
headerItemPanel.add(headerContent); |
|
syncHeaderColor(headerItemPanel); |
|
} |
|
} |
|
|
|
public void updateCell(int row, int column, Component component) { |
|
|
|
int x = row - 1; |
|
int y = column - 1; |
|
|
|
syncCellColor(row, component); |
|
|
|
JPanel cellPanel = this.cellPanels[x][y]; |
|
if (ArrayUtils.getLength(cellPanel.getComponents()) == 1) { |
|
cellPanel.remove(0); |
|
} |
|
cellPanel.add(component); |
|
} |
|
|
|
/** |
|
* 为单元格Panel更新tooltip |
|
* |
|
* @param row 行数 |
|
* @param column 列数 |
|
* @param value tooltip值 |
|
*/ |
|
public void updateCellToolTip(int row, int column, String value) { |
|
int x = row - 1; |
|
int y = column - 1; |
|
JPanel cellPanel = this.cellPanels[x][y]; |
|
cellPanel.setToolTipText(value); |
|
} |
|
|
|
public void updateCell(int row, int column, String value) { |
|
|
|
UILabel cellContent = new UILabel(value); |
|
syncCellColor(row, cellContent); |
|
this.updateCell(row, column, cellContent); |
|
} |
|
|
|
private void syncHeaderColor(Component component) { |
|
|
|
ColorUtils.syncBackground(component, DEFAULT_HEADER_COLOR); |
|
} |
|
|
|
private void syncCellColor(int row, Component component) { |
|
|
|
Color rowColor = getRowColorByRowNumber(row); |
|
ColorUtils.syncBackground(component, rowColor); |
|
component.setBackground(rowColor); |
|
} |
|
|
|
}
|
|
|