001: /*
002: * SSHTools - Java SSH2 API
003: *
004: * Copyright (C) 2002-2003 Lee David Painter and Contributors.
005: *
006: * Contributions made by:
007: *
008: * Brett Smith
009: * Richard Pernavas
010: * Erwin Bolwidt
011: *
012: * This program is free software; you can redistribute it and/or
013: * modify it under the terms of the GNU General Public License
014: * as published by the Free Software Foundation; either version 2
015: * of the License, or (at your option) any later version.
016: *
017: * This program is distributed in the hope that it will be useful,
018: * but WITHOUT ANY WARRANTY; without even the implied warranty of
019: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
020: * GNU General Public License for more details.
021: *
022: * You should have received a copy of the GNU General Public License
023: * along with this program; if not, write to the Free Software
024: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
025: */
026: package com.sshtools.common.ui;
027:
028: import org.apache.commons.logging.Log;
029: import org.apache.commons.logging.LogFactory;
030:
031: import java.awt.Image;
032: import java.awt.Toolkit;
033:
034: import java.io.FilePermission;
035:
036: import java.net.URL;
037:
038: import java.security.AccessControlException;
039: import java.security.AccessController;
040:
041: import javax.swing.ImageIcon;
042:
043: /**
044: *
045: *
046: * @author $author$
047: * @version $Revision: 1.20 $
048: */
049: public class ResourceIcon extends ImageIcon {
050: private static Log log = LogFactory.getLog(ResourceIcon.class
051: .getName());
052: Class cls;
053:
054: /**
055: * Creates a new ResourceIcon object.
056: *
057: * @param cls
058: * @param image
059: */
060: public ResourceIcon(Class cls, String image) {
061: super ();
062: this .cls = cls;
063:
064: if (image.startsWith("/")) {
065: loadImage(image);
066: } else {
067: String path = "/" + cls.getPackage().getName();
068: path = path.replace('.', '/');
069: path += ("/" + image);
070: loadImage(path);
071: }
072: }
073:
074: /**
075: * Creates a new ResourceIcon object.
076: *
077: * @param url
078: */
079: public ResourceIcon(URL url) {
080: super (url);
081: }
082:
083: /**
084: * Creates a new ResourceIcon object.
085: *
086: * @param imageName
087: * @deprecated Having this available is now bad practice since most of our
088: * software is plugable; each class requesting a resource should do so from
089: * the class loader that loaded the class, to keep track of images a class
090: * should also not be requesting a resource that is outside its own package.
091: *
092: * For resources outside of a package, we should think about creating static
093: * helper class to store them.
094: *
095: * Use the ResourceIcon(Class cls, String image) constructor instead providing
096: * the class instance of the class using the image.
097: *
098: */
099: 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
121: .checkPermission(new FilePermission(
122: imageName, "read"));
123: }
124:
125: image = Toolkit.getDefaultToolkit().getImage(imageName);
126: } catch (AccessControlException ace) {
127: log
128: .error("Icon "
129: + imageName
130: + " could not be located as a "
131: + "resource, and the current security manager will not "
132: + "allow checking for a local file.");
133: }
134: }
135:
136: if (image != null) {
137: this.setImage(image);
138: }
139: }
140: }
|