FileDocCategorySizeDatePackage
ThemeManager.javaAPI DocExample8142Sat Jan 24 10:44:34 GMT 2004je3.gui

ThemeManager

public class ThemeManager extends Object
This class reads theme descriptions from a GUIResourceBundle and uses them to specify colors and fonts for the Metal look-and-feel. It also demonstrates an undocumented feature for turning Swing notification sounds on and off.

Fields Summary
JFrame
frame
GUIResourceBundle
resources
Constructors Summary
public ThemeManager(JFrame frame, GUIResourceBundle resources)
Build a ThemeManager for the frame and resource bundle. If there is a default theme specified, apply it to the frame

	this.frame = frame;
	this.resources = resources;
	String defaultName = getDefaultThemeName();
	if (defaultName != null) setTheme(defaultName);
    
Methods Summary
public java.lang.String[]getAllThemeNames()
Get the list of all known theme names. The returned values are theme property names, not theme display names.

	java.util.List names = resources.getStringList("themelist");
	return (String[]) names.toArray(new String[names.size()]);
    
public java.lang.StringgetDefaultThemeName()
Get the name of the default theme, or null

	return resources.getString("defaultTheme", null);
    
public java.lang.StringgetDisplayName(java.lang.String themeName)
Get the "display name" or label of the named theme

	return resources.getString(themeName + ".name", null);
    
public javax.swing.JMenugetThemeMenu()
Get a JMenu that lists all known themes by display name and installs any selected theme.

	String[] names = getAllThemeNames();
	String defaultName = getDefaultThemeName();
	JMenu menu = new JMenu("Themes");
	ButtonGroup buttongroup = new ButtonGroup();
	for(int i = 0; i < names.length; i++) {
	    final String themeName = names[i];
	    String displayName = getDisplayName(themeName);
	    JMenuItem item = menu.add(new JRadioButtonMenuItem(displayName));
	    buttongroup.add(item);
	    if (themeName.equals(defaultName)) item.setSelected(true);
	    item.addActionListener(new ActionListener() {
		    public void actionPerformed(ActionEvent event) {
			setTheme(themeName);
		    }
		});
	}
	return menu;
    
public voidsetTheme(java.lang.String themeName)
Look up the named theme, and apply it to the frame

	// Look up the theme in the resource bundle
	Theme theme = new Theme(resources, themeName);

	// Make it the current theme
	MetalLookAndFeel.setCurrentTheme(theme);

	// Re-apply the Metal look-and-feel to install new theme
	try { UIManager.setLookAndFeel(new MetalLookAndFeel()); }
	catch(UnsupportedLookAndFeelException e) {}

	// If the theme has an audio playlist, then set it
	if (theme.playlist != null)
	    UIManager.put("AuditoryCues.playList", theme.playlist);

	// Propagate the new l&f across the entire component tree of the frame
	SwingUtilities.updateComponentTreeUI(frame);