| 20 | import java.util.function.Consumer; |
| 21 | |
| 22 | @Slf4j |
| 23 | public class GetIconUtil { |
| 24 | private static final int MAX_CONSUMER_THREAD_NUM = 4; |
| 25 | private static final FileSystemView FILE_SYSTEM_VIEW = FileSystemView.getFileSystemView(); |
| 26 | private static final Task EMPTY_TASK = new Task(null, 0, 0); |
| 27 | private final ConcurrentHashMap<String, ImageIcon> constantIconMap = new ConcurrentHashMap<>(); |
| 28 | private final SynchronousQueue<Task> workingQueue = new SynchronousQueue<>(); |
| 29 | private final ConcurrentHashMap<String, IconCache> cacheIconMap = new ConcurrentHashMap<>(); |
| 30 | |
| 31 | private static volatile GetIconUtil INSTANCE = null; |
| 32 | |
| 33 | private record IconCache( |
| 34 | long cacheCreateTime, |
| 35 | ImageIcon icon |
| 36 | ) { |
| 37 | } |
| 38 | |
| 39 | private GetIconUtil() { |
| 40 | initIconCache(); |
| 41 | startWorkingThread(); |
| 42 | clearIconCacheThread(); |
| 43 | } |
| 44 | |
| 45 | public static GetIconUtil getInstance() { |
| 46 | if (INSTANCE == null) { |
| 47 | synchronized (GetIconUtil.class) { |
| 48 | if (INSTANCE == null) { |
| 49 | INSTANCE = new GetIconUtil(); |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | return INSTANCE; |
| 54 | } |
| 55 | |
| 56 | public ImageIcon changeIcon(ImageIcon icon, int width, int height) { |
| 57 | if (icon == null) { |
| 58 | return null; |
| 59 | } |
| 60 | Image image = icon.getImage().getScaledInstance(width, height, Image.SCALE_FAST); |
| 61 | return new ImageIcon(image); |
| 62 | } |
| 63 | |
| 64 | private void initIconCache() { |
| 65 | constantIconMap.put("dllImageIcon", Objects.requireNonNull((ImageIcon) FILE_SYSTEM_VIEW.getSystemIcon(new File("user", "emptyRecycleBin.dll")))); |
| 66 | constantIconMap.put("folderImageIcon", Objects.requireNonNull((ImageIcon) FILE_SYSTEM_VIEW.getSystemIcon(new File("user")))); |
| 67 | constantIconMap.put("txtImageIcon", Objects.requireNonNull((ImageIcon) FILE_SYSTEM_VIEW.getSystemIcon(new File("user\\cmds.txt")))); |
| 68 | constantIconMap.put("blankIcon", new ImageIcon(Objects.requireNonNull(GetIconUtil.class.getResource("/icons/blank.png")))); |
| 69 | constantIconMap.put("recycleBin", new ImageIcon(Objects.requireNonNull(GetIconUtil.class.getResource("/icons/recyclebin.png")))); |
| 70 | constantIconMap.put("updateIcon", new ImageIcon(Objects.requireNonNull(GetIconUtil.class.getResource("/icons/update.png")))); |
| 71 | constantIconMap.put("helpIcon", new ImageIcon(Objects.requireNonNull(GetIconUtil.class.getResource("/icons/help.png")))); |
| 72 | constantIconMap.put("completeIcon", new ImageIcon(Objects.requireNonNull(GetIconUtil.class.getResource("/icons/complete.png")))); |
| 73 | constantIconMap.put("loadingIcon", new ImageIcon(Objects.requireNonNull(GetIconUtil.class.getResource("/icons/loading.gif")))); |
| 74 | } |
| 75 | |
| 76 | public ImageIcon getCommandIcon(String commandName, int width, int height) { |
| 77 | if (commandName == null || commandName.isEmpty()) { |
| 78 | if (IsDebug.isDebug()) { |
| 79 | log.warn("Command is empty"); |
nothing calls this directly
no outgoing calls
no test coverage detected