|
1 /* |
|
2 * CDDL HEADER START |
|
3 * |
|
4 * The contents of this file are subject to the terms of the |
|
5 * Common Development and Distribution License (the "License"). |
|
6 * You may not use this file except in compliance with the License. |
|
7 * |
|
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE |
|
9 * or http://www.opensolaris.org/os/licensing. |
|
10 * See the License for the specific language governing permissions |
|
11 * and limitations under the License. |
|
12 * |
|
13 * When distributing Covered Code, include this CDDL HEADER in each |
|
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. |
|
15 * If applicable, add the following below this CDDL HEADER, with the |
|
16 * fields enclosed by brackets "[]" replaced with your own identifying |
|
17 * information: Portions Copyright [yyyy] [name of copyright owner] |
|
18 * |
|
19 * CDDL HEADER END |
|
20 */ |
|
21 |
|
22 /* |
|
23 * Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved. |
|
24 */ |
|
25 |
|
26 package com.oracle.solaris.vp.util.misc.finder; |
|
27 |
|
28 import java.net.URL; |
|
29 import javax.swing.ImageIcon; |
|
30 |
|
31 public class IconFinder extends ResourceFinder { |
|
32 // |
|
33 // Instance data |
|
34 // |
|
35 |
|
36 private ObjectCache<String, ImageIcon> cache = |
|
37 new ObjectCache<String, ImageIcon>(); |
|
38 |
|
39 // |
|
40 // IconFinder methods |
|
41 // |
|
42 |
|
43 /** |
|
44 * Clear the cache of retrieved {@code ImageIcon}s. |
|
45 */ |
|
46 public void clearCache() { |
|
47 synchronized (cache) { |
|
48 cache.clear(); |
|
49 } |
|
50 } |
|
51 |
|
52 /** |
|
53 * Return an {@code ImageIcon} for the given resource name. All packages |
|
54 * in the hierarchy above the given package are searched, using the given |
|
55 * {@code ClassLoader}. See the class notes for details on the search |
|
56 * algorithm. |
|
57 * |
|
58 * @param loader |
|
59 * the {@code ClassLoader} to load the resource, or {@code |
|
60 * null} to use the system {@code ClassLoader} |
|
61 * |
|
62 * @param pkg |
|
63 * a point in the package hierarchy to search for the resource |
|
64 * |
|
65 * @param name |
|
66 * the name of the resource |
|
67 * |
|
68 * @return a {@code URL} object, or {@code null} if no resource with |
|
69 * this name is found |
|
70 */ |
|
71 public ImageIcon getIcon(ClassLoader loader, String pkg, String name) { |
|
72 URL url = getResource(loader, pkg, name); |
|
73 return url == null ? null : getIcon(url); |
|
74 } |
|
75 |
|
76 /** |
|
77 * Return an {@code ImageIcon} for the given resource name. All packages in |
|
78 * the hierarchy above the given package are searched, using the caller's |
|
79 * {@code ClassLoader}. See the class notes for details on the search |
|
80 * algorithm. |
|
81 * |
|
82 * @param pkg |
|
83 * a point in the package hierarchy to search for the resource |
|
84 * |
|
85 * @param name |
|
86 * the name of the resource |
|
87 * |
|
88 * @return a {@code URL} object, or {@code null} if no resource with |
|
89 * this name is found |
|
90 */ |
|
91 public ImageIcon getIcon(String pkg, String name) { |
|
92 URL url = getResource(pkg, name); |
|
93 return url == null ? null : getIcon(url); |
|
94 } |
|
95 |
|
96 /** |
|
97 * Return an {@code ImageIcon} for the given resource name. All packages in |
|
98 * the hierarchy above the calling class are searched, using the caller's |
|
99 * {@code ClassLoader}. See the class notes for details on the search |
|
100 * algorithm. |
|
101 * |
|
102 * @param name |
|
103 * the name of the resource |
|
104 * |
|
105 * @return a {@code URL} object, or {@code null} if no resource with |
|
106 * this name is found |
|
107 */ |
|
108 public ImageIcon getIcon(String name) { |
|
109 URL url = getResource(name); |
|
110 return url == null ? null : getIcon(url); |
|
111 } |
|
112 |
|
113 // |
|
114 // Private methods |
|
115 // |
|
116 |
|
117 /** |
|
118 * Retrieve from cache, or create, an {@code ImageIcon} for the given {@code |
|
119 * URL}. |
|
120 */ |
|
121 private ImageIcon getIcon(URL url) { |
|
122 String key = url.toString(); |
|
123 ImageIcon icon = null; |
|
124 |
|
125 synchronized (cache) { |
|
126 if (cache.containsKey(key)) { |
|
127 // May be null |
|
128 icon = cache.get(key); |
|
129 } else { |
|
130 icon = new ImageIcon(url); |
|
131 cache.put(key, icon); |
|
132 } |
|
133 } |
|
134 |
|
135 return icon; |
|
136 } |
|
137 } |