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.
108 lines
3.6 KiB
108 lines
3.6 KiB
package com.fine.theme.light.ui; |
|
|
|
import com.fine.theme.utils.FineUIStyle; |
|
import com.formdev.flatlaf.ui.FlatTableHeaderUI; |
|
import com.fr.stable.StringUtils; |
|
import sun.swing.DefaultLookup; |
|
|
|
import javax.swing.BorderFactory; |
|
import javax.swing.JComponent; |
|
import javax.swing.JTable; |
|
import javax.swing.SwingConstants; |
|
import javax.swing.UIManager; |
|
import javax.swing.border.Border; |
|
import javax.swing.plaf.ComponentUI; |
|
import javax.swing.plaf.UIResource; |
|
import javax.swing.table.DefaultTableCellRenderer; |
|
import javax.swing.table.JTableHeader; |
|
import java.awt.Color; |
|
import java.awt.Component; |
|
import java.awt.Graphics; |
|
|
|
/** |
|
* 应用于 JTable 的UI |
|
* |
|
* @author lemon |
|
* @since |
|
* Created on |
|
*/ |
|
public class FineTableHeaderUI extends FlatTableHeaderUI { |
|
protected static Color selectionBackground = UIManager.getColor("Table.background"); |
|
|
|
@Override |
|
public void installUI(JComponent c) { |
|
super.installUI(c); |
|
JTableHeader header = (JTableHeader) c; |
|
header.setDefaultRenderer(new TableHeaderRenderer()); |
|
JTable table = header.getTable(); |
|
|
|
if (table != null) { |
|
table.setDefaultRenderer(Object.class, new TableRenderer()); |
|
} |
|
} |
|
|
|
|
|
@Override |
|
public void paint(Graphics g, JComponent c) { |
|
FineUIStyle.setStyle(((JTableHeader) c).getTable(), FineUIStyle.DEFAULT_TABLE); |
|
super.paint(g, c); |
|
} |
|
|
|
/** |
|
* 表头 render |
|
*/ |
|
public static class TableHeaderRenderer extends DefaultTableCellRenderer implements UIResource { |
|
public TableHeaderRenderer() { |
|
} |
|
|
|
@Override |
|
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { |
|
Border border = DefaultLookup.getBorder(this, this.ui, "TableHeader.cellBorder"); |
|
this.setText(value == null ? "" : value.toString()); |
|
this.setHorizontalAlignment(SwingConstants.LEFT); |
|
this.setBorder(border); |
|
return this; |
|
} |
|
} |
|
|
|
|
|
/** |
|
* UI |
|
* @param c |
|
* @return |
|
*/ |
|
public static ComponentUI createUI(JComponent c) { |
|
return new FineTableHeaderUI(); |
|
} |
|
|
|
/** |
|
* 表身 render |
|
*/ |
|
public static class TableRenderer extends DefaultTableCellRenderer { |
|
public TableRenderer() { |
|
setHorizontalAlignment(SwingConstants.LEFT); |
|
} |
|
|
|
@Override |
|
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { |
|
Class<?> columnClass = table.getColumnClass(0); |
|
Border border; |
|
if (column == table.getColumnCount() - 1) { |
|
border = BorderFactory.createCompoundBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, UIManager.getColor("defaultBorderColor")), |
|
UIManager.getBorder("Table.cellNoFocusBorder")); |
|
} else { |
|
border = BorderFactory.createCompoundBorder(BorderFactory.createMatteBorder(0, 0, 1, 1, UIManager.getColor("defaultBorderColor")), |
|
UIManager.getBorder("Table.cellNoFocusBorder")); |
|
} |
|
if (isSelected && columnClass != Boolean.class) { |
|
selectionBackground = UIManager.getColor( "Table.selectionBackground"); |
|
} else { |
|
selectionBackground = UIManager.getColor("Table.background"); |
|
} |
|
setBackground(selectionBackground); |
|
setText(value == null ? StringUtils.BLANK : String.valueOf(value)); |
|
setBorder(border); |
|
return this; |
|
} |
|
} |
|
}
|
|
|