package com.yaka;

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

class CompositeLayoutExample implements LayoutExample {

    private JComponent makeSexPanel() {
        JPanel sexpanel = new JPanel();
        sexpanel.setLayout(new BoxLayout(sexpanel, BoxLayout.Y_AXIS));

        sexpanel.add(new JCheckBox("M"));
        sexpanel.add(new JCheckBox("F"));

        return sexpanel;
    }

    private JComponent makeTopForm() {
        JPanel form = new JPanel();

        form.setLayout(new GridBagLayout());

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.anchor = GridBagConstraints.WEST;

        form.add(new JLabel("Name: "), gbc);
        gbc.gridy++;
        form.add(new JLabel("Sex: "), gbc);
        gbc.gridy++;
        form.add(new JLabel("City: "), gbc);

        gbc.gridx++;
        gbc.gridy = 0;
        form.add(new JTextField(24), gbc);
        gbc.gridy++;
        form.add(makeSexPanel(), gbc);
        gbc.gridy++;
        form.add(new JTextField(24), gbc);

        return form;
    }

    private JComponent makeForm() {
        JPanel form = new JPanel();

        form.setLayout(new BoxLayout(form, BoxLayout.Y_AXIS));
        form.setBorder(new EmptyBorder(10, 10, 10, 10));

        JComponent topForm = makeTopForm();
        topForm.setAlignmentX(Component.LEFT_ALIGNMENT);

        JTextArea message = new JTextArea();
        message.setBorder(new EtchedBorder(Color.LIGHT_GRAY, Color.GRAY));
        message.setAlignmentX(Component.LEFT_ALIGNMENT);

        form.add(topForm);
        form.add(new JLabel("Message: ", SwingConstants.LEFT));
        form.add(message);

        return form;
    }

    private JComponent makeTabs() {
        JTabbedPane tabs = new JTabbedPane();

        tabs.addTab("Standard form", makeForm());
        tabs.addTab("Second tab", new JPanel());
        tabs.addTab("Hot chick", new JPanel());

        return tabs;
    }

    @Override
    public JPanel getContent() {
        JPanel panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));

        panel.add(makeTabs());

        return panel;
    }

    @Override
    public String getName() {
        return "Composite Layout";
    }
}
