package net.sf.jpluck.swing;
import com.jgoodies.plaf.plastic.PlasticLookAndFeel;
import com.jgoodies.plaf.plastic.PlasticTheme;
import javax.swing.AbstractListModel;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JComboBox;
import javax.swing.JList;
import java.awt.Component;
import java.util.Iterator;
import java.util.List;
public class PlasticThemeCombo extends JComboBox {
private static List themeList = PlasticLookAndFeel.getInstalledThemes();
public static PlasticTheme getTheme(String name) {
for (Iterator iterator = themeList.iterator(); iterator.hasNext();) {
PlasticTheme plasticTheme = (PlasticTheme) iterator.next();
if (plasticTheme.getName().equals(name)) {
return plasticTheme;
}
}
return null;
}
public PlasticThemeCombo() {
setModel(new ThemeListModel());
setRenderer(new CellRenderer());
}
public void apply() {
PlasticLookAndFeel.setMyCurrentTheme((PlasticTheme) getSelectedItem());
}
public void setSelectedTheme(String name) {
PlasticTheme theme = getTheme(name);
if (theme != null) {
setSelectedItem(theme);
} else {
name = "Experience Blue";
setSelectedItem(getTheme(name));
}
}
public PlasticTheme getSelectedTheme() {
return (PlasticTheme) getSelectedItem();
}
private class CellRenderer extends DefaultListCellRenderer {
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
Component comp = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
if (value != null) {
setText(((PlasticTheme) value).getName());
}
return comp;
}
}
public class ThemeListModel extends AbstractListModel implements ComboBoxModel {
private Object selected;
public int getSize() {
return themeList.size();
}
public Object getElementAt(int index) {
return themeList.get(index);
}
public void setSelectedItem(Object anItem) {
selected = anItem;
}
public Object getSelectedItem() {
return selected;
}
}
}