From 65c7695c14d2ae1a5d41e72c060f65f43dffbd3e Mon Sep 17 00:00:00 2001 From: Konstantin Bulenkov Date: Sun, 11 Sep 2016 02:34:04 +0200 Subject: [PATCH] Ability to use Icon path patchers by 3rd party plugins --- .../com/intellij/openapi/util/IconLoader.java | 29 ++++++++++++++----- .../openapi/util/IconPathPatcher.java | 7 ++++- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/platform/util/src/com/intellij/openapi/util/IconLoader.java b/platform/util/src/com/intellij/openapi/util/IconLoader.java index 91d673143021..b313b0e39dac 100644 --- a/platform/util/src/com/intellij/openapi/util/IconLoader.java +++ b/platform/util/src/com/intellij/openapi/util/IconLoader.java @@ -184,9 +184,13 @@ public final class IconLoader { } @Nullable - public static Icon findIcon(@NotNull String path, @NotNull final Class aClass, boolean computeNow, boolean strict) { + public static Icon findIcon(@NotNull String path, @NotNull Class aClass, boolean computeNow, boolean strict) { String originalPath = path; - path = patchPath(path); + Pair patchedPath = patchPath(path); + path = patchedPath.first; + if (patchedPath.second != null) { + aClass = patchedPath.second; + } if (isReflectivePath(path)) return getReflectiveIcon(path, aClass.getClassLoader()); URL myURL = aClass.getResource(path); @@ -202,14 +206,15 @@ public final class IconLoader { return icon; } - private static String patchPath(@NotNull String path) { + @NotNull + private static Pair patchPath(@NotNull String path) { for (IconPathPatcher patcher : ourPatchers) { String newPath = patcher.patchPath(path); if (newPath != null) { - path = newPath; + return Pair.create(newPath, patcher.getContextClass(path)); } } - return path; + return Pair.create(path, null); } private static boolean isReflectivePath(@NotNull String path) { @@ -240,7 +245,11 @@ public final class IconLoader { @Nullable public static Icon findIcon(@NotNull String path, @NotNull ClassLoader classLoader) { String originalPath = path; - path = patchPath(path); + Pair patchedPath = patchPath(path); + path = patchedPath.first; + if (patchedPath.second != null) { + classLoader = patchedPath.second.getClassLoader(); + } if (isReflectivePath(path)) return getReflectiveIcon(path, classLoader); if (!StringUtil.startsWithChar(path, '/')) return null; @@ -389,8 +398,12 @@ public final class IconLoader { myScaledIconsCache.clear(); if (numberOfPatchers != ourPatchers.size()) { numberOfPatchers = ourPatchers.size(); - String path = myOriginalPath == null ? null : patchPath(myOriginalPath); - if (myClassLoader != null&& path != null && path.startsWith("/")) { + Pair patchedPath = patchPath(myOriginalPath); + String path = myOriginalPath == null ? null : patchedPath.first; + if (patchedPath.second != null) { + myClassLoader = patchedPath.second.getClassLoader(); + } + if (myClassLoader != null && path != null && path.startsWith("/")) { path = path.substring(1); final URL url = myClassLoader.getResource(path); if (url != null) { diff --git a/platform/util/src/com/intellij/openapi/util/IconPathPatcher.java b/platform/util/src/com/intellij/openapi/util/IconPathPatcher.java index e19d80be060a..f7af082fccd9 100644 --- a/platform/util/src/com/intellij/openapi/util/IconPathPatcher.java +++ b/platform/util/src/com/intellij/openapi/util/IconPathPatcher.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2015 JetBrains s.r.o. + * Copyright 2000-2016 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -28,4 +28,9 @@ public abstract class IconPathPatcher { */ @Nullable public abstract String patchPath(String path); + + @Nullable + public Class getContextClass(String path) { + return null; + } }