/*
 * 1.0 code.
*/

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

public class LayoutExample extends Frame {
    private boolean inAnApplet = true;
    Button border, flow, panel, card, grid, gridBag; 
    Button OK, cancel, reset, two, three, four;
    Panel p1, p2, p3, p4, cards;
    String BUTTONPANEL = "Show panel with Buttons";
    String TEXTPANEL = "Show panel with TextField";
    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");

	p1.add(border);
        p1.add(flow);
        p1.add(card);
        p1.add(grid);
        p1.add(gridBag);

	p2.add(new Label("<--  Select a layout to view ..."));
    }

    public boolean handleEvent(Event e) {
        if (e.id == Event.WINDOW_DESTROY) {
            if (inAnApplet) {
                dispose();
                return true;
            } else {
                System.exit(0);
            }
        }   
        return super.handleEvent(e);
    }

    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);
   }

   public boolean action(Event e, Object o){

	if(e.target.equals(OK)){
		p2.removeAll();
		p2.setLayout(new FlowLayout());
		p2.add(new Label("<--  Select a layout to view ..."));
		super.validate();
	}

        if(e.target.equals(cancel)){
                p2.removeAll();
		gridBag();
                super.validate();
        }

        if(e.target.equals(two)){
                p2.removeAll();
                p2.setLayout(new GridLayout(0,2));
                two = new Button("2 cols.");
                three = new Button("3 cols.");
                four = new Button("4 cols.");
                OK = new Button("OK");
                p2.add(two);
                p2.add(three);
                p2.add(four);
                p2.add(OK);
                super.validate();
        }

        if(e.target.equals(three)){
                p2.removeAll();
                p2.setLayout(new GridLayout(0,3));
                two = new Button("2 cols.");
                three = new Button("3 cols.");
                four = new Button("4 cols.");
                OK = new Button("OK");
                p2.add(two);
                p2.add(three);
                p2.add(four);
                p2.add(OK);
                super.validate();
        }

        if(e.target.equals(four)){
                p2.removeAll();
                p2.setLayout(new GridLayout(0,4));
                two = new Button("2 cols.");
                three = new Button("3 cols.");
                four = new Button("4 cols.");
                OK = new Button("OK");
                p2.add(two);
                p2.add(three);
                p2.add(four);
                p2.add(OK);
                super.validate();
        }

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


	if(e.target.equals(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.addItem("Apple tree");
        	items.addItem("Orange tree");
        	items.addItem("Pear tree");
        	items.addItem("Peach tree");
        	items.addItem("Cherry tree");
        	items.addItem("Apricot tree");

        	OK = new Button("OK");
        	p2.add("North", dialogLabel);
        	p2.add("South", OK);
        	p2.add("Center", items);

		super.validate();
	}
       
        if(e.target.equals(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));

                super.validate();
        }

        if(e.target.equals(card)){
                p2.removeAll();
        	p2.setLayout(new BorderLayout());

                Panel c2 = new Panel();
        	Choice c = new Choice();
        	c.addItem(BUTTONPANEL);
        	c.addItem(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("Cancel");
		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);
	
                super.validate();
        }


        if(e.target.equals(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");

                p2.add(new Label("Grid Layout     1 col."));

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

        if(e.target.equals(gridBag)){
                p2.removeAll();
		gridBag();
                super.validate();
        }

        if (e.target instanceof Choice) {
            ((CardLayout)cards.getLayout()).show(cards,(String)o);
            return true;
        }
 
	return true;
   }


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