/*
 * Swing 1.0.2 
 * Code taken from 
 * http://java.sun.com/developer/technicalArticles/GUI/AWTLayoutMgr/LayoutExampleSwing.java
*/

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class LayoutExample extends JFrame implements ActionListener,
                                                     ItemListener {
   private boolean inAnApplet = true;
   JButton border, flow, panel, card, grid, gridBag; 
   JButton OK, cancel, reset, two, three, four;
   JPanel p1, p2, p3, p4, cards;
   JTextField tfield;

   public LayoutExample() {

      getContentPane().setLayout(new GridLayout(1,2));
      setFont(new Font("Helvetica", Font.PLAIN, 14));

      p1 = new JPanel();
      p1.setLayout(new GridLayout(8,1));
      p2 = new JPanel();

      p1.setBackground(Color.gray);
      p2.setBackground(Color.white);
      getContentPane().add(p1);
      getContentPane().add(p2);

      border = new JButton("Border Layout");
      flow = new JButton("Flow Layout");
      card = new JButton("Card Layout");
      grid = new JButton("Grid Layout");
      gridBag = new JButton("GridBag Layout");

      border.addActionListener(this);
      flow.addActionListener(this);
      card.addActionListener(this);
      grid.addActionListener(this);
      gridBag.addActionListener(this);

      p1.add(border);
      p1.add(flow);
      p1.add(card);
      p1.add(grid);
      p1.add(gridBag);

      p2.add(new JLabel("<--  Select a layout to view ..."));

      addWindowListener(new WindowAdapter(){
         public void windowClosing(WindowEvent event){
            if (inAnApplet) {
               dispose();
            } else {
               System.exit(0);
            }
         }
      });
   }

   public void gridBag(){
      GridBagConstraints c = new GridBagConstraints();
      GridBagLayout gridbag = new GridBagLayout();
      p2.setLayout(gridbag);

      c.fill = GridBagConstraints.BOTH;
      c.weightx = 1.0;
      c.gridwidth = GridBagConstraints.REMAINDER;

      JLabel top = new JLabel("GridBag Layout");
      top.setHorizontalAlignment(JLabel.CENTER);
      gridbag.setConstraints(top, c);
      p2.add(top);

      c.gridwidth = GridBagConstraints.RELATIVE;
      JTextField name = new JTextField("Name:", 25);
      gridbag.setConstraints(name, c);
      p2.add(name);

      c.gridwidth = GridBagConstraints.REMAINDER;

      JTextField addr = new JTextField("Address:", 25);
      gridbag.setConstraints(addr, c);
      p2.add(addr);

      JTextArea comments = new JTextArea(3, 25);
      comments.setEditable(true);
      comments.setText("Comments:");
      gridbag.setConstraints(comments, c);
      p2.add(comments);

      
      c.insets = new Insets(15, 0, 0, 0);
      c.fill=GridBagConstraints.NONE;
      c.gridwidth = GridBagConstraints.RELATIVE;
      OK = new JButton("OK");
      gridbag.setConstraints(OK, c);
      p2.add(OK);

      c.gridheight=1;
      cancel = new JButton("Cancel");
      gridbag.setConstraints(cancel, c);
      p2.add(cancel);

      OK.addActionListener(this);
      cancel.addActionListener(this);
      name.addActionListener(this);
      addr.addActionListener(this);
   }

   public void actionPerformed(ActionEvent event){

      Object source = event.getSource();

      if( source == OK){
         p2.removeAll();
         p2.setLayout(new FlowLayout());
         p2.add(new JLabel("<--  Select a layout to view ..."));
         p2.validate();
         p2.repaint();
      }

      if( source == cancel){
         p2.removeAll();
         gridBag();
         p2.validate();
         p2.repaint();
      }

      if(source == two){
         p2.removeAll();
         p2.setLayout(new GridLayout(0,2));
         OK = new JButton("OK");
         p2.add(two);
         p2.add(three);
         p2.add(four);
         p2.add(OK);
         OK.addActionListener(this);
         p2.validate();
         p2.repaint();
      }

      if( source == three){
         p2.removeAll();
         p2.setLayout(new GridLayout(0,3));
         OK = new JButton("OK");
         p2.add(two);
         p2.add(three);
         p2.add(four);
         p2.add(OK);
         OK.addActionListener(this);
         p2.validate();
         p2.repaint();
      }

      if( source == four){
         p2.removeAll();
         p2.setLayout(new GridLayout(0,4));
         OK = new JButton("OK");
         p2.add(two);
         p2.add(three);
         p2.add(four);
         p2.add(OK);
         OK.addActionListener(this);
         p2.validate();
         p2.repaint();
      }

      if( source == tfield){
         p2.removeAll();
         p2.setLayout(new FlowLayout());
         p2.add(new JLabel("<--  Select a layout to view ..."));
         p2.validate();
         p2.repaint();
      }

      if( source == border){
         p2.removeAll();
         p2.setLayout(new BorderLayout());

         JLabel dialogLabel = new JLabel();
         dialogLabel.setHorizontalAlignment(JLabel.CENTER);
         dialogLabel.setText("Border Layout");

         String[] data = {"Apple tree", "Orange tree", "Pear tree", 
                          "Peach tree", "Cherry tree", "Apricot tree"};
         JList items = new JList(data);

         OK = new JButton("OK");
         p2.add("North", dialogLabel);
         p2.add("South", OK);
         p2.add("Center", items);
         OK.addActionListener(this);
         p2.validate();
         p2.repaint();
      }
      
      if( source == flow){
         p2.removeAll();
         p2.setLayout(new FlowLayout());

         p2.add(new JLabel("Flow Layout"));
         OK = new JButton("OK");
         p2.add(new JButton("An extremely long name"));      
         p2.add(new JButton("Short"));
         p2.add(new JButton("Medium Length"));
         p2.add(OK);
         p2.add(new JScrollBar(JScrollBar.HORIZONTAL));
         OK.addActionListener(this);
         p2.validate();
         p2.repaint();
      }


      if( source == card){
         p2.removeAll();
         String BUTTONPANEL = "Show panel with Buttons";
         String TEXTPANEL = "Show panel with TextField";
         p2.setLayout(new BorderLayout());

         JPanel c2 = new JPanel();
         c2.setBackground(Color.white);

         Choice c = new Choice();
         c.add(BUTTONPANEL);
         c.add(TEXTPANEL);
         c2.add(c);

         JLabel cardLabel = new JLabel("Card Layout");
         cardLabel.setHorizontalAlignment(JLabel.CENTER);
         p2.add("North", cardLabel);
         p2.add("Center", c2);

         cards = new JPanel();
         cards.setLayout(new CardLayout());

         p3 = new JPanel();
         p3.setBackground(Color.white);
         p4 = new JPanel();
         p4.setBackground(Color.white);


         OK = new JButton("OK");
         JButton quit = new JButton("Quit");
         reset = new JButton("Reset");
         p3.add(OK);
         p3.add(quit);
         p3.add(reset);

         tfield = new JTextField("Enter name:", 20);
         p4.add(tfield);

         cards.add(BUTTONPANEL, p3);
         cards.add(TEXTPANEL, p4);
         p2.add("South", cards);
         OK.addActionListener(this);
         c.addItemListener(this);
         tfield.addActionListener(this);

         p2.validate();
         p2.repaint();
      }

      if( source == grid){
         p2.removeAll();
         p2.setLayout(new GridLayout(5,1));

         two = new JButton("2 cols.");
         three = new JButton("3 cols.");
         four = new JButton("4 cols.");
         OK = new JButton("OK");
         OK.addActionListener(this);
         two.addActionListener(this);
         three.addActionListener(this);
         four.addActionListener(this);
         p2.add(new JLabel("Grid Layout    1 col."));

         p2.add(two);
         p2.add(three);
         p2.add(four);
         p2.add(OK);
         p2.validate();
         p2.repaint();
      }

      if( source == gridBag){
         p2.removeAll();
         gridBag();
         p2.validate();
         p2.repaint();
      }
   }

   public void itemStateChanged(ItemEvent event) {

      CardLayout cl = (CardLayout)(cards.getLayout());
      cl.show(cards, (String)event.getItem());
   }

   public static void main(String args[]) {

      try{
         UIManager.setLookAndFeel(
            UIManager.getCrossPlatformLookAndFeelClassName());
      }catch (Exception e) {
         System.err.println("Couldn't use the cross-platform"
                           + "look and feel: " + e);
      }

      JFrame frame = new LayoutExample();

      WindowListener l = new WindowAdapter() {
         public void windowClosing(WindowEvent e) {
               System.exit(0);
         }
      };

      frame.addWindowListener(l);
      frame.pack();
      frame.setVisible(true);
   }
}
