Java Reference
In-Depth Information
Example 10•28: ThemeManager.java (continued)
/**
* Get a JMenu that lists all known themes by display name and
* installs any selected theme.
**/
public JMenu getThemeMenu() {
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;
}
/**
* This class extends the DefaultMetalTheme class to return Color and
* Font values read from a GUIResourceBundle
**/
public static class Theme extends DefaultMetalTheme {
// These fields are the values returned by this Theme
String displayName;
FontUIResource controlFont, menuFont, smallFont;
FontUIResource systemFont, userFont, titleFont;
ColorUIResource primary1, primary2, primary3;
ColorUIResource secondary1, secondary2, secondary3;
/**
* This constructor reads all the values it needs from the
* GUIResourceBundle. It uses intelligent defaults if properties
* are not specified.
**/
public Theme(GUIResourceBundle resources, String name) {
// Use this theme object to get default font values from
DefaultMetalTheme defaultTheme = new DefaultMetalTheme();
// Look up the display name of the theme
displayName = resources.getString(name + ".name", null);
// Look up the fonts for the theme
Font control = resources.getFont(name + ".controlFont", null);
Font menu = resources.getFont(name + ".menuFont", null);
Font small = resources.getFont(name + ".smallFont", null);
Font system = resources.getFont(name + ".systemFont", null);
Font user = resources.getFont(name + ".userFont", null);
Font title = resources.getFont(name + ".titleFont", null);
// Convert fonts to FontUIResource, or get defaults
if (control != null) controlFont = new FontUIResource(control);
Search WWH ::




Custom Search