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.

IconManager.java 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * Copyright (c) 2006-2008 Chris Smith, Shane Mc Cormack, Gregory Holmes
  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 com.dmdirc;
  23. import com.dmdirc.config.IdentityManager;
  24. import com.dmdirc.interfaces.ConfigChangeListener;
  25. import com.dmdirc.util.URLBuilder;
  26. import java.awt.Image;
  27. import java.awt.Toolkit;
  28. import java.io.File;
  29. import java.io.IOException;
  30. import java.net.URL;
  31. import java.util.HashMap;
  32. import java.util.Map;
  33. import javax.swing.Icon;
  34. import javax.swing.ImageIcon;
  35. /**
  36. * The icon manager provides a standard way to access icons for use in DMDirc.
  37. * It allows the user to override the default actions using config settings
  38. * under the icons domain.
  39. *
  40. * @author chris
  41. */
  42. public final class IconManager implements ConfigChangeListener {
  43. /** Previously created IconManager instance. */
  44. private static final IconManager ME = new IconManager();
  45. /** A map of existing icons. */
  46. private final Map<String, Icon> icons;
  47. /** A map of existing images. */
  48. private final Map<String, Image> images;
  49. /** Creates a new instance of IconManager. */
  50. private IconManager() {
  51. icons = new HashMap<String, Icon>();
  52. images = new HashMap<String, Image>();
  53. IdentityManager.getGlobalConfig().addChangeListener("icon", this);
  54. }
  55. /**
  56. * Returns an instance of IconManager.
  57. *
  58. * @return Instance of IconManager
  59. */
  60. public static IconManager getIconManager() {
  61. return ME;
  62. }
  63. /**
  64. * Retrieves the icon with the specified type. Returns null if the icon
  65. * wasn't found.
  66. *
  67. * @param type The name of the icon type to retrieve
  68. *
  69. * @return The icon that should be used for the specified type
  70. */
  71. public Icon getIcon(final String type) {
  72. if (!icons.containsKey(type)) {
  73. icons.put(type, new ImageIcon(getScaledImage(
  74. new ImageIcon(getIconURL(type)).getImage(), 16, 16)));
  75. }
  76. return icons.get(type);
  77. }
  78. /**
  79. * Retrieves the image with the specified type. Returns null if the icon
  80. * wasn't found.
  81. *
  82. * @param type The name of the icon type to retrieve
  83. *
  84. * @return The image that should be used for the specified type
  85. */
  86. public Image getImage(final String type) {
  87. if (!images.containsKey(type)) {
  88. images.put(type, Toolkit.getDefaultToolkit().createImage(getIconURL(type)));
  89. }
  90. return images.get(type);
  91. }
  92. /**
  93. * Returns a scaled image.
  94. *
  95. * @param image Image to scale
  96. * @param width Width of resulting image
  97. * @param height Height of resulting image
  98. *
  99. * @return Scaled Image
  100. */
  101. private Image getScaledImage(final Image image,
  102. final int width, final int height) {
  103. return image.getScaledInstance(width , height, Image.SCALE_SMOOTH);
  104. }
  105. /**
  106. * Retrieves the URL of a specified icon type.
  107. *
  108. * @param type The name of the icon type to retrieve
  109. *
  110. * @return The URL that should be used to retrieve the specified icon
  111. */
  112. private URL getIconURL(final String type) {
  113. final ClassLoader cldr = Thread.currentThread().getContextClassLoader();
  114. final URL defaultURL = cldr.getResource("com/dmdirc/res/" + type + ".png");
  115. //Get the path for the url
  116. final String path = IdentityManager.getGlobalConfig().getOption("icon",
  117. type, "dmdirc://com/dmdirc/res/" + type + ".png");
  118. //Get the url for the speficied path
  119. URL imageURL = URLBuilder.buildURL(path);
  120. try {
  121. //if the path didnt exist see if its a file
  122. if (imageURL == null) {
  123. final File file = new File(path);
  124. if (file.exists()) {
  125. imageURL = file.toURI().toURL();
  126. } else {
  127. imageURL = defaultURL;
  128. }
  129. }
  130. //check if the url has content
  131. if (imageURL == null || imageURL.getContent() == null) {
  132. imageURL = defaultURL;
  133. }
  134. } catch (IOException ex) {
  135. imageURL = defaultURL;
  136. }
  137. if (imageURL == null) {
  138. imageURL = cldr.getResource("com/dmdirc/res/icon.png");
  139. }
  140. return imageURL;
  141. }
  142. /** {@inheritDoc} */
  143. public void configChanged(final String domain, final String key) {
  144. if ("icon".equals(domain)) {
  145. if (images.containsKey(key)) {
  146. images.remove(key);
  147. }
  148. if (icons.containsKey(key)) {
  149. icons.remove(key);
  150. }
  151. }
  152. }
  153. }