import java.awt.Color;
import java.awt.event.*;
import java.awt.event.ActionListener;

import javax.swing.*;

/**
 * @author Remi Forax
 *
 */
public class PlafTest {

  private static JPanel createTestPanel() {
    JPanel panel=new JPanel(null);
    panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS));
    panel.add(new JButton("un bouton"));
    panel.add(new JToggleButton("un bouton à état"));
    panel.add(new JLabel("Ceci est un text"));
    panel.add(new JTextField("Un champ de texte"));
    panel.add(new JPasswordField("mon mot de passe"));
    panel.add(new JTextArea("Une zone de texte"));
    panel.add(new JCheckBox("une boîte à cocher"));
    panel.add(new JRadioButton("une boîte à cocher (aussi)"));
    return panel;
  }

  static void setText(JCheckBox checkBox,UIManager.LookAndFeelInfo info, String state) {
    checkBox.setText(info.getName()+" ("+state+')');
  }

  public static void main(String[] args) {
    
    UIManager.installLookAndFeel("GTK","com.sun.java.swing.plaf.gtk.GTKLookAndFeel");
    UIManager.installLookAndFeel("Ext Windows", "com.jgoodies.plaf.windows.ExtWindowsLookAndFeel");
    UIManager.installLookAndFeel("plastic", "com.jgoodies.plaf.plastic.PlasticLookAndFeel");
    UIManager.installLookAndFeel("plastic 3D", "com.jgoodies.plaf.plastic.Plastic3DLookAndFeel"); 
    UIManager.installLookAndFeel("plastic XP", "com.jgoodies.plaf.plastic.PlasticXPLookAndFeel");
    
    final JFrame frame=new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    JPanel panel=new JPanel(null);
    panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS));
    panel.setBorder(BorderFactory.createTitledBorder(
      BorderFactory.createLineBorder(Color.BLACK), "Look and Feel"));
    ButtonGroup group=new ButtonGroup();
    
    UIManager.LookAndFeelInfo[] infos=UIManager.getInstalledLookAndFeels();
    for(int i=0;i<infos.length;i++) {
      final UIManager.LookAndFeelInfo info=infos[i];
      final JCheckBox checkBox=new JCheckBox();
      checkBox.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
          
          // try to install l&f
          try {
            UIManager.setLookAndFeel(info.getClassName());
            setText(checkBox,info,"supported");
          } catch (UnsupportedLookAndFeelException e) {
            setText(checkBox,info,"not supported");
            checkBox.setEnabled(false);
          } catch (Exception e) {
            setText(checkBox,info,"not found");
            checkBox.setEnabled(false);
          } 
          
          // refresh frame ui
          SwingUtilities.updateComponentTreeUI(frame);
          frame.pack();
        }
      });
      
      boolean isDefaultLF=info.getClassName().equals(
        UIManager.getLookAndFeel().getClass().getName());
      checkBox.setSelected(isDefaultLF);
      setText(checkBox,info,isDefaultLF?"supported":"not verified");
      
      group.add(checkBox);
      panel.add(checkBox);
    }
    
    JSplitPane pane=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,true,
      panel,createTestPanel());
    frame.setContentPane(pane);
    frame.pack();
    frame.show();
  }
}
