Package net.sf.jpluck.swing

Source Code of net.sf.jpluck.swing.LAFSelectBox$CellRenderer

package net.sf.jpluck.swing;

import java.awt.Component;
import java.awt.Window;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JList;
import javax.swing.LookAndFeel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

import net.sf.jpluck.ui.ExceptionDialog;

public class LAFSelectBox extends JComboBox {

    private List lafList = new ArrayList();
    private List windowList = new ArrayList();

    public LAFSelectBox() {
        super();
        setModel(new Model());
        setRenderer(new CellRenderer());
        UIManager.LookAndFeelInfo[] info = UIManager.getInstalledLookAndFeels();
        // For some reason we get duplicate LookAndFeelInfo entries. We use this Set to keep track of duplicates.
        Set classSet = new HashSet();
        for (int i = 0; i < info.length; i++) {
            String className = info[i].getClassName();
            if (className.equals("com.sun.java.swing.plaf.motif.MotifLookAndFeel")) {
                // Motif LAF has bugs with sizing buttons so we skip it
                continue;
            } else if (className.equals("com.sun.java.swing.plaf.windows.WindowsLookAndFeel") &&
                    !System.getProperty("os.name").startsWith("Windows")) {
                // Skip Windows L&F on non-Windows platforms.
                continue;
            }
            if (classSet.contains(className)) {
                continue;
            }
            classSet.add(className);
            lafList.add(info[i]);
        }
        Collections.sort(lafList, new Comparator() {
            public int compare(Object o1, Object o2) {
                UIManager.LookAndFeelInfo info1 = (UIManager.LookAndFeelInfo) o1;
                UIManager.LookAndFeelInfo info2 = (UIManager.LookAndFeelInfo) o2;
                return (info1.getName().compareTo(info2.getName()));
            }
        });
    }

    public void selectCurrent() {
        String className = UIManager.getLookAndFeel().getClass().getName();
        for (Iterator iterator = lafList.iterator(); iterator.hasNext();) {
            UIManager.LookAndFeelInfo info = (UIManager.LookAndFeelInfo) iterator.next();
            if (info.getClassName().equals(className)) {
                setSelectedItem(info);
            }
        }
    }

    public void select(String className) {
        for (Iterator iterator = lafList.iterator(); iterator.hasNext();) {
            UIManager.LookAndFeelInfo lookAndFeelInfo = (UIManager.LookAndFeelInfo) iterator.next();
            if (lookAndFeelInfo.getClassName().equals(className)) {
                setSelectedItem(lookAndFeelInfo);
                break;
            }
        }
    }

    public UIManager.LookAndFeelInfo getSelectedLAF() {
        return (UIManager.LookAndFeelInfo) getSelectedItem();
    }

    public void apply() {
        UIManager.LookAndFeelInfo info = (UIManager.LookAndFeelInfo) getSelectedItem();
        try {
            String className = info.getClassName();
            if (!className.equals(UIManager.getLookAndFeel().getClass().getName())) {
                Class cls = Class.forName(className);
                UIManager.setLookAndFeel((LookAndFeel) cls.newInstance());
                for (Iterator iterator = windowList.iterator(); iterator.hasNext();) {
                    Window window = (Window) iterator.next();
                    SwingUtilities.updateComponentTreeUI(window);
                    if (window instanceof JDialog) {
                        window.pack();
                    }
                }
            }
        } catch (Exception e) {
            ExceptionDialog.show(null, e);
        }
    }

    public void register(Window window) {
        if (window != null && !windowList.contains(window)) {
            windowList.add(window);
        }
    }

    private class Model extends DefaultComboBoxModel {
        public int getSize() {
            return lafList.size();
        }

        public Object getElementAt(int index) {
            return lafList.get(index);
        }
    }

    private static 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) {
                this.setText(((UIManager.LookAndFeelInfo) value).getName());
            }
            return comp;
        }
    }
}
TOP

Related Classes of net.sf.jpluck.swing.LAFSelectBox$CellRenderer

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.