/*
 * JDK 1.1.5 
*/

import java.awt.*;
import java.awt.event.*;

public class LayoutExample extends Frame 
			implements ActionListener,
					ItemListener {
    private boolean inAnApplet = true;
    Button border, flow, panel, card, grid, gridBag; 
    Button OK, cancel, reset, two, three, four;
    Panel p1, p2, p3, p4, cards;
    TextField tfield;

    public LayoutExample() {

        setLayout(new GridLayout(1,2));
        setFont(new Font("Helvetica", Font.PLAIN, 14));

	p1 = new Panel();
	p1.setLayout(new GridLayout(8,1));
	p2 = new Panel();

	p1.setBackground(Color.cyan);
	p2.setBackground(Color.white);
	add(p1);
	add(p2);

	border = new Button("Border Layout");
	flow = new Button("Flow Layout");
	card = new Button("Card Layout");
	grid = new Button("Grid Layout");
	gridBag = new Button("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 Label("<--  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;

        Label top = new Label("GridBag Layout");
        top.setAlignment(Label.CENTER);
        gridbag.setConstraints(top, c);
        p2.add(top);

        c.gridwidth = GridBagConstraints.RELATIVE;
        TextField name = new TextField("Name:", 25);
        gridbag.setConstraints(name, c);
        p2.add(name);

        c.gridwidth = GridBagConstraints.REMAINDER;

        TextField addr = new TextField("Address:", 25);
        gridbag.setConstraints(addr, c);
        p2.add(addr);

        TextArea comments = new TextArea(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 Button("OK");
        gridbag.setConstraints(OK, c);
        p2.add(OK);

	c.gridheight=1;
        cancel = new Button("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 Label("<--  Select a layout to view ..."));
		p2.validate();
	}

        if( source == cancel){
                p2.removeAll();
		gridBag();
                p2.validate();
        }

        if(source == two){
                p2.removeAll();
                p2.setLayout(new GridLayout(0,2));
                OK = new Button("OK");
                p2.add(two);
                p2.add(three);
                p2.add(four);
                p2.add(OK);
		OK.addActionListener(this);

                p2.validate();
        }

        if( source == three){
                p2.removeAll();
                p2.setLayout(new GridLayout(0,3));
                OK = new Button("OK");
                p2.add(two);
                p2.add(three);
                p2.add(four);
                p2.add(OK);
                OK.addActionListener(this);
                p2.validate();
        }

        if( source == four){
                p2.removeAll();
                p2.setLayout(new GridLayout(0,4));
                OK = new Button("OK");
                p2.add(two);
                p2.add(three);
                p2.add(four);
                p2.add(OK);
                OK.addActionListener(this);
                p2.validate();
        }

        if( source == tfield){
                p2.removeAll();
                p2.setLayout(new FlowLayout());
		p2.add(new Label("<--  Select a layout to view ..."));
                p2.validate();
        }

	if( source == border){
		p2.removeAll();
        	p2.setLayout(new BorderLayout());

        	Label dialogLabel = new Label();
        	dialogLabel.setAlignment(Label.CENTER);
        	dialogLabel.setText("Border Layout");

        	List items = new List(6, true);
        	items.add("Apple tree");
        	items.add("Orange tree");
        	items.add("Pear tree");
        	items.add("Peach tree");
        	items.add("Cherry tree");
        	items.add("Apricot tree");

        	OK = new Button("OK");
        	p2.add("North", dialogLabel);
        	p2.add("South", OK);
        	p2.add("Center", items);
                OK.addActionListener(this);
		p2.validate();
	}
       
        if( source == flow){
                p2.removeAll();
                p2.setLayout(new FlowLayout());

		p2.add(new Label("Flow Layout"));
		OK = new Button("OK");
		p2.add(new Button("An extremely long name"));	
		p2.add(new Button("Short"));
		p2.add(new Button("Medium Length"));
		p2.add(OK);
		p2.add(new Scrollbar(Scrollbar.HORIZONTAL));
                OK.addActionListener(this);
                p2.validate();
        }


        if( source == card){
                p2.removeAll();
    		String BUTTONPANEL = "Show panel with Buttons";
    		String TEXTPANEL = "Show panel with TextField";
        	p2.setLayout(new BorderLayout());

        	Panel c2 = new Panel();
        	Choice c = new Choice();
        	c.add(BUTTONPANEL);
        	c.add(TEXTPANEL);
        	c2.add(c);

        	Label cardLabel = new Label("Card Layout");
        	cardLabel.setAlignment(Label.CENTER);
        	p2.add("North", cardLabel);
        	p2.add("Center", c2);

        	cards = new Panel();
        	cards.setLayout(new CardLayout());

        	p3 = new Panel();
        	p4 = new Panel();

        	OK = new Button("OK");
        	Button quit = new Button("Quit");
        	reset = new Button("Reset");
        	p3.add(OK);
        	p3.add(quit);
        	p3.add(reset);

        	tfield = new TextField("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();
        }

        if( source == grid){
                p2.removeAll();
		p2.setLayout(new GridLayout(5,1));

                two = new Button("2 cols.");
                three = new Button("3 cols.");
                four = new Button("4 cols.");
		OK = new Button("OK");
                OK.addActionListener(this);
		two.addActionListener(this);
		three.addActionListener(this);
		four.addActionListener(this);
                p2.add(new Label("Grid Layout     1 col."));

                p2.add(two);
                p2.add(three);
                p2.add(four);
		p2.add(OK);
                p2.validate();
        }

        if( source == gridBag){
                p2.removeAll();
		gridBag();
                p2.validate();
        }
   }

   public void itemStateChanged(ItemEvent event) {

	CardLayout cl = (CardLayout)(cards.getLayout());
	cl.show(cards, (String)event.getItem());
   }

   public static void main(String args[]) {
        LayoutExample window = new LayoutExample();
        window.inAnApplet = false;
        window.setTitle("Layout Example Application");
        window.pack();
        window.setVisible(true);
    }
}
