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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  5. * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  7. * permit persons to whom the Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  10. * Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  14. * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  15. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. */
  17. package com.dmdirc.addons.ui_swing.components;
  18. import com.dmdirc.config.GlobalConfig;
  19. import com.dmdirc.config.provider.AggregateConfigProvider;
  20. import com.dmdirc.config.provider.ConfigChangeListener;
  21. import com.dmdirc.util.URLBuilder;
  22. import java.awt.Image;
  23. import java.awt.Toolkit;
  24. import java.io.IOException;
  25. import java.net.URL;
  26. import java.util.Calendar;
  27. import java.util.Date;
  28. import java.util.GregorianCalendar;
  29. import java.util.HashMap;
  30. import java.util.Map;
  31. import javax.inject.Inject;
  32. import javax.inject.Singleton;
  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. It allows the user to
  37. * override the default actions using config settings under the icons domain.
  38. */
  39. @Singleton
  40. public class IconManager implements ConfigChangeListener {
  41. /** A map of existing icons. */
  42. private final Map<String, Icon> icons;
  43. /** A map of existing images. */
  44. private final Map<String, Image> images;
  45. /** Config manager to retrieve settings from. */
  46. private final AggregateConfigProvider configManager;
  47. /** URL builder to use for icons. */
  48. private final URLBuilder urlBuilder;
  49. /**
  50. * Creates a new instance of IconManager.
  51. *
  52. * @param configManager Config manager to retrieve settings from
  53. * @param urlBuilder URL builder to use for icons.
  54. */
  55. @Inject
  56. public IconManager(
  57. @GlobalConfig final AggregateConfigProvider configManager,
  58. final URLBuilder urlBuilder) {
  59. this.configManager = configManager;
  60. this.urlBuilder = urlBuilder;
  61. icons = new HashMap<>();
  62. images = new HashMap<>();
  63. configManager.addChangeListener("icon", this);
  64. }
  65. /**
  66. * Retrieves the icon with the specified type. Returns null if the icon wasn't found.
  67. *
  68. * @param type The name of the icon type to retrieve
  69. *
  70. * @return The icon that should be used for the specified type
  71. */
  72. public Icon getIcon(final String type) {
  73. if (icons.containsKey(type)) {
  74. return icons.get(type);
  75. }
  76. if (!icons.containsKey(type)) {
  77. final URL iconURL = getIconURL(type);
  78. final Image iconImage = Toolkit.getDefaultToolkit().getImage(iconURL);
  79. final Image scaledIconImage = getScaledImage(iconImage, 16, 16);
  80. icons.put(type, new ImageIcon(scaledIconImage));
  81. }
  82. return icons.get(type);
  83. }
  84. /**
  85. * Retrieves the icon with the specified type. Returns null if the icon wasn't found.
  86. *
  87. * @param type The name of the icon type to retrieve
  88. * @param width width of the image
  89. * @param height height of the image
  90. *
  91. * @return The icon that should be used for the specified type
  92. *
  93. * @since 0.6.3m1
  94. */
  95. public Icon getScaledIcon(final String type, final int width, final int height) {
  96. return new ImageIcon(getScaledImage(new ImageIcon(getIconURL(type)).
  97. getImage(), width, height));
  98. }
  99. /**
  100. * Retrieves the image with the specified type. Returns null if the icon wasn't found.
  101. *
  102. * @param type The name of the icon type to retrieve
  103. *
  104. * @return The image that should be used for the specified type
  105. */
  106. public Image getImage(final String type) {
  107. if (!images.containsKey(type)) {
  108. images.put(type, Toolkit.getDefaultToolkit().createImage(getIconURL(type)));
  109. }
  110. return images.get(type);
  111. }
  112. /**
  113. * Returns a scaled image.
  114. *
  115. * @param image Image to scale
  116. * @param width Width of resulting image
  117. * @param height Height of resulting image
  118. *
  119. * @return Scaled Image
  120. */
  121. private Image getScaledImage(final Image image,
  122. final int width, final int height) {
  123. return image.getScaledInstance(width, height, Image.SCALE_SMOOTH);
  124. }
  125. /**
  126. * Retrieves the URL of a specified icon type.
  127. *
  128. * @param type The name of the icon type to retrieve
  129. *
  130. * @return The URL that should be used to retrieve the specified icon
  131. */
  132. private URL getIconURL(final String type) {
  133. final String iconType = getSpecialIcons(type);
  134. final ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
  135. final ClassLoader classLoader = contextClassLoader == null
  136. ? getClass().getClassLoader() : contextClassLoader;
  137. final URL defaultURL = classLoader.getResource("com/dmdirc/res/" + iconType + ".png");
  138. //Get the path for the url
  139. final String path = configManager.hasOptionString("icon", iconType)
  140. ? configManager.getOption("icon", iconType)
  141. : "dmdirc://com/dmdirc/res/" + iconType + ".png";
  142. //Get the url for the specified path
  143. URL imageURL = urlBuilder.getUrl(path);
  144. if (imageURL == null && defaultURL != null) {
  145. imageURL = defaultURL;
  146. }
  147. if (imageURL == null) {
  148. imageURL = classLoader.getResource("com/dmdirc/res/icon.png");
  149. }
  150. //Check URL points to a valid location
  151. try {
  152. imageURL.openConnection().connect();
  153. } catch (IOException ex) {
  154. imageURL = classLoader.getResource("com/dmdirc/res/icon.png");
  155. }
  156. if (imageURL == null) {
  157. throw new IllegalArgumentException("Unable to load icon type '"
  158. + iconType + "', and unable to load default");
  159. }
  160. return imageURL;
  161. }
  162. private String getSpecialIcons(final String type) {
  163. final Calendar cal = new GregorianCalendar();
  164. cal.setTime(new Date());
  165. if (cal.get(Calendar.MONTH) == Calendar.DECEMBER
  166. && cal.get(Calendar.DAY_OF_MONTH) >= 12
  167. && cal.get(Calendar.DAY_OF_MONTH) <= 31
  168. && ("icon".equals(type) || "logo".equals(type))) {
  169. return "logo-special";
  170. }
  171. return type;
  172. }
  173. @Override
  174. public void configChanged(final String domain, final String key) {
  175. if ("icon".equals(domain)) {
  176. if (images.containsKey(key)) {
  177. images.remove(key);
  178. }
  179. if (icons.containsKey(key)) {
  180. icons.remove(key);
  181. }
  182. }
  183. }
  184. }