import java.awt.*;
import java.awt.event.*;
import java.awt.event.ActionListener;

import javax.swing.*;

/**
 * @author Remi Forax
 *
 */
public class LayoutExamples3 {

  private static JPanel createPanel(LayoutManager layout) {
    JPanel panel=new JPanel(layout);
    return initPanel(panel);
  }
  
  private static JPanel createPanel(LayoutManager2 layout,Object[] constraints) {
    JPanel panel=createPanel((LayoutManager)layout);
    for(int i=0;i<panel.getComponentCount();i++) {
      Component c=panel.getComponent(i);
      layout.addLayoutComponent(c, constraints[i]);
    }
    return panel;
  }
  
  private static JPanel initPanel(JPanel panel) {
    for(int i=0;i<5;i++)
      panel.add(new JButton("button "+i));
    return panel;
  }
  
  private static JPanel createBoxPanel(int axis) {
    JPanel panel=new JPanel(null);
    BoxLayout layout=new BoxLayout(panel,axis);
    panel.setLayout(layout);
    return initPanel(panel);
  }

  private static GridBagConstraints createGridBagConstraints(
    int width,int height,int anchor,int fill,int weightx,int weighty) {
      
    GridBagConstraints constraints=new GridBagConstraints();
    
    constraints.gridwidth=width;
    constraints.gridheight=height;
    constraints.anchor=anchor;
    
    constraints.fill=fill;
    constraints.weightx=weightx;
    constraints.weighty=weighty;
    return constraints;
  }

  private static JInternalFrame createInternalFrame(String title) {
    JInternalFrame internalFrame=new JInternalFrame(title,true,true,true,true);
    internalFrame.setVisible(true);
    return internalFrame;
  }

  public static void main(String[] args) {
    
    String[] borderConstraints=new String[] {
      BorderLayout.NORTH,
      BorderLayout.SOUTH,
      BorderLayout.WEST,
      BorderLayout.EAST,
      BorderLayout.CENTER
    };
    
    GridBagConstraints[] gridBagConstraints=new GridBagConstraints[] {
      createGridBagConstraints(2,1,GridBagConstraints.CENTER,
        GridBagConstraints.HORIZONTAL,2,1),
      createGridBagConstraints(GridBagConstraints.REMAINDER,1,GridBagConstraints.CENTER,
        GridBagConstraints.NONE,1,1),
      createGridBagConstraints(GridBagConstraints.RELATIVE,1,GridBagConstraints.CENTER,
        GridBagConstraints.BOTH,2,1),
      createGridBagConstraints(GridBagConstraints.REMAINDER,1,GridBagConstraints.CENTER,
        GridBagConstraints.HORIZONTAL,1,1),
      createGridBagConstraints(GridBagConstraints.REMAINDER,1,GridBagConstraints.CENTER,
        GridBagConstraints.HORIZONTAL,3,2)
    };
    
    JPanel[] panels=new JPanel[]{
      createPanel(new FlowLayout()),
      createPanel(new GridLayout(2,3)),
      createPanel(new BorderLayout(2,3),borderConstraints),
      createPanel(new GridBagLayout(),gridBagConstraints),
      createBoxPanel(BoxLayout.X_AXIS),
      createBoxPanel(BoxLayout.Y_AXIS)
    };
    
    final JInternalFrame jif=createInternalFrame("");
    
    JPanel mainPanel=new JPanel(null);
    mainPanel.setLayout(new BoxLayout(mainPanel,BoxLayout.Y_AXIS));
    ButtonGroup group=new ButtonGroup();
    for(int i=0;i<panels.length;i++) {
      final JPanel panel=panels[i];
      JCheckBox checkBox=new JCheckBox(panel.getLayout().getClass().getName());
      checkBox.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
          jif.setTitle(panel.getLayout().getClass().getName());
          jif.setContentPane(panel);
          jif.pack();
        }
      });
      
      mainPanel.add(checkBox);
      group.add(checkBox);
    }
    
    ((JCheckBox)mainPanel.getComponent(0)).setSelected(true);
    jif.setTitle(panels[0].getLayout().getClass().getName());
    jif.setContentPane(panels[0]);
    jif.pack();
    
    JInternalFrame main=createInternalFrame("main");
    main.setContentPane(mainPanel);
    main.pack();
    
    JDesktopPane pane=new JDesktopPane();
    pane.add(main);
    pane.add(jif);
    
    JFrame frame=new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setContentPane(pane);
    frame.setSize(400,300);
    frame.show();
  }
}
