/**
 * @(#)PyramidControlPanel.java
 * @author JLMJ
 * @date 2001-10-09
 */
package minoids1.controls;

import minoids1.viewer.MinoidsViewerPanel;
import minoids1.Minoid;
import java.awt.*;
import java.awt.event.*;

public class PyramidControlDialog extends AbstractControlDialog
    implements ItemListener, ActionListener
{
    private MinoidsViewerPanel viewer;
    private Minoid[] minoids;
    private ControlPanel controlPanel;
    
    private Panel northPanel, centerPanel,southPanel;
    
    private int order = 3;
    private Choice orderChoice;

    private Button minoidsControlButton;
    private Button minoidsSetClearAllButton;
    
    public PyramidControlDialog(MinoidsViewerPanel viewer, Minoid[] minoids,
                       ControlPanel controlPanel, Frame parent)
    {
        super(parent);
        setTitle("Pyramid Control");
        this.viewer = viewer;
        this.minoids = minoids;
        this.controlPanel = controlPanel;
        
        northPanel = new Panel();
        centerPanel = new Panel();
        southPanel = new Panel();

        orderChoice = new Choice();
        for (int i=0; i < 7; i++)
            orderChoice.addItem(i + "");
        orderChoice.addItemListener(this);
        orderChoice.select(order);
        
        minoidsControlButton = new Button("Show Controls");
        minoidsControlButton.addActionListener(this);
        minoidsSetClearAllButton = new Button("Set All");
        minoidsSetClearAllButton.addActionListener(this);
        
        
        centerPanel.add(minoidsControlButton);
        centerPanel.add(minoidsSetClearAllButton);

        northPanel.add(new Label("Order:"));
        northPanel.add(orderChoice);

        setLayout(new BorderLayout());
        add(northPanel, BorderLayout.NORTH);
        add(centerPanel, BorderLayout.CENTER);
        add(southPanel, BorderLayout.SOUTH);
        pack();
    }

    private void setAllMinoids() {
        minoidsSetClearAllButton.setLabel("Clear All Minoids");
        controlPanel.setAll();
    }

    public void updateMinoids() {
        viewer.removeAllMinoids();
        minoids = Minoid.getPyramidMinoids(order);
        controlPanel.updateControls(minoids);
        setAllMinoids();
        pack();
    }
    
    public void itemStateChanged(ItemEvent e) {
        if (e.getSource() instanceof Choice) {
            order = orderChoice.getSelectedIndex();
            updateMinoids();
        }
        pack();
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() instanceof Button) {
            Button b = (Button)e.getSource();
            if (b == minoidsControlButton) {
                if (b.getLabel().equals("Show Controls")) {
                    b.setLabel("Hide Controls");
                    southPanel.add(controlPanel);
                } else {
                    b.setLabel("Show Controls");
                    southPanel.removeAll();
                }
                pack();
            } else if (b == minoidsSetClearAllButton) {
                if (b.getLabel().equals("Set All Minoids")) {
                    b.setLabel("Clear All Minoids");
                    controlPanel.setAll();
                } else {
                    b.setLabel("Set All Minoids");
                    controlPanel.clearAll();
                }
                pack();
            }
        }
    }

}



