/**
 * Title:        Projeto para Teste de possíveis Bug´s<p>
 * Description:  Este projeto direciona-se apenas ŕ eventuais testes de
 * Bug´s no Java<p>
 * Copyright:    Copyright (c) Ivo Baehr Junior<p>
 * Company:      Consistem Sistemas<p>
 * @author Ivo Baehr Junior
 * @version 1.0
 */

import javax.swing.*;
import java.awt.*;
import javax.swing.border.*;
import java.awt.event.*;


public class JFrameFocusTest extends JFrame {
  JPanel jPanelDados = new JPanel();
  GridBagLayout gridBagLayout1 = new GridBagLayout();
  BorderLayout borderLayout1 = new BorderLayout();
  JLabel jLabelCombo = new JLabel();
  JLabel jLabelMsgFocus = new JLabel();
  JLabel jLabelMsg1 = new JLabel();
  JLabel jLabelMsg2 = new JLabel();
  JPanel jPanelLookAndFeel = new JPanel();
  FlowLayout flowLayout1 = new FlowLayout();
  JRadioButton jRadioButton1 = new JRadioButton();
  JRadioButton jRadioButton2 = new JRadioButton();
  JRadioButton jRadioButton3 = new JRadioButton();
  TitledBorder titledBorder1;
  JComboBox jComboBox1 = new JComboBox(new String [] {"Test 1","Test 2","Test 3"});
  ButtonGroup bGroup = new ButtonGroup();

  public class Focus extends FocusAdapter  {
    public void focusGained(FocusEvent e) {
        jComboBox1_focusGainded(e);
    }

    public void focusLost(FocusEvent e) {
        jComboBox1_focusLost(e);
    }

  }

  public class Action implements ActionListener {
      public void actionPerformed(ActionEvent e) {
        jRadioButton_actionPerformed(e);
      }
  }

  public class Window extends WindowAdapter {
    public void windowClosing(WindowEvent e) {
      System.exit(0);
    }
  }

  public JFrameFocusTest() {
    try {
      jbInit();
    }
    catch(Exception e) {
      e.printStackTrace();
    }
  }

  public static void main(String[] args) {
    JFrameFocusTest JFrameFocusTest1 = new JFrameFocusTest();
  }

  private void jbInit() throws Exception {
    titledBorder1 = new TitledBorder(BorderFactory.createLineBorder(new Color(153, 153, 153),2),"Change Look And Feel");
    this.setTitle("Test Focus Event");
    this.getContentPane().setLayout(borderLayout1);
    jComboBox1.addFocusListener(new Focus());
    jPanelDados.setLayout(gridBagLayout1);
    jLabelCombo.setText("Combo");
    jLabelMsgFocus.setText("Message Focus");
    jLabelMsg1.setText("When the Focus Event happen");
    jLabelMsg2.setText("the JLabel above display \"Event Happen\"");
    jPanelLookAndFeel.setLayout(flowLayout1);
    jRadioButton1.setText("Metal");
    jRadioButton1.addActionListener(new Action());
    jRadioButton2.setText("Windows");
    jRadioButton2.addActionListener(new Action());
    jRadioButton3.setText("Motif");
    jRadioButton3.addActionListener(new Action());
    jPanelLookAndFeel.setBorder(titledBorder1);
    this.getContentPane().add(jPanelDados, BorderLayout.CENTER);
    jPanelDados.add(jLabelCombo, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0
    ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
    jPanelDados.add(jLabelMsgFocus, new GridBagConstraints(0, 2, 2, 1, 0.0, 0.0
    ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
    jPanelDados.add(jLabelMsg1, new GridBagConstraints(0, 3, 2, 1, 0.0, 0.0
    ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(10, 10, 0, 0), 0, 0));
    jPanelDados.add(jLabelMsg2, new GridBagConstraints(0, 4, 2, 1, 0.0, 0.0
    ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
    jPanelDados.add(jPanelLookAndFeel, new GridBagConstraints(0, 0, 2, 1, 0.0, 0.0
    ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
    jPanelLookAndFeel.add(jRadioButton3, null);
    jPanelLookAndFeel.add(jRadioButton2, null);
    jPanelLookAndFeel.add(jRadioButton1, null);
    jPanelDados.add(jComboBox1, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0
    ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
    this.bGroup.add(this.jRadioButton1);
    this.bGroup.add(this.jRadioButton2);
    this.bGroup.add(this.jRadioButton3);
    this.pack();
    this.show();
    this.addWindowListener(new Window());
    this.jRadioButton1.setSelected(true);

  }

  void jRadioButton_actionPerformed(ActionEvent e) {
    String currentLookAndFeel = "";
    if (e.getSource() == this.jRadioButton3)  // Look and Feel Motif
      currentLookAndFeel    = "com.sun.java.swing.plaf.motif.MotifLookAndFeel";

    if (e.getSource() == this.jRadioButton2)  // Look and Feel Windows
     currentLookAndFeel  = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";

    if (e.getSource() == this.jRadioButton1)  // Look and Feel Metal
      currentLookAndFeel  = "javax.swing.plaf.metal.MetalLookAndFeel";

    try {
      UIManager.setLookAndFeel(currentLookAndFeel);
      SwingUtilities.updateComponentTreeUI(this);
    } catch (Exception ex) {
      System.out.println("Failed loading L&F: " + currentLookAndFeel);
      System.out.println(ex);
    }
    this.jLabelMsgFocus.setText("Don´t have focus in JComboBox");
  }

  void jComboBox1_focusGainded(FocusEvent e) {
    this.jLabelMsgFocus.setText("Focus Gained Happen!");
  }

  void jComboBox1_focusLost(FocusEvent e) {
    this.jLabelMsgFocus.setText("Focus Lost Happen!");
  }

}
