|
|
|
@ -24,11 +24,19 @@ import java.util.HashMap;
|
|
|
|
|
public class IconManager { |
|
|
|
|
|
|
|
|
|
public static boolean initialized = false; |
|
|
|
|
public static ArrayList<IconSet> iconSets = new ArrayList<>(2);; |
|
|
|
|
public static HashMap<String, WeakReference<Icon>> cache = new HashMap<>(60);; |
|
|
|
|
public static ArrayList<IconSet> iconSets = new ArrayList<>(2); |
|
|
|
|
; |
|
|
|
|
public static HashMap<String, WeakReference<Icon>> cache = new HashMap<>(60); |
|
|
|
|
; |
|
|
|
|
public static HashMap<String, WeakReference<Icon>> disableCache = new HashMap<>(60); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 获取图标集 |
|
|
|
|
* |
|
|
|
|
* @param id 图标集ID |
|
|
|
|
* @return 图标集 |
|
|
|
|
*/ |
|
|
|
|
public static IconSet getSet(String id) { |
|
|
|
|
for (IconSet set : iconSets) { |
|
|
|
|
if (set.getId().equals(id)) { |
|
|
|
@ -38,16 +46,28 @@ public class IconManager {
|
|
|
|
|
throw new IconException("[IconManager] Can not find icon set by id: " + id); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 添加图标集 |
|
|
|
|
* |
|
|
|
|
* @param set 图标集 |
|
|
|
|
*/ |
|
|
|
|
public static void addSet(@NotNull IconSet set) { |
|
|
|
|
if (!iconSets.contains(set)) { |
|
|
|
|
iconSets.add(set); |
|
|
|
|
// 清理可能来自其他图集相同名称图标
|
|
|
|
|
clearIconSetCache(set); |
|
|
|
|
} else { |
|
|
|
|
throw new IconException("[IconManager] IconSet by id:" + set.getId() + "is added!"); |
|
|
|
|
throw new IconException("[IconManager] IconSet by id:" + set.getId() + "is added!"); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 根据图标ID获取图标 |
|
|
|
|
* |
|
|
|
|
* @param id 图标ID |
|
|
|
|
* @param <I> 图标类型 |
|
|
|
|
* @return 图标 |
|
|
|
|
*/ |
|
|
|
|
@NotNull |
|
|
|
|
public static <I extends Icon> I getIcon(String id) { |
|
|
|
|
Icon icon = findIcon(id); |
|
|
|
@ -57,6 +77,13 @@ public class IconManager {
|
|
|
|
|
return (I) icon; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 获取灰化图标 |
|
|
|
|
* |
|
|
|
|
* @param id 图标ID |
|
|
|
|
* @param <I> 图标类型 |
|
|
|
|
* @return 图标 |
|
|
|
|
*/ |
|
|
|
|
@NotNull |
|
|
|
|
public static <I extends Icon> I getDisableIcon(String id) { |
|
|
|
|
Icon icon = findDisableIcon(id); |
|
|
|
@ -67,7 +94,7 @@ public class IconManager {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Nullable |
|
|
|
|
public static <I extends Icon> I findDisableIcon(String id) { |
|
|
|
|
private static <I extends Icon> I findDisableIcon(String id) { |
|
|
|
|
final WeakReference<Icon> reference = disableCache.get(id); |
|
|
|
|
I icon = reference != null ? (I) reference.get() : null; |
|
|
|
|
if (icon == null) { |
|
|
|
@ -83,7 +110,7 @@ public class IconManager {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Nullable |
|
|
|
|
public static <I extends Icon> I findIcon(String id) { |
|
|
|
|
private static <I extends Icon> I findIcon(String id) { |
|
|
|
|
final WeakReference<Icon> reference = cache.get(id); |
|
|
|
|
I icon = reference != null ? (I) reference.get() : null; |
|
|
|
|
if (icon == null) { |
|
|
|
@ -98,15 +125,21 @@ public class IconManager {
|
|
|
|
|
return icon; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 清理所有缓存 |
|
|
|
|
*/ |
|
|
|
|
public static void clearCache() { |
|
|
|
|
cache.clear(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 清理图标缓存 |
|
|
|
|
*/ |
|
|
|
|
public static void clearIconCache(String id) { |
|
|
|
|
cache.remove(id); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static void clearIconSetCache(@NotNull IconSet set) { |
|
|
|
|
private static void clearIconSetCache(@NotNull IconSet set) { |
|
|
|
|
for (String id : set.getIds()) { |
|
|
|
|
cache.remove(id); |
|
|
|
|
} |
|
|
|
|