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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * Copyright (c) 2006-2007 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 java.awt.Image;
  24. import java.awt.Toolkit;
  25. import java.io.File;
  26. import java.io.IOException;
  27. import java.net.URL;
  28. import javax.swing.Icon;
  29. import javax.swing.ImageIcon;
  30. /**
  31. * The icon manager provides a standard way to access icons for use in DMDirc.
  32. * It allows the user to override the default actions using config settings
  33. * under the icons domain.
  34. *
  35. * @author chris
  36. */
  37. public final class IconManager {
  38. /**
  39. * Creates a new instance of IconManager.
  40. */
  41. private IconManager() {
  42. // Not meant to be used
  43. }
  44. /**
  45. * Retrieves the icon with the specified type. Returns null if the icon
  46. * wasn't found.
  47. *
  48. * @param type The name of the icon type to retrieve
  49. *
  50. * @return The icon that should be used for the specified type
  51. */
  52. public static Icon getIcon(final String type) {
  53. return new ImageIcon(getScaledImage(new ImageIcon(getIconURL(type)).getImage(), 16, 16));
  54. }
  55. /**
  56. * Retrieves the image with the specified type. Returns null if the icon
  57. * wasn't found.
  58. *
  59. * @param type The name of the icon type to retrieve
  60. *
  61. * @return The image that should be used for the specified type
  62. */
  63. public static Image getImage(final String type) {
  64. return Toolkit.getDefaultToolkit().getDefaultToolkit().createImage(getIconURL(type));
  65. }
  66. /**
  67. * Returns a scaled image.
  68. *
  69. * @param image Image to scale
  70. * @param width Width of resulting image
  71. * @param height Height of resulting image
  72. *
  73. * @return Scaled Image
  74. */
  75. private static Image getScaledImage(final Image image,
  76. final int width, final int height) {
  77. return image.getScaledInstance(width , height, Image.SCALE_SMOOTH);
  78. }
  79. /**
  80. * Retrieves the URL of a specified icon type.
  81. *
  82. * @param type The name of the icon type to retrieve
  83. *
  84. * @return The URL that should be used to retrieve the specified icon
  85. */
  86. private static URL getIconURL(final String type) {
  87. final ClassLoader cldr = Thread.currentThread().getContextClassLoader();
  88. final URL defaultURL = cldr.getResource("com/dmdirc/res/" + type + ".png");
  89. //Get the path for the url
  90. final String path = Config.getOption("icon", type, "com/dmdirc/res/" + type + ".png");
  91. //Get the url for the speficied path
  92. URL imageURL = cldr.getResource(path);
  93. try {
  94. //if the path didnt exist see if its a file
  95. if (imageURL == null) {
  96. final File file = new File(path);
  97. if (file.exists()) {
  98. imageURL = file.toURI().toURL();
  99. } else {
  100. imageURL = defaultURL;
  101. }
  102. }
  103. //check if the url has content
  104. if (imageURL.getContent() == null) {
  105. imageURL = defaultURL;
  106. }
  107. } catch (IOException ex) {
  108. imageURL = defaultURL;
  109. }
  110. return imageURL;
  111. }
  112. }