1 /*
2 * SSHTools - Java SSH2 API
3 *
4 * Copyright (C) 2002-2003 Lee David Painter and Contributors.
5 *
6 * Contributions made by:
7 *
8 * Brett Smith
9 * Richard Pernavas
10 * Erwin Bolwidt
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 */
26 package com.sshtools.common.ui;
27
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30
31 import java.awt.Image;
32 import java.awt.Toolkit;
33
34 import java.io.FilePermission;
35
36 import java.net.URL;
37
38 import java.security.AccessControlException;
39 import java.security.AccessController;
40
41 import javax.swing.ImageIcon;
42
43
44 /**
45 *
46 *
47 * @author $author$
48 * @version $Revision: 1.20 $
49 */
50 public class ResourceIcon extends ImageIcon {
51 private static Log log = LogFactory.getLog(ResourceIcon.class.getName());
52 Class cls;
53
54 /**
55 * Creates a new ResourceIcon object.
56 *
57 * @param cls
58 * @param image
59 */
60 public ResourceIcon(Class cls, String image) {
61 super();
62 this.cls = cls;
63
64 if (image.startsWith("/")) {
65 loadImage(image);
66 } else {
67 String path = "/" + cls.getPackage().getName();
68 path = path.replace('.', '/');
69 path += ("/" + image);
70 loadImage(path);
71 }
72 }
73
74 /**
75 * Creates a new ResourceIcon object.
76 *
77 * @param url
78 */
79 public ResourceIcon(URL url) {
80 super(url);
81 }
82
83 /**
84 * Creates a new ResourceIcon object.
85 *
86 * @param imageName
87 * @deprecated Having this available is now bad practice since most of our
88 * software is plugable; each class requesting a resource should do so from
89 * the class loader that loaded the class, to keep track of images a class
90 * should also not be requesting a resource that is outside its own package.
91 *
92 * For resources outside of a package, we should think about creating static
93 * helper class to store them.
94 *
95 * Use the ResourceIcon(Class cls, String image) constructor instead providing
96 * the class instance of the class using the image.
97 *
98 */
99 public ResourceIcon(String imageName) {
100 super();
101 this.cls = getClass();
102 loadImage(imageName);
103 }
104
105 /**
106 *
107 *
108 * @param imageName
109 */
110 protected void loadImage(String imageName) {
111 Image image = null;
112 URL url = cls.getResource(imageName);
113
114 if (url != null) {
115 log.debug(url.toString());
116 image = Toolkit.getDefaultToolkit().getImage(url);
117 } else {
118 try {
119 if (System.getSecurityManager() != null) {
120 AccessController.checkPermission(new FilePermission(
121 imageName, "read"));
122 }
123
124 image = Toolkit.getDefaultToolkit().getImage(imageName);
125 } catch (AccessControlException ace) {
126 log.error("Icon " + imageName + " could not be located as a " +
127 "resource, and the current security manager will not " +
128 "allow checking for a local file.");
129 }
130 }
131
132 if (image != null) {
133 this.setImage(image);
134 }
135 }
136 }