Desktop tool for browsing account info from EVE-Online
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ImageManager.java 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Copyright (c) 2009 Chris Smith
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package uk.co.md87.evetool;
  23. import java.awt.Image;
  24. import java.io.File;
  25. import java.io.IOException;
  26. import java.util.ArrayList;
  27. import java.util.List;
  28. import java.util.concurrent.atomic.AtomicInteger;
  29. import java.util.logging.Level;
  30. import java.util.logging.Logger;
  31. import javax.imageio.ImageIO;
  32. import uk.co.md87.evetool.api.io.Downloader;
  33. import uk.co.md87.evetool.api.io.QueueSizeListener;
  34. /**
  35. *
  36. * TODO: Document ImageManager
  37. * @author chris
  38. */
  39. public class ImageManager {
  40. protected static final List<String> REQUESTS = new ArrayList<String>();
  41. protected static final String ILLEGAL_CHARS = "[\\\\\"/:\\*\\?\"<>\\|]";
  42. private static final List<QueueSizeListener> listeners = new ArrayList<QueueSizeListener>();
  43. private static final AtomicInteger queueSize = new AtomicInteger(0);
  44. protected final String cacheDir;
  45. public ImageManager(final String cacheDir) {
  46. this.cacheDir = cacheDir;
  47. final File dir = new File(cacheDir);
  48. if (!dir.isDirectory() && !dir.mkdirs()) {
  49. Logger.getLogger(ImageManager.class.getName()).log(Level.SEVERE,
  50. "Unable to create image cache directory");
  51. }
  52. }
  53. public Image getImage(final ImageType type, final Object ... arguments) throws IOException {
  54. final String url = type.getUrl(arguments);
  55. final String cacheUrl = url.replaceAll(ILLEGAL_CHARS, "_");
  56. synchronized (REQUESTS) {
  57. fireQueueSizeChange(queueSize.incrementAndGet());
  58. while (REQUESTS.contains(url)) {
  59. try {
  60. REQUESTS.wait();
  61. } catch (InterruptedException ex) {
  62. // Ignore
  63. }
  64. }
  65. REQUESTS.add(url);
  66. }
  67. try {
  68. final File file = new File(cacheDir, cacheUrl);
  69. if (!file.exists()) {
  70. Downloader.downloadPage(url, file);
  71. }
  72. return ImageIO.read(file);
  73. } finally {
  74. synchronized (REQUESTS) {
  75. fireQueueSizeChange(queueSize.decrementAndGet());
  76. REQUESTS.remove(url);
  77. REQUESTS.notifyAll();
  78. }
  79. }
  80. }
  81. public static void addQueueSizeListener(final QueueSizeListener listener) {
  82. synchronized (listeners) {
  83. listeners.add(listener);
  84. listener.queueSizeUpdate(queueSize.get());
  85. }
  86. }
  87. protected static void fireQueueSizeChange(final int queueSize) {
  88. synchronized (listeners) {
  89. for (QueueSizeListener listener : listeners) {
  90. listener.queueSizeUpdate(queueSize);
  91. }
  92. }
  93. }
  94. }