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.theme.widget.style.ThemedWidgetStyle; import com.fr.design.layout.FRGUIPaneFactory; import com.fr.general.FRFont; import com.fr.widgettheme.theme.widget.theme.WidgetThemeDisplayConstants; import com.fr.stable.Constants; import javax.swing.Icon; import javax.swing.ImageIcon; import javax.swing.JPanel; import java.awt.Color; import java.awt.Dimension; import java.awt.Font; 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; /** * 字体的颜色 */ 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) { this.reportTheme = reportTheme; ThemedWidgetStyle widgetStyle = (ThemedWidgetStyle) this.reportTheme.getWidgetStyle(); //主题色设置为透明或者插件启动前已有的主题启动插件后主题色为null if (widgetStyle.getThemeColor() == null) { widgetStyle.setThemeColor(DEFAULT_THEME_COLOR); } } /** * 填充圆角矩形背景色 */ public void paintBgColor(Graphics g, ThemedWidgetStyle widgetStyle) { this.paintBgColor(g, widgetStyle, DEFAULT_ALPHA); } public boolean isDefaultStyle() { ThemedWidgetStyle widgetStyle = (ThemedWidgetStyle) reportTheme.getWidgetStyle(); return widgetStyle.getStyleType() == ThemedWidgetStyle.DEFAULT_STYLE; } public Color getThemeColor() { ThemedWidgetStyle widgetStyle = (ThemedWidgetStyle) reportTheme.getWidgetStyle(); return widgetStyle.getThemeColor(); } protected Icon setStyleTwoIcon(Icon icon, Icon defaultIcon) { if (this.reportTheme != null && !isDefaultStyle()) { if (icon instanceof ImageIcon) { ImageIcon imageIcon = (ImageIcon) icon; BufferedImage bufferedImage = ImageUtils.colorImage(ImageUtils.imageIconToBufferedImage(imageIcon), getThemeColor()); return new ImageIcon(bufferedImage); } } return defaultIcon; } /** * 填充圆角矩形背景色 */ public void paintBgColor(Graphics g, ThemedWidgetStyle widgetStyle, int alpha) { Color themeColor = widgetStyle.getThemeColor(); g.setColor(new Color(themeColor.getRed(), themeColor.getGreen(), themeColor.getBlue(), alpha)); g.fillRoundRect(0, 0, getSize().width - 1, getSize().height - 1, widgetStyle.getBorderStyle().getRadius(), widgetStyle.getBorderStyle().getRadius()); //需要重新绘制一遍字体,否则会被颜色填充给遮住 Graphics2D g2d = (Graphics2D) g.create(); FRFont font = FRFont.getInstance(FRFont.DEFAULT_FONTNAME, Font.PLAIN, widgetStyle.getFontSizeStyle().getFontSize(), textColor); 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(); ThemedWidgetStyle widgetStyle = (ThemedWidgetStyle) reportTheme.getWidgetStyle(); FRFont font = FRFont.getInstance(FRFont.DEFAULT_FONTNAME, Font.PLAIN, widgetStyle.getFontSizeStyle().getFontSize(), textColor); //每个预览格子通用的字体绘制 BaseUtils.drawStringStyleInRotation(g2d, getWidth(), getHeight(), this.value, Style.getInstance(font).deriveHorizontalAlignment(Constants.LEFT) .deriveTextStyle(Style.TEXTSTYLE_SINGLELINE), NO_SCALE_RESOLUTION); } }