package com.fr.widgettheme.theme.panel; import com.fr.base.BaseUtils; import com.fr.base.Style; import com.fr.base.theme.TemplateTheme; import com.fr.widgettheme.ThemePreviewTerminal; import com.fr.widgettheme.theme.widget.style.ThemeTextStyle; import com.fr.widgettheme.theme.widget.style.ThemedWidgetStyle; import com.fr.design.layout.FRGUIPaneFactory; import com.fr.general.FRFont; import com.fr.stable.Constants; import com.fr.widgettheme.theme.widget.theme.WidgetThemeDisplayConstants; import javax.swing.Icon; import javax.swing.ImageIcon; import javax.swing.JPanel; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.LayoutManager; import java.awt.image.BufferedImage; /** * 主题界面预览单元格子 * * @author John.Ying * @since 11.0 * Created on 2023/3/18 */ public class ControlPreviewCell extends JPanel { protected static final LayoutManager DEFAULT = FRGUIPaneFactory.createRightFlowLayout(); protected static final Color DEFAULT_COLOR = new Color(210, 210, 210); protected static final Color DEFAULT_THEME_COLOR = new Color(54, 133, 242); protected static final String DEFAULT_MESSAGE = ""; protected static final int NO_SCALE_RESOLUTION = 100; protected static final int DEFAULT_ALPHA = 255; /** * 格子文本数据 */ protected String value; protected TemplateTheme reportTheme; /** * 主题预览类型,默认为PC端 */ protected ThemePreviewTerminal themePreviewTerminal = ThemePreviewTerminal.PC; /** * 字体的颜色 */ protected Color textColor = DEFAULT_COLOR; public Color getTextColor() { return textColor; } public void setTextColor(Color textColor) { this.textColor = textColor; } public ControlPreviewCell() { this(DEFAULT, DEFAULT_MESSAGE); } public ControlPreviewCell(String value) { this(DEFAULT, value); } public ControlPreviewCell(LayoutManager layoutManager, String value) { this.setLayout(layoutManager); this.setOpaque(false); this.value = value; this.setPreferredSize(new Dimension(80, 30)); } /** * 主题样式变化后监听改变 */ public void refresh(TemplateTheme reportTheme, ThemePreviewTerminal type) { this.reportTheme = reportTheme; this.themePreviewTerminal = type; ThemedWidgetStyle widgetStyle = this.themePreviewTerminal.getThemeWidgetStyle(reportTheme); //主题色设置为透明或者插件启动前已有的主题启动插件后主题色为null if (widgetStyle.getThemeColor() == null) { widgetStyle.setThemeColor(DEFAULT_THEME_COLOR); } } /** * 填充圆角矩形背景色 */ public void paintBgColor(Graphics g, ThemedWidgetStyle widgetStyle) { this.paintBgColor(g, widgetStyle, DEFAULT_ALPHA); } public Color getIconColor() { return this.themePreviewTerminal.getThemeWidgetStyle(reportTheme).getIconColor(); } protected Icon setStyleTwoIcon(Icon icon, Icon defaultIcon) { if (this.reportTheme != null) { if (icon instanceof ImageIcon) { ImageIcon imageIcon = (ImageIcon) icon; BufferedImage bufferedImage = ImageUtils.colorImage(ImageUtils.imageIconToBufferedImage(imageIcon), getIconColor()); return new ImageIcon(bufferedImage); } } return defaultIcon; } /** * 填充圆角矩形背景色 */ public void paintBgColor(Graphics g, ThemedWidgetStyle widgetStyle, int alpha) { ThemedWidgetStyle themeWidgetStyle = this.themePreviewTerminal.getThemeWidgetStyle(this.reportTheme); Color themeColor = themeWidgetStyle.getThemeColor(); themeColor = themeColor == null ? WidgetThemeDisplayConstants.DEFAULT_TRANSPARENT_COLOR : themeColor; g.setColor(new Color(themeColor.getRed(), themeColor.getGreen(), themeColor.getBlue(), alpha)); g.fillRoundRect(0, 0, getSize().width - 1, getSize().height - 1, (int) widgetStyle.getBorderStyle().getRadius(), (int) widgetStyle.getBorderStyle().getRadius()); //需要重新绘制一遍字体,否则会被颜色填充给遮住 Graphics2D g2d = (Graphics2D) g.create(); ThemeTextStyle textStyle = themeWidgetStyle.getTextStyle(); FRFont font = FRFont.getInstance(textStyle.getName(), textStyle.getCompositeFontStyle(), textStyle.getFontSize(), textStyle.getFontColor()); BaseUtils.drawStringStyleInRotation(g2d, getWidth(), getHeight(), this.value, Style.getInstance(font).deriveHorizontalAlignment(Constants.LEFT) .deriveTextStyle(Style.TEXTSTYLE_SINGLELINE), NO_SCALE_RESOLUTION); } @Override public void paintComponent(Graphics g) { super.paintComponent(g); if (this.reportTheme == null) { return; } Graphics2D g2d = (Graphics2D) g.create(); ThemeTextStyle textStyle = this.themePreviewTerminal.getThemeWidgetStyle(reportTheme).getTextStyle(); FRFont font = FRFont.getInstance(textStyle.getName(), textStyle.getCompositeFontStyle(), textStyle.getFontSize(), textStyle.getFontColor()); //每个预览格子通用的字体绘制 BaseUtils.drawStringStyleInRotation(g2d, getWidth(), getHeight(), this.value, Style.getInstance(font).deriveHorizontalAlignment(Constants.LEFT) .deriveTextStyle(Style.TEXTSTYLE_SINGLELINE), NO_SCALE_RESOLUTION); } }