001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2006, Geotools Project Managment Committee (PMC)
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or (at your option) any later version.
010: *
011: * This library 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 GNU
014: * Lesser General Public License for more details.
015: */
016: package org.geotools.gui.swing;
017:
018: // J2SE dependencies
019: import java.net.URL;
020: import java.util.HashMap;
021: import java.util.Map;
022: import javax.swing.Icon;
023: import javax.swing.ImageIcon;
024: import javax.swing.JButton;
025:
026: /**
027: * A factory for {@link Icon}. This class caches some of the created icons. This factory should be
028: * used only for small icons, since they may be cached for the JVM lifetime. This class is used
029: * especially for <A HREF="http://java.sun.com/developer/techDocs/hi/repository/">Java look and
030: * feel Graphics Repository</A>. This class is thread safe.
031: *
032: * @since 2.3
033: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/extension/widgets-swing/src/main/java/org/geotools/gui/swing/IconFactory.java $
034: * @version $Id: IconFactory.java 20883 2006-08-07 13:48:09Z jgarnett $
035: * @author Martin Desruisseaux
036: */
037: public final class IconFactory {
038: /**
039: * The default factory instance.
040: */
041: public static final IconFactory DEFAULT = new IconFactory();
042:
043: /**
044: * The class loader to uses.
045: */
046: private final ClassLoader loader;
047:
048: /**
049: * The icons already loaded.
050: */
051: private final Map icons = new HashMap();
052:
053: /**
054: * Do not allows instantiation of this class.
055: */
056: private IconFactory() {
057: loader = getClass().getClassLoader();
058: }
059:
060: /**
061: * Returns the icon for the specified name, or {@code null} if none. If this method has
062: * already been invoked for the specified path, then the previously created icon is returned.
063: *
064: * @param path The icon path, relative to the application classpath.
065: * @return The icon, or {@code null} if no image was found for the specified path.
066: */
067: public synchronized Icon getIcon(final String path) {
068: Icon icon = (Icon) icons.get(path);
069: if (icon == null) {
070: URL url = loader.getResource(path);
071: if (url != null) {
072: icon = new ImageIcon(url);
073: icons.put(path, icon);
074: }
075: }
076: return icon;
077: }
078:
079: /**
080: * Returns an icon for the specified name and description, or {@code null} if none.
081: *
082: * @param path The icon path, relative to the application classpath.
083: * @param description brief textual description of the image, or {@code null} if none.
084: * @return The icon, or {@code null} if no image was found for the specified path.
085: */
086: public Icon getIcon(final String path, final String description) {
087: Icon icon = getIcon(path);
088: if (description != null) {
089: if (icon instanceof ImageIcon) {
090: icon = new ImageIcon(((ImageIcon) icon).getImage(),
091: description);
092: }
093: }
094: return icon;
095: }
096:
097: /**
098: * Returns a button with the specified image.
099: *
100: * @param path The icon path, relative to the application classpath.
101: * @param description brief textual description of the image, or {@code null} if none.
102: * @param fallback A text to put in the button if the image were not found.
103: */
104: public JButton getButton(final String path,
105: final String description, final String fallback) {
106: final Icon icon = getIcon(path, description);
107: final JButton button;
108: if (icon != null) {
109: button = new JButton(icon);
110: } else {
111: button = new JButton(fallback);
112: }
113: button.setToolTipText(description);
114: return button;
115: }
116: }
|