View Javadoc
1   package com.jsql.view.swing.util;
2   
3   import com.formdev.flatlaf.extras.FlatSVGIcon;
4   
5   import javax.swing.*;
6   import java.awt.*;
7   import java.util.Objects;
8   
9   public class ModelSvgIcon {
10      private final Color from;
11      private final String toDarkUi;
12      private final Color toDark;
13      private final FlatSVGIcon icon;
14      private String keyLabel;
15      private String keyTooltip;
16  
17      ModelSvgIcon(String name, float scale) {
18          this(name, Color.BLACK, "ComboBox.foreground", scale);
19      }
20  
21      ModelSvgIcon(String name, int from) {
22          this(name, new Color(from), "ComboBox.foreground", 0.02f);
23      }
24  
25      ModelSvgIcon(String name, Color from, String toDark, float scale) {
26          this(name, from, toDark, UIManager.getColor(toDark), scale);
27      }
28  
29      ModelSvgIcon(String name, Color from, String toDarkUi, Color toDark, float scale) {
30          this.from = from;
31          this.toDarkUi = toDarkUi;
32          this.toDark = toDark;
33          this.icon = this.createSvgIcon(
34              name,
35              from,
36              toDarkUi != null
37              ? UIManager.getColor(toDarkUi)
38              : toDark,
39              scale
40          );
41      }
42  
43      public FlatSVGIcon createSvgIcon(String name, Color from, Color toDark, float scale) {
44          return new FlatSVGIcon(Objects.requireNonNull(UiUtil.class.getClassLoader().getResource(String.format(
45              name.endsWith(".svg") ? "%s" : "swing/images/icons/%s.svg",
46              name
47          ))))
48          .setColorFilter(new FlatSVGIcon.ColorFilter().add(from, null, toDark))
49          .derive(scale);
50      }
51  
52      public void setColorFilter() {
53          this.icon.setColorFilter(new FlatSVGIcon.ColorFilter().add(
54              this.from,
55              null,
56              this.toDarkUi != null
57              ? UIManager.getColor(this.toDarkUi)
58              : this.toDark
59          ));
60      }
61  
62      public ModelSvgIcon withTab(String keyLabel, String keyTooltip) {
63          this.keyLabel = keyLabel;
64          this.keyTooltip = keyTooltip;
65          return this;
66      }
67  
68      public FlatSVGIcon getIcon() {
69          return this.icon;
70      }
71  
72      public String getKeyLabel() {
73          return this.keyLabel;
74      }
75  
76      public String getKeyTooltip() {
77          return this.keyTooltip;
78      }
79  }