/**
 * @(#)BoxControlPanel.java
 * @author JLMJ
 * @date 2001-10-09
 */
package minoids1.controls;

import minoids1.Minoid;
import minoids1.viewer.MinoidsViewerPanel;
import java.awt.*;
import java.awt.event.*;

public class BoxControlDialog extends AbstractControlDialog
    implements ItemListener, ActionListener
{
    private MinoidsViewerPanel viewer;
    private Minoid[] minoids;
    private ControlPanel controlPanel;
    
    private Panel northPanel, centerPanel, southPanel;
    
    private int x = 1, y = 2, z = 3;
    private Choice xChoice, yChoice, zChoice;
    
    private Button minoidsControlButton;
    private Button minoidsSetClearAllButton;
    
    public BoxControlDialog(MinoidsViewerPanel viewer, Minoid[] minoids,
                   ControlPanel controlPanel, Frame parent)
    {
        super(parent);
        setTitle("Box Control");
    
        this.viewer = viewer;
        this.minoids = minoids;
        this.controlPanel = controlPanel;
        
        northPanel = new Panel();
        centerPanel = new Panel();
        southPanel = new Panel();
        
        xChoice = new Choice();
        yChoice = new Choice();
        zChoice = new Choice();
        for (int i=0; i < 6; i++) {
            xChoice.addItem(i + "");
            yChoice.addItem(i + "");
            zChoice.addItem(i + "");
        }
        xChoice.addItemListener(this);
        yChoice.addItemListener(this);
        zChoice.addItemListener(this);
        
        xChoice.select(x);
        yChoice.select(y);
        zChoice.select(z);
        
        minoidsControlButton = new Button("Show Controls");
        minoidsControlButton.addActionListener(this);
        minoidsSetClearAllButton = new Button("Set All");
        minoidsSetClearAllButton.addActionListener(this);
        
        northPanel.add(new Label("X"));
        northPanel.add(xChoice);
        northPanel.add(new Label("Y"));
        northPanel.add(yChoice);
        northPanel.add(new Label("Z"));
        northPanel.add(zChoice);
        
        centerPanel.add(minoidsControlButton);
        centerPanel.add(minoidsSetClearAllButton);
        
        setLayout(new BorderLayout());
        add(northPanel, BorderLayout.NORTH);
        add(centerPanel, BorderLayout.CENTER);
        add(southPanel, BorderLayout.SOUTH);
    }
    
    private void setAllMinoids() {
        minoidsSetClearAllButton.setLabel("Clear All Minoids");
        controlPanel.setAll();
    }
    
    public void updateMinoids() {
        viewer.removeAllMinoids();
        minoids = Minoid.getBoxMinoids(x, y, z);
        controlPanel.updateControls(minoids);
        setAllMinoids();
        pack();
    }
    
    public void itemStateChanged(ItemEvent e) {
        if (e.getSource() instanceof Choice) {
            x = xChoice.getSelectedIndex();
            y = yChoice.getSelectedIndex();
            z = zChoice.getSelectedIndex();
            updateMinoids();
        }
    }
    
    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();
            }
        }
    }
}


