| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.bulenkov.iconloader.util; |
| 18 |
|
| 19 |
import com.bulenkov.iconloader.IconLoader; |
| 20 |
import com.bulenkov.iconloader.RetinaImage; |
| 21 |
import org.jetbrains.annotations.NonNls; |
| 22 |
import org.jetbrains.annotations.NotNull; |
| 23 |
import org.jetbrains.annotations.Nullable; |
| 24 |
|
| 25 |
import javax.swing.*; |
| 26 |
import java.awt.*; |
| 27 |
import java.awt.image.ImageFilter; |
| 28 |
import java.io.IOException; |
| 29 |
import java.io.InputStream; |
| 30 |
import java.io.Serializable; |
| 31 |
import java.net.HttpURLConnection; |
| 32 |
import java.net.URL; |
| 33 |
import java.net.URLConnection; |
| 34 |
import java.util.ArrayList; |
| 35 |
import java.util.concurrent.ConcurrentMap; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
public class ImageLoader implements Serializable { |
| 41 |
|
| 42 |
|
| 43 |
private static final ConcurrentMap<String, Image> ourCache = new ConcurrentSoftValueHashMap<String, Image>(); |
| 44 |
|
| 45 |
private static class ImageDesc { |
| 46 |
public enum Type { |
| 47 |
PNG, |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
UNDEFINED; |
| 57 |
|
| 58 |
public Image load(URL url, InputStream stream, float scale) throws IOException { |
| 59 |
return ImageLoader.load(stream, (int)scale); |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
public final String path; |
| 64 |
public final @Nullable Class cls; |
| 65 |
public final float scale; |
| 66 |
public final Type type; |
| 67 |
public final boolean original; |
| 68 |
|
| 69 |
public ImageDesc(String path, Class cls, float scale, Type type) { |
| 70 |
this(path, cls, scale, type, false); |
| 71 |
} |
| 72 |
|
| 73 |
public ImageDesc(String path, Class cls, float scale, Type type, boolean original) { |
| 74 |
this.path = path; |
| 75 |
this.cls = cls; |
| 76 |
this.scale = scale; |
| 77 |
this.type = type; |
| 78 |
this.original = original; |
| 79 |
} |
| 80 |
|
| 81 |
@Nullable |
| 82 |
public Image load() throws IOException { |
| 83 |
String cacheKey = null; |
| 84 |
InputStream stream = null; |
| 85 |
URL url = null; |
| 86 |
if (cls != null) { |
| 87 |
|
| 88 |
stream = cls.getResourceAsStream(path); |
| 89 |
if (stream == null) return null; |
| 90 |
} |
| 91 |
if (stream == null) { |
| 92 |
url = new URL(path); |
| 93 |
URLConnection connection = url.openConnection(); |
| 94 |
if (connection instanceof HttpURLConnection) { |
| 95 |
if (!original) return null; |
| 96 |
connection.addRequestProperty("User-Agent", "IntelliJ"); |
| 97 |
|
| 98 |
cacheKey = path; |
| 99 |
Image image = ourCache.get(cacheKey); |
| 100 |
if (image != null) return image; |
| 101 |
} |
| 102 |
stream = connection.getInputStream(); |
| 103 |
} |
| 104 |
Image image = type.load(url, stream, scale); |
| 105 |
if (image != null && cacheKey != null) { |
| 106 |
ourCache.put(cacheKey, image); |
| 107 |
} |
| 108 |
return image; |
| 109 |
} |
| 110 |
|
| 111 |
@Override |
| 112 |
public String toString() { |
| 113 |
return path + ", scale: " + scale + ", type: " + type; |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
private static class ImageDescList extends ArrayList<ImageDesc> { |
| 118 |
private ImageDescList() {} |
| 119 |
|
| 120 |
@Nullable |
| 121 |
public Image load() { |
| 122 |
return load(ImageConverterChain.create()); |
| 123 |
} |
| 124 |
|
| 125 |
@Nullable |
| 126 |
public Image load(@NotNull ImageConverterChain converters) { |
| 127 |
for (ImageDesc desc : this) { |
| 128 |
try { |
| 129 |
Image image = desc.load(); |
| 130 |
if (image == null) continue; |
| 131 |
|
| 132 |
return converters.convert(image, desc); |
| 133 |
} |
| 134 |
catch (IOException ignore) { |
| 135 |
} |
| 136 |
} |
| 137 |
return null; |
| 138 |
} |
| 139 |
|
| 140 |
public static ImageDescList create(@NotNull String file, |
| 141 |
@Nullable Class cls, |
| 142 |
boolean dark, |
| 143 |
boolean retina, |
| 144 |
boolean allowFloatScaling) |
| 145 |
{ |
| 146 |
ImageDescList vars = new ImageDescList(); |
| 147 |
if (retina || dark) { |
| 148 |
final String name = getNameWithoutExtension(file); |
| 149 |
final String ext = getExtension(file); |
| 150 |
|
| 151 |
float scale = calcScaleFactor(allowFloatScaling); |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
if (dark && retina) { |
| 164 |
vars.add(new ImageDesc(name + "@2x_dark." + ext, cls, 2f, ImageDesc.Type.PNG)); |
| 165 |
} |
| 166 |
|
| 167 |
if (dark) { |
| 168 |
vars.add(new ImageDesc(name + "_dark." + ext, cls, 1f, ImageDesc.Type.PNG)); |
| 169 |
} |
| 170 |
|
| 171 |
if (retina) { |
| 172 |
vars.add(new ImageDesc(name + "@2x." + ext, cls, 2f, ImageDesc.Type.PNG)); |
| 173 |
} |
| 174 |
} |
| 175 |
vars.add(new ImageDesc(file, cls, 1f, ImageDesc.Type.PNG, true)); |
| 176 |
return vars; |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
private interface ImageConverter { |
| 181 |
Image convert(@Nullable Image source, ImageDesc desc); |
| 182 |
} |
| 183 |
|
| 184 |
private static class ImageConverterChain extends ArrayList<ImageConverter> { |
| 185 |
private ImageConverterChain() {} |
| 186 |
|
| 187 |
public static ImageConverterChain create() { |
| 188 |
return new ImageConverterChain(); |
| 189 |
} |
| 190 |
|
| 191 |
public ImageConverterChain withFilter(final ImageFilter filter) { |
| 192 |
return with(new ImageConverter() { |
| 193 |
@Override |
| 194 |
public Image convert(Image source, ImageDesc desc) { |
| 195 |
return ImageUtil.filter(source, filter); |
| 196 |
} |
| 197 |
}); |
| 198 |
} |
| 199 |
|
| 200 |
public ImageConverterChain withRetina() { |
| 201 |
return with(new ImageConverter() { |
| 202 |
@Override |
| 203 |
public Image convert(Image source, ImageDesc desc) { |
| 204 |
if (source != null && UIUtil.isRetina() && desc.scale > 1) { |
| 205 |
return RetinaImage.createFrom(source, (int)desc.scale, ourComponent); |
| 206 |
} |
| 207 |
return source; |
| 208 |
} |
| 209 |
}); |
| 210 |
} |
| 211 |
|
| 212 |
public ImageConverterChain with(ImageConverter f) { |
| 213 |
add(f); |
| 214 |
return this; |
| 215 |
} |
| 216 |
|
| 217 |
public Image convert(Image image, ImageDesc desc) { |
| 218 |
for (ImageConverter f : this) { |
| 219 |
image = f.convert(image, desc); |
| 220 |
} |
| 221 |
return image; |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
public static final Component ourComponent = new Component() { |
| 226 |
}; |
| 227 |
|
| 228 |
private static boolean waitForImage(Image image) { |
| 229 |
if (image == null) return false; |
| 230 |
if (image.getWidth(null) > 0) return true; |
| 231 |
MediaTracker mediatracker = new MediaTracker(ourComponent); |
| 232 |
mediatracker.addImage(image, 1); |
| 233 |
try { |
| 234 |
mediatracker.waitForID(1, 5000); |
| 235 |
} |
| 236 |
catch (InterruptedException ex) { |
| 237 |
ex.printStackTrace(); |
| 238 |
} |
| 239 |
return !mediatracker.isErrorID(1); |
| 240 |
} |
| 241 |
|
| 242 |
@Nullable |
| 243 |
public static Image loadFromUrl(@NotNull URL url) { |
| 244 |
return loadFromUrl(url, true); |
| 245 |
} |
| 246 |
|
| 247 |
@Nullable |
| 248 |
public static Image loadFromUrl(@NotNull URL url, boolean allowFloatScaling) { |
| 249 |
return loadFromUrl(url, allowFloatScaling, null); |
| 250 |
} |
| 251 |
|
| 252 |
@Nullable |
| 253 |
public static Image loadFromUrl(@NotNull URL url, boolean allowFloatScaling, ImageFilter filter) { |
| 254 |
final float scaleFactor = calcScaleFactor(allowFloatScaling); |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
final boolean scaleImages = (scaleFactor > 1.0f) && !UIUtil.isRetina(); |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
final boolean loadRetinaImages = UIUtil.isRetina() || scaleImages; |
| 264 |
|
| 265 |
return ImageDescList.create(url.toString(), null, UIUtil.isUnderDarcula(), loadRetinaImages, allowFloatScaling).load( |
| 266 |
ImageConverterChain.create(). |
| 267 |
withFilter(filter). |
| 268 |
withRetina(). |
| 269 |
with(new ImageConverter() { |
| 270 |
public Image convert(Image source, ImageDesc desc) { |
| 271 |
if (source != null && scaleImages ) { |
| 272 |
if (desc.path.contains("@2x")) |
| 273 |
return scaleImage(source, scaleFactor / 2.0f); |
| 274 |
else |
| 275 |
return scaleImage(source, scaleFactor); |
| 276 |
} |
| 277 |
return source; |
| 278 |
} |
| 279 |
})); |
| 280 |
} |
| 281 |
|
| 282 |
private static float calcScaleFactor(boolean allowFloatScaling) { |
| 283 |
float scaleFactor = allowFloatScaling ? JBUI.scale(1f) : JBUI.scale(1f) > 1.5f ? 2f : 1f; |
| 284 |
assert scaleFactor >= 1.0f : "By design, only scale factors >= 1.0 are supported"; |
| 285 |
return scaleFactor; |
| 286 |
} |
| 287 |
|
| 288 |
@NotNull |
| 289 |
private static Image scaleImage(Image image, float scale) { |
| 290 |
int w = image.getWidth(null); |
| 291 |
int h = image.getHeight(null); |
| 292 |
if (w <= 0 || h <= 0) { |
| 293 |
return image; |
| 294 |
} |
| 295 |
int width = (int)(scale * w); |
| 296 |
int height = (int)(scale * h); |
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
return Scalr.resize(ImageUtil.toBufferedImage(image), Scalr.Method.QUALITY, width, height); |
| 301 |
} |
| 302 |
|
| 303 |
@Nullable |
| 304 |
public static Image loadFromUrl(URL url, boolean dark, boolean retina) { |
| 305 |
return loadFromUrl(url, dark, retina, null); |
| 306 |
} |
| 307 |
|
| 308 |
@Nullable |
| 309 |
public static Image loadFromUrl(URL url, boolean dark, boolean retina, ImageFilter filter) { |
| 310 |
return ImageDescList.create(url.toString(), null, dark, retina, true). |
| 311 |
load(ImageConverterChain.create().withFilter(filter).withRetina()); |
| 312 |
} |
| 313 |
|
| 314 |
@Nullable |
| 315 |
public static Image loadFromResource(@NonNls @NotNull String s) { |
| 316 |
Class callerClass = ReflectionUtil.getGrandCallerClass(); |
| 317 |
if (callerClass == null) return null; |
| 318 |
return loadFromResource(s, callerClass); |
| 319 |
} |
| 320 |
|
| 321 |
@Nullable |
| 322 |
public static Image loadFromResource(@NonNls @NotNull String path, @NotNull Class aClass) { |
| 323 |
return ImageDescList.create(path, aClass, UIUtil.isUnderDarcula(), UIUtil.isRetina() || JBUI.scale(1.0f) >= 1.5f, true). |
| 324 |
load(ImageConverterChain.create().withRetina()); |
| 325 |
} |
| 326 |
|
| 327 |
public static Image loadFromStream(@NotNull final InputStream inputStream) { |
| 328 |
return loadFromStream(inputStream, 1); |
| 329 |
} |
| 330 |
|
| 331 |
public static Image loadFromStream(@NotNull final InputStream inputStream, final int scale) { |
| 332 |
return loadFromStream(inputStream, scale, null); |
| 333 |
} |
| 334 |
|
| 335 |
public static Image loadFromStream(@NotNull final InputStream inputStream, final int scale, ImageFilter filter) { |
| 336 |
Image image = load(inputStream, scale); |
| 337 |
ImageDesc desc = new ImageDesc("", null, scale, ImageDesc.Type.UNDEFINED); |
| 338 |
return ImageConverterChain.create().withFilter(filter).withRetina().convert(image, desc); |
| 339 |
} |
| 340 |
|
| 341 |
private static Image load(@NotNull final InputStream inputStream, final int scale) { |
| 342 |
if (scale <= 0) throw new IllegalArgumentException("Scale must be 1 or greater"); |
| 343 |
try { |
| 344 |
BufferExposingByteArrayOutputStream outputStream = new BufferExposingByteArrayOutputStream(); |
| 345 |
try { |
| 346 |
byte[] buffer = new byte[1024]; |
| 347 |
while (true) { |
| 348 |
final int n = inputStream.read(buffer); |
| 349 |
if (n < 0) break; |
| 350 |
outputStream.write(buffer, 0, n); |
| 351 |
} |
| 352 |
} |
| 353 |
finally { |
| 354 |
inputStream.close(); |
| 355 |
} |
| 356 |
|
| 357 |
Image image = Toolkit.getDefaultToolkit().createImage(outputStream.getInternalBuffer(), 0, outputStream.size()); |
| 358 |
|
| 359 |
waitForImage(image); |
| 360 |
|
| 361 |
return image; |
| 362 |
} |
| 363 |
catch (Exception ex) { |
| 364 |
ex.printStackTrace(); |
| 365 |
} |
| 366 |
|
| 367 |
return null; |
| 368 |
} |
| 369 |
|
| 370 |
public static boolean isGoodSize(final Icon icon) { |
| 371 |
return IconLoader.isGoodSize(icon); |
| 372 |
} |
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
public static java.util.List<Pair<String, Integer>> getFileNames(@NotNull String file) { |
| 378 |
return getFileNames(file, false, false); |
| 379 |
} |
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
public static java.util.List<Pair<String, Integer>> getFileNames(@NotNull String file, boolean dark, boolean retina) { |
| 385 |
new UnsupportedOperationException("unsupported method").printStackTrace(); |
| 386 |
return new ArrayList<Pair<String, Integer>>(); |
| 387 |
} |
| 388 |
|
| 389 |
@NotNull |
| 390 |
public static String getNameWithoutExtension(@NotNull String name) { |
| 391 |
int i = name.lastIndexOf('.'); |
| 392 |
if (i != -1) { |
| 393 |
name = name.substring(0, i); |
| 394 |
} |
| 395 |
return name; |
| 396 |
} |
| 397 |
|
| 398 |
@NotNull |
| 399 |
public static String getExtension(@NotNull String fileName) { |
| 400 |
int index = fileName.lastIndexOf('.'); |
| 401 |
if (index < 0) return ""; |
| 402 |
return fileName.substring(index + 1); |
| 403 |
} |
| 404 |
} |