[Japi-cvs] SF.net SVN: japi: [213] libs/swing-action/trunk/src/net/sf/japi/swing
Status: Beta
Brought to you by:
christianhujer
From: <chr...@us...> - 2006-11-26 16:20:24
|
Revision: 213 http://svn.sourceforge.net/japi/?rev=213&view=rev Author: christianhujer Date: 2006-11-26 08:20:19 -0800 (Sun, 26 Nov 2006) Log Message: ----------- Made swing-action compilable (added missing IconManager class). Modified Paths: -------------- libs/swing-action/trunk/src/net/sf/japi/swing/ActionFactory.java Added Paths: ----------- libs/swing-action/trunk/src/net/sf/japi/swing/IconManager.java Modified: libs/swing-action/trunk/src/net/sf/japi/swing/ActionFactory.java =================================================================== --- libs/swing-action/trunk/src/net/sf/japi/swing/ActionFactory.java 2006-11-26 15:54:27 UTC (rev 212) +++ libs/swing-action/trunk/src/net/sf/japi/swing/ActionFactory.java 2006-11-26 16:20:19 UTC (rev 213) @@ -24,6 +24,7 @@ import java.awt.event.ActionEvent; import java.lang.reflect.Field; import java.text.MessageFormat; +import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import java.util.Map; @@ -31,7 +32,6 @@ import java.util.ResourceBundle; import static java.util.ResourceBundle.getBundle; import java.util.WeakHashMap; -import java.util.ArrayList; import java.util.prefs.Preferences; import static java.util.prefs.Preferences.userNodeForPackage; import javax.swing.AbstractAction; @@ -50,16 +50,16 @@ import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JOptionPane; +import javax.swing.JPopupMenu; import javax.swing.JToolBar; -import javax.swing.JPopupMenu; import static javax.swing.KeyStroke.getKeyStroke; -import org.jetbrains.annotations.NonNls; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; import static net.sf.japi.swing.IconManager.getDefaultIconManager; import static net.sf.japi.swing.ReflectionAction.REFLECTION_MESSAGE_PROVIDER; import static net.sf.japi.swing.ReflectionAction.REFLECTION_TARGET; import static net.sf.japi.swing.ToggleAction.REFLECTION_PROPERTY_NAME; +import org.jetbrains.annotations.NonNls; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; /** Class for creating and initializing {@link Action}s that are localized, user configurable and may invoke their final methods using Reflection; * also handles extremely much useful stuff for i18n/l10n. Added: libs/swing-action/trunk/src/net/sf/japi/swing/IconManager.java =================================================================== --- libs/swing-action/trunk/src/net/sf/japi/swing/IconManager.java (rev 0) +++ libs/swing-action/trunk/src/net/sf/japi/swing/IconManager.java 2006-11-26 16:20:19 UTC (rev 213) @@ -0,0 +1,137 @@ +package net.sf.japi.swing; + +import java.net.URL; +import java.security.AccessController; +import java.security.PrivilegedAction; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.WeakHashMap; +import javax.swing.Icon; +import javax.swing.ImageIcon; +import org.jetbrains.annotations.Nullable; + +/** Class to handle icons. + * Default size is 16. + * Instances must have an associated ClassLoader, otherwise several methods will not work properly but throw a NullPointerException instead. + * So if you do not provide a ClassLoader, be sure the class you provide has one, or if you use the no-arg constructor resp. the default instance, be + * sure the IconManager class itself was loaded with some ClassLoader other than <code>null</code>. + * @author <a href="mailto:ch...@ri...">Christian Hujer</a> + * @todo this class should be refactored into a more generic version and accessible through ActionFactory? + * @todo it should be possible to initialize the paths through properties + */ +public final class IconManager { + + /** The default IconManager. */ + private static final IconManager DEFAULT_ICON_MANAGER = createDefaultIconManager(); + + /** Create the default IconManager. + * @return default IconManager + */ + private static IconManager createDefaultIconManager() { + final IconManager defaultIconManager = new IconManager(); + defaultIconManager.iconPaths.add("icons/"); + defaultIconManager.iconPaths.add("toolbarButtonGraphics/"); + return defaultIconManager; + } + + /** ClassLoader to get icons from, must not be null. */ + private final ClassLoader classLoader; + + /** The available sizes provided by this IconManager. */ + private final int[] availableSizes = { 16, 24 }; + + /** The icon cache. + * Key: short name for icon, which is likely to be used as a relative file name. + * Value: Icon + */ + private final Map<String,Icon> smallCache = new WeakHashMap<String,Icon>(); + + /** The paths to search icons in. */ + private final List<String> iconPaths = new ArrayList<String>(); + + /** Get the default IconManager. + * The ClassLoader in use is the classloader IconManager was loaded with, whatever classloader that was. + * @return default IconManaager + */ + public static IconManager getDefaultIconManager() { + return DEFAULT_ICON_MANAGER; + } + + /** Create a IconManager. + * Uses the IconManager's class loader. + * Only use this if you want to be independent of the global icon size settings. + * The recommended way to get a default IconManager instance is {#getDefaultIconManager()}. + */ + public IconManager() { + this(getContextClassLoader()); + //this(IconManager.class.getClassLoader()); + } + + private static ClassLoader getContextClassLoader() { + return AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() { + public ClassLoader run() { + try { + return Thread.currentThread().getContextClassLoader(); + } catch (final SecurityException e) { + return getClass().getClassLoader(); + } + } + }); + } + + /** Create an IconManager. + * @param clazz Class to get ClassLoader for IconManager + */ + public IconManager(final Class<?> clazz) { + classLoader = clazz.getClassLoader(); + } + + /** Create an IconManager. + * @param cl ClassLoader to create IconManager for + */ + public IconManager(final ClassLoader cl) { + classLoader = cl; + } + + /** Return the available sizes for icons. + * @return available icon sizes + */ + public int[] getAvailableSizes() { + return availableSizes.clone(); + } + + /** Load an icon. + * @param s icon name, like "general/About" or "navigation/Forward" + * @return Icon for <var>s</var> + */ + @Nullable + public Icon getIcon(final String s) { + Icon icon = smallCache.get(s); + if (icon == null) { + URL url = null; + // Search the configured class loader + for (final Iterator<String> it = iconPaths.iterator(); url == null && it.hasNext();) { + final String path = it.next(); + final String iconPath = path + (path.endsWith("/") ? "" : "/") + s + ".gif"; + url = classLoader.getResource(iconPath); + } + if (url == null) { + // if searching the configured class loader failed, search the system class loader + for (final Iterator<String> it = iconPaths.iterator(); url == null && it.hasNext();) { + final String path = it.next(); + final String iconPath = path + (path.endsWith("/") ? "" : "/") + s + ".gif"; + url = ClassLoader.getSystemResource(iconPath); + } + } + if (url == null) { + return null; + } + icon = new ImageIcon(url); + smallCache.put(s, icon); + } + return icon; + } + +} // class IconManager Property changes on: libs/swing-action/trunk/src/net/sf/japi/swing/IconManager.java ___________________________________________________________________ Name: svn:mime-type + text/plain Name: svn:eol-style + LF This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |