package com.lamatek.swingextras;
import javax.swing.*;
import java.awt.Component;
import java.awt.event.ActionEvent;
import com.lamatek.event.ActionAdapter;
/**
* Provides a quick and easy JMenu which allows the user to change
* the look and feel of the application on the fly. There is no need to add
* MenuItems to LAFMenu, they are created on the fly including all required
* event listeners, etc. Simply add a LAFMenu to your MenuBar and you're ready to go.
* You should specify the parent JFrame of your application to ensure proper
* component update upon a L&F change.
*/
public class LAFMenu extends JMenu {
private Component parent;
/**
* Constructs a new LAFMenu with the given parent. When a
* look and feel is selected the component tree will be updated
* up to the parent.
*
* @param parent The top most component to be updated upon a look and feel change.
*/
public LAFMenu(Component parent) {
super("L&F");
this.parent = parent;
addItems();
}
/**
* Creates a new LAFMenu with the given title and parent.
*
* @param title The menu title as used in certain L&F implementations.
* @param parent The topmost component to be updated upon a look and feel change.
*/
public LAFMenu(String title, Component parent) {
super(title);
this.parent = parent;
addItems();
}
private LAFMenuItem add(LAFMenuItem item) {
super.add(item);
item.setParent(parent);
return item;
}
private void addItems() {
UIManager.LookAndFeelInfo[] lf = UIManager.getInstalledLookAndFeels();
for (int i = 0; i < lf.length; i++) {
add(new LAFMenuItem(lf[i].getName(), lf[i].getClassName()));
}
}
class LAFMenuItem extends JMenuItem {
private String lookAndFeelName;
private Component parent;
LAFMenuItem(String description, String lafName) {
super(description);
this.lookAndFeelName = lafName;
addActionListener(new ActionAdapter() {
public void actionPerformed(ActionEvent e) {
try {
UIManager.setLookAndFeel(lookAndFeelName);
SwingUtilities.updateComponentTreeUI(parent);
parent.repaint();
}
catch(Exception ex) {
}
}
});
}
protected void setParent(Component parent) {
this.parent = parent;
}
}
}