Browse Source

REPORT-92161 设计器-图标-数据源相关-部分图标无反色处理,选中后就看不到图标了

release/11.0
obo 1 year ago
parent
commit
d1d46dc547
  1. 49
      designer-base/src/main/java/com/fr/design/gui/controlpane/JListControlPane.java
  2. 67
      designer-base/src/main/java/com/fr/design/utils/IconColorUtils.java

49
designer-base/src/main/java/com/fr/design/gui/controlpane/JListControlPane.java

@ -7,7 +7,6 @@ import com.fr.design.gui.ilist.JNameEdList;
import com.fr.design.gui.ilist.ListModelElement;
import com.fr.design.gui.ilist.ModNameActionListener;
import com.fr.design.i18n.Toolkit;
import com.fr.design.utils.IconColorUtils;
import com.fr.design.utils.gui.GUICoreUtils;
import com.fr.form.event.Listener;
import com.fr.general.ComparatorUtils;
@ -18,12 +17,13 @@ import com.fr.stable.Nameable;
import com.fr.stable.StringUtils;
import com.fr.stable.core.PropertyChangeAdapter;
import javax.swing.DefaultListCellRenderer;
import javax.swing.DefaultListModel;
import javax.swing.Icon;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.ListCellRenderer;
import javax.swing.ListSelectionModel;
import javax.swing.SwingUtilities;
import javax.swing.event.ListSelectionEvent;
@ -346,25 +346,46 @@ public abstract class JListControlPane extends JControlPane implements ListContr
}
protected class NameableListCellRenderer extends
DefaultListCellRenderer {
JPanel implements ListCellRenderer<Object> {
private final JLabel textLabel;
private final JLabel iconLabel;
/**
* JList默认单元格渲染器的选中背景色
*/
private final Color selectedBgColor = new Color(65, 155, 249);
protected NameableListCellRenderer() {
setLayout(new BorderLayout());
this.textLabel = new JLabel();
this.iconLabel = new JLabel();
add(this.textLabel, BorderLayout.CENTER);
add(this.iconLabel, BorderLayout.WEST);
this.iconLabel.setBackground(Color.WHITE);
//iconLabel和textLabel的背景颜色不会被JList背景颜色覆盖,开发者自定义
this.textLabel.setOpaque(true);
this.iconLabel.setOpaque(true);
}
@Override
public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {
super.getListCellRendererComponent(list, value, index, isSelected,
cellHasFocus);
if (value instanceof ListModelElement) {
ListModelElement element = ((ListModelElement) value);
Nameable nameable = element.wrapper;
this.setText(nameable.getName());
this.textLabel.setText(nameable.getName());
boolean iconSet = false;
for (NameableCreator creator : JListControlPane.this.creators()) {
if (creator.menuIcon() != null && creator.acceptObject2Populate(nameable) != null) {
Icon icon = creator.menuIcon();
this.iconLabel.setIcon(creator.menuIcon());
if(isSelected) {
icon = IconColorUtils.convert(icon, Color.WHITE);
this.textLabel.setBackground(selectedBgColor);
this.textLabel.setForeground(Color.WHITE);
} else {
this.textLabel.setBackground(Color.WHITE);
this.textLabel.setForeground(Color.BLACK);
}
this.setIcon(icon);
this.setToolTipText(creator.createTooltip());
iconSet = true;
break;
@ -376,8 +397,16 @@ public abstract class JListControlPane extends JControlPane implements ListContr
}
return this;
}
}
/**
* 改造后兼容基类NoIconNameableListCellRenderer使用,添加此setIcon函数
*
* @param icon 图标,可为null
*/
public void setIcon(Icon icon) {
this.iconLabel.setIcon(icon);
}
}
@Override
public BasicBeanPane createPaneByCreators(NameableCreator creator) {
return Reflect.on(creator.getUpdatePane()).create().get();

67
designer-base/src/main/java/com/fr/design/utils/IconColorUtils.java

@ -1,67 +0,0 @@
package com.fr.design.utils;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
/**
* 对Icon进行颜色处理的工具类
*
* @author obo
* @since 11.0
* Created on 2023/4/3
*/
public final class IconColorUtils {
/**
* 转色处理并返回处理后的Icon
*
* @param icon 需要转色处理的icon
* @param color 需要转的颜色,可能要和视觉沟通
* @return 转色后的icon
*/
public static Icon convert(Icon icon, Color color) {
BufferedImage image = iconToImage(icon);
Image convertedImage = createConvertedImage(image, color);
return new ImageIcon(convertedImage);
}
/**
* 获取icon中的image对象,虽然设计器中未来大部分图标会读取为SVGIcon类型,其中直接包含image对象
* 但存在某些特殊情况,例如绘制带警告图标的数据集时,无法直接获取image对象,因此统一使用此方法获取
*
* @param icon 需要获取image的icon
* @return 获取icon对应的image
*/
private static BufferedImage iconToImage(Icon icon) {
BufferedImage image = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = image.createGraphics();
icon.paintIcon(null, g2d, 0, 0);
g2d.dispose();
return image;
}
/**
* 转色
*
* @param image 需要转色的image
* @param color 需要转的颜色,可能要和视觉沟通
* @return 转色后的image
*/
private static Image createConvertedImage(BufferedImage image, Color color) {
int colorRgb = (color.getRGB() & 0x00FFFFFF);
for (int x = 0; x < image.getWidth(); x++) {
for (int y = 0; y < image.getHeight(); y++) {
int originalArgb = image.getRGB(x, y);
int alpha = (originalArgb >> 24) & 0xFF;
int newArgb = (alpha << 24) | colorRgb;
image.setRGB(x, y, newArgb);
}
}
return image;
}
}
Loading…
Cancel
Save