KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sshtools > ui > swing > ResourceIcon


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sshtools.ui.swing;
21
22 import java.net.URL JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.awt.Image JavaDoc;
25 import java.awt.Toolkit JavaDoc;
26 import javax.swing.ImageIcon JavaDoc;
27
28 /**
29  * Icon that can use a <code>Class</code> reference to determine a
30  * <code>ClassLoader</code> to use to load an icon resource.
31  *
32  * @author $Author: brett $
33  */

34 public class ResourceIcon extends ImageIcon JavaDoc {
35     // Private instance variables
36
private Class JavaDoc cls;
37
38     private HashMap JavaDoc imageCache = new HashMap JavaDoc();
39
40     public ResourceIcon(String JavaDoc image) {
41         super();
42         init(getClass(), image);
43     }
44
45     /**
46      * Creates a new ResourceIcon given a class to derive the class loader from
47      * and the image path.
48      *
49      * @param cls class to derive class loader for image from
50      * @param image image path
51      */

52     public ResourceIcon(Class JavaDoc cls, String JavaDoc image) {
53         super();
54         init(cls, image);
55     }
56
57     public ResourceIcon(URL JavaDoc iconResource) {
58         super(iconResource);
59     }
60
61     public void init(Class JavaDoc cls, String JavaDoc image) {
62         this.cls = cls;
63         if (image.startsWith("/")) { //$NON-NLS-1$
64
loadImage(image);
65         } else {
66             int idx = cls.getName().lastIndexOf('.');
67             String JavaDoc packageName = idx == -1 ? null : cls.getName().substring(0, idx);
68             String JavaDoc path = packageName == null ? "" : "/" + packageName; //$NON-NLS-1$ //$NON-NLS-2$
69
path = path.replace('.', '/');
70             path += ("/" + image); //$NON-NLS-1$
71
loadImage(path);
72         }
73     }
74
75     protected void loadImage(String JavaDoc imageName) {
76         Image JavaDoc image = (Image JavaDoc) imageCache.get(imageName);
77         if (image == null) {
78             URL JavaDoc url = cls.getResource(imageName);
79             if (url != null) {
80                 image = Toolkit.getDefaultToolkit().getImage(url);
81             } else {
82                 url = cls.getClassLoader().getResource(imageName);
83                 if (url != null) {
84                     image = Toolkit.getDefaultToolkit().getImage(url);
85                 } else {
86                     image = Toolkit.getDefaultToolkit().getImage(imageName);
87                 }
88             }
89             imageCache.put(imageName, image);
90         }
91         if (image != null) {
92             this.setImage(image);
93         } else {
94             System.err.println(Messages.getString("ResourceIcon.nullImage")); //$NON-NLS-1$
95
}
96     }
97 }
98
Popular Tags