Browse Source

fix: FineDayLabelUI 抽离,代码质量调整

fbp/release
lemon 2 months ago
parent
commit
d956e697d5
  1. 103
      designer-base/src/main/java/com/fine/theme/light/ui/FineCalendarPaneUI.java
  2. 140
      designer-base/src/main/java/com/fine/theme/light/ui/FineDayLabelUI.java
  3. 3
      designer-base/src/main/java/com/fr/design/gui/date/CalendarNumberField.java
  4. 9
      designer-base/src/main/java/com/fr/design/gui/date/UIDayLabel.java
  5. 1
      designer-base/src/main/resources/com/fine/theme/light/ui/laf/FineLaf.properties

103
designer-base/src/main/java/com/fine/theme/light/ui/FineCalendarPaneUI.java

@ -1,9 +1,7 @@
package com.fine.theme.light.ui;
import com.formdev.flatlaf.ui.FlatRoundBorder;
import com.formdev.flatlaf.ui.FlatUIUtils;
import com.fr.design.gui.date.UICalendarPanel;
import com.fr.design.gui.date.UIDayLabel;
import javax.swing.JComponent;
import javax.swing.UIManager;
@ -11,26 +9,17 @@ import javax.swing.border.LineBorder;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.PanelUI;
import java.awt.Color;
import java.awt.Component;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
/**
* {@link UICalendarPanel} UI 样式
*
* @author lemon
* @since 12.0
* Created on 2024/09/22
*/
public class FineCalendarPaneUI extends PanelUI {
protected Color defaultBackground;
protected Color selectedBackground;
protected Color hoverBackground;
protected Color pressedBackground;
protected Color otherMonthForeground;
protected int arc;
/**
* @param shared
@ -51,19 +40,11 @@ public class FineCalendarPaneUI extends PanelUI {
}
/**
*
* @param c the component where this UI delegate is being installed
*
*/
public void installUI(JComponent c) {
super.installUI(c);
selectedBackground = UIManager.getColor("Calendar.day.selectedBackground");
hoverBackground = UIManager.getColor("Calendar.day.hoverBackground");
pressedBackground = UIManager.getColor("Calendar.day.pressedBackground");
defaultBackground = UIManager.getColor("Calendar.background");
otherMonthForeground = UIManager.getColor("Calendar.dayOtherMonth.foreground");
arc = UIManager.getInt("Calendar.day.arc");
//renderer this
c.setBackground(defaultBackground);
@ -75,86 +56,4 @@ public class FineCalendarPaneUI extends PanelUI {
super.uninstallUI(c);
}
/**
* UICalendarPanel paint, 目前只对 {@link UIDayLabel} 样式自定义其余使用默认样式
* @param g the <code>Graphics</code> context in which to paint
* @param c the component being painted;
* this argument is often ignored,
* but might be used if the UI object is stateless
* and shared by multiple components
*
*/
public void paint(Graphics g, JComponent c) {
if (c instanceof UICalendarPanel) {
UICalendarPanel calendar = (UICalendarPanel) c;
paintComponent(calendar);
}
}
private void paintComponent(JComponent component) {
if (component instanceof UIDayLabel) {
paintDayLabel((UIDayLabel) component);
return;
}
for (Component c : component.getComponents()) {
paintComponent((JComponent) c);
}
}
private void paintDayLabel(UIDayLabel label) {
if (!label.isSmallLabel()) {
return;
}
label.setBorder(new DayLabelRoundedBorder());
if (!label.isCurrentMonth()) {
label.setForeground(otherMonthForeground);
}
}
private Color getBackgroundColor(UIDayLabel dayLabel) {
if (dayLabel.isSelected()) {
return selectedBackground;
}
if (dayLabel.isHovered()) {
return hoverBackground;
}
if (dayLabel.isPressed()) {
return pressedBackground;
}
return defaultBackground;
}
/**
* {@link UIDayLabel} border 样式
*/
private class DayLabelRoundedBorder extends FlatRoundBorder {
public DayLabelRoundedBorder() {
}
@Override
public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
Graphics2D g2 = (Graphics2D)g.create();
try {
FlatUIUtils.setRenderingHints(g2);
g2.setColor(getBackgroundColor((UIDayLabel) c));
g2.fillRoundRect(0, 0, width , height, arc, arc);
// 避免文字被背景色覆盖
UIDayLabel dayLabel = (UIDayLabel) c;
g2.setColor(dayLabel.getForeground());
FontMetrics metrics = g2.getFontMetrics(dayLabel.getFont());
int x1 = (width - metrics.stringWidth(dayLabel.getText())) / 2;
int y1 = ((height - metrics.getHeight()) / 2) + metrics.getAscent();
g2.drawString(dayLabel.getText(), x1, y1);
} finally {
g2.dispose();
}
}
}
}

140
designer-base/src/main/java/com/fine/theme/light/ui/FineDayLabelUI.java

@ -0,0 +1,140 @@
package com.fine.theme.light.ui;
import com.formdev.flatlaf.ui.FlatLabelUI;
import com.formdev.flatlaf.ui.FlatRoundBorder;
import com.formdev.flatlaf.ui.FlatUIUtils;
import com.fr.design.gui.date.UIDayLabel;
import javax.swing.JComponent;
import javax.swing.UIManager;
import javax.swing.plaf.ComponentUI;
import java.awt.Color;
import java.awt.Component;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
/**
* {@link UIDayLabel} UI 样式
*
* @author lemon
* @since 12.0
* Created on 2024/09/22
*/
public class FineDayLabelUI extends FlatLabelUI {
protected Color defaultBackground;
protected Color selectedBackground;
protected Color hoverBackground;
protected Color pressedBackground;
protected Color otherMonthForeground;
protected int arc;
/**
* @since 2
*/
protected FineDayLabelUI() {
super(false);
}
/**
* 创建UI
*
* @param c 组件
* @return ComponentUI
*/
public static ComponentUI createUI(JComponent c) {
return FlatUIUtils.createSharedUI(FineDayLabelUI.class, FineDayLabelUI::new);
}
/**
* @param c the component where this UI delegate is being installed
*/
public void installUI(JComponent c) {
super.installUI(c);
selectedBackground = UIManager.getColor("Calendar.day.selectedBackground");
hoverBackground = UIManager.getColor("Calendar.day.hoverBackground");
pressedBackground = UIManager.getColor("Calendar.day.pressedBackground");
defaultBackground = UIManager.getColor("Calendar.background");
otherMonthForeground = UIManager.getColor("Calendar.dayOtherMonth.foreground");
arc = UIManager.getInt("Calendar.day.arc");
}
@Override
public void uninstallUI(JComponent c) {
super.uninstallUI(c);
}
/**
* UICalendarPanel paint, 目前只对 {@link UIDayLabel} 样式自定义其余使用默认样式
*
* @param g the <code>Graphics</code> context in which to paint
* @param c the component being painted;
* this argument is often ignored,
* but might be used if the UI object is stateless
* and shared by multiple components
*/
public void paint(Graphics g, JComponent c) {
super.paint(g, c);
if (c instanceof UIDayLabel) {
paintDayLabel((UIDayLabel) c);
}
}
private void paintDayLabel(UIDayLabel label) {
if (!label.isSmallLabel()) {
label.setBackground(getBackgroundColor(label));
return;
}
label.setBorder(new DayLabelRoundedBorder());
if (!label.isCurrentMonth()) {
label.setForeground(otherMonthForeground);
}
}
private Color getBackgroundColor(UIDayLabel dayLabel) {
if (dayLabel.isSelected()) {
return selectedBackground;
}
if (dayLabel.isHovered()) {
return hoverBackground;
}
if (dayLabel.isPressed()) {
return pressedBackground;
}
return defaultBackground;
}
/**
* {@link UIDayLabel} border 样式
*/
private class DayLabelRoundedBorder extends FlatRoundBorder {
public DayLabelRoundedBorder() {
}
@Override
public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
Graphics2D g2 = (Graphics2D) g.create();
try {
FlatUIUtils.setRenderingHints(g2);
g2.setColor(getBackgroundColor((UIDayLabel) c));
g2.fillRoundRect(0, 0, width, height, arc, arc);
// 避免文字被背景色覆盖
UIDayLabel dayLabel = (UIDayLabel) c;
g2.setColor(dayLabel.getForeground());
FontMetrics metrics = g2.getFontMetrics(dayLabel.getFont());
int x1 = (width - metrics.stringWidth(dayLabel.getText())) / 2;
int y1 = ((height - metrics.getHeight()) / 2) + metrics.getAscent();
g2.drawString(dayLabel.getText(), x1, y1);
} finally {
g2.dispose();
}
}
}
}

3
designer-base/src/main/java/com/fr/design/gui/date/CalendarNumberField.java

@ -13,7 +13,6 @@ import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import static com.fine.theme.utils.FineUIScale.createScaleDimension;
import static com.fine.theme.utils.FineUIScale.scale;
/**
* Created with IntelliJ IDEA.
@ -22,7 +21,7 @@ import static com.fine.theme.utils.FineUIScale.scale;
* Time: 上午11:06
* To change this template use File | Settings | File Templates.
*/
public class CalendarNumberField extends UINumberField {
class CalendarNumberField extends UINumberField {
private static final int NUM_TEN = 10;

9
designer-base/src/main/java/com/fr/design/gui/date/UIDayLabel.java

@ -1,20 +1,20 @@
package com.fr.design.gui.date;
import com.fr.design.gui.ilable.UILabel;
import com.fr.design.utils.DesignUtils;
import java.text.SimpleDateFormat;
import java.util.Date;
import static com.fine.theme.utils.FineUIScale.scale;
/**
* 日期控件day label
*
* @author lemon
* @since 12.0
* Created on 2024/09/22
*/
public class UIDayLabel extends UILabel {
private static final String UI_CLASS_ID = "DayLabelUI";
private Date date = null;
private int index;
@ -103,4 +103,9 @@ public class UIDayLabel extends UILabel {
return this.index;
}
@Override
public String getUIClassID() {
return UI_CLASS_ID;
}
}

1
designer-base/src/main/resources/com/fine/theme/light/ui/laf/FineLaf.properties

@ -8,6 +8,7 @@ ComboBoxUI=com.fine.theme.light.ui.FineComboBoxUI
DesktopIconUI=com.formdev.flatlaf.ui.FlatDesktopIconUI
DesktopPaneUI=com.formdev.flatlaf.ui.FlatDesktopPaneUI
CalendarPaneUI=com.fine.theme.light.ui.FineCalendarPaneUI
DayLabelUI=com.fine.theme.light.ui.FineDayLabelUI
EditorPaneUI=com.formdev.flatlaf.ui.FlatEditorPaneUI
FileChooserUI=com.formdev.flatlaf.ui.FlatFileChooserUI
FormattedTextFieldUI=com.formdev.flatlaf.ui.FlatFormattedTextFieldUI

Loading…
Cancel
Save