package edu.csusb.danby.applet;

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


/**
* a convenience constructor for menus
* @version Tue Jul 23 08:29:22 PDT 2002
*/
public class DMenu extends JMenu{
    protected DMenuAction dMenuAction; // class implementing DMenuAction
    protected int numberItems; //number of items on menu
                                //with default value
    protected boolean radio=false; //whether to have radioButton behavior
    protected ButtonGroup radioGroup;
    protected String menuLabel;
    protected JMenuItem[] jmi;
            
      
/**
*  DMenu constructor
* @param aDMenuAction the host DMenuAction
* @param aMenuLabel the label for the menu
* @param itemLabel  an array of labels for the menu items
* @param radio  indicates whether menu should have radio button behavior
* @param returnIndex indicates whether the menuAction() method in DMenuAction
*   should call using the menu item index or the name.
*/  
  public DMenu(DMenuAction aDMenuAction,  String aMenuLabel, 
                    String[] itemLabel, boolean radio, boolean returnIndex){
    IndexedActionListener[] ial;

    dMenuAction=aDMenuAction;  //Will usually need this for doAction();
    //radio = aRadio;  
    menuLabel = aMenuLabel;
    numberItems = itemLabel.length;
    
    setText(menuLabel);
        
    ial = new IndexedActionListener[numberItems];
    
    if (radio){
        //JRadioButtonMenuItem[] jmi;
        jmi  = new JRadioButtonMenuItem[numberItems];
        
        radioGroup = new ButtonGroup();
        for (int i=0; i< numberItems; i++){
            if (returnIndex){
                ial[i] = new IndexedActionListener(i);
                }
            else {
                ial[i] = new IndexedActionListener(itemLabel[i]);
            }
            jmi[i] = new JRadioButtonMenuItem(itemLabel[i]);
            jmi[i].addActionListener( ial[i]);
            add(jmi[i]);
            radioGroup.add(jmi[i]);
            }//end for	
        }//end if
    else{
        //JMenuItem[] jmi;
        jmi = new JMenuItem[numberItems];
        for (int i=0; i< numberItems; i++){
            ial[i] = new IndexedActionListener(itemLabel[i]);
            jmi[i] = new JMenuItem(itemLabel[i]);
            jmi[i].addActionListener( ial[i]);
            add(jmi[i]);
            }//end for
        }//end else
    } //end Constructor method
    
    private class IndexedActionListener implements ActionListener{
        String itemLabel;
        int index;
        boolean returnIndex = false;
    
        public IndexedActionListener(String aItemLabel){
            itemLabel = aItemLabel;
            }
        public IndexedActionListener(int i){
            index = i;
            returnIndex = true;
            }
            
        public void actionPerformed(ActionEvent e){
            if (returnIndex) {
                dMenuAction.menuAction(menuLabel, index);
                }
            else {
                dMenuAction.menuAction(menuLabel, itemLabel);
            }
        }
    }
    
/**
*  DMenu constructor
* @param aDMenuAction the host DMenuAction
* @param aMenuLabel the label for the menu
* @param itemLabel  an array of labels for the menu items
* @param radio  indicates whether menu should have radio button behavior
*/  
    public DMenu(DMenuAction aDMenuAction, String menuLabel, String[] itemLabel, 
                            boolean radio){
        this(aDMenuAction, menuLabel, itemLabel, radio, false);
    }
    
/**
*  DMenu constructor
* @param aDMenuAction the host DMenuAction
* @param aMenuLabel the label for the menu
* @param itemLabel  an array of labels for the menu items
*/  
    public DMenu(DMenuAction aDMenuAction, String menuLabel,  String[] itemLabel){
        this(aDMenuAction, menuLabel, itemLabel, false);
    }

    
/** 
* enables or disables menu items as indicated in the argument
* @param enabled a list of indices to be enabled
*/
    public void setEnabledItems(boolean[] enabled){
        if (enabled.length != numberItems) {
                System.out.println("enabledArray size error");
                }
        else {
            for (int i=0; i< numberItems; i++){
                getItem(i).setEnabled(enabled[i]);
                }
            }
    }
       
    /**
    * select the indicated radiu menu item
    */
    public void setSelectedItem(int k){
        jmi[k].setSelected(true);
    }
                 

    public void setActionCommand(String[] actionCommandName){
        if (actionCommandName.length != numberItems) {
                System.out.println("setActionCommand size error");
                }
        else {
            for (int i=0; i< numberItems; i++){
                getItem(i).setActionCommand(actionCommandName[i]);
                }
            }
    }

}
