File Source: IconManager.java
1 /*
2 * Copyright (c) 2006-2009 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
23 package com.dmdirc.ui;
24
25 import com.dmdirc.config.IdentityManager;
26 import com.dmdirc.interfaces.ConfigChangeListener;
27 import com.dmdirc.util.URLBuilder;
28
29 import java.awt.Image;
30 import java.awt.Toolkit;
31 import java.net.URL;
32 import java.util.HashMap;
33 import java.util.Map;
34
35 import javax.swing.Icon;
36 import javax.swing.ImageIcon;
37
38 /**
39 * The icon manager provides a standard way to access icons for use in DMDirc.
40 * It allows the user to override the default actions using config settings
41 * under the icons domain.
42 *
43 * @author chris
44 */
45 public final class IconManager implements ConfigChangeListener {
46
47 /** Previously created IconManager instance. */
/*
P/P * Method: com.dmdirc.ui.IconManager__static_init
*
* Postconditions:
* ME == &new IconManager(IconManager__static_init#1)
* new HashMap(IconManager#1) num objects == 1
* new HashMap(IconManager#2) num objects == 1
* new IconManager(IconManager__static_init#1) num objects == 1
* ME.icons == &new HashMap(IconManager#1)
* ME.images == &new HashMap(IconManager#2)
*/
48 private static final IconManager ME = new IconManager();
49
50 /** A map of existing icons. */
51 private final Map<String, Icon> icons;
52
53 /** A map of existing images. */
54 private final Map<String, Image> images;
55
56 /** Creates a new instance of IconManager. */
/*
P/P * Method: void com.dmdirc.ui.IconManager()
*
* Presumptions:
* com.dmdirc.config.IdentityManager:getGlobalConfig(...)@61 != null
*
* Postconditions:
* this.icons == &new HashMap(IconManager#1)
* this.images == &new HashMap(IconManager#2)
* new HashMap(IconManager#1) num objects == 1
* new HashMap(IconManager#2) num objects == 1
*/
57 private IconManager() {
58 icons = new HashMap<String, Icon>();
59 images = new HashMap<String, Image>();
60
61 IdentityManager.getGlobalConfig().addChangeListener("icon", this);
62 }
63
64 /**
65 * Returns an instance of IconManager.
66 *
67 * @return Instance of IconManager
68 */
69 public static IconManager getIconManager() {
/*
P/P * Method: IconManager getIconManager()
*
* Postconditions:
* return_value == &new IconManager(IconManager__static_init#1)
*/
70 return ME;
71 }
72
73 /**
74 * Retrieves the icon with the specified type. Returns null if the icon
75 * wasn't found.
76 *
77 * @param type The name of the icon type to retrieve
78 *
79 * @return The icon that should be used for the specified type
80 */
81 public Icon getIcon(final String type) {
/*
P/P * Method: Icon getIcon(String)
*
* Preconditions:
* this.icons != null
*
* Presumptions:
* java.awt.Toolkit:getDefaultToolkit(...)@83 != null
* java.awt.Toolkit:getImage(...)@83 != null
*
* Postconditions:
* init'ed(return_value)
*
* Test Vectors:
* java.util.Map:containsKey(...)@85: {1}, {0}
*/
82 final URL iconURL = getIconURL(type);
83 final Image iconImage = Toolkit.getDefaultToolkit().getImage(iconURL);
84 final Image scaledIconImage = getScaledImage(iconImage, 16, 16);
85 if (!icons.containsKey(type)) {
86 icons.put(type, new ImageIcon(scaledIconImage));
87 }
88 return icons.get(type);
89 }
90
91 /**
92 * Retrieves the icon with the specified type. Returns null if the icon
93 * wasn't found.
94 *
95 * @param type The name of the icon type to retrieve
96 * @param width width of the image
97 * @param height height of the image
98 *
99 * @return The icon that should be used for the specified type
100 *
101 * @since 0.6.3m1
102 */
103 public Icon getScaledIcon(final String type, final int width, final int height) {
/*
P/P * Method: Icon getScaledIcon(String, int, int)
*
* Presumptions:
* javax.swing.ImageIcon:getImage(...)@104 != null
*
* Postconditions:
* return_value == &new ImageIcon(getScaledIcon#1)
* new ImageIcon(getScaledIcon#1) num objects == 1
*/
104 return new ImageIcon(getScaledImage(new ImageIcon(getIconURL(type)).
105 getImage(), width, height));
106 }
107
108 /**
109 * Retrieves the image with the specified type. Returns null if the icon
110 * wasn't found.
111 *
112 * @param type The name of the icon type to retrieve
113 *
114 * @return The image that should be used for the specified type
115 */
116 public Image getImage(final String type) {
/*
P/P * Method: Image getImage(String)
*
* Preconditions:
* this.images != null
*
* Presumptions:
* java.awt.Toolkit:getDefaultToolkit(...)@118 != null
*
* Postconditions:
* init'ed(return_value)
*
* Test Vectors:
* java.util.Map:containsKey(...)@117: {1}, {0}
*/
117 if (!images.containsKey(type)) {
118 images.put(type, Toolkit.getDefaultToolkit().createImage(getIconURL(type)));
119 }
120 return images.get(type);
121 }
122
123 /**
124 * Returns a scaled image.
125 *
126 * @param image Image to scale
127 * @param width Width of resulting image
128 * @param height Height of resulting image
129 *
130 * @return Scaled Image
131 */
132 private Image getScaledImage(final Image image,
133 final int width, final int height) {
/*
P/P * Method: Image getScaledImage(Image, int, int)
*
* Preconditions:
* image != null
*
* Postconditions:
* init'ed(return_value)
*/
134 return image.getScaledInstance(width , height, Image.SCALE_SMOOTH);
135 }
136
137 /**
138 * Retrieves the URL of a specified icon type.
139 *
140 * @param type The name of the icon type to retrieve
141 *
142 * @return The URL that should be used to retrieve the specified icon
143 */
144 private URL getIconURL(final String type) {
/*
P/P * Method: URL getIconURL(String)
*
* Presumptions:
* com.dmdirc.config.IdentityManager:getGlobalConfig(...)@149 != null
* java.lang.ClassLoader:getResource(...)@161 != null
* java.lang.Thread:currentThread(...)@145 != null
* java.lang.Thread:getContextClassLoader(...)@145 != null
*
* Postconditions:
* return_value != null
*
* Test Vectors:
* com.dmdirc.config.ConfigManager:hasOptionString(...)@149: {0}, {1}
* com.dmdirc.util.URLBuilder:buildURL(...)@154: Inverse{null}, Addr_Set{null}
* java.lang.ClassLoader:getResource(...)@146: Addr_Set{null}, Inverse{null}
*/
145 final ClassLoader cldr = Thread.currentThread().getContextClassLoader();
146 final URL defaultURL = cldr.getResource("com/dmdirc/res/" + type + ".png");
147
148 //Get the path for the url
149 final String path = IdentityManager.getGlobalConfig().hasOptionString("icon", type)
150 ? IdentityManager.getGlobalConfig().getOption("icon", type)
151 : "dmdirc://com/dmdirc/res/" + type + ".png";
152
153 //Get the url for the speficied path
154 URL imageURL = URLBuilder.buildURL(path);
155
156 if (imageURL == null && defaultURL != null) {
157 imageURL = defaultURL;
158 }
159
160 if (imageURL == null && defaultURL == null) {
161 imageURL = cldr.getResource("com/dmdirc/res/icon.png");
162
163 if (imageURL == null) {
164 throw new IllegalArgumentException("Unable to load icon type '"
165 + type + "', and unable to load default");
166 }
167 }
168
169 return imageURL;
170 }
171
172 /** {@inheritDoc} */
173 @Override
174 public void configChanged(final String domain, final String key) {
/*
P/P * Method: void configChanged(String, String)
*
* Preconditions:
* (soft) this.icons != null
* (soft) this.images != null
*
* Test Vectors:
* java.lang.String:equals(...)@175: {0}, {1}
* java.util.Map:containsKey(...)@176: {0}, {1}
* java.util.Map:containsKey(...)@179: {0}, {1}
*/
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
185 }
SofCheck Inspector Build Version : 2.17854
| IconManager.java |
2009-Jun-25 01:54:24 |
| IconManager.class |
2009-Sep-02 17:04:17 |