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 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. * Copyright (c) 2006-2015 DMDirc Developers
  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.addons.ui_swing.components;
  23. import com.dmdirc.config.GlobalConfig;
  24. import com.dmdirc.interfaces.config.AggregateConfigProvider;
  25. import com.dmdirc.interfaces.config.ConfigChangeListener;
  26. import com.dmdirc.util.URLBuilder;
  27. import java.awt.Image;
  28. import java.awt.Toolkit;
  29. import java.io.IOException;
  30. import java.net.URL;
  31. import java.util.Calendar;
  32. import java.util.Date;
  33. import java.util.GregorianCalendar;
  34. import java.util.HashMap;
  35. import java.util.Map;
  36. import javax.inject.Inject;
  37. import javax.inject.Singleton;
  38. import javax.swing.Icon;
  39. import javax.swing.ImageIcon;
  40. /**
  41. * The icon manager provides a standard way to access icons for use in DMDirc. It allows the user to
  42. * override the default actions using config settings under the icons domain.
  43. */
  44. @Singleton
  45. public class IconManager implements ConfigChangeListener {
  46. /** A map of existing icons. */
  47. private final Map<String, Icon> icons;
  48. /** A map of existing images. */
  49. private final Map<String, Image> images;
  50. /** Config manager to retrieve settings from. */
  51. private final AggregateConfigProvider configManager;
  52. /** URL builder to use for icons. */
  53. private final URLBuilder urlBuilder;
  54. /**
  55. * Creates a new instance of IconManager.
  56. *
  57. * @param configManager Config manager to retrieve settings from
  58. * @param urlBuilder URL builder to use for icons.
  59. */
  60. @Inject
  61. public IconManager(
  62. @GlobalConfig final AggregateConfigProvider configManager,
  63. final URLBuilder urlBuilder) {
  64. this.configManager = configManager;
  65. this.urlBuilder = urlBuilder;
  66. icons = new HashMap<>();
  67. images = new HashMap<>();
  68. configManager.addChangeListener("icon", this);
  69. }
  70. /**
  71. * Retrieves the icon with the specified type. Returns null if the icon wasn't found.
  72. *
  73. * @param type The name of the icon type to retrieve
  74. *
  75. * @return The icon that should be used for the specified type
  76. */
  77. public Icon getIcon(final String type) {
  78. if (icons.containsKey(type)) {
  79. return icons.get(type);
  80. }
  81. if (!icons.containsKey(type)) {
  82. final URL iconURL = getIconURL(type);
  83. final Image iconImage = Toolkit.getDefaultToolkit().getImage(iconURL);
  84. final Image scaledIconImage = getScaledImage(iconImage, 16, 16);
  85. icons.put(type, new ImageIcon(scaledIconImage));
  86. }
  87. return icons.get(type);
  88. }
  89. /**
  90. * Retrieves the icon with the specified type. Returns null if the icon wasn't found.
  91. *
  92. * @param type The name of the icon type to retrieve
  93. * @param width width of the image
  94. * @param height height of the image
  95. *
  96. * @return The icon that should be used for the specified type
  97. *
  98. * @since 0.6.3m1
  99. */
  100. public Icon getScaledIcon(final String type, final int width, final int height) {
  101. return new ImageIcon(getScaledImage(new ImageIcon(getIconURL(type)).
  102. getImage(), width, height));
  103. }
  104. /**
  105. * Retrieves the image with the specified type. Returns null if the icon wasn't found.
  106. *
  107. * @param type The name of the icon type to retrieve
  108. *
  109. * @return The image that should be used for the specified type
  110. */
  111. public Image getImage(final String type) {
  112. if (!images.containsKey(type)) {
  113. images.put(type, Toolkit.getDefaultToolkit().createImage(getIconURL(type)));
  114. }
  115. return images.get(type);
  116. }
  117. /**
  118. * Returns a scaled image.
  119. *
  120. * @param image Image to scale
  121. * @param width Width of resulting image
  122. * @param height Height of resulting image
  123. *
  124. * @return Scaled Image
  125. */
  126. private Image getScaledImage(final Image image,
  127. final int width, final int height) {
  128. return image.getScaledInstance(width, height, Image.SCALE_SMOOTH);
  129. }
  130. /**
  131. * Retrieves the URL of a specified icon type.
  132. *
  133. * @param type The name of the icon type to retrieve
  134. *
  135. * @return The URL that should be used to retrieve the specified icon
  136. */
  137. private URL getIconURL(final String type) {
  138. final String iconType = getSpecialIcons(type);
  139. final ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
  140. final ClassLoader classLoader = contextClassLoader == null
  141. ? getClass().getClassLoader() : contextClassLoader;
  142. final URL defaultURL = classLoader.getResource("com/dmdirc/res/" + iconType + ".png");
  143. //Get the path for the url
  144. final String path = configManager.hasOptionString("icon", iconType)
  145. ? configManager.getOption("icon", iconType)
  146. : "dmdirc://com/dmdirc/res/" + iconType + ".png";
  147. //Get the url for the specified path
  148. URL imageURL = urlBuilder.getUrl(path);
  149. if (imageURL == null && defaultURL != null) {
  150. imageURL = defaultURL;
  151. }
  152. if (imageURL == null) {
  153. imageURL = classLoader.getResource("com/dmdirc/res/icon.png");
  154. }
  155. //Check URL points to a valid location
  156. try {
  157. imageURL.openConnection().connect();
  158. } catch (IOException ex) {
  159. imageURL = classLoader.getResource("com/dmdirc/res/icon.png");
  160. }
  161. if (imageURL == null) {
  162. throw new IllegalArgumentException("Unable to load icon type '"
  163. + iconType + "', and unable to load default");
  164. }
  165. return imageURL;
  166. }
  167. private String getSpecialIcons(final String type) {
  168. final Calendar cal = new GregorianCalendar();
  169. cal.setTime(new Date());
  170. if (cal.get(Calendar.MONTH) == Calendar.DECEMBER
  171. && cal.get(Calendar.DAY_OF_MONTH) >= 12
  172. && cal.get(Calendar.DAY_OF_MONTH) <= 31
  173. && ("icon".equals(type) || "logo".equals(type))) {
  174. return "logo-special";
  175. }
  176. return type;
  177. }
  178. @Override
  179. public void configChanged(final String domain, final String key) {
  180. if ("icon".equals(domain)) {
  181. if (images.containsKey(key)) {
  182. images.remove(key);
  183. }
  184. if (icons.containsKey(key)) {
  185. icons.remove(key);
  186. }
  187. }
  188. }
  189. }