001: /*
002: * IconUtilities.java
003: *
004: * Copyright (C) 2002, 2003, 2004, 2005, 2006 Takis Diakoumis
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License
008: * as published by the Free Software Foundation; either version 2
009: * of the License, or any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: *
020: */
021:
022: package org.underworldlabs.swing.util;
023:
024: import java.net.URL;
025: import java.util.HashMap;
026: import java.util.Map;
027:
028: import javax.swing.ImageIcon;
029:
030: /* ----------------------------------------------------------
031: * CVS NOTE: Changes to the CVS repository prior to the
032: * release of version 3.0.0beta1 has meant a
033: * resetting of CVS revision numbers.
034: * ----------------------------------------------------------
035: */
036:
037: /**
038: * Icon and image loader and cache.<br>
039: * This aims to load images from jar file resources not
040: * local file system paths.
041: *
042: * @author Takis Diakoumis
043: * @version $Revision: 1.7 $
044: * @date $Date: 2006/09/06 09:30:38 $
045: */
046: public class IconUtilities {
047:
048: /** default icon resource path in this package tree */
049: private static final String ICON_PATH = "/org/underworldlabs/swing/icons/";
050:
051: /** Icons repository */
052: private static Map<String, ImageIcon> icons = new HashMap<String, ImageIcon>();
053:
054: public static ImageIcon loadImage(String name) {
055: return new ImageIcon(IconUtilities.class.getResource(name));
056: }
057:
058: public static ImageIcon loadIcon(String name) {
059: return loadIcon(name, false);
060: }
061:
062: public static ImageIcon loadIcon(String name, boolean store) {
063: ImageIcon icon = null;
064:
065: if (icons.containsKey(name)) {
066: icon = icons.get(name);
067: } else {
068: URL url = IconUtilities.class.getResource(name);
069: if (url != null) {
070: icon = new ImageIcon(url);
071: }
072: // try the default resource path
073: else {
074: icon = loadDefaultIconResource(name, false);
075: }
076:
077: if (store) {
078: icons.put(name, icon);
079: }
080: }
081: return icon;
082: }
083:
084: public static ImageIcon loadDefaultIconResource(String name,
085: boolean store) {
086: ImageIcon icon = null;
087: name = ICON_PATH + name;
088: if (icons.containsKey(name)) {
089: icon = icons.get(name);
090: } else {
091: URL url = IconUtilities.class.getResource(name);
092: if (url == null) {
093: throw new RuntimeException(
094: "Icon at resource path not found: " + name);
095: }
096: icon = new ImageIcon(url);
097:
098: if (store) {
099: icons.put(name, icon);
100: }
101: }
102: return icon;
103: }
104:
105: // prevent instantiation
106: private IconUtilities() {
107: }
108:
109: }
|